pksimba
pksimba

Reputation: 193

Google Cloud API Create instance from machine image

I want to create an instance from an image machine. I manage to do this from gcloud SKD:

gcloud beta compute instances create "sandbox" \
    --zone "europe-west1-b" \
    --source-machine-image "projects/my-project-id/global/machineImages/my-machine-image" 

The instance is created without problem.

But when I try to do it through the API, it doesn't work. I have a status response code of 200, the machine tries to start and then an error 400 in the console Activity without additional details.

import requests
import google.auth
import google.auth.transport.requests
from googleapiclient import discovery                                                                                                                                                                              
import json 

def spawn() : 

    config = { 
              "name": "sandbox", 
              "sourceMachineImage": "projects/my-project-id/global/machineImages/my-machine-image"
    }   

    compute = discovery.build('compute', 'beta')

    credentials,pid = google.auth.load_credentials_from_file("cred.json", scopes=['https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/compute', 'https://www.googleapis.com/auth/devstorage.full_control', 'https://www.googleapis.com/auth/devstorage.read_write'])

    request = google.auth.transport.requests.Request()
    credentials.refresh(request)
    token = credentials.token

    headers = {"Authorization" : "Bearer {}".format(token), "Content-Type" :"application/json"}

    r = requests.post("https://www.googleapis.com/compute/beta/projects/my-projet-id/zones/europe-west1-b/instances", data = json.dumps(config), headers = headers)

    print(r.status_code)
    print(r.json())

The error log on google cloud console Activity panel :

Failed : Create VM 
Invalid argument (HTTP 400): INVALID_PARAMETER

Upvotes: 1

Views: 1139

Answers (2)

Junaid Kiyani
Junaid Kiyani

Reputation: 31

Change sourceMachineImage parameter to sourceInstance config = { "name": "sandbox", "sourceInstance": "projects/my-project-id/global/machineImages/my-machine-image" }

Upvotes: 0

DazWilkin
DazWilkin

Reputation: 40061

See Google's Python SDK documentation for Compute Engine.

There's a misplaced example that shows creating an instance under Authorizing Requests

I encourage you to use the Google-provided SDK. It's guaranteed to work, provides some abstractions to make coding easier and eases the auth flow too.

If you really want to use the raw REST API...

there be monsters...

A good way to template any Google API is to use Google's APIs Explorer tool.

For Compute Engine v1 API, you can identify the instances.insert which corresponds to gcloud compute instances create or, if you prefer gcloud beta compute instances create is matched here and, not only do you get the query, request and response objects documented for you but you can try the API in the browser and there are (admittedly trivial) code samples.

For Cloud, the Console often provides REST and command-line equivalents too.

enter image description here

But, please use the SDK ;-)

Upvotes: 1

Related Questions