def get_console_output(self, xml_bytes): root = XML(xml_bytes) output_node = root.find("output") instance_id = root.find("instanceId").text.decode("ascii").strip() timestamp = parse_timestamp(root.find("timestamp").text) console_text = output_node.text.decode("base64").decode("utf-8") return model.ConsoleOutput( instance_id, timestamp, console_text, )
def from_xml(cls, xml_bytes): root = XML(xml_bytes) owner_node = root.find("Owner") owner = Owner(owner_node.findtext("ID"), owner_node.findtext("DisplayName")) acl_node = root.find("AccessControlList") acl = [] for grant_node in acl_node.findall("Grant"): grantee_node = grant_node.find("Grantee") grantee = Grantee(grantee_node.findtext("ID"), grantee_node.findtext("DisplayName")) permission = grant_node.findtext("Permission") acl.append(Grant(grantee, permission)) return cls(owner, acl)
def describe_volumes(self, xml_bytes): """Parse the XML returned by the C{DescribeVolumes} function. @param xml_bytes: XML bytes with a C{DescribeVolumesResponse} root element. @return: A list of L{Volume} instances. TODO: attachementSetItemResponseType#deleteOnTermination """ root = XML(xml_bytes) result = [] for volume_data in root.find("volumeSet"): volume_id = volume_data.findtext("volumeId") size = int(volume_data.findtext("size")) snapshot_id = volume_data.findtext("snapshotId") availability_zone = volume_data.findtext("availabilityZone") status = volume_data.findtext("status") create_time = volume_data.findtext("createTime") create_time = datetime.strptime( create_time[:19], "%Y-%m-%dT%H:%M:%S") volume = model.Volume( volume_id, size, status, create_time, availability_zone, snapshot_id) result.append(volume) for attachment_data in volume_data.find("attachmentSet"): instance_id = attachment_data.findtext("instanceId") device = attachment_data.findtext("device") status = attachment_data.findtext("status") attach_time = attachment_data.findtext("attachTime") attach_time = datetime.strptime( attach_time[:19], "%Y-%m-%dT%H:%M:%S") attachment = model.Attachment( instance_id, device, status, attach_time) volume.attachments.append(attachment) return result
def snapshots(self, xml_bytes): """Parse the XML returned by the C{DescribeSnapshots} function. @param xml_bytes: XML bytes with a C{DescribeSnapshotsResponse} root element. @return: A list of L{Snapshot} instances. TODO: ownersSet, restorableBySet, ownerId, volumeSize, description, ownerAlias. """ root = XML(xml_bytes) result = [] for snapshot_data in root.find("snapshotSet"): snapshot_id = snapshot_data.findtext("snapshotId") volume_id = snapshot_data.findtext("volumeId") status = snapshot_data.findtext("status") start_time = snapshot_data.findtext("startTime") start_time = datetime.strptime( start_time[:19], "%Y-%m-%dT%H:%M:%S") progress = snapshot_data.findtext("progress")[:-1] progress = float(progress or "0") / 100. snapshot = model.Snapshot( snapshot_id, volume_id, status, start_time, progress) result.append(snapshot) return result
def _parse_describe_volumes(self, xml_bytes): root = XML(xml_bytes) result = [] for volume_data in root.find("volumeSet"): volume_id = volume_data.findtext("volumeId") size = int(volume_data.findtext("size")) status = volume_data.findtext("status") availability_zone = volume_data.findtext("availabilityZone") snapshot_id = volume_data.findtext("snapshotId") create_time = volume_data.findtext("createTime") create_time = datetime.strptime(create_time[:19], "%Y-%m-%dT%H:%M:%S") volume = model.Volume(volume_id, size, status, create_time, availability_zone, snapshot_id) result.append(volume) for attachment_data in volume_data.find("attachmentSet"): instance_id = attachment_data.findtext("instanceId") status = attachment_data.findtext("status") device = attachment_data.findtext("device") attach_time = attachment_data.findtext("attachTime") attach_time = datetime.strptime(attach_time[:19], "%Y-%m-%dT%H:%M:%S") attachment = model.Attachment(instance_id, device, status, attach_time) volume.attachments.append(attachment) return result
def describe_instances(self, xml_bytes): """ Parse the reservations XML payload that is returned from an AWS describeInstances API call. Instead of returning the reservations as the "top-most" object, we return the object that most developers and their code will be interested in: the instances. In instances reservation is available on the instance object. The following instance attributes are optional: * ami_launch_index * key_name * kernel_id * product_codes * ramdisk_id * reason @param xml_bytes: raw XML payload from AWS. """ root = XML(xml_bytes) results = [] # May be a more elegant way to do this: for reservation_data in root.find("reservationSet"): # Create a reservation object with the parsed data. reservation = model.Reservation( reservation_id=reservation_data.findtext("reservationId"), owner_id=reservation_data.findtext("ownerId")) # Get the list of instances. instances = self.instances_set( reservation_data, reservation) results.extend(instances) return results
def _parse_describe_availability_zones(self, xml_bytes): results = [] root = XML(xml_bytes) for zone_data in root.find("availabilityZoneInfo"): zone_name = zone_data.findtext("zoneName") zone_state = zone_data.findtext("zoneState") results.append(model.AvailabilityZone(zone_name, zone_state)) return results
def _parse_describe_addresses(self, xml_bytes): results = [] root = XML(xml_bytes) for address_data in root.find("addressesSet"): address = address_data.findtext("publicIp") instance_id = address_data.findtext("instanceId") results.append((address, instance_id)) return results
def _parse_terminate_instances(self, xml_bytes): root = XML(xml_bytes) result = [] # May be a more elegant way to do this: for instance in root.find("instancesSet"): instanceId = instance.findtext("instanceId") previousState = instance.find("previousState").findtext("name") shutdownState = instance.find("shutdownState").findtext("name") result.append((instanceId, previousState, shutdownState)) return result
def _parse_describe_keypairs(self, xml_bytes): results = [] root = XML(xml_bytes) keypairs = root.find("keySet") if not keypairs: return results for keypair_data in keypairs: key_name = keypair_data.findtext("keyName") key_fingerprint = keypair_data.findtext("keyFingerprint") results.append(model.Keypair(key_name, key_fingerprint)) return results
def _parse_list_buckets(self, xml_bytes): """ Parse XML bucket list response. """ root = XML(xml_bytes) buckets = [] for bucket_data in root.find("Buckets"): name = bucket_data.findtext("Name") date_text = bucket_data.findtext("CreationDate") date_time = parseTime(date_text) bucket = Bucket(name, date_time) buckets.append(bucket) return buckets
def _parse_list_buckets(self, xml_bytes): """ Parse XML bucket list response. """ root = XML(xml_bytes) buckets = [] for bucket_data in root.find("Buckets"): name = bucket_data.findtext("Name") date_text = bucket_data.findtext("CreationDate") date_time = Time.fromISO8601TimeAndDate(date_text).asDatetime() bucket = model.Bucket(name, date_time) buckets.append(bucket) return buckets
def _parse_snapshots(self, xml_bytes): root = XML(xml_bytes) result = [] for snapshot_data in root.find("snapshotSet"): snapshot_id = snapshot_data.findtext("snapshotId") volume_id = snapshot_data.findtext("volumeId") status = snapshot_data.findtext("status") start_time = snapshot_data.findtext("startTime") start_time = datetime.strptime(start_time[:19], "%Y-%m-%dT%H:%M:%S") progress = snapshot_data.findtext("progress")[:-1] progress = float(progress or "0") / 100.0 snapshot = model.Snapshot(snapshot_id, volume_id, status, start_time, progress) result.append(snapshot) return result
def describe_addresses(self, xml_bytes): """Parse the XML returned by the C{DescribeAddresses} function. @param xml_bytes: XML bytes with a C{DescribeAddressesResponse} root element. @return: a C{list} of L{tuple} of (publicIp, instancId). """ results = [] root = XML(xml_bytes) for address_data in root.find("addressesSet"): address = address_data.findtext("publicIp") instance_id = address_data.findtext("instanceId") results.append((address, instance_id)) return results
def describe_availability_zones(self, xml_bytes): """Parse the XML returned by the C{DescribeAvailibilityZones} function. @param xml_bytes: XML bytes with a C{DescribeAvailibilityZonesResponse} root element. @return: a C{list} of L{AvailabilityZone}. """ results = [] root = XML(xml_bytes) for zone_data in root.find("availabilityZoneInfo"): zone_name = zone_data.findtext("zoneName") zone_state = zone_data.findtext("zoneState") results.append(model.AvailabilityZone(zone_name, zone_state)) return results
def _parse_snapshots(self, xml_bytes): root = XML(xml_bytes) result = [] for snapshot_data in root.find("snapshotSet"): snapshot_id = snapshot_data.findtext("snapshotId") volume_id = snapshot_data.findtext("volumeId") status = snapshot_data.findtext("status") start_time = snapshot_data.findtext("startTime") start_time = datetime.strptime(start_time[:19], "%Y-%m-%dT%H:%M:%S") progress = snapshot_data.findtext("progress")[:-1] progress = float(progress or "0") / 100. snapshot = model.Snapshot(snapshot_id, volume_id, status, start_time, progress) result.append(snapshot) return result
def describe_keypairs(self, xml_bytes): """Parse the XML returned by the C{DescribeKeyPairs} function. @param xml_bytes: XML bytes with a C{DescribeKeyPairsResponse} root element. @return: a C{list} of L{Keypair}. """ results = [] root = XML(xml_bytes) keypairs = root.find("keySet") if keypairs is None: return results for keypair_data in keypairs: key_name = keypair_data.findtext("keyName") key_fingerprint = keypair_data.findtext("keyFingerprint") results.append(model.Keypair(key_name, key_fingerprint)) return results
def terminate_instances(self, xml_bytes): """Parse the XML returned by the C{TerminateInstances} function. @param xml_bytes: XML bytes with a C{TerminateInstancesResponse} root element. @return: An iterable of C{tuple} of (instanceId, previousState, shutdownState) for the ec2 instances that where terminated. """ root = XML(xml_bytes) result = [] # May be a more elegant way to do this: for instance in root.find("instancesSet"): instanceId = instance.findtext("instanceId") previousState = instance.find("previousState").findtext("name") shutdownState = instance.find("shutdownState").findtext("name") result.append((instanceId, previousState, shutdownState)) return result
def _parse_run_instances(self, xml_bytes): """ Parse the reservations XML payload that is returned from an AWS RunInstances API call. """ root = XML(xml_bytes) # Get the security group information. groups = [] for group_data in root.find("groupSet"): group_id = group_data.findtext("groupId") groups.append(group_id) # Create a reservation object with the parsed data. reservation = model.Reservation( reservation_id=root.findtext("reservationId"), owner_id=root.findtext("ownerId"), groups=groups ) # Get the list of instances. instances = self._parse_instances_set(root, reservation) return instances
def terminate_instances(self, xml_bytes): """Parse the XML returned by the C{TerminateInstances} function. @param xml_bytes: XML bytes with a C{TerminateInstancesResponse} root element. @return: An iterable of C{tuple} of (instanceId, previousState, shutdownState) for the ec2 instances that where terminated. """ root = XML(xml_bytes) result = [] # May be a more elegant way to do this: for instance in root.find("instancesSet"): instanceId = instance.findtext("instanceId") previousState = instance.find("previousState").findtext( "name") shutdownState = instance.find("shutdownState").findtext( "name") result.append((instanceId, previousState, shutdownState)) return result
def _parse_run_instances(self, xml_bytes): """ Parse the reservations XML payload that is returned from an AWS RunInstances API call. """ root = XML(xml_bytes) # Get the security group information. groups = [] for group_data in root.find("groupSet"): group_id = group_data.findtext("groupId") groups.append(group_id) # Create a reservation object with the parsed data. reservation = model.Reservation( reservation_id=root.findtext("reservationId"), owner_id=root.findtext("ownerId"), groups=groups) # Get the list of instances. instances = self._parse_instances_set(root, reservation) return instances