1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
import boto.ec2 |
4
|
|
|
import boto.route53 |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class FieldLists(): |
8
|
|
|
ADDRESS = ['public_ip', 'instance_id', 'domain', 'allocation_id', 'association_id', |
9
|
|
|
'network_interface_id', 'network_interface_owner_id', 'private_ip_address'] |
10
|
|
|
INSTANCE = ['id', 'public_dns_name', 'private_dns_name', 'state', 'state_code', |
11
|
|
|
'previous_state', 'previous_state_code', 'key_name', 'instance_type', |
12
|
|
|
'launch_time', 'image_id', 'placement', 'placement_group', 'placement_tenancy', |
13
|
|
|
'kernel', 'ramdisk', 'architecture', 'hypervisor', 'virtualization_type', |
14
|
|
|
'ami_launch_index', 'monitored', 'monitoring_state', 'spot_instance_request_id', |
15
|
|
|
'subnet_id', 'vpc_id', 'private_ip_address', 'ip_address', 'platform', |
16
|
|
|
'root_device_name', 'root_device_type', 'state_reason'] |
17
|
|
|
VOLUME = ['id', 'create_time', 'status', 'size', 'snapshot_id', 'zone', 'type', 'iops', |
18
|
|
|
'encrypted'] |
19
|
|
|
EC2ZONE = ['name', 'state', 'region_name', 'messages'] |
20
|
|
|
RECORD = ['alias_dns_name', 'alias_evaluate_target_health', 'alias_hosted_zone_id', 'failover', |
21
|
|
|
'health_check', 'identifier', 'name', 'region', 'resource_records', 'ttl', 'type', |
22
|
|
|
'weight'] |
23
|
|
|
R53ZONE = ['callerreference', 'config', 'id', 'name', 'resourcerecordsetcount'] |
24
|
|
|
R53STATUS = ['comment', 'id', 'status', 'submittedat'] |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class ResultSets(object): |
28
|
|
|
|
29
|
|
|
def __init__(self): |
30
|
|
|
self.foo = "" |
31
|
|
|
|
32
|
|
|
def selector(self, output): |
33
|
|
|
if isinstance(output, boto.ec2.instance.Reservation): |
34
|
|
|
return self.parseReservation(output) |
35
|
|
|
elif isinstance(output, boto.ec2.instance.Instance): |
36
|
|
|
return self.parseInstance(output) |
37
|
|
|
elif isinstance(output, boto.ec2.volume.Volume): |
38
|
|
|
return self.parseVolume(output) |
39
|
|
|
elif isinstance(output, boto.ec2.zone.Zone): |
40
|
|
|
return self.parseEC2Zone(output) |
41
|
|
|
elif isinstance(output, boto.ec2.address.Address): |
42
|
|
|
return self.parseAddress(output) |
43
|
|
|
elif isinstance(output, boto.route53.record.Record): |
44
|
|
|
return self.parseRecord(output) |
45
|
|
|
elif isinstance(output, boto.route53.zone.Zone): |
46
|
|
|
return self.parseR53Zone(output) |
47
|
|
|
elif isinstance(output, boto.route53.status.Status): |
48
|
|
|
return self.parseR53Status(output) |
49
|
|
|
else: |
50
|
|
|
return output |
51
|
|
|
|
52
|
|
|
def formatter(self, output): |
53
|
|
|
formatted = [] |
54
|
|
|
if isinstance(output, list): |
55
|
|
|
for o in output: |
56
|
|
|
formatted.append(self.selector(o)) |
57
|
|
|
else: |
58
|
|
|
formatted.append(self.selector(output)) |
59
|
|
|
return formatted |
60
|
|
|
|
61
|
|
|
def parseReservation(self, output): |
62
|
|
|
instance_list = [] |
63
|
|
|
for instance in output.instances: |
64
|
|
|
instance_data = self.parseInstance(instance) |
65
|
|
|
instance_data['owner_id'] = output.owner_id |
66
|
|
|
instance_list.append(instance_data) |
67
|
|
|
return instance_list |
68
|
|
|
|
69
|
|
|
def parseAddress(self, output): |
70
|
|
|
instance_data = {field: getattr(output, field) for field in FieldLists.ADDRESS} |
71
|
|
|
return instance_data |
72
|
|
|
|
73
|
|
|
def parseInstance(self, output): |
74
|
|
|
instance_data = {field: getattr(output, field) for field in FieldLists.INSTANCE} |
75
|
|
|
return instance_data |
76
|
|
|
|
77
|
|
|
def parseVolume(self, output): |
78
|
|
|
volume_data = {field: getattr(output, field) for field in FieldLists.VOLUME} |
79
|
|
|
return volume_data |
80
|
|
|
|
81
|
|
|
def parseEC2Zone(self, output): |
82
|
|
|
zone_data = {field: getattr(output, field) for field in FieldLists.EC2ZONE} |
83
|
|
|
return zone_data |
84
|
|
|
|
85
|
|
|
def parseRecord(self, output): |
86
|
|
|
record_data = {field: getattr(output, field) for field in FieldLists.RECORD} |
87
|
|
|
return record_data |
88
|
|
|
|
89
|
|
|
def parseR53Zone(self, output): |
90
|
|
|
zone_data = {field: getattr(output, field) for field in FieldLists.R53ZONE} |
91
|
|
|
return zone_data |
92
|
|
|
|
93
|
|
|
def parseR53Status(self, output): |
94
|
|
|
status_data = {field: getattr(output, field) for field in FieldLists.R53STATUS} |
95
|
|
|
return status_data |
96
|
|
|
|