def update_server(self, server_id, name=None, meta=None, accessIPv4=None, accessIPv6=None, disk_config=None): doc = Document() server = 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 = Element("metadata") server.append(metadata) for k, v in meta: meta = Element("meta", key=k) meta.append(Text(v)) metadata.append(meta) resp, body = self.put('servers/%s' % str(server_id), str(doc)) return resp, xml_to_json(etree.fromstring(body))
def update_server(self, server_id, name=None, meta=None, access_ip_v4=None, access_ip_v6=None, disk_config=None): doc = Document() server = Element("server") doc.append(server) if name is not None: server.add_attr("name", name) if access_ip_v4 or access_ip_v6: server.add_attr('xmlns:os-access-ips', "http://docs.openstack.org/compute/ext/" "os-access-ips/api/v3") if access_ip_v4 is not None: server.add_attr("os-access-ips:access_ip_v4", access_ip_v4) if access_ip_v6 is not None: server.add_attr("os-access-ips:access_ip_v6", access_ip_v6) if disk_config is not None: server.add_attr('xmlns:os-disk-config', "http://docs.openstack.org" "/compute/ext/disk_config/api/v3") server.add_attr("os-disk-config:disk_config", disk_config) if meta is not None: metadata = Element("metadata") server.append(metadata) for k, v in meta: meta = Element("meta", key=k) meta.append(Text(v)) metadata.append(meta) resp, body = self.put('servers/%s' % str(server_id), str(doc), self.headers) return resp, xml_to_json(etree.fromstring(body))
def _metadata_body(image_id, meta): post_body = Document("metadata") for k, v in meta: text = Text(v) metadata = Element("meta", text, key=k) post_body.append(metadata) return post_body
def create_interface(self, server, port_id=None, network_id=None, fixed_ip=None): doc = Document() iface = Element('interfaceAttachment') if port_id: _port_id = Element('port_id') _port_id.append(Text(port_id)) iface.append(_port_id) if network_id: _network_id = Element('net_id') _network_id.append(Text(network_id)) iface.append(_network_id) if fixed_ip: _fixed_ips = Element('fixed_ips') _fixed_ip = Element('fixed_ip') _ip_address = Element('ip_address') _ip_address.append(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 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_floating_ip_to_server(self, floating_ip, server_id): """Associate the provided floating IP to a specific server.""" url = "servers/%s/action" % str(server_id) doc = Document() server = Element("addFloatingIp") doc.append(server) server.add_attr("address", floating_ip) resp, body = self.post(url, str(doc)) return resp, body
def disassociate_floating_ip_from_server(self, floating_ip, server_id): """Disassociate the provided floating IP from a specific server.""" url = "servers/%s/action" % str(server_id) doc = Document() server = Element("removeFloatingIp") doc.append(server) server.add_attr("address", floating_ip) resp, body = self.post(url, str(doc), self.headers) return resp, body
def add_flavor_access(self, flavor_id, tenant_id): """Add flavor access for the specified tenant.""" doc = Document() server = Element("add_tenant_access") doc.append(server) server.add_attr("tenant_id", tenant_id) resp, body = self.post("flavors/%s/action" % str(flavor_id), str(doc), self.headers) body = self._parse_array_access(etree.fromstring(body)) return resp, body
def remove_flavor_access(self, flavor_id, tenant_id): """Remove flavor access from the specified tenant.""" doc = Document() server = Element("removeTenantAccess") doc.append(server) server.add_attr("tenant", tenant_id) resp, body = self.post('flavors/%s/action' % str(flavor_id), str(doc)) body = self._parse_array_access(etree.fromstring(body)) 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 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_flavor_extra_spec(self, flavor_id, key, **kwargs): """Update extra Specs details of the mentioned flavor and key.""" doc = Document() for (k, v) in kwargs.items(): element = Element(k) doc.append(element) value = Text(v) element.append(value) resp, body = self.put("flavors/%s/flavor-extra-specs/%s" % (flavor_id, key), str(doc), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, {key: body}
def create_floating_ip(self, pool_name=None): """Allocate a floating IP to the project.""" url = 'os-floating-ips' if pool_name: doc = Document() pool = Element("pool") pool.append(Text(pool_name)) doc.append(pool) resp, body = self.post(url, str(doc), self.headers) else: resp, body = self.post(url, None, self.headers) body = self._parse_floating_ip(etree.fromstring(body)) return resp, body
def update_aggregate(self, aggregate_id, name, availability_zone=None): """Update a aggregate.""" put_body = Element("aggregate", name=name, availability_zone=availability_zone) resp, body = self.put('os-aggregates/%s' % str(aggregate_id), str(Document(put_body)), self.headers) aggregate = self._format_aggregate(etree.fromstring(body)) return resp, aggregate
def create_aggregate(self, name, availability_zone=None): """Creates a new aggregate.""" post_body = Element("aggregate", name=name, availability_zone=availability_zone) resp, body = self.post('os-aggregates', str(Document(post_body)), self.headers) aggregate = self._format_aggregate(etree.fromstring(body)) return resp, aggregate
def set_flavor_extra_spec(self, flavor_id, specs): """Sets extra Specs to the mentioned flavor.""" extra_specs = Element("extra_specs") for key in specs.keys(): extra_specs.add_attr(key, specs[key]) resp, body = self.post('flavors/%s/os-extra_specs' % flavor_id, str(Document(extra_specs)), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, body
def update_volume(self, volume_id, **kwargs): """Updates the Specified Volume.""" put_body = Element("volume", xmlns=XMLNS_11, **kwargs) resp, body = self.put('volumes/%s' % volume_id, str(Document(put_body)), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, body
def extend_volume(self, volume_id, extend_size): """Extend a volume.""" post_body = Element("os-extend", new_size=extend_size) url = 'volumes/%s/action' % str(volume_id) resp, body = self.post(url, str(Document(post_body)), self.headers) if body: body = xml_to_json(etree.fromstring(body)) return resp, body
def update_volume_readonly(self, volume_id, readonly): """Update the Specified Volume readonly.""" post_body = Element("os-update_readonly_flag", readonly=readonly) url = 'volumes/%s/action' % str(volume_id) resp, body = self.post(url, str(Document(post_body)), self.headers) if body: body = 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 = 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 create_service(self, serv_type, name=None, description=None): post_body = Element("service", xmlns=XMLNS, name=name, description=description, type=serv_type) resp, body = self.post("services", str(Document(post_body))) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_snapshot(self, snapshot_id, **kwargs): """Updates a snapshot.""" put_body = Element("snapshot", xmlns=XMLNS_11, **kwargs) resp, body = self.put('snapshots/%s' % snapshot_id, str(Document(put_body)), self.headers) body = xml_to_json(etree.fromstring(body)) return resp, 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))) body = xml_to_json(etree.fromstring(body)) return resp, body
def action(self, server_id, action_name, response_key, **kwargs): if 'xmlns' not in kwargs: kwargs['xmlns'] = XMLNS_11 doc = Document((Element(action_name, **kwargs))) resp, body = self.post("servers/%s/action" % server_id, str(doc), self.headers) if response_key is not None: 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 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_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 upload_volume(self, volume_id, image_name, disk_format): """Uploads a volume in Glance.""" post_body = Element("os-volume_upload_image", image_name=image_name, disk_format=disk_format) url = 'volumes/%s/action' % str(volume_id) resp, body = self.post(url, str(Document(post_body))) volume = xml_to_json(etree.fromstring(body)) return resp, volume
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 reset_snapshot_status(self, snapshot_id, status): """Reset the specified snapshot's status.""" post_body = Element("os-reset_status", status=status ) url = 'snapshots/%s/action' % str(snapshot_id) resp, body = self.post(url, str(Document(post_body)), self.headers) if body: body = xml_to_json(etree.fromstring(body)) return resp, body
def resize(self, server_id, flavor_ref, disk_config=None): resize = Element("resize", xmlns=XMLNS_11, flavorRef=flavor_ref) if disk_config is not None: raise Exception("Sorry, disk_config not supported via XML yet") return self.post('servers/%s/action' % server_id, str(Document(resize)), self.headers)
def attach_volume(self, volume_id, instance_uuid, mountpoint): """Attaches a volume to a given instance on a given mountpoint.""" post_body = Element("os-attach", instance_uuid=instance_uuid, mountpoint=mountpoint) url = 'volumes/%s/action' % str(volume_id) resp, body = self.post(url, str(Document(post_body)), self.headers) if body: body = xml_to_json(etree.fromstring(body)) return resp, body
def reset_volume_status(self, volume_id, status): """Reset the Specified Volume's Status.""" post_body = Element("os-reset_status", status=status ) url = 'volumes/%s/action' % str(volume_id) resp, body = self.post(url, str(Document(post_body)), self.headers) if body: body = xml_to_json(etree.fromstring(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 = Element("user", xmlns=XMLNS, **kwargs) resp, body = self.put('users/%s' % user_id, str(Document(update_user)), self.headers) body = self._parse_body(etree.fromstring(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 update_endpoint(self, endpoint_id, service_id=None, interface=None, url=None, region=None, enabled=None): """Updates an endpoint with given parameters.""" doc = Document() endpoint = Element("endpoint") doc.append(endpoint) if service_id: endpoint.add_attr("service_id", service_id) if interface: endpoint.add_attr("interface", interface) if url: endpoint.add_attr("url", url) if region: endpoint.add_attr("region", region) if enabled is not None: endpoint.add_attr("enabled", enabled) resp, body = self.patch("endpoints/%s" % str(endpoint_id), str(doc), self.headers) body = self._parse_body(etree.fromstring(body)) return resp, body
def update_policy(self, policy_id, **kwargs): """Updates a policy.""" resp, body = self.get_policy(policy_id) type = kwargs.get('type') update_policy = Element("policy", xmlns=XMLNS, type=type) url = 'policies/%s' % policy_id resp, body = self.patch(url, str(Document(update_policy)), self.headers) body = self._parse_body(etree.fromstring(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 create_volume_transfer(self, vol_id, display_name=None): """Create a volume transfer.""" post_body = Element("transfer", volume_id=vol_id) if display_name: post_body.add_attr('name', display_name) resp, body = self.post('os-volume-transfer', str(Document(post_body)), self.headers) volume = xml_to_json(etree.fromstring(body)) return resp, volume
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 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 update_snapshot_status(self, snapshot_id, status, progress): """Update the specified snapshot's status.""" post_body = Element("os-update_snapshot_status", status=status, progress=progress ) url = 'snapshots/%s/action' % str(snapshot_id) resp, body = self.post(url, str(Document(post_body))) if body: body = xml_to_json(etree.fromstring(body)) return resp, body
def update_server(self, server_id, name=None, meta=None, accessIPv4=None, accessIPv6=None): doc = Document() server = 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 meta is not None: metadata = Element("metadata") server.append(metadata) for k, v in meta: meta = Element("meta", key=k) meta.append(Text(v)) metadata.append(meta) resp, body = self.put("servers/%s" % str(server_id), str(doc), self.headers) return resp, xml_to_json(etree.fromstring(body))
def live_migrate_server(self, server_id, dest_host, use_block_migration): """This should be called with administrator privileges .""" req_body = Element("os-migrateLive", xmlns=XMLNS_11, disk_over_commit=False, block_migration=use_block_migration, host=dest_host) resp, body = self.post("servers/%s/action" % str(server_id), str(Document(req_body)), self.headers) return resp, body
def create_keypair(self, name, pub_key=None): doc = Document() keypair_element = Element("keypair") if pub_key: public_key_element = Element("public_key") public_key_text = Text(pub_key) public_key_element.append(public_key_text) keypair_element.append(public_key_element) name_element = Element("name") name_text = 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_to_json(etree.fromstring(body)) return resp, body