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 create_domain(self, name, **kwargs): """Creates a domain.""" description = kwargs.get('description', None) en = kwargs.get('enabled', True) post_body = common.Element("domain", xmlns=XMLNS, name=name, description=description, enabled=str(en).lower()) resp, body = self.post('domains', str(common.Document(post_body))) self.expected_success(201, resp.status) body = self._parse_body(etree.fromstring(body)) return resp, body
def serialize_list(self, body, root_name=None, item_name=None): # expecting dict in form # body = {'resources': [res_dict1, res_dict2, ...] post_body = common.Element(root_name) post_body.add_attr('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') for item in body[body.keys()[0]]: elt = common.Element(item_name) for name, attr in item.items(): elt_content = self._get_element(name, attr) elt.append(elt_content) post_body.append(elt) return str(common.Document(post_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_project(self, name, **kwargs): """Creates a project.""" description = kwargs.get('description', None) en = kwargs.get('enabled', 'true') domain_id = kwargs.get('domain_id', 'default') post_body = common.Element("project", xmlns=XMLNS, description=description, domain_id=domain_id, enabled=str(en).lower(), name=name) resp, body = self.post('projects', str(common.Document(post_body))) body = self._parse_body(etree.fromstring(body)) return resp, body
def create_group(self, name, **kwargs): """Creates a group.""" description = kwargs.get('description', None) domain_id = kwargs.get('domain_id', 'default') project_id = kwargs.get('project_id', None) post_body = common.Element("group", xmlns=XMLNS, name=name, description=description, domain_id=domain_id, project_id=project_id) resp, body = self.post('groups', str(common.Document(post_body))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_group(self, group_id, **kwargs): """Updates a group.""" _, body = self.get_group(group_id) name = kwargs.get('name', body['name']) description = kwargs.get('description', body['description']) post_body = common.Element("group", xmlns=XMLNS, name=name, description=description) resp, body = self.patch('groups/%s' % group_id, str(common.Document(post_body))) self.expected_success(200, resp.status) body = self._parse_body(etree.fromstring(body)) return resp, body
def disable_service(self, host_name, binary): """ Disable service on a host host_name: Name of host binary: Service binary """ post_body = xml_utils.Element("service") post_body.add_attr('binary', binary) post_body.add_attr('host', host_name) resp, body = self.put('os-services/disable', str(xml_utils.Document(post_body))) body = xml_utils.xml_to_json(etree.fromstring(body)) return resp, body
def create_user(self, name, password, tenant_id, email, **kwargs): """Create a user.""" create_user = xml.Element("user", xmlns=XMLNS, name=name, password=password, email=email) if tenant_id: create_user.add_attr('tenantId', tenant_id) if 'enabled' in kwargs: create_user.add_attr('enabled', str(kwargs['enabled']).lower()) resp, body = self.post('users', str(xml.Document(create_user))) return resp, self._parse_resp(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 update_quota_class_set(self, quota_class_id, **kwargs): """ Updates the quota class's limits for one or more resources. """ post_body = xml_utils.Element("quota_class_set", xmlns=xml_utils.XMLNS_11, **kwargs) resp, body = self.put('os-quota-class-sets/%s' % str(quota_class_id), str(xml_utils.Document(post_body))) body = xml_utils.xml_to_json(etree.fromstring(body)) body = format_quota(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_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 create_tenant(self, name, **kwargs): """ Create a tenant name (required): New tenant name description: Description of new tenant (default is none) enabled <true|false>: Initial tenant status (default is true) """ en = kwargs.get('enabled', 'true') create_tenant = xml.Element("tenant", xmlns=XMLNS, name=name, description=kwargs.get('description', ''), enabled=str(en).lower()) resp, body = self.post('tenants', str(xml.Document(create_tenant))) return resp, self._parse_resp(body)
def update_domain(self, domain_id, **kwargs): """Updates a domain.""" resp, body = self.get_domain(domain_id) description = kwargs.get('description', body['description']) en = kwargs.get('enabled', body['enabled']) name = kwargs.get('name', body['name']) post_body = common.Element("domain", xmlns=XMLNS, name=name, description=description, enabled=str(en).lower()) resp, body = self.patch('domains/%s' % domain_id, str(common.Document(post_body))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_tenant(self, tenant_id, **kwargs): """Updates a tenant.""" resp, body = self.get_tenant(tenant_id) name = kwargs.get('name', body['name']) desc = kwargs.get('description', body['description']) en = kwargs.get('enabled', body['enabled']) update_tenant = xml.Element("tenant", xmlns=XMLNS, id=tenant_id, name=name, description=desc, enabled=str(en).lower()) resp, body = self.post('tenants/%s' % tenant_id, str(xml.Document(update_tenant))) return resp, self._parse_resp(body)
def create_snapshot(self, volume_id, **kwargs): """Creates a new snapshot. volume_id(Required): id of the volume. force: Create a snapshot even if the volume attached (Default=False) display_name: Optional snapshot Name. display_description: User friendly snapshot description. """ # NOTE(afazekas): it should use the volume namespace snapshot = common.Element("snapshot", xmlns=common.XMLNS_11, volume_id=volume_id) for key, value in kwargs.items(): snapshot.add_attr(key, value) resp, body = self.post('snapshots', str(common.Document(snapshot))) body = common.xml_to_json(etree.fromstring(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 update_region(self, region_id, **kwargs): """Updates an region with given parameters. """ description = kwargs.get('description', None) update_region = common.Element("region", xmlns=XMLNS, description=description) if 'parent_region_id' in kwargs: update_region.append(common.Element('parent_region_id', kwargs.get('parent_region_id'))) resp, body = self.patch('regions/%s' % str(region_id), str(common.Document(update_region))) self.expected_success(200, resp.status) body = self._parse_body(etree.fromstring(body)) return resp, body
def serialize(self, body): # TODO(enikanorov): implement better json to xml conversion # expecting the dict with single key root = body.keys()[0] post_body = common.Element(root) post_body.add_attr('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') elements = set() for name, attr in body[root].items(): elt = self._get_element(name, attr) post_body.append(elt) if ":" in name: elements.add(name.split(":")[0]) if elements: self._add_namespaces(post_body, elements) return str(common.Document(post_body))
def update_service(self, service_id, **kwargs): """Updates a service_id.""" resp, body = self.get_service(service_id) name = kwargs.get('name', body['name']) description = kwargs.get('description', body['description']) type = kwargs.get('type', body['type']) update_service = common.Element("service", xmlns=XMLNS, id=service_id, name=name, description=description, type=type) resp, body = self.patch('services/%s' % service_id, str(common.Document(update_service))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_project(self, project_id, **kwargs): """Updates a Project.""" resp, body = self.get_project(project_id) name = kwargs.get('name', body['name']) desc = kwargs.get('description', body['description']) en = kwargs.get('enabled', body['enabled']) domain_id = kwargs.get('domain_id', body['domain_id']) post_body = common.Element("project", xmlns=XMLNS, name=name, description=desc, enabled=str(en).lower(), domain_id=domain_id) resp, body = self.patch('projects/%s' % project_id, str(common.Document(post_body))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_quota_set(self, tenant_id, gigabytes=None, volumes=None, snapshots=None): post_body = {} element = xml.Element("quota_set") if gigabytes is not None: post_body['gigabytes'] = gigabytes if volumes is not None: post_body['volumes'] = volumes if snapshots is not None: post_body['snapshots'] = snapshots xml.deep_dict_to_xml(element, post_body) resp, body = self.put('os-quota-sets/%s' % tenant_id, str(xml.Document(element))) body = xml.xml_to_json(etree.fromstring(body)) return resp, self._format_quota(body)
def create_user(self, user_name, **kwargs): """Creates a user.""" password = kwargs.get('password', None) email = kwargs.get('email', None) en = kwargs.get('enabled', 'true') project_id = kwargs.get('project_id', None) description = kwargs.get('description', None) domain_id = kwargs.get('domain_id', 'default') post_body = common.Element("user", xmlns=XMLNS, name=user_name, password=password, description=description, email=email, enabled=str(en).lower(), project_id=project_id, domain_id=domain_id) resp, body = self.post('users', str(common.Document(post_body))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_user(self, user_id, name, **kwargs): """Updates a user.""" resp, body = self.get_user(user_id) email = kwargs.get('email', body['email']) en = kwargs.get('enabled', body['enabled']) project_id = kwargs.get('project_id', body['project_id']) description = kwargs.get('description', body['description']) domain_id = kwargs.get('domain_id', body['domain_id']) update_user = common.Element("user", xmlns=XMLNS, name=name, email=email, project_id=project_id, domain_id=domain_id, description=description, enabled=str(en).lower()) resp, body = self.patch('users/%s' % user_id, str(common.Document(update_user))) body = self._parse_body(etree.fromstring(body)) 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 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_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_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