Exemple #1
0
    def list(self, compartment_id=None, filter=None):
        if compartment_id is None:
            compartment_id = self.compartment_id

        load_balancers = oci.pagination.list_call_get_all_results(
            self.client.list_load_balancers,
            compartment_id=compartment_id).data

        # Convert to Json object
        load_balancers_json = self.toJson(load_balancers)
        logger.debug(str(load_balancers_json))

        # Filter results
        self.load_balancers_json = self.filterJsonObjectList(
            load_balancers_json, filter)
        logger.debug(str(self.load_balancers_json))

        # Find instance ocids associated with the backend ip addresses
        oci_private_ips = OCIPrivateIps(self.config, self.configfile)
        oci_vnics_attachments = OCIVnicAttachments(
            self.config, self.configfile, compartment_id=self.compartment_id)
        for load_balancer in self.load_balancers_json:
            load_balancer['instance_ids'] = []
            for key in load_balancer['backend_sets']:
                for backend in load_balancer['backend_sets'][key]['backends']:
                    for ip_address in oci_private_ips.list(
                            subnet_id=load_balancer['subnet_ids'][0],
                            ip_address=backend['ip_address']):
                        vnics_attachments = oci_vnics_attachments.list(
                            vnic_id=ip_address['vnic_id'])
                        load_balancer['instance_ids'].extend([
                            vnic_attachment['instance_id']
                            for vnic_attachment in vnics_attachments
                        ])

        # Build List of Loadbalancer Objects
        self.load_balancers_obj = []
        for load_balancer in self.load_balancers_json:
            # If subnet_id is not part of the object assign the first entry in subnet_ids
            load_balancer['subnet_id'] = load_balancer.get(
                'subnet_id', load_balancer['subnet_ids'][0])
            self.load_balancers_obj.append(
                OCILoadBalancer(self.config, self.configfile, self.profile,
                                load_balancer))

        return self.load_balancers_json
Exemple #2
0
    def list(self, compartment_id=None, filter=None):
        if compartment_id is None:
            compartment_id = self.compartment_id

        # Add filter to only return "RUNNING", "STARTING", "STOPPING", "STOPPED" Compartments
        if filter is None:
            filter = {}

        if 'lifecycle_state' not in filter:
            filter['lifecycle_state'] = ["RUNNING", "STARTING", "STOPPING", "STOPPED"]

        instances = oci.pagination.list_call_get_all_results(self.client.list_instances, compartment_id=compartment_id).data

        # Convert to Json object
        instances_json = self.toJson(instances)

        # Filter results
        self.instances_json = self.filterJsonObjectList(instances_json, filter)


        # Get Volume Attachments as a single call and loop through them to see if they are associated with the instance.
        volume_attachments = OCIVolumeAttachments(config=self.config, configfile=self.configfile, profile=self.profile, compartment_id=compartment_id).list()

        # Get VNic Attachments as a single call and loop through them to see if they are associated with the instance.
        vnic_attachments = OCIVnicAttachments(config=self.config, configfile=self.configfile, profile=self.profile, compartment_id=compartment_id).list()

        for instance in self.instances_json:
            # Get OS Details
            image = OCIImages(config=self.config, configfile=self.configfile, profile=self.profile, compartment_id=compartment_id).get(instance['source_details']['image_id'])
            instance['source_details']['os'] = image['operating_system']
            instance['source_details']['version'] = image['operating_system_version']
            # Decode Cloud Init Yaml
            if 'metadata' in instance and 'user_data' in instance['metadata']:
                instance['metadata']['user_data'] = base64.b64decode(instance['metadata']['user_data']).decode('utf-8')
            # Add Attached Block Storage Volumes
            instance['block_storage_volume_ids'] = [va['volume_id'] for va in volume_attachments if va['instance_id'] == instance['id']]
            # Add Vnic Attachments
            instance['vnics'] = [va for va in vnic_attachments if va['instance_id'] == instance['id']]
            if len(instance['vnics']) > 0:
                instance['primary_vnic'] = instance['vnics']
            # Add first vnic as the default subnet
            instance['subnet_id'] = instance['vnics'][0]['subnet_id'] if len(instance['vnics']) > 0 else ''
            # Get Volume Attachments as a single call and loop through them to see if they are associated with the instance.
            boot_volume_attachments = OCIBootVolumeAttachments(config=self.config, configfile=self.configfile, profile=self.profile, compartment_id=compartment_id, availability_domain=instance['availability_domain'], instance_id=instance['id']).list()
            instance['boot_volume_size_in_gbs'] = boot_volume_attachments[0]['boot_volume']['size_in_gbs']
            instance['is_pv_encryption_in_transit_enabled'] = boot_volume_attachments[0]['is_pv_encryption_in_transit_enabled']
            # Build object list
            self.instances_obj.append(OCIInstance(self.config, self.configfile, self.profile, instance))

        logJson(self.instances_json)
        logger.info(str(self.instances_json))
        return self.instances_json
Exemple #3
0
 def getVnicAttachments(self):
     return OCIVnicAttachments(self.config, self.configfile, self.profile,
                               self.data['compartment_id'], self.data['id'])