def _metadata_body(self, meta): post_body = Element('metadata') for k, v in meta.items(): data = Element('meta', key=k) data.append(Text(v)) post_body.append(data) return post_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 = 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 = Element(k) element.append(Text(content=str(v))) group_rule.append(element) url = 'os-security-group-rules' resp, body = self.post(url, str(Document(group_rule)), self.headers) body = self._parse_body(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 = 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 = Element(k) element.append(Text(content=str(v))) group_rule.append(element) url = "os-security-group-rules" resp, body = self.post(url, str(Document(group_rule)), self.headers) body = self._parse_body(etree.fromstring(body)) return resp, body
def _metadata_body(self, meta): post_body = Element('metadata') for k, v in meta.items(): data = Element('meta', key=k) data.append(Text(v)) post_body.append(data) return post_body
def auth(self, user, password, tenant): passwordCreds = Element("passwordCredentials", username=user, password=password) auth = Element("auth", tenantName=tenant) auth.append(passwordCreds) headers = {"Content-Type": "application/xml"} resp, body = self.post(self.auth_url, headers=headers, body=str(Document(auth))) return resp, body
def create_network(self, name): uri = '%s/networks' % (self.uri_prefix) post_body = Element("network") p2 = Element("name", name) post_body.append(p2) resp, body = self.post(uri, str(Document(post_body)), self.headers) 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 = Element("member") p2 = Element("admin_state_up", admin_state_up) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_vip(self, vip_id, new_name): uri = '%s/lb/vips/%s' % (self.uri_prefix, str(vip_id)) put_body = Element("vip") p2 = Element("name", new_name) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def _metadata_body(self, meta): post_body = Element('metadata') for k, v in meta.items(): data = Element('meta', key=k) # Escape value to allow for special XML chars data.append(Text(escape(v))) post_body.append(data) return post_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 = Element("meta", key=key) post_body.append(Text(v)) resp, body = self.put("images/%s/metadata/%s" % (str(image_id), key), str(Document(post_body)), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, body
def set_server_metadata_item(self, server_id, key, meta): doc = Document() for k, v in meta.items(): meta_element = Element("meta", key=k) meta_element.append(Text(v)) doc.append(meta_element) resp, body = self.put("servers/%s/metadata/%s" % (str(server_id), key), str(doc), self.headers) return resp, xml_to_json(etree.fromstring(body))
def update_subnet(self, subnet_id, name): uri = '%s/subnets/%s' % (self.uri_prefix, str(subnet_id)) subnet = Element("subnet") p2 = Element("name", name) subnet.append(p2) resp, body = self.put(uri, str(Document(subnet)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_pool(self, pool_id, new_name): uri = '%s/lb/pools/%s' % (self.uri_prefix, str(pool_id)) put_body = Element("pool") p2 = Element("name", new_name) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_health_monitor(self, admin_state_up, uuid): uri = '%s/lb/health_monitors/%s' % (self.uri_prefix, str(uuid)) put_body = Element("health_monitor") p2 = Element("admin_state_up", admin_state_up) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body)), self.headers) 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 = Element("security_group") p2 = Element("name", name) post_body.append(p2) resp, body = self.post(uri, str(Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_router(self, router_id, **kwargs): uri = '%s/routers/%s' % (self.uri_prefix, router_id) router = Element("router") for element, content in kwargs.iteritems(): router.append(Element(element, content)) resp, body = self.put(uri, str(Document(router)), self.headers) 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 = Element("security_group") p2 = Element("name", name) post_body.append(p2) resp, body = self.post(uri, str(Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_subnet(self, subnet_id, name): uri = '%s/subnets/%s' % (self.uri_prefix, str(subnet_id)) subnet = Element("subnet") p2 = Element("name", name) subnet.append(p2) resp, body = self.put(uri, str(Document(subnet))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_network(self, net_id, name): uri = '%s/networks/%s' % (self.uri_prefix, str(net_id)) network = Element("network") p2 = Element("name", name) network.append(p2) resp, body = self.put(uri, str(Document(network))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_vip(self, vip_id, new_name): uri = '%s/lb/vips/%s' % (self.uri_prefix, str(vip_id)) put_body = Element("vip") p2 = Element("name", new_name) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_router(self, name, **kwargs): uri = '%s/routers' % (self.uri_prefix) router = Element("router") router.append(Element("name", name)) deep_dict_to_xml(router, kwargs) resp, body = self.post(uri, str(Document(router)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_network(self, net_id, name): uri = '%s/networks/%s' % (self.uri_prefix, str(net_id)) network = Element("network") p2 = Element("name", name) network.append(p2) resp, body = self.put(uri, str(Document(network)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_pool(self, pool_id, new_name): uri = '%s/lb/pools/%s' % (self.uri_prefix, str(pool_id)) put_body = Element("pool") p2 = Element("name", new_name) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def _metadata_body(self, meta): post_body = Element('metadata') for k, v in meta.items(): data = Element('meta', key=k) # Escape value to allow for special XML chars data.append(Text(escape(v))) post_body.append(data) return post_body
def update_port(self, port_id, name): uri = '%s/ports/%s' % (self.uri_prefix, str(port_id)) port = Element("port") p2 = Element("name", name) port.append(p2) resp, body = self.put(uri, str(Document(port))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_port(self, port_id, name): uri = '%s/ports/%s' % (self.uri_prefix, str(port_id)) port = Element("port") p2 = Element("name", name) port.append(p2) resp, body = self.put(uri, str(Document(port)), self.headers) 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 = Element("member") p2 = Element("admin_state_up", admin_state_up) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_network(self, name): uri = '%s/networks' % (self.uri_prefix) post_body = Element("network") p2 = Element("name", name) post_body.append(p2) resp, body = self.post(uri, str(Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_router(self, router_id, **kwargs): uri = '%s/routers/%s' % (self.uri_prefix, router_id) router = Element("router") for element, content in kwargs.iteritems(): router.append(Element(element, content)) resp, body = self.put(uri, str(Document(router))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_router(self, name, **kwargs): uri = '%s/routers' % (self.uri_prefix) router = Element("router") router.append(Element("name", name)) deep_dict_to_xml(router, kwargs) resp, body = self.post(uri, str(Document(router))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_health_monitor(self, admin_state_up, uuid): uri = '%s/lb/health_monitors/%s' % (self.uri_prefix, str(uuid)) put_body = Element("health_monitor") p2 = Element("admin_state_up", admin_state_up) put_body.append(p2) resp, body = self.put(uri, str(Document(put_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_floating_ip(self, ext_network_id, **kwargs): uri = '%s/floatingips' % (self.uri_prefix) floatingip = Element('floatingip') floatingip.append(Element("floating_network_id", ext_network_id)) for element, content in kwargs.iteritems(): floatingip.append(Element(element, content)) resp, body = self.post(uri, str(Document(floatingip)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_agent(self, agent_id, agent_info): uri = '%s/agents/%s' % (self.uri_prefix, agent_id) agent = Element('agent') for (key, value) in agent_info.items(): p = Element(key, value) agent.append(p) resp, body = self.put(uri, str(Document(agent))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_floating_ip(self, ext_network_id, **kwargs): uri = '%s/floatingips' % (self.uri_prefix) floatingip = Element('floatingip') floatingip.append(Element("floating_network_id", ext_network_id)) for element, content in kwargs.iteritems(): floatingip.append(Element(element, content)) resp, body = self.post(uri, str(Document(floatingip))) body = _root_tag_fetcher_and_xml_to_json_parse(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 = Element('meta', key=k) put_body.append(Text(v)) url = "snapshots/%s/metadata/%s" % (str(snapshot_id), str(id)) resp, body = self.put(url, str(Document(put_body)), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, 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 = Element('meta', key=k) put_body.append(Text(v)) url = "volumes/%s/metadata/%s" % (str(volume_id), str(id)) resp, body = self.put(url, str(Document(put_body))) body = xml_to_json(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 = Element(root) for name, attr in body[root].items(): elt = Element(name, attr) post_body.append(elt) return str(Document(post_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 = Element('meta', key=key) post_body.append(Text(v)) resp, body = self.put('images/%s/metadata/%s' % (str(image_id), key), str(Document(post_body)), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, body
def update_agent(self, agent_id, agent_info): uri = '%s/agents/%s' % (self.uri_prefix, agent_id) agent = Element('agent') for (key, value) in agent_info.items(): p = Element(key, value) agent.append(p) resp, body = self.put(uri, str(Document(agent))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def auth(self, user, password, tenant): passwordCreds = Element("passwordCredentials", username=user, password=password) auth = Element("auth", tenantName=tenant) auth.append(passwordCreds) resp, body = self.post(self.auth_url, headers=self.headers, body=str(Document(auth))) return resp, body['access']
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 = Element('meta', key=k) put_body.append(Text(v)) url = "volumes/%s/metadata/%s" % (str(volume_id), str(id)) resp, body = self.put(url, str(Document(put_body))) body = xml_to_json(etree.fromstring(body)) return resp, body
def set_server_metadata_item(self, server_id, key, meta): doc = Document() for k, v in meta.items(): meta_element = Element("meta", key=k) meta_element.append(Text(v)) doc.append(meta_element) resp, body = self.put('servers/%s/metadata/%s' % (str(server_id), key), str(doc), self.headers) return resp, xml_to_json(etree.fromstring(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 = Element("health_monitor") p1 = Element("id", health_monitor_id,) post_body.append(p1) resp, body = self.post(uri, str(Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def auth(self, user, password, tenant): passwordCreds = Element("passwordCredentials", username=user, password=password) auth = Element("auth", tenantName=tenant) auth.append(passwordCreds) headers = {'Content-Type': 'application/xml'} resp, body = self.post(self.auth_url, headers=headers, body=str(Document(auth))) 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 = Element("health_monitor") p1 = Element("id", health_monitor_id,) post_body.append(p1) resp, body = self.post(uri, str(Document(post_body))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def auth(self, user, password, tenant): passwordCreds = Element("passwordCredentials", username=user, password=password) auth = Element("auth", tenantName=tenant) auth.append(passwordCreds) resp, body = self.post(self.auth_url, headers=self.headers, body=str(Document(auth))) return resp, body['access']
def serialize(self, body): #TODO(enikanorov): implement better json to xml conversion # expecting the dict with single key root = body.keys()[0] post_body = 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(Document(post_body))
def set_server_metadata(self, server_id, meta, no_metadata_field=False): doc = Document() if not no_metadata_field: metadata = Element("metadata") doc.append(metadata) for k, v in meta.items(): meta_element = Element("meta", key=k) meta_element.append(Text(v)) metadata.append(meta_element) resp, body = self.put('servers/%s/metadata' % str(server_id), str(doc)) return resp, xml_to_json(etree.fromstring(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 = Element(key) xml_body.append(Text(value)) resp, body = self.post(url, str(Document(xml_body)), self.headers) return resp, body
def set_server_metadata(self, server_id, meta, no_metadata_field=False): doc = Document() if not no_metadata_field: metadata = Element("metadata") doc.append(metadata) for k, v in meta.items(): meta_element = Element("meta", key=k) meta_element.append(Text(v)) metadata.append(meta_element) resp, body = self.put('servers/%s/metadata' % str(server_id), str(doc)) return resp, xml_to_json(etree.fromstring(body))
def update_server_metadata(self, server_id, meta): doc = Document() metadata = Element("metadata") doc.append(metadata) for k, v in meta.items(): meta_element = Element("meta", key=k) meta_element.append(Text(v)) metadata.append(meta_element) resp, body = self.post("/servers/%s/metadata" % str(server_id), str(doc), headers=self.headers) body = xml_to_json(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 = 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(Document(post_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 = Element(key) xml_body.append(Text(value)) resp, body = self.post(url, str(Document(xml_body)), self.headers) return resp, body
def create_port(self, net_uuid, **kwargs): uri = '%s/ports' % (self.uri_prefix) port = Element("port") p1 = Element('network_id', net_uuid) port.append(p1) for key, val in kwargs.items(): key = Element(key, val) port.append(key) resp, body = self.post(uri, str(Document(port))) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_port(self, net_uuid, **kwargs): uri = '%s/ports' % (self.uri_prefix) port = Element("port") p1 = Element('network_id', net_uuid) port.append(p1) for key, val in kwargs.items(): key = Element(key, val) port.append(key) resp, body = self.post(uri, str(Document(port)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def update_host(self, hostname, **kwargs): """Update a host.""" request_body = Element("updates") if kwargs: for k, v in kwargs.iteritems(): request_body.append(Element(k, v)) resp, body = self.put("os-hosts/%s" % str(hostname), str(Document(request_body))) node = etree.fromstring(body) body = [xml_to_json(x) for x in node.getchildren()] return resp, body
def update_server_metadata(self, server_id, meta): doc = Document() metadata = Element("metadata") doc.append(metadata) for k, v in meta.items(): meta_element = Element("meta", key=k) meta_element.append(Text(v)) metadata.append(meta_element) resp, body = self.post("/servers/%s/metadata" % str(server_id), str(doc), headers=self.headers) body = xml_to_json(etree.fromstring(body)) return resp, body
def create_subnet(self, net_uuid, cidr): uri = '%s/subnets' % (self.uri_prefix) subnet = Element("subnet") p2 = Element("network_id", net_uuid) p3 = Element("cidr", cidr) p4 = Element("ip_version", 4) subnet.append(p2) subnet.append(p3) subnet.append(p4) resp, body = self.post(uri, str(Document(subnet)), self.headers) body = _root_tag_fetcher_and_xml_to_json_parse(body) return resp, body
def create_bulk_network(self, count, names): uri = '%s/networks' % (self.uri_prefix) post_body = Element("networks") for i in range(count): p1 = Element("network") p2 = Element("name", names[i]) p1.append(p2) post_body.append(p1) resp, body = self.post(uri, str(Document(post_body)), self.headers) networks = self._parse_array(etree.fromstring(body)) networks = {"networks": networks} return resp, networks