chef-boneyard / chef-provisioning-google

Chef Provisioning driver for the Google Cloud Platform
Apache License 2.0
8 stars 8 forks source link

Allow specifying subnetworks when creating VMs #11

Closed erjohnso closed 7 years ago

erjohnso commented 8 years ago

New VMs should support setting network parameters such as network, subnetwork, and specified private IP address.

erjohnso commented 7 years ago

Going to close this issue out also. Specifying subnetworks at instance create time is supported like any other GCE instance parameter. For the following snippet, I pre-created a custom network and subnet in the web console. From there, I used the web console UI to walk through setting all desired instance variables, and then copied the generated REST body into the script below. I used the ruby module to convert the JSON resource request body into a ruby data structure.

NOTE: The only change I made to the JSON resource was to remove the name attribute since the instance name is specified with the chef-provisioning stanza.

require "chef/provisioning/google_driver"
require "json"

with_driver "google:us-central1-f:graphite-demos",
  :google_credentials => {
    :json_key_path => "/home/erjohnso/chef-prov-sa.json",
    :google_client_email => "chef-prov-sa@graphite-demos.iam.gserviceaccount.com"
  }

google_key_pair "chef_default" do
  private_key_path "chef_test"
  public_key_path "chef_test.pub"
end

json_body = '{
  "zone": "projects/graphite-demos/zones/us-central1-f",
  "machineType": "projects/graphite-demos/zones/us-central1-f/machineTypes/n1-standard-1",
  "metadata": {
    "items": [
      {
        "key": "key1",
        "value": "val1"
      },
      {
        "key": "key2",
        "value": "val2"
      }
    ]
  },
  "tags": {
    "items": [
      "tags",
      "are",
      "supported"
    ]
  },
  "disks": [
    {
      "type": "PERSISTENT",
      "boot": true,
      "mode": "READ_WRITE",
      "autoDelete": true,
      "deviceName": "chef-prov-test",
      "initializeParams": {
        "sourceImage": "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20161027",
        "diskType": "projects/graphite-demos/zones/us-central1-f/diskTypes/pd-standard",
        "diskSizeGb": "10"
      }
    },
    {
      "type": "PERSISTENT",
      "mode": "READ_WRITE",
      "source": "projects/graphite-demos/zones/us-central1-f/disks/new-disk-test",
      "autoDelete": true,
      "boot": false,
      "interface": "SCSI",
      "deviceName": "new-disk-test"
    }
  ],
  "canIpForward": false,
  "networkInterfaces": [
    {
      "network": "projects/graphite-demos/global/networks/opsmgr",
      "subnetwork": "projects/graphite-demos/regions/us-central1/subnetworks/opsmgr-subnet",
      "accessConfigs": [
        {
          "name": "External NAT",
          "type": "ONE_TO_ONE_NAT"
        }
      ]
    }
  ],
  "description": "Test instance via chef-provisioning-google",
  "scheduling": {
    "preemptible": false,
    "onHostMaintenance": "MIGRATE",
    "automaticRestart": true
  },
  "serviceAccounts": [
    {
      "email": "default",
      "scopes": [
        "https://www.googleapis.com/auth/cloud-platform"
      ]
    }
  ]
}'

ruby_body = JSON.parse(json_body)

machine "chef-prov-test" do
  machine_options insert_options: ruby_body,
  key_name: "chef_test"
  action [:converge]
end

Creating the instance with chef-provisioner worked as expected. And per the issue title, it's clear that the instance was created in a custom network (opsmgr) and subnet (opsmgr-subnet) 👍 .