Last active
July 21, 2020 07:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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