Completed
Pull Request — master (#305)
by Manas
01:50
created

ResultSets.parseEC2Object()   C

Complexity

Conditions 7

Size

Total Lines 25

Duplication

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