Skip to content

Commit b23205b

Browse files
committed
Refactoring
1 parent 425a4a8 commit b23205b

File tree

34 files changed

+1210
-1170
lines changed

34 files changed

+1210
-1170
lines changed

Diff for: awsec2instances-capi/awshelper.py

+121-121
Original file line numberDiff line numberDiff line change
@@ -11,142 +11,142 @@
1111
region = 'eu-west-1' # AWS region
1212

1313
def describe_instances():
14-
"""
15-
Describes all EC2 instances associated with an AWS account
16-
"""
17-
# Create an EC2 Client
18-
ec2client = boto3.client('ec2', region_name=region)
19-
# Describe instances
20-
response = ec2client.describe_instances()
21-
print(response)
22-
print('\nInstances:')
23-
for i in response['Reservations']:
24-
for j in i['Instances']:
25-
print('state of the instance "' + j['InstanceId'] + '" is: "' + j['State']['Name'] + '"')
26-
return
14+
"""
15+
Describes all EC2 instances associated with an AWS account
16+
"""
17+
# Create an EC2 Client
18+
ec2_client = boto3.client('ec2', region_name=region)
19+
# Describe instances
20+
response = ec2_client.describe_instances()
21+
print(response)
22+
print('\nInstances:')
23+
for i in response['Reservations']:
24+
for j in i['Instances']:
25+
print('state of the instance "' + j['InstanceId'] + '" is: "' + j['State']['Name'] + '"')
26+
return
2727

2828

2929
def run_instance():
30-
"""
31-
Run an EC2 instance
32-
"""
33-
ami_id = "ami-785db401" # AMI Id
34-
instance_type = "t2.micro" # Instance Type
35-
36-
# Create an EC2 Client
37-
ec2client = boto3.client('ec2', region_name=region)
38-
# Run an instance
39-
response = ec2client.run_instances(ImageId=ami_id, InstanceType=instance_type,
40-
MaxCount=1, MinCount=1)
41-
print(response)
42-
Instance_id = response['Instances'][0]['InstanceId']
43-
print('\nInstance Id: ' + Instance_id)
44-
print('Image Id: ' + response['Instances'][0]['ImageId'])
45-
print('Instance Type: ' + response['Instances'][0]['InstanceType'])
46-
print('State: ' + response['Instances'][0]['State']['Name'])
47-
48-
return Instance_id
30+
"""
31+
Run an EC2 instance
32+
"""
33+
ami_id = "ami-785db401" # AMI Id
34+
instance_type = "t2.micro" # Instance Type
35+
36+
# Create an EC2 Client
37+
ec2_client = boto3.client('ec2', region_name=region)
38+
# Run an instance
39+
response = ec2_client.run_instances(ImageId=ami_id, InstanceType=instance_type,
40+
MaxCount=1, MinCount=1)
41+
print(response)
42+
Instance_id = response['Instances'][0]['InstanceId']
43+
print('\nInstance Id: ' + Instance_id)
44+
print('Image Id: ' + response['Instances'][0]['ImageId'])
45+
print('Instance Type: ' + response['Instances'][0]['InstanceType'])
46+
print('State: ' + response['Instances'][0]['State']['Name'])
47+
48+
return Instance_id
4949

5050

