def create_interface(self, server, port_id=None, network_id=None, fixed_ip=None): doc = xml_utils.Document() iface = xml_utils.Element('interfaceAttachment') if port_id: _port_id = xml_utils.Element('port_id') _port_id.append(xml_utils.Text(port_id)) iface.append(_port_id) if network_id: _network_id = xml_utils.Element('net_id') _network_id.append(xml_utils.Text(network_id)) iface.append(_network_id) if fixed_ip: _fixed_ips = xml_utils.Element('fixed_ips') _fixed_ip = xml_utils.Element('fixed_ip') _ip_address = xml_utils.Element('ip_address') _ip_address.append(xml_utils.Text(fixed_ip)) _fixed_ip.append(_ip_address) _fixed_ips.append(_fixed_ip) iface.append(_fixed_ips) doc.append(iface) resp, body = self.post('servers/%s/os-interface' % server, body=str(doc)) body = self._process_xml_interface(etree.fromstring(body)) return resp, body
def create_security_group_rule(self, parent_group_id, ip_proto, from_port, to_port, **kwargs): """ Creating a new security group rules. parent_group_id :ID of Security group ip_protocol : ip_proto (icmp, tcp, udp). from_port: Port at start of range. to_port : Port at end of range. Following optional keyword arguments are accepted: cidr : CIDR for address range. group_id : ID of the Source group """ group_rule = xml_utils.Element("security_group_rule") elements = dict() elements['cidr'] = kwargs.get('cidr') elements['group_id'] = kwargs.get('group_id') elements['parent_group_id'] = parent_group_id elements['ip_protocol'] = ip_proto elements['from_port'] = from_port elements['to_port'] = to_port for k, v in elements.items(): if v is not None: element = xml_utils.Element(k) element.append(xml_utils.Text(content=str(v))) group_rule.append(element) url = 'os-security-group-rules' resp, body = self.post(url, str(xml_utils.Document(group_rule))) body = self._parse_body(etree.fromstring(body)) return resp, body
def _metadata_body(self, meta): post_body = xml_utils.Element('metadata') for k, v in meta.items(): data = xml_utils.Element('meta', key=k) data.append(xml_utils.Text(v)) post_body.append(data) return post_body
def update_server(self, server_id, name=None, meta=None, accessIPv4=None, accessIPv6=None, disk_config=None): doc = xml_utils.Document() server = xml_utils.Element("server") doc.append(server) if name is not None: server.add_attr("name", name) if accessIPv4 is not None: server.add_attr("accessIPv4", accessIPv4) if accessIPv6 is not None: server.add_attr("accessIPv6", accessIPv6) if disk_config is not None: server.add_attr( 'xmlns:OS-DCF', "http://docs.openstack.org/" "compute/ext/disk_config/api/v1.1") server.add_attr("OS-DCF:diskConfig", disk_config) if meta is not None: metadata = xml_utils.Element("metadata") server.append(metadata) for k, v in meta: meta = xml_utils.Element("meta", key=k) meta.append(xml_utils.Text(v)) metadata.append(meta) resp, body = self.put('servers/%s' % str(server_id), str(doc)) return resp, xml_utils.xml_to_json(etree.fromstring(body))
def create_volume(self, size, display_name=None, metadata=None): """Creates a new Volume. :param size: Size of volume in GB. (Required) :param display_name: Optional Volume Name. :param metadata: An optional dictionary of values for metadata. """ volume = xml_utils.Element("volume", xmlns=xml_utils.XMLNS_11, size=size) if display_name: volume.add_attr('display_name', display_name) if metadata: _metadata = xml_utils.Element('metadata') volume.append(_metadata) for key, value in metadata.items(): meta = xml_utils.Element('meta') meta.add_attr('key', key) meta.append(xml_utils.Text(value)) _metadata.append(meta) resp, body = self.post('os-volumes', str(xml_utils.Document(volume))) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, body
def update_image_metadata_item(self, image_id, key, meta): """Sets the value for a specific image metadata key.""" post_body = xml_utils.Document('meta', xml_utils.Text(meta), key=key) resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key), post_body) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, body['meta']
def create_volume_type(self, name, **kwargs): """ Creates a new Volume_type. name(Required): Name of volume_type. Following optional keyword arguments are accepted: extra_specs: A dictionary of values to be used as extra_specs. """ vol_type = common.Element("volume_type", xmlns=common.XMLNS_11) if name: vol_type.add_attr('name', name) extra_specs = kwargs.get('extra_specs') if extra_specs: _extra_specs = common.Element('extra_specs') vol_type.append(_extra_specs) for key, value in extra_specs.items(): spec = common.Element('extra_spec') spec.add_attr('key', key) spec.append(common.Text(value)) _extra_specs.append(spec) resp, body = self.post('types', str(common.Document(vol_type))) body = common.xml_to_json(etree.fromstring(body)) self.expected_success(200, resp.status) return resp, body
def rebuild(self, server_id, image_ref, **kwargs): kwargs['imageRef'] = image_ref if 'disk_config' in kwargs: kwargs['OS-DCF:diskConfig'] = kwargs['disk_config'] del kwargs['disk_config'] kwargs['xmlns:OS-DCF'] = "http://docs.openstack.org/"\ "compute/ext/disk_config/api/v1.1" kwargs['xmlns:atom'] = "http://www.w3.org/2005/Atom" if 'xmlns' not in kwargs: kwargs['xmlns'] = xml_utils.XMLNS_11 attrs = kwargs.copy() if 'metadata' in attrs: del attrs['metadata'] rebuild = xml_utils.Element("rebuild", **attrs) if 'metadata' in kwargs: metadata = xml_utils.Element("metadata") rebuild.append(metadata) for k, v in kwargs['metadata'].items(): meta = xml_utils.Element("meta", key=k) meta.append(xml_utils.Text(v)) metadata.append(meta) resp, body = self.post('servers/%s/action' % server_id, str(xml_utils.Document(rebuild))) server = self._parse_server(etree.fromstring(body)) return resp, server
def _metadata_body(self, meta): post_body = common.Element('metadata') for k, v in meta.items(): data = common.Element('meta', key=k) data.append(common.Text(v)) post_body.append(data) return post_body
def _metadata_body(self, meta): post_body = common.Element('metadata') for k, v in meta.items(): data = common.Element('meta', key=k) # Escape value to allow for special XML chars data.append(common.Text(saxutils.escape(v))) post_body.append(data) return post_body
def update_volume_metadata_item(self, volume_id, id, meta_item): """Update metadata item for the volume.""" for k, v in meta_item.items(): put_body = common.Element('meta', key=k) put_body.append(common.Text(v)) url = "volumes/%s/metadata/%s" % (str(volume_id), str(id)) resp, body = self.put(url, str(common.Document(put_body))) body = common.xml_to_json(etree.fromstring(body)) return resp, body
def set_server_metadata_item(self, server_id, key, meta): doc = xml_utils.Document() for k, v in meta.items(): meta_element = xml_utils.Element("meta", key=k) meta_element.append(xml_utils.Text(v)) doc.append(meta_element) resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key), str(doc)) return resp, xml_utils.xml_to_json(etree.fromstring(body))
def set_image_metadata_item(self, image_id, key, meta): """Sets the value for a specific image metadata key.""" for k, v in meta.items(): post_body = xml_utils.Element('meta', key=key) post_body.append(xml_utils.Text(v)) resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key), str(xml_utils.Document(post_body))) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, body
def update_snapshot_metadata_item(self, snapshot_id, id, meta_item): """Update metadata item for the snapshot.""" for k, v in meta_item.items(): put_body = common.Element('meta', key=k) put_body.append(common.Text(v)) url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) resp, body = self.put(url, str(common.Document(put_body))) body = common.xml_to_json(etree.fromstring(body)) self.expected_success(200, resp.status) return resp, body
def reserve_fixed_ip(self, ip, body): """This reserves and unreserves fixed ips.""" url = "os-fixed-ips/%s/action" % (ip) # NOTE(maurosr): First converts the dict body to a json string then # accept any action key value here to permit tests to cover cases with # invalid actions raising badrequest. key, value = body.popitem() xml_body = xml_utils.Element(key) xml_body.append(xml_utils.Text(value)) resp, body = self.post(url, str(xml_utils.Document(xml_body))) return resp, body
def set_server_metadata(self, server_id, meta, no_metadata_field=False): doc = xml_utils.Document() if not no_metadata_field: metadata = xml_utils.Element("metadata") doc.append(metadata) for k, v in meta.items(): meta_element = xml_utils.Element("meta", key=k) meta_element.append(xml_utils.Text(v)) metadata.append(meta_element) resp, body = self.put('servers/%s/metadata' % str(server_id), str(doc)) return resp, xml_utils.xml_to_json(etree.fromstring(body))
def create_keypair(self, name, pub_key=None): doc = xml_utils.Document() keypair_element = xml_utils.Element("keypair") if pub_key: public_key_element = xml_utils.Element("public_key") public_key_text = xml_utils.Text(pub_key) public_key_element.append(public_key_text) keypair_element.append(public_key_element) name_element = xml_utils.Element("name") name_text = xml_utils.Text(name) name_element.append(name_text) keypair_element.append(name_element) doc.append(keypair_element) resp, body = self.post("os-keypairs", body=str(doc)) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, body
def update_server_metadata(self, server_id, meta): doc = xml_utils.Document() metadata = xml_utils.Element("metadata") doc.append(metadata) for k, v in meta.items(): meta_element = xml_utils.Element("meta", key=k) meta_element.append(xml_utils.Text(v)) metadata.append(meta_element) resp, body = self.post("/servers/%s/metadata" % str(server_id), str(doc)) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, body
def create_floating_ip(self, pool_name=None): """Allocate a floating IP to the project.""" url = 'os-floating-ips' if pool_name: doc = xml_utils.Document() pool = xml_utils.Element("pool") pool.append(xml_utils.Text(pool_name)) doc.append(pool) resp, body = self.post(url, str(doc)) else: resp, body = self.post(url, None) body = self._parse_floating_ip(etree.fromstring(body)) return resp, body
def set_metadata(self, aggregate_id, meta): """Replaces the aggregate's existing metadata with new metadata.""" post_body = xml_utils.Element("set_metadata") metadata = xml_utils.Element("metadata") post_body.append(metadata) for k, v in meta.items(): meta = xml_utils.Element(k) meta.append(xml_utils.Text(v)) metadata.append(meta) resp, body = self.post('os-aggregates/%s/action' % aggregate_id, str(xml_utils.Document(post_body))) aggregate = self._format_aggregate(etree.fromstring(body)) return resp, aggregate
def update_security_group(self, security_group_id, name=None, description=None): """ Update a security group. security_group_id: a security_group to update name: new name of security group description: new description of security group """ security_group = xml_utils.Element("security_group") if name: sg_name = xml_utils.Element("name") sg_name.append(xml_utils.Text(content=name)) security_group.append(sg_name) if description: des = xml_utils.Element("description") des.append(xml_utils.Text(content=description)) security_group.append(des) resp, body = self.put('os-security-groups/%s' % str(security_group_id), str(xml_utils.Document(security_group))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_flavor_extra_spec(self, flavor_id, key, **kwargs): """Update extra Specs details of the mentioned flavor and key.""" doc = xml_utils.Document() for (k, v) in kwargs.items(): element = xml_utils.Element(k) doc.append(element) value = xml_utils.Text(v) element.append(value) resp, body = self.put( 'flavors/%s/os-extra_specs/%s' % (flavor_id, key), str(doc)) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, {key: body}
def create_security_group(self, name, description): """ Creates a new security group. name (Required): Name of security group. description (Required): Description of security group. """ security_group = xml_utils.Element("security_group", name=name) des = xml_utils.Element("description") des.append(xml_utils.Text(content=description)) security_group.append(des) resp, body = self.post('os-security-groups', str(xml_utils.Document(security_group))) body = self._parse_body(etree.fromstring(body)) return resp, body
def test_parse_resp_body_dict(self): self.rest_client.dict_tags = [ "fake_dict", ] body_dict = xml.Element(self.rest_client.dict_tags[0]) for i in range(2): body_dict.append( xml.Element("fake_item", xml.Text(self.values[i]), key=self.keys[i])) body = self.rest_client._parse_resp(str(xml.Document(body_dict))) self.assertEqual(self.dict_expected["body_dict"], body)
def create_image(self, server_id, name, meta=None): """Creates an image of the original server.""" post_body = xml_utils.Element('createImage', name=name) if meta: metadata = xml_utils.Element('metadata') post_body.append(metadata) for k, v in meta.items(): data = xml_utils.Element('meta', key=k) data.append(xml_utils.Text(v)) metadata.append(data) resp, body = self.post('servers/%s/action' % str(server_id), str(xml_utils.Document(post_body))) return resp, body
def create_credential(self, access_key, secret_key, user_id, project_id): """Creates a credential.""" cred_type = 'ec2' access = ""access": "%s"" % access_key secret = ""secret": "%s"" % secret_key blob = common.Element('blob', xmlns=XMLNS) blob.append(common.Text("{%s , %s}" % (access, secret))) credential = common.Element('credential', project_id=project_id, type=cred_type, user_id=user_id) credential.append(blob) resp, body = self.post('credentials', str(common.Document(credential))) body = self._parse_body(etree.fromstring(body)) body['blob'] = json.loads(body['blob']) return resp, body
def create_volume(self, size=None, **kwargs): """Creates a new Volume. :param size: Size of volume in GB. :param display_name: Optional Volume Name(only for V1). :param name: Optional Volume Name(only for V2). :param display_name: Optional Volume Name. :param metadata: An optional dictionary of values for metadata. :param volume_type: Optional Name of volume_type for the volume :param snapshot_id: When specified the volume is created from this snapshot :param imageRef: When specified the volume is created from this image """ # for bug #1293885: # If no size specified, read volume size from CONF if size is None: size = CONF.volume.volume_size # NOTE(afazekas): it should use a volume namespace volume = common.Element("volume", xmlns=common.XMLNS_11, size=size) if 'metadata' in kwargs: _metadata = common.Element('metadata') volume.append(_metadata) for key, value in kwargs['metadata'].items(): meta = common.Element('meta') meta.add_attr('key', key) meta.append(common.Text(value)) _metadata.append(meta) attr_to_add = kwargs.copy() del attr_to_add['metadata'] else: attr_to_add = kwargs for key, value in attr_to_add.items(): volume.add_attr(key, value) resp, body = self.post('volumes', str(common.Document(volume))) body = common.xml_to_json(etree.fromstring(body)) self.expected_success(self.create_resp, resp.status) return resp, body
def update_credential(self, credential_id, **kwargs): """Updates a credential.""" resp, body = self.get_credential(credential_id) cred_type = kwargs.get('type', body['type']) access_key = kwargs.get('access_key', body['blob']['access']) secret_key = kwargs.get('secret_key', body['blob']['secret']) project_id = kwargs.get('project_id', body['project_id']) user_id = kwargs.get('user_id', body['user_id']) access = ""access": "%s"" % access_key secret = ""secret": "%s"" % secret_key blob = common.Element('blob', xmlns=XMLNS) blob.append(common.Text("{%s , %s}" % (access, secret))) credential = common.Element('credential', project_id=project_id, type=cred_type, user_id=user_id) credential.append(blob) resp, body = self.patch('credentials/%s' % credential_id, str(common.Document(credential))) body = self._parse_body(etree.fromstring(body)) body['blob'] = json.loads(body['blob']) return resp, body
def create_volume_type_extra_specs(self, vol_type_id, extra_spec): """ Creates a new Volume_type extra spec. vol_type_id: Id of volume_type. extra_specs: A dictionary of values to be used as extra_specs. """ url = "types/%s/extra_specs" % str(vol_type_id) extra_specs = common.Element("extra_specs", xmlns=common.XMLNS_11) if extra_spec: if isinstance(extra_spec, list): extra_specs.append(extra_spec) else: for key, value in extra_spec.items(): spec = common.Element('extra_spec') spec.add_attr('key', key) spec.append(common.Text(value)) extra_specs.append(spec) else: extra_specs = None resp, body = self.post(url, str(common.Document(extra_specs))) body = common.xml_to_json(etree.fromstring(body)) return resp, body
def update_volume_type_extra_specs(self, vol_type_id, extra_spec_name, extra_spec): """ Update a volume_type extra spec. vol_type_id: Id of volume_type. extra_spec_name: Name of the extra spec to be updated. extra_spec: A dictionary of with key as extra_spec_name and the updated value. """ url = "types/%s/extra_specs/%s" % (str(vol_type_id), str(extra_spec_name)) extra_specs = common.Element("extra_specs", xmlns=common.XMLNS_11) if extra_spec is not None: for key, value in extra_spec.items(): spec = common.Element('extra_spec') spec.add_attr('key', key) spec.append(common.Text(value)) extra_specs.append(spec) resp, body = self.put(url, str(common.Document(extra_specs))) body = common.xml_to_json(etree.fromstring(body)) return resp, body