2017-02-16 110 views
0

我試圖使用Ruby的谷歌API客戶端創建谷歌計算平臺(GCP)的部署。使用部署管理器的谷歌API時如何設置部署配置?

我對configuation YAML文件:

resources: 

- name: my-vm 
    type: compute.v1.instance 
    properties: 
    zone: europe-west1-b 
    machineType: zones/europe-west1-b/machineTypes/f1-micro 
    disks: 
    - deviceName: boot 
     type: PERSISTENT 
     boot: true 
     autoDelete: true 
     initializeParams: 
     sourceImage: global/images/myvm-1487178154 
    networkInterfaces: 
    - network: $(ref.my-subnet.selfLink) 
     networkIP: 172.31.54.11 

# Create the network for the machines 
- name: my-subnet 
    type: compute.v1.network 
    properties: 
    IPv4Range: 172.31.54.0/24 

我已經測試了這個作品使用gcloud命令行工具。

我現在想要使用API​​來做到這一點的紅寶石。我有以下代碼:

require 'google/apis/deploymentmanager_v2' 
require 'googleauth' 
require 'googleauth/stores/file_token_store' 

SCOPE = Google::Apis::DeploymentmanagerV2::AUTH_CLOUD_PLATFORM 
PROJECT_ID = "my-project" 

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "./service_account.json" 

deployment_manager = Google::Apis::DeploymentmanagerV2::DeploymentManagerService.new 
deployment_manager.authorization = Google::Auth.get_application_default([SCOPE]) 

所有這一切都在工作,我授權,但我有一個deployment_manager對象我可以工作。

我想用insert_deployment方法,具有以下特徵:

#insert_deployment(project, deployment_object = nil, preview: nil, fields: nil, quota_user: nil, user_ip: nil, options: nil) {|result, err| ... } ⇒ Google::Apis::DeploymentmanagerV2::Operation 

deployment_object類型是 '谷歌::蜜蜂:: DeploymentmanagerV2 ::部署'。我可以創建這個對象,但是那時候我不知道如何導入YAML文件我都到這是能夠執行程式設計部署。

還有一種叫ConfigFile類,它似乎類似於指定--config卻又不知如何將文件加載到這個也不再把它放進了insert_deployment正確的對象的命令行選項。

回答

0

我曾了這一點。

不同類需要被嵌套,使得配置被拾取。例如:

require 'google/apis/deploymentmanager_v2' 
require 'googleauth' 

SCOPE = Google::Apis::DeploymentmanagerV2::AUTH_CLOUD_PLATFORM 
PROJECT_ID = "my-project" 

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "./service_account.json" 

# Create a target configuration 
target_configuration = Google::Apis::DeploymentmanagerV2::TargetConfiguration.new(config: {content: File.read('gcp.yaml')}) 

# Now create a deployment object 
deployment = Google::Apis::DeploymentmanagerV2::Deployment.new(target: target_configuration, name: 'ruby-api-deployment') 

# Attempt the deployment 
response = deployment_manager.insert_deployment(PROJECT_ID, deployment) 

希望這可以幫助別人