|
15 | 15 |
|
16 | 16 |
|
17 | 17 | def list_instances():
|
18 |
| - """ |
19 |
| - List all Compute Engine VM instances associated with an Google Cloud account |
20 |
| - """ |
21 |
| - # Build and initialize the API |
22 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
| 18 | + """ |
| 19 | + List all Compute Engine VM instances associated with an Google Cloud account |
| 20 | + """ |
| 21 | + # Build and initialize the API |
| 22 | + compute = googleapiclient.discovery.build('compute', 'v1') |
23 | 23 |
|
24 |
| - print('Listing VM instances ...') |
| 24 | + print('Listing VM instances ...') |
25 | 25 |
|
26 |
| - # Describe instances |
27 |
| - response = compute.instances().list(project=PROJECT_NAME, zone=ZONE_NAME).execute() |
28 |
| - print('Instances in project "%s" and zone "%s":' % (PROJECT_NAME, ZONE_NAME)) |
29 |
| - if (response.get('items')): |
30 |
| - for instance in response['items']: |
31 |
| - print(' - Id: ' + instance['id']) |
32 |
| - print(' Name: ' + instance['name']) |
33 |
| - print(' Status: ' + instance['status']) |
34 |
| - print(' Machine type: ' + instance['machineType']) |
35 |
| - else: |
36 |
| - print('NO instances') |
| 26 | + # Describe instances |
| 27 | + response = compute.instances().list(project=PROJECT_NAME, zone=ZONE_NAME).execute() |
| 28 | + print('Instances in project "%s" and zone "%s":' % (PROJECT_NAME, ZONE_NAME)) |
| 29 | + if (response.get('items')): |
| 30 | + for instance in response['items']: |
| 31 | + print(' - Id: ' + instance['id']) |
| 32 | + print(' Name: ' + instance['name']) |
| 33 | + print(' Status: ' + instance['status']) |
| 34 | + print(' Machine type: ' + instance['machineType']) |
| 35 | + else: |
| 36 | + print('NO instances') |
37 | 37 |
|
38 |
| - return |
| 38 | + return |
39 | 39 |
|
40 | 40 |
|
41 | 41 | def create_instance():
|
42 |
| - """ |
43 |
| - Create a Compute Engine VM instance |
44 |
| - """ |
45 |
| - # Build and initialize the API |
46 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
47 |
| - |
48 |
| - print('Creating VM instance ...') |
49 |
| - |
50 |
| - # Get the latest image |
51 |
| - image_response = compute.images().getFromFamily( |
52 |
| - project=IMAGE_PROJECT_NAME, family=IMAGE_NAME).execute() |
53 |
| - source_disk_image = image_response['selfLink'] |
54 |
| - |
55 |
| - # Configure the machine |
56 |
| - #machine_type = 'zones/' + ZONE_NAME + '/machineTypes/' + INSTANCE_TYPE |
57 |
| - machine_type = 'zones/%s/machineTypes/%s' % (ZONE_NAME, INSTANCE_TYPE) |
58 |
| - |
59 |
| - config = { |
60 |
| - 'name': INSTANCE_NAME, |
61 |
| - 'machineType': machine_type, |
62 |
| - |
63 |
| - # Specify the boot disk and the image to use as a source. |
64 |
| - 'disks': [ |
65 |
| - { |
66 |
| - 'boot': True, |
67 |
| - 'autoDelete': True, |
68 |
| - 'initializeParams': { |
69 |
| - 'sourceImage': source_disk_image, |
70 |
| - } |
71 |
| - } |
72 |
| - ], |
73 |
| - |
74 |
| - # Specify a network interface with NAT to access the public |
75 |
| - # internet. |
76 |
| - 'networkInterfaces': [{ |
77 |
| - 'network': 'global/networks/default', |
78 |
| - 'accessConfigs': [ |
79 |
| - {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'} |
80 |
| - ] |
81 |
| - }], |
82 |
| - |
83 |
| - # Allow the instance to access cloud storage and logging. |
84 |
| - 'serviceAccounts': [{ |
85 |
| - 'email': 'default', |
86 |
| - 'scopes': [ |
87 |
| - 'https://www.googleapis.com/auth/devstorage.read_write', |
88 |
| - 'https://www.googleapis.com/auth/logging.write' |
89 |
| - ] |
90 |
| - }] |
91 |
| - } |
92 |
| - |
93 |
| - response = compute.instances().insert(project=PROJECT_NAME, |
94 |
| - zone=ZONE_NAME, |
95 |
| - body=config).execute() |
96 |
| - |
97 |
| - print('Instance Id: ' + response['targetId']) |
98 |
| - |
99 |
| - return response['targetId'] |
| 42 | + """ |
| 43 | + Create a Compute Engine VM instance |
| 44 | + """ |
| 45 | + # Build and initialize the API |
| 46 | + compute = googleapiclient.discovery.build('compute', 'v1') |
| 47 | + |
| 48 | + print('Creating VM instance ...') |
| 49 | + |
| 50 | + # Get the latest image |
| 51 | + image_response = compute.images().getFromFamily( |
| 52 | + project=IMAGE_PROJECT_NAME, family=IMAGE_NAME).execute() |
| 53 | + source_disk_image = image_response['selfLink'] |
| 54 | + |
| 55 | + # Configure the machine |
| 56 | + #machine_type = 'zones/' + ZONE_NAME + '/machineTypes/' + INSTANCE_TYPE |
| 57 | + machine_type = 'zones/%s/machineTypes/%s' % (ZONE_NAME, INSTANCE_TYPE) |
| 58 | + |
| 59 | + config = { |
| 60 | + 'name': INSTANCE_NAME, |
| 61 | + 'machineType': machine_type, |
| 62 | + |
| 63 | + # Specify the boot disk and the image to use as a source. |
| 64 | + 'disks': [ |
| 65 | + { |
| 66 | + 'boot': True, |
| 67 | + 'autoDelete': True, |
| 68 | + 'initializeParams': { |
| 69 | + 'sourceImage': source_disk_image, |
| 70 | + } |
| 71 | + } |
| 72 | + ], |
| 73 | + |
| 74 | + # Specify a network interface with NAT to access the public |
| 75 | + # internet. |
| 76 | + 'networkInterfaces': [{ |
| 77 | + 'network': 'global/networks/default', |
| 78 | + 'accessConfigs': [ |
| 79 | + {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'} |
| 80 | + ] |
| 81 | + }], |
| 82 | + |
| 83 | + # Allow the instance to access cloud storage and logging. |
| 84 | + 'serviceAccounts': [{ |
| 85 | + 'email': 'default', |
| 86 | + 'scopes': [ |
| 87 | + 'https://www.googleapis.com/auth/devstorage.read_write', |
| 88 | + 'https://www.googleapis.com/auth/logging.write' |
| 89 | + ] |
| 90 | + }] |
| 91 | + } |
| 92 | + |
| 93 | + response = compute.instances().insert(project=PROJECT_NAME, |
| 94 | + zone=ZONE_NAME, |
| 95 | + body=config).execute() |
| 96 | + |
| 97 | + print('Instance Id: ' + response['targetId']) |
| 98 | + |
| 99 | + return response['targetId'] |
100 | 100 |
|
101 | 101 |
|
102 | 102 | def list_instance(instance_id):
|
103 |
| - """ |
104 |
| - List a Compute Engine VM instance |
105 |
| - """ |
106 |
| - # Build and initialize the API |
107 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
| 103 | + """ |
| 104 | + List a Compute Engine VM instance |
| 105 | + """ |
| 106 | + # Build and initialize the API |
| 107 | + compute = googleapiclient.discovery.build('compute', 'v1') |
108 | 108 |
|
109 |
| - print('Listing VM instance ...') |
110 |
| - print('Instance Id: ' + instance_id) |
| 109 | + print('Listing VM instance ...') |
| 110 | + print('Instance Id: ' + instance_id) |
111 | 111 |
|
112 |
| - # List the VM instance |
113 |
| - response = compute.instances().get(project=PROJECT_NAME, zone=ZONE_NAME, instance=INSTANCE_NAME).execute() |
| 112 | + # List the VM instance |
| 113 | + response = compute.instances().get(project=PROJECT_NAME, zone=ZONE_NAME, instance=INSTANCE_NAME).execute() |
114 | 114 |
|
115 |
| - print(' - Id: ' + response['id']) |
116 |
| - print(' Name: ' + response['name']) |
117 |
| - print(' Status: ' + response['status']) |
118 |
| - print(' Machine type: ' + response['machineType']) |
| 115 | + print(' - Id: ' + response['id']) |
| 116 | + print(' Name: ' + response['name']) |
| 117 | + print(' Status: ' + response['status']) |
| 118 | + print(' Machine type: ' + response['machineType']) |
119 | 119 |
|
120 |
| - return |
| 120 | + return |
121 | 121 |
|
122 | 122 |
|
123 | 123 | def start_instance(instance_id):
|
124 |
| - """ |
125 |
| - Start a Compute Engine VM instance |
126 |
| - """ |
127 |
| - # Build and initialize the API |
128 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
| 124 | + """ |
| 125 | + Start a Compute Engine VM instance |
| 126 | + """ |
| 127 | + # Build and initialize the API |
| 128 | + compute = googleapiclient.discovery.build('compute', 'v1') |
129 | 129 |
|
130 |
| - print('Starting VM instance ...') |
131 |
| - print('Instance Id: ' + instance_id) |
| 130 | + print('Starting VM instance ...') |
| 131 | + print('Instance Id: ' + instance_id) |
132 | 132 |
|
133 |
| - # Start VM instance |
134 |
| - compute.instances().start( |
135 |
| - project=PROJECT_NAME, |
136 |
| - zone=ZONE_NAME, |
137 |
| - instance=INSTANCE_NAME).execute() |
| 133 | + # Start VM instance |
| 134 | + compute.instances().start( |
| 135 | + project=PROJECT_NAME, |
| 136 | + zone=ZONE_NAME, |
| 137 | + instance=INSTANCE_NAME).execute() |
138 | 138 |
|
139 |
| - return |
| 139 | + return |
140 | 140 |
|
141 | 141 |
|
142 | 142 | def stop_instance(instance_id):
|
143 |
| - """ |
144 |
| - Stop a Compute Engine VM instance |
145 |
| - """ |
146 |
| - # Build and initialize the API |
147 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
| 143 | + """ |
| 144 | + Stop a Compute Engine VM instance |
| 145 | + """ |
| 146 | + # Build and initialize the API |
| 147 | + compute = googleapiclient.discovery.build('compute', 'v1') |
148 | 148 |
|
149 |
| - print('Stopping VM instance ...') |
150 |
| - print('Instance Id: ' + instance_id) |
| 149 | + print('Stopping VM instance ...') |
| 150 | + print('Instance Id: ' + instance_id) |
151 | 151 |
|
152 |
| - # Stop VM instance |
153 |
| - compute.instances().stop( |
154 |
| - project=PROJECT_NAME, |
155 |
| - zone=ZONE_NAME, |
156 |
| - instance=INSTANCE_NAME).execute() |
| 152 | + # Stop VM instance |
| 153 | + compute.instances().stop( |
| 154 | + project=PROJECT_NAME, |
| 155 | + zone=ZONE_NAME, |
| 156 | + instance=INSTANCE_NAME).execute() |
157 | 157 |
|
158 |
| - return |
| 158 | + return |
159 | 159 |
|
160 | 160 |
|
161 | 161 | def reset_instance(instance_id):
|
162 |
| - """ |
163 |
| - Reset a Compute Engine VM instance |
164 |
| - """ |
165 |
| - # Build and initialize the API |
166 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
| 162 | + """ |
| 163 | + Reset a Compute Engine VM instance |
| 164 | + """ |
| 165 | + # Build and initialize the API |
| 166 | + compute = googleapiclient.discovery.build('compute', 'v1') |
167 | 167 |
|
168 |
| - print('Resetting VM instance ...') |
169 |
| - print('Instance Id: ' + instance_id) |
| 168 | + print('Resetting VM instance ...') |
| 169 | + print('Instance Id: ' + instance_id) |
170 | 170 |
|
171 |
| - # Reset VM instance |
172 |
| - compute.instances().reset( |
173 |
| - project=PROJECT_NAME, |
174 |
| - zone=ZONE_NAME, |
175 |
| - instance=INSTANCE_NAME).execute() |
| 171 | + # Reset VM instance |
| 172 | + compute.instances().reset( |
| 173 | + project=PROJECT_NAME, |
| 174 | + zone=ZONE_NAME, |
| 175 | + instance=INSTANCE_NAME).execute() |
176 | 176 |
|
177 |
| - return |
| 177 | + return |
178 | 178 |
|
179 | 179 |
|
180 | 180 | def delete_instance(instance_id):
|
181 |
| - """ |
182 |
| - Delete a Compute Engine VM instance |
183 |
| - """ |
184 |
| - # Build and initialize the API |
185 |
| - compute = googleapiclient.discovery.build('compute', 'v1') |
186 |
| - |
187 |
| - print('Deleting VM instance ...') |
188 |
| - print('Instance Id: ' + instance_id) |
189 |
| - |
190 |
| - # Delete VM instance |
191 |
| - compute.instances().delete( |
192 |
| - project=PROJECT_NAME, |
193 |
| - zone=ZONE_NAME, |
194 |
| - instance=INSTANCE_NAME).execute() |
195 |
| - |
196 |
| - return |
| 181 | + """ |
| 182 | + Delete a Compute Engine VM instance |
| 183 | + """ |
| 184 | + # Build and initialize the API |
| 185 | + compute = googleapiclient.discovery.build('compute', 'v1') |
| 186 | + |
| 187 | + print('Deleting VM instance ...') |
| 188 | + print('Instance Id: ' + instance_id) |
| 189 | + |
| 190 | + # Delete VM instance |
| 191 | + compute.instances().delete( |
| 192 | + project=PROJECT_NAME, |
| 193 | + zone=ZONE_NAME, |
| 194 | + instance=INSTANCE_NAME).execute() |
| 195 | + |
| 196 | + return |
0 commit comments