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
def getVolumeAttachments(self): return OCIVolumeAttachments(self.config, self.configfile, self.profile, self.data['compartment_id'], self.data['id'])