Skip to content

Instantly share code, notes, and snippets.

@feczo
Last active August 29, 2015 14:02
Show Gist options
  • Save feczo/94acaa9e3c05d634e472 to your computer and use it in GitHub Desktop.
Save feczo/94acaa9e3c05d634e472 to your computer and use it in GitHub Desktop.
Clone a GCE instance directly through the API

Section A = cloning the disk

  1. go to the cloud console https://console.developers.google.com
  2. go to snapshots and click on 'New snapshot'
  3. select your source disk
  4. Take note of the Name field and click 'Create'
  5. go to disks click on 'New disk'
  6. flip Source Type to Snapshot
  7. make sure you define the zone matching where the instance is going to be hosted
  8. select source snapshot created at step 4
  9. take note of the Name field and click 'Create'
  10. delete your snapshot created at step 4

Section B = creating the instance

  1. go to the cloud console https://console.developers.google.com
  2. select your project and instance you will end up at a location like: console.developers.google.com/project/[project number]/compute/instancesDetail/zones/[zone name]/instances/[instance name]
  3. scrool down to the bottom of the page
  4. click on 'Equivalent REST'
  5. copy the JSON contents of the popup window
  6. go to https://developers.google.com/apis-explorer/#p/compute/v1/compute.instances.insert
  7. flip 'Authorize requests using OAuth 2.0' to ON in the top right corner
  8. in the top right corner of the 'Request body' field pull down the triangle menu and select the Freeform editor
  9. fill in the project and zone fields
  10. paste your JSON copied at step 5
  11. make sure you remove any orphan comas in JSON lists while you are doing the next step Example:

removing from natIP line requires to remove the , at the end of the name line

   {
      "kind": "compute#accessConfig",
      "type": "ONE_TO_ONE_NAT",
      "name": "External NAT",
      "natIP": "130.211.71.121"
    }

result should be

   {
      "kind": "compute#accessConfig",
      "type": "ONE_TO_ONE_NAT",
      "name": "External NAT"
    }
  1. remove the lines starting with the below key:
  • id
  • status
  • creationTimestamp
  • fingerprint (two of them)
  • natIP
  • networkIP
  • selfLink
  • deviceName
  • $$hashKey
  1. change the name
  2. update disks / source to the disk cloned in section A step 9
  3. optionally edit any other options you wish to change

if you were not careful at step 11, you will get a response of:

400 Bad Request

  • Show headers -

{ "error": { "errors": [ { "domain": "global", "reason": "parseError",

you can use a validator like below to get hints where you made the mistake http://jsonformatter.curiousconcept.com/

if everything went well you will receive:

Response

200 OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment