使用Google API进行部署管理器时,如何设置部署配置



我正在尝试使用Ruby Google API客户端在Google Compute Platform(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
      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在Ruby中进行此操作。我有以下代码:

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类型是'Google :: Apis :: 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)

希望这对某人有帮助

最新更新