ivan-zhu | a2c9cd5 | 2013-08-20 20:01:44 +0800 | [diff] [blame] | 1 | # Copyright 2013 NEC Corporation |
| 2 | # Copyright 2013 IBM Corp. |
| 3 | # All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import urllib |
| 18 | |
| 19 | from lxml import etree |
| 20 | from tempest.common.rest_client import RestClientXML |
| 21 | from tempest.services.compute.xml.common import Document |
| 22 | from tempest.services.compute.xml.common import Element |
| 23 | from tempest.services.compute.xml.common import xml_to_json |
| 24 | |
| 25 | |
ivan-zhu | 5c86ae6 | 2013-08-20 21:09:01 +0800 | [diff] [blame] | 26 | class ServicesV3ClientXML(RestClientXML): |
ivan-zhu | a2c9cd5 | 2013-08-20 20:01:44 +0800 | [diff] [blame] | 27 | |
| 28 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
ivan-zhu | 5c86ae6 | 2013-08-20 21:09:01 +0800 | [diff] [blame] | 29 | super(ServicesV3ClientXML, self).__init__(config, username, password, |
| 30 | auth_url, tenant_name) |
| 31 | self.service = self.config.compute.catalog_v3_type |
ivan-zhu | a2c9cd5 | 2013-08-20 20:01:44 +0800 | [diff] [blame] | 32 | |
| 33 | def list_services(self, params=None): |
| 34 | url = 'os-services' |
| 35 | if params: |
| 36 | url += '?%s' % urllib.urlencode(params) |
| 37 | |
| 38 | resp, body = self.get(url, self.headers) |
| 39 | node = etree.fromstring(body) |
| 40 | body = [xml_to_json(x) for x in node.getchildren()] |
| 41 | return resp, body |
| 42 | |
| 43 | def enable_service(self, host_name, binary): |
| 44 | """ |
| 45 | Enable service on a host |
| 46 | host_name: Name of host |
| 47 | binary: Service binary |
| 48 | """ |
| 49 | post_body = Element("service") |
| 50 | post_body.add_attr('binary', binary) |
| 51 | post_body.add_attr('host', host_name) |
| 52 | |
| 53 | resp, body = self.put('os-services/enable', str(Document(post_body)), |
| 54 | self.headers) |
| 55 | body = xml_to_json(etree.fromstring(body)) |
| 56 | return resp, body |
| 57 | |
| 58 | def disable_service(self, host_name, binary): |
| 59 | """ |
| 60 | Disable service on a host |
| 61 | host_name: Name of host |
| 62 | binary: Service binary |
| 63 | """ |
| 64 | post_body = Element("service") |
| 65 | post_body.add_attr('binary', binary) |
| 66 | post_body.add_attr('host', host_name) |
| 67 | |
| 68 | resp, body = self.put('os-services/disable', str(Document(post_body)), |
| 69 | self.headers) |
| 70 | body = xml_to_json(etree.fromstring(body)) |
| 71 | return resp, body |