1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
import boto |
4
|
|
|
import six |
5
|
|
|
#import boto.ec2 |
6
|
|
|
#import boto.route53 |
7
|
|
|
#import boto.s3 |
8
|
|
|
#import boto.vpc |
9
|
|
|
|
10
|
|
|
class FieldLists(): |
11
|
|
|
ADDRESS = ['public_ip', 'instance_id', 'domain', 'allocation_id', 'association_id', |
12
|
|
|
'network_interface_id', 'network_interface_owner_id', 'private_ip_address'] |
13
|
|
|
BUCKET = ["LoggingGroup", "connection", "creation_date", "name"] |
14
|
|
|
INSTANCE = ['id', 'public_dns_name', 'private_dns_name', 'state', 'state_code', |
15
|
|
|
'previous_state', 'previous_state_code', 'key_name', 'instance_type', |
16
|
|
|
'launch_time', 'image_id', 'placement', 'placement_group', 'placement_tenancy', |
17
|
|
|
'kernel', 'ramdisk', 'architecture', 'hypervisor', 'virtualization_type', |
18
|
|
|
'ami_launch_index', 'monitored', 'monitoring_state', 'spot_instance_request_id', |
19
|
|
|
'subnet_id', 'vpc_id', 'private_ip_address', 'ip_address', 'platform', |
20
|
|
|
'root_device_name', 'root_device_type', 'state_reason'] |
21
|
|
|
VOLUME = ['id', 'create_time', 'status', 'size', 'snapshot_id', 'zone', 'type', 'iops', |
22
|
|
|
'encrypted'] |
23
|
|
|
EC2ZONE = ['name', 'state', 'region_name', 'messages'] |
24
|
|
|
RECORD = ['alias_dns_name', 'alias_evaluate_target_health', 'alias_hosted_zone_id', 'failover', |
25
|
|
|
'health_check', 'identifier', 'name', 'region', 'resource_records', 'ttl', 'type', |
26
|
|
|
'weight'] |
27
|
|
|
R53ZONE = ['callerreference', 'config', 'id', 'name', 'resourcerecordsetcount'] |
28
|
|
|
R53STATUS = ['comment', 'id', 'status', 'submittedat'] |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class ResultSets(object): |
32
|
|
|
|
33
|
|
|
def __init__(self): |
34
|
|
|
self.foo = "" |
35
|
|
|
|
36
|
|
|
def selector(self, output): |
37
|
|
|
if isinstance(output, boto.ec2.instance.Reservation): |
38
|
|
|
return self.parseReservation(output) |
39
|
|
|
elif isinstance(output, boto.ec2.instance.Instance): |
40
|
|
|
return self.parseInstance(output) |
41
|
|
|
elif isinstance(output, boto.ec2.volume.Volume): |
42
|
|
|
return self.parseVolume(output) |
43
|
|
|
elif isinstance(output, boto.ec2.zone.Zone): |
44
|
|
|
return self.parseEC2Zone(output) |
45
|
|
|
elif isinstance(output, boto.ec2.address.Address): |
46
|
|
|
return self.parseAddress(output) |
47
|
|
|
elif isinstance(output, boto.route53.record.Record): |
48
|
|
|
return self.parseRecord(output) |
49
|
|
|
elif isinstance(output, boto.route53.zone.Zone): |
50
|
|
|
return self.parseR53Zone(output) |
51
|
|
|
elif isinstance(output, boto.route53.status.Status): |
52
|
|
|
return self.parseR53Status(output) |
53
|
|
|
elif isinstance(output, boto.ec2.ec2object.EC2Object): |
54
|
|
|
return self.parseEC2Object(output) |
55
|
|
|
#elif isinstance(output, boto.s3.bucket.Bucket): |
56
|
|
|
# return self.parseBucket(output) |
57
|
|
|
else: |
58
|
|
|
return output |
59
|
|
|
|
60
|
|
|
def formatter(self, output): |
61
|
|
|
formatted = [] |
62
|
|
|
if isinstance(output, list): |
63
|
|
|
for o in output: |
64
|
|
|
formatted.append(self.selector(o)) |
65
|
|
|
else: |
66
|
|
|
formatted.append(self.selector(output)) |
67
|
|
|
return formatted |
68
|
|
|
|
69
|
|
|
def parseReservation(self, output): |
70
|
|
|
instance_list = [] |
71
|
|
|
for instance in output.instances: |
72
|
|
|
instance_data = self.parseInstance(instance) |
73
|
|
|
instance_data['owner_id'] = output.owner_id |
74
|
|
|
instance_list.append(instance_data) |
75
|
|
|
return instance_list |
76
|
|
|
|
77
|
|
|
def parseAddress(self, output): |
78
|
|
|
instance_data = {field: getattr(output, field) for field in FieldLists.ADDRESS} |
79
|
|
|
return instance_data |
80
|
|
|
|
81
|
|
|
def parseInstance(self, output): |
82
|
|
|
instance_data = {field: getattr(output, field) for field in FieldLists.INSTANCE} |
83
|
|
|
return instance_data |
84
|
|
|
|
85
|
|
|
def parseVolume(self, output): |
86
|
|
|
volume_data = {field: getattr(output, field) for field in FieldLists.VOLUME} |
87
|
|
|
return volume_data |
88
|
|
|
|
89
|
|
|
def parseEC2Zone(self, output): |
90
|
|
|
zone_data = {field: getattr(output, field) for field in FieldLists.EC2ZONE} |
91
|
|
|
return zone_data |
92
|
|
|
|
93
|
|
|
def parseRecord(self, output): |
94
|
|
|
record_data = {field: getattr(output, field) for field in FieldLists.RECORD} |
95
|
|
|
return record_data |
96
|
|
|
|
97
|
|
|
def parseR53Zone(self, output): |
98
|
|
|
zone_data = {field: getattr(output, field) for field in FieldLists.R53ZONE} |
99
|
|
|
return zone_data |
100
|
|
|
|
101
|
|
|
def parseR53Status(self, output): |
102
|
|
|
status_data = {field: getattr(output, field) for field in FieldLists.R53STATUS} |
103
|
|
|
return status_data |
104
|
|
|
|
105
|
|
|
def parseBucket(self, output): |
106
|
|
|
bucket_data = {field: getattr(output, field) for field in FieldLists.BUCKET} |
107
|
|
|
return bucket_data |
108
|
|
|
|
109
|
|
|
def parseEC2Object(self, output): |
110
|
|
|
# Looks like everything that is an EC2Object pretty much only has these extra |
111
|
|
|
# 'unparseable' properties so handle region and connection specially. |
112
|
|
|
output = vars(output) |
113
|
|
|
del output['connection'] |
114
|
|
|
# special handling for region since name here is better than id. |
115
|
|
|
region = output.get('region', None) |
116
|
|
|
output['region'] = region.name if region else '' |
117
|
|
|
# now anything that is an EC2Object get some special marshalling care. |
118
|
|
|
for k, v in six.iteritems(output): |
119
|
|
|
if isinstance(v, boto.ec2.ec2object.EC2Object): |
120
|
|
|
# Better not to assume each EC2Object has an id. If not found |
121
|
|
|
# resort to the str of the object which should have something meaningful. |
122
|
|
|
output[k] = getattr(v, 'id', str(v)) |
123
|
|
|
# Generally unmarshallable object might be hiding in list so better to |
124
|
|
|
if isinstance(v, list): |
125
|
|
|
v_list = [] |
126
|
|
|
for item in v: |
127
|
|
|
# avoid touching the basic types. |
128
|
|
|
if isinstance(item, (basestring, bool, int, long, float)): |
|
|
|
|
129
|
|
|
v_list.append(v) |
130
|
|
|
else: |
131
|
|
|
v_list.append(str(item)) |
132
|
|
|
output[k] = v_list |
133
|
|
|
return output |
134
|
|
|
|