使用部署管理器的谷歌API时如何设置部署配置?

问题描述:

我试图使用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正确的对象的命令行选项。

我曾了这一点。

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

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) 

希望这可以帮助别人