5151
def describe_instance(instance_id):
52-
"""
53-
Describes an EC2 instance
54-
"""
55-
# Create an EC2 Client
56-
ec2client = boto3.client('ec2', region_name=region)
57-
try:
58-
# Describe an instance
59-
response = ec2client.describe_instances(InstanceIds=[instance_id])
60-
print(response)
61-
print('\nInstance Id: ' + instance_id)
62-
print('Instance Id: ' + response['Reservations'][0]['Instances'][0]['InstanceId'])
63-
print('Image Id: ' + response['Reservations'][0]['Instances'][0]['ImageId'])
64-
print('Instance Type: ' + response['Reservations'][0]['Instances'][0]['InstanceType'])
65-
print('State: ' + response['Reservations'][0]['Instances'][0]['State']['Name'])
66-
if response['Reservations'][0]['Instances'][0]['State']['Name'] == 'running':
67-
print('Private DNS Name: ' + response['Reservations'][0]['Instances'][0]['PrivateDnsName'])
68-
print('Private IP: ' + response['Reservations'][0]['Instances'][0]['PrivateIpAddress'])
69-
print('Public DNS Name: ' + response['Reservations'][0]['Instances'][0]['PublicDnsName'])
70-
print('Public IP: ' + response['Reservations'][0]['Instances'][0]['PublicIpAddress'])
71-
except botocore.exceptions.ClientError as e:
72-
if e.response['Error']['Code'] == "MissingParameter":
73-
print("Error: Missing instance id!!")
74-
else:
75-
raise
76-
return
52+
"""
53+
Describes an EC2 instance
54+
"""
55+
# Create an EC2 Client
56+
ec2_client = boto3.client('ec2', region_name=region)
57+
try:
58+
# Describe an instance
59+
response = ec2_client.describe_instances(InstanceIds=[instance_id])
60+
print(response)
61+
print('\nInstance Id: ' + instance_id)
62+
print('Instance Id: ' + response['Reservations'][0]['Instances'][0]['InstanceId'])
63+
print('Image Id: ' + response['Reservations'][0]['Instances'][0]['ImageId'])
64+
print('Instance Type: ' + response['Reservations'][0]['Instances'][0]['InstanceType'])
65+
print('State: ' + response['Reservations'][0]['Instances'][0]['State']['Name'])
66+
if response['Reservations'][0]['Instances'][0]['State']['Name'] == 'running':
67+
print('Private DNS Name: ' + response['Reservations'][0]['Instances'][0]['PrivateDnsName'])
68+
print('Private IP: ' + response['Reservations'][0]['Instances'][0]['PrivateIpAddress'])
69+
print('Public DNS Name: ' + response['Reservations'][0]['Instances'][0]['PublicDnsName'])
70+
print('Public IP: ' + response['Reservations'][0]['Instances'][0]['PublicIpAddress'])
71+
except botocore.exceptions.ClientError as e:
72+
if e.response['Error']['Code'] == "MissingParameter":
73+
print("Error: Missing instance id!!")
74+
else:
75+
raise
76+
return
7777

7878

7979
def start_instance(instance_id):
80-
"""
81-
Start an EC2 instance
82-
"""
83-
# Create an EC2 Client
84-
ec2client = boto3.client('ec2', region_name=region)
85-
try:
86-
# Start an instance
87-
response = ec2client.start_instances(InstanceIds=[instance_id], DryRun=False)
88-
print(response)
89-
print("\nSuccessfully starting instance: ", instance_id)
90-
except botocore.exceptions.ClientError as e:
91-
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
92-
print("Error: Invalid instance id!!")
93-
else:
94-
raise
95-
return
80+
"""
81+
Start an EC2 instance
82+
"""
83+
# Create an EC2 Client
84+
ec2_client = boto3.client('ec2', region_name=region)
85+
try:
86+
# Start an instance
87+
response = ec2_client.start_instances(InstanceIds=[instance_id], DryRun=False)
88+
print(response)
89+
print("\nSuccessfully starting instance: ", instance_id)
90+
except botocore.exceptions.ClientError as e:
91+
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
92+
print("Error: Invalid instance id!!")
93+
else:
94+
raise
95+
return
9696

9797

9898
def stop_instance(instance_id):
99-
"""
100-
Stop an EC2 instance
101-
"""
102-
# Create an EC2 Client
103-
ec2client = boto3.client('ec2', region_name=region)
104-
try:
105-
# Stop an instance
106-
response = ec2client.stop_instances(InstanceIds=[instance_id], DryRun=False)
107-
print(response)
108-
print("\nSuccessfully stopping instance: ", instance_id)
109-
except botocore.exceptions.ClientError as e:
110-
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
111-
print("Error: Invalid instance id!!")
112-
else:
113-
raise
114-
return
99+
"""
100+
Stop an EC2 instance
101+
"""
102+
# Create an EC2 Client
103+
ec2_client = boto3.client('ec2', region_name=region)
104+
try:
105+
# Stop an instance
106+
response = ec2_client.stop_instances(InstanceIds=[instance_id], DryRun=False)
107+
print(response)
108+
print("\nSuccessfully stopping instance: ", instance_id)
109+
except botocore.exceptions.ClientError as e:
110+
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
111+
print("Error: Invalid instance id!!")
112+
else:
113+
raise
114+
return
115115

116116

117117
def reboot_instance(instance_id):
118-
"""
119-
Reboot an EC2 instance
120-
"""
121-
# Create an EC2 Client
122-
ec2client = boto3.client('ec2', region_name=region)
123-
try:
124-
# Reboot an instance
125-
response = ec2client.reboot_instances(InstanceIds=[instance_id], DryRun=False)
126-
print(response)
127-
print("\nSuccessfully rebooting instance: ", instance_id)
128-
except botocore.exceptions.ClientError as e:
129-
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
130-
print("Error: Invalid instance id!!")
131-
else:
132-
raise
133-
return
118+
"""
119+
Reboot an EC2 instance
120+
"""
121+
# Create an EC2 Client
122+
ec2_client = boto3.client('ec2', region_name=region)
123+
try:
124+
# Reboot an instance
125+
response = ec2_client.reboot_instances(InstanceIds=[instance_id], DryRun=False)
126+
print(response)
127+
print("\nSuccessfully rebooting instance: ", instance_id)
128+
except botocore.exceptions.ClientError as e:
129+
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
130+
print("Error: Invalid instance id!!")
131+
else:
132+
raise
133+
return
134134

135135

136136
def terminate_instance(instance_id):
137-
"""
138-
Terminate an EC2 instance
139-
"""
140-
# Create an EC2 Client
141-
ec2client = boto3.client('ec2', region_name=region)
142-
try:
143-
# Terminate an instance
144-
response = ec2client.terminate_instances(InstanceIds=[instance_id], DryRun=False)
145-
print(response)
146-
print("\nSuccessfully terminating instance: ", instance_id)
147-
except botocore.exceptions.ClientError as e:
148-
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
149-
print("Error: Invalid instance id!!")
150-
else:
151-
raise
152-
return
137+
"""
138+
Terminate an EC2 instance
139+
"""
140+
# Create an EC2 Client
141+
ec2_client = boto3.client('ec2', region_name=region)
142+
try:
143+
# Terminate an instance
144+
response = ec2_client.terminate_instances(InstanceIds=[instance_id], DryRun=False)
145+
print(response)
146+
print("\nSuccessfully terminating instance: ", instance_id)
147+
except botocore.exceptions.ClientError as e:
148+
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
149+
print("Error: Invalid instance id!!")
150+
else:
151+
raise
152+
return

Diff for: awsec2instances-capi/ec2instances.py

+40-40
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,50 @@
88
import awshelper
99

1010
def print_menu():
11-
print('\nMENU')
12-
print('0 = Quit')
13-
print('1 = Describe all instances')
14-
print('2 = Run new instance')
15-
print('3 = Describe instance')
16-
print('4 = Start instance')
17-
print('5 = Stop instance')
18-
print('6 = Reboot instance')
19-
print('7 = Terminate instance')
20-
return
11+
print('\nMENU')
12+
print('0 = Quit')
13+
print('1 = Describe all instances')
14+
print('2 = Run new instance')
15+
print('3 = Describe instance')
16+
print('4 = Start instance')
17+
print('5 = Stop instance')
18+
print('6 = Reboot instance')
19+
print('7 = Terminate instance')
20+
return
2121

2222

2323
def main():
24-
instance_id = ''
25-
option = -1
26-
27-
while option != 0:
28-
print_menu()
29-
try:
30-
option = int(input('\nEnter an option? '))
31-
if option == 0:
32-
print('Bye')
33-
elif option == 1:
34-
awshelper.describe_instances()
35-
elif option == 2:
36-
instance_id = awshelper.run_instance()
37-
elif option == 3:
38-
awshelper.describe_instance(instance_id)
39-
elif option == 4:
40-
awshelper.start_instance(instance_id)
41-
elif option == 5:
42-
awshelper.stop_instance(instance_id)
43-
elif option == 6:
44-
awshelper.reboot_instance(instance_id)
45-
elif option == 7:
46-
awshelper.terminate_instance(instance_id)
47-
else:
48-
print('\nERROR: Enter a valid option!!')
49-
except ValueError:
50-
print('\nERROR: Enter a valid option!!')
51-
52-
return
24+
instance_id = ''
25+
option = -1
26+
27+
while option != 0:
28+
print_menu()
29+
try:
30+
option = int(input('\nEnter an option? '))
31+
if option == 0:
32+
print('Bye')
33+
elif option == 1:
34+
awshelper.describe_instances()
35+
elif option == 2:
36+
instance_id = awshelper.run_instance()
37+
elif option == 3:
38+
awshelper.describe_instance(instance_id)
39+
elif option == 4:
40+
awshelper.start_instance(instance_id)
41+
elif option == 5:
42+
awshelper.stop_instance(instance_id)
43+
elif option == 6:
44+
awshelper.reboot_instance(instance_id)
45+
elif option == 7:
46+
awshelper.terminate_instance(instance_id)
47+
else:
48+
print('\nERROR: Enter a valid option!!')
49+
except ValueError:
50+
print('\nERROR: Enter a valid option!!')
51+
52+
return
5353

5454

5555
# This is the standard boilerplate that calls the main() function.
5656
if __name__ == '__main__':
57-
main()
57+
main()

0 commit comments

Comments
 (0)