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 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 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 _parse_versioning_config(self, xml_bytes): """Parse a C{VersioningConfiguration} XML document.""" root = XML(xml_bytes) mfa_delete = root.findtext("MfaDelete") status = root.findtext("Status") return VersioningConfiguration(mfa_delete=mfa_delete, status=status)
def _parse_website_config(self, xml_bytes): """Parse a C{WebsiteConfiguration} XML document.""" root = XML(xml_bytes) index_suffix = root.findtext("IndexDocument/Suffix") error_key = root.findtext("ErrorDocument/Key") return WebsiteConfiguration(index_suffix, error_key)
def _parse_notification_config(self, xml_bytes): """Parse a C{NotificationConfiguration} XML document.""" root = XML(xml_bytes) topic = root.findtext("TopicConfiguration/Topic") event = root.findtext("TopicConfiguration/Event") return NotificationConfiguration(topic, event)
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 _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 allocate_address(self, xml_bytes): """Parse the XML returned by the C{AllocateAddress} function. @param xml_bytes: XML bytes with a C{AllocateAddress} root element. @return: The public ip address as a string. """ address_data = XML(xml_bytes) return address_data.findtext("publicIp")
def _parse_website_config(self, response): """Parse a C{WebsiteConfiguration} XML document.""" status, xml_bytes = response root = XML(xml_bytes) index_suffix = root.findtext("IndexDocument/Suffix") error_key = root.findtext("ErrorDocument/Key") return WebsiteConfiguration(index_suffix, error_key)
def parse_receive_message(data): result = [] element = XML(data).find('ReceiveMessageResult') for i in element.getchildren(): receipt = i.findtext('ReceiptHandle').strip() body = base64.b64decode(i.findtext('Body')) result.append(Message(receipt, body)) return result
def truth_return(self, xml_bytes): """Parse the XML for a truth value. @param xml_bytes: XML bytes. @return: True if the node contains "return" otherwise False. """ root = XML(xml_bytes) return root.findtext("return") == "true"
def _parse_notification_config(self, response): """Parse a C{NotificationConfiguration} XML document.""" status, xml_bytes = response root = XML(xml_bytes) topic = root.findtext("TopicConfiguration/Topic") event = root.findtext("TopicConfiguration/Event") return NotificationConfiguration(topic, event)
def _parse_versioning_config(self, response): """Parse a C{VersioningConfiguration} XML document.""" status, xml_bytes = response root = XML(xml_bytes) mfa_delete = root.findtext("MfaDelete") status = root.findtext("Status") return VersioningConfiguration(mfa_delete=mfa_delete, status=status)
def process_batch_result(data, root, success_tag): result = [] element = XML(data).find(root) for i in element.getchildren(): if i.tag == success_tag: result.append(True) else: result.append(False) return result
def describe_security_groups(self, xml_bytes): """Parse the XML returned by the C{DescribeSecurityGroups} function. @param xml_bytes: XML bytes with a C{DescribeSecurityGroupsResponse} root element. @return: A list of L{SecurityGroup} instances. """ root = XML(xml_bytes) result = [] for group_info in root.findall("securityGroupInfo/item"): name = group_info.findtext("groupName") description = group_info.findtext("groupDescription") owner_id = group_info.findtext("ownerId") allowed_groups = [] allowed_ips = [] ip_permissions = group_info.find("ipPermissions") if ip_permissions is None: ip_permissions = () for ip_permission in ip_permissions: # openstack doesn't handle self authorized groups properly # XXX this is an upstream problem and should be addressed there # lp bug #829609 ip_protocol = ip_permission.findtext("ipProtocol") from_port = ip_permission.findtext("fromPort") to_port = ip_permission.findtext("toPort") if from_port: from_port = int(from_port) if to_port: to_port = int(to_port) for groups in ip_permission.findall("groups/item") or (): user_id = groups.findtext("userId") group_name = groups.findtext("groupName") if user_id and group_name: if (user_id, group_name) not in allowed_groups: allowed_groups.append((user_id, group_name)) for ip_ranges in ip_permission.findall("ipRanges/item") or (): cidr_ip = ip_ranges.findtext("cidrIp") allowed_ips.append( model.IPPermission(ip_protocol, from_port, to_port, cidr_ip)) allowed_groups = [ model.UserIDGroupPair(user_id, group_name) for user_id, group_name in allowed_groups ] security_group = model.SecurityGroup(name, description, owner_id=owner_id, groups=allowed_groups, ips=allowed_ips) result.append(security_group) return result
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 import_keypair(self, xml_bytes, key_material): """Extract the key name and the fingerprint from the result. TODO: there is no corresponding method in the 2009-11-30 version of the ec2 wsdl. Delete this? """ keypair_data = XML(xml_bytes) key_name = keypair_data.findtext("keyName") key_fingerprint = keypair_data.findtext("keyFingerprint") return model.Keypair(key_name, key_fingerprint, key_material)
def from_xml(cls, xml_bytes): """ Create an instance of this from XML bytes. @param xml_bytes: C{str} bytes of XML to parse @return: an instance of L{MultipartInitiationResponse} """ root = XML(xml_bytes) return cls(root.findtext('Bucket'), root.findtext('Key'), root.findtext('UploadId'))
def parse_queue_attributes(data): result = {} str_attrs = ['Policy', 'QueueArn'] element = XML(data).find('GetQueueAttributesResult') for i in element.getchildren(): attr = i.findtext('Name').strip() value = i.findtext('Value').strip() if attr not in str_attrs: value = int(value) result[attr] = value return result
def describe_security_groups(self, xml_bytes): """Parse the XML returned by the C{DescribeSecurityGroups} function. @param xml_bytes: XML bytes with a C{DescribeSecurityGroupsResponse} root element. @return: A list of L{SecurityGroup} instances. """ root = XML(xml_bytes) result = [] for group_info in root.findall("securityGroupInfo/item"): id = group_info.findtext("groupId") name = group_info.findtext("groupName") description = group_info.findtext("groupDescription") owner_id = group_info.findtext("ownerId") allowed_groups = [] allowed_ips = [] ip_permissions = group_info.find("ipPermissions") if ip_permissions is None: ip_permissions = () for ip_permission in ip_permissions: # openstack doesn't handle self authorized groups properly # XXX this is an upstream problem and should be addressed there # lp bug #829609 ip_protocol = ip_permission.findtext("ipProtocol") from_port = ip_permission.findtext("fromPort") to_port = ip_permission.findtext("toPort") if from_port: from_port = int(from_port) if to_port: to_port = int(to_port) for groups in ip_permission.findall("groups/item") or (): user_id = groups.findtext("userId") group_name = groups.findtext("groupName") if user_id and group_name: if (user_id, group_name) not in allowed_groups: allowed_groups.append((user_id, group_name)) for ip_ranges in ip_permission.findall("ipRanges/item") or (): cidr_ip = ip_ranges.findtext("cidrIp") allowed_ips.append( model.IPPermission( ip_protocol, from_port, to_port, cidr_ip)) allowed_groups = [model.UserIDGroupPair(user_id, group_name) for user_id, group_name in allowed_groups] security_group = model.SecurityGroup( id, name, description, owner_id=owner_id, groups=allowed_groups, ips=allowed_ips) result.append(security_group) 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 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 create_keypair(self, xml_bytes): """Parse the XML returned by the C{CreateKeyPair} function. @param xml_bytes: XML bytes with a C{CreateKeyPairResponse} root element. @return: The L{Keypair} instance created. """ keypair_data = XML(xml_bytes) key_name = keypair_data.findtext("keyName") key_fingerprint = keypair_data.findtext("keyFingerprint") key_material = keypair_data.findtext("keyMaterial") return model.Keypair(key_name, key_fingerprint, key_material)
def attach_volume(self, xml_bytes): """Parse the XML returned by the C{AttachVolume} function. @param xml_bytes: XML bytes with a C{AttachVolumeResponse} root element. @return: a C{dict} with status and attach_time keys. """ root = XML(xml_bytes) status = root.findtext("status") attach_time = root.findtext("attachTime") attach_time = datetime.strptime(attach_time[:19], "%Y-%m-%dT%H:%M:%S") return {"status": status, "attach_time": attach_time}
def from_xml(cls, xml_bytes): """ Create an instance of this class from XML bytes. @param xml_bytes: C{str} bytes of XML to parse @return: an instance of L{MultipartCompletionResponse} """ root = XML(xml_bytes) return cls(root.findtext('Location'), root.findtext('Bucket'), root.findtext('Key'), root.findtext('ETag'))
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 attach_volume(self, xml_bytes): """Parse the XML returned by the C{AttachVolume} function. @param xml_bytes: XML bytes with a C{AttachVolumeResponse} root element. @return: a C{dict} with status and attach_time keys. """ root = XML(xml_bytes) status = root.findtext("status") attach_time = root.findtext("attachTime") attach_time = datetime.strptime( attach_time[:19], "%Y-%m-%dT%H:%M:%S") return {"status": status, "attach_time": attach_time}
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 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 _parse_lifecycle_config(self, xml_bytes): """Parse a C{LifecycleConfiguration} XML document.""" root = XML(xml_bytes) rules = [] for content_data in root.findall("Rule"): id = content_data.findtext("ID") prefix = content_data.findtext("Prefix") status = content_data.findtext("Status") expiration = int(content_data.findtext("Expiration/Days")) rules.append( LifecycleConfigurationRule(id, prefix, status, expiration)) return LifecycleConfiguration(rules)
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 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_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_get_bucket(self, response): status, xml_bytes = response root = XML(xml_bytes) name = root.findtext("Name") prefix = root.findtext("Prefix") marker = root.findtext("Marker") max_keys = root.findtext("MaxKeys") is_truncated = root.findtext("IsTruncated") contents = [] for content_data in root.findall("Contents"): key = content_data.findtext("Key") date_text = content_data.findtext("LastModified") modification_date = parseTime(date_text) etag = content_data.findtext("ETag") size = content_data.findtext("Size") storage_class = content_data.findtext("StorageClass") owner_id = content_data.findtext("Owner/ID") owner_display_name = content_data.findtext("Owner/DisplayName") owner = ItemOwner(owner_id, owner_display_name) content_item = BucketItem(key, modification_date, etag, size, storage_class, owner) contents.append(content_item) common_prefixes = [] for prefix_data in root.findall("CommonPrefixes"): common_prefixes.append(prefix_data.text) return BucketListing(name, prefix, marker, max_keys, is_truncated, contents, common_prefixes)
def _parse_lifecycle_config(self, response): """Parse a C{LifecycleConfiguration} XML document.""" status, xml_bytes = response root = XML(xml_bytes) rules = [] for content_data in root.findall("Rule"): id = content_data.findtext("ID") prefix = content_data.findtext("Prefix") status = content_data.findtext("Status") expiration = int(content_data.findtext("Expiration/Days")) rules.append( LifecycleConfigurationRule(id, prefix, status, expiration)) return LifecycleConfiguration(rules)
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