Skip to content

Instantly share code, notes, and snippets.

@vladwa
Last active July 21, 2020 07:56
Show Gist options
  • Save vladwa/fb1d92767f8d44d07d9ed3c187aca33a to your computer and use it in GitHub Desktop.
Save vladwa/fb1d92767f8d44d07d9ed3c187aca33a to your computer and use it in GitHub Desktop.
Python code snippet to fetch the compute instance details by connecting to Google API Client using Python client library for Google's discovery.
#vars
# SERVICE_ACCOUNT_FILE --> Service Account Json file
# project --> Gcp Project Name
# zone --> zone in which instance is created
# instance_name --> Instance name
from google.oauth2 import service_account
from googleapiclient import discovery
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
SERVICE_ACCOUNT_FILE = "account.json" ##Service Account Json file
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = discovery.build('compute', 'v1',credentials=credentials)
service = get_compute_service()
request = service.instances().get(project=project,zone=zone,instance=instance_name)
instance_response = request.execute()
print instance_response['networkInterfaces'][0]['networkIP'] # print the instnace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment