Skip to content

Commit 1ac28da

Browse files
committed
Refactoring
1 parent 30e1951 commit 1ac28da

File tree

17 files changed

+635
-621
lines changed

17 files changed

+635
-621
lines changed

Diff for: gcloudcomputeengine/computeenginehelper.py

+145-145
Original file line numberDiff line numberDiff line change
@@ -15,182 +15,182 @@
1515

1616

1717
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')
2323

24-
print('Listing VM instances ...')
24+
print('Listing VM instances ...')
2525

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')
3737

38-
return
38+
return
3939

4040

4141
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']
100100

101101

102102
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')
108108

109-
print('Listing VM instance ...')
110-
print('Instance Id: ' + instance_id)
109+
print('Listing VM instance ...')
110+
print('Instance Id: ' + instance_id)
111111

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()
114114

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'])
119119

120-
return
120+
return
121121

122122

123123
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')
129129

130-
print('Starting VM instance ...')
131-
print('Instance Id: ' + instance_id)
130+
print('Starting VM instance ...')
131+
print('Instance Id: ' + instance_id)
132132

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()
138138

139-
return
139+
return
140140

141141

142142
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')
148148

149-
print('Stopping VM instance ...')
150-
print('Instance Id: ' + instance_id)
149+
print('Stopping VM instance ...')
150+
print('Instance Id: ' + instance_id)
151151

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()
157157

158-
return
158+
return
159159

160160

161161
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')
167167

168-
print('Resetting VM instance ...')
169-
print('Instance Id: ' + instance_id)
168+
print('Resetting VM instance ...')
169+
print('Instance Id: ' + instance_id)
170170

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()
176176

177-
return
177+
return
178178

179179

180180
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

Comments
 (0)