def add_router_interface_with_subnet_id(self, router_id, subnet_id): uri = '%s/routers/%s/add_router_interface' % (self.uri_prefix, router_id) subnet = common.Element("subnet_id", subnet_id) resp, body = self.put(uri, str(common.Document(subnet))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def remove_router_interface_with_port_id(self, router_id, port_id): uri = '%s/routers/%s/remove_router_interface' % (self.uri_prefix, router_id) port = common.Element("port_id", port_id) resp, body = self.put(uri, str(common.Document(port))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def auth(self, user, password, tenant): passwordCreds = xml.Element("passwordCredentials", username=user, password=password) auth = xml.Element("auth", tenantName=tenant) auth.append(passwordCreds) resp, body = self.post(self.auth_url, body=str(xml.Document(auth))) return resp, body['access']
def create_router(self, name, **kwargs): uri = '%s/routers' % (self.uri_prefix) router = common.Element("router") router.append(common.Element("name", name)) common.deep_dict_to_xml(router, kwargs) resp, body = self.post(uri, str(common.Document(router))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def test_parse_resp_body_list(self): self.rest_client.list_tags = ["fake_list", ] body_list = xml.Element(self.rest_client.list_tags[0]) for i in range(2): body_list.append(xml.Element("fake_item", **self.list_expected["body_list"][i])) body = self.rest_client._parse_resp(str(xml.Document(body_list))) self.assertEqual(self.list_expected["body_list"], body)
def update_router(self, router_id, **kwargs): uri = '%s/routers/%s' % (self.uri_prefix, router_id) router = common.Element("router") for element, content in kwargs.iteritems(): router.append(common.Element(element, content)) resp, body = self.put(uri, str(common.Document(router))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_security_group(self, name): uri = '%s/security-groups' % (self.uri_prefix) post_body = common.Element("security_group") p2 = common.Element("name", name) post_body.append(p2) resp, body = self.post(uri, str(common.Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_member(self, admin_state_up, member_id): uri = '%s/lb/members/%s' % (self.uri_prefix, str(member_id)) put_body = common.Element("member") p2 = common.Element("admin_state_up", admin_state_up) put_body.append(p2) resp, body = self.put(uri, str(common.Document(put_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_user(self, user_id, **kwargs): """Updates a user.""" if 'enabled' in kwargs: kwargs['enabled'] = str(kwargs['enabled']).lower() update_user = xml.Element("user", xmlns=XMLNS, **kwargs) resp, body = self.put('users/%s' % user_id, str(xml.Document(update_user))) return resp, self._parse_resp(body)
def update_agent(self, agent_id, agent_info): uri = '%s/agents/%s' % (self.uri_prefix, agent_id) agent = common.Element('agent') for (key, value) in agent_info.items(): p = common.Element(key, value) agent.append(p) resp, body = self.put(uri, str(common.Document(agent))) body = _root_tag_fetcher_and_xml_to_json_parse(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_floating_ip(self, ext_network_id, **kwargs): uri = '%s/floatingips' % (self.uri_prefix) floatingip = common.Element('floatingip') floatingip.append(common.Element("floating_network_id", ext_network_id)) for element, content in kwargs.iteritems(): floatingip.append(common.Element(element, content)) resp, body = self.post(uri, str(common.Document(floatingip))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def associate_health_monitor_with_pool(self, health_monitor_id, pool_id): uri = '%s/lb/pools/%s/health_monitors' % (self.uri_prefix, pool_id) post_body = common.Element("health_monitor") p1 = common.Element("id", health_monitor_id,) post_body.append(p1) resp, body = self.post(uri, str(common.Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_service(self, name, service_type, **kwargs): """Create a service.""" OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0" create_service = xml.Element("service", xmlns=OS_KSADM, name=name, type=service_type, description=kwargs.get('description')) resp, body = self.post('OS-KSADM/services', str(xml.Document(create_service))) return resp, self._parse_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') for name, attr in body[root].items(): elt = self._get_element(name, attr) post_body.append(elt) return str(common.Document(post_body))
def create_member(self, address, protocol_port, pool_id): uri = '%s/lb/members' % (self.uri_prefix) post_body = common.Element("member") p1 = common.Element("address", address) p2 = common.Element("protocol_port", protocol_port) p3 = common.Element("pool_id", pool_id) post_body.append(p1) post_body.append(p2) post_body.append(p3) resp, body = self.post(uri, str(common.Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(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 create_user(self, name, password, tenant_id, email, **kwargs): """Create a user.""" create_user = xml.Element("user", xmlns=XMLNS, name=name, password=password, tenantId=tenant_id, email=email) 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_security_group_rule(self, secgroup_id, direction='ingress', **kwargs): uri = '%s/security-group-rules' % (self.uri_prefix) rule = common.Element("security_group_rule") p1 = common.Element('security_group_id', secgroup_id) p2 = common.Element('direction', direction) rule.append(p1) rule.append(p2) for key, val in kwargs.items(): key = common.Element(key, val) rule.append(key) resp, body = self.post(uri, str(common.Document(rule))) body = _root_tag_fetcher_and_xml_to_json_parse(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_floating_ip(self, floating_ip_id, **kwargs): uri = '%s/floatingips/%s' % (self.uri_prefix, floating_ip_id) floatingip = common.Element('floatingip') floatingip.add_attr('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') for element, content in kwargs.iteritems(): if content is None: xml_elem = common.Element(element) xml_elem.add_attr("xsi:nil", "true") floatingip.append(xml_elem) else: floatingip.append(common.Element(element, content)) resp, body = self.put(uri, str(common.Document(floatingip))) body = _root_tag_fetcher_and_xml_to_json_parse(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_role(self, name): """Create a role.""" create_role = xml.Element("role", xmlns=XMLNS, name=name) resp, body = self.post('OS-KSADM/roles', str(xml.Document(create_role))) return resp, self._parse_resp(body)
def enable_disable_user(self, user_id, enabled): """Enables or disables a user.""" enable_user = xml.Element("user", enabled=str(enabled).lower()) resp, body = self.put('users/%s/enabled' % user_id, str(xml.Document(enable_user))) return resp, self._parse_resp(body)
def test_parse_resp_body_item(self): body_item = xml.Element("item", **self.item_expected) body = self.rest_client._parse_resp(str(xml.Document(body_item))) self.assertEqual(self.item_expected, body)