Ruby 使用Google API for deployment Manager时,如何设置部署配置?
我试图使用Ruby Google API客户端在Google计算平台(GCP)上创建一个部署 我有一个用于配置的YAML文件:Ruby 使用Google API for deployment Manager时,如何设置部署配置?,ruby,google-api,google-compute-engine,Ruby,Google Api,Google Compute Engine,我试图使用Ruby Google API客户端在Google计算平台(GCP)上创建一个部署 我有一个用于配置的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
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
命令行工具测试了这一点
我现在想在Ruby中使用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])
所有这一切都是因为我经过了身份验证,并且我有一个部署管理器
对象可以使用
我想使用具有以下签名的insert\u部署
方法:
#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\u对象的类型是“Google::api::DeploymentmanagerV2::deployment”。我可以创建这个对象,但是我不知道如何将YAML文件导入到这个对象中,以便能够以编程方式执行部署
还有另一个名为ConfigFile
的类,它似乎类似于指定--config
的命令行选项,但我同样不知道如何将文件加载到此类中,也不知道如何将其转换为插入部署的正确对象
需要嵌套不同的类,以便提取配置。例如:
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)
希望这对别人有帮助