blob: a6ada0430931116ca901497dbca91aae1f74b5ed [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from oslo_serialization import jsonutils as json
14from six.moves.urllib import parse as urllib
15
16from tempest.lib.common import rest_client
17
18
19class BaseNetworkClient(rest_client.RestClient):
20
21 """Base class for Tempest REST clients for Neutron.
22
23 Child classes use v2 of the Neutron API, since the V1 API has been
24 removed from the code base.
25 """
26
27 version = '2.0'
28 uri_prefix = "v2.0"
29
30 def list_resources(self, uri, **filters):
31 req_uri = self.uri_prefix + uri
32 if filters:
33 req_uri += '?' + urllib.urlencode(filters, doseq=1)
34 resp, body = self.get(req_uri)
35 body = json.loads(body)
36 self.expected_success(200, resp.status)
37 return rest_client.ResponseBody(resp, body)
38
39 def delete_resource(self, uri):
40 req_uri = self.uri_prefix + uri
41 resp, body = self.delete(req_uri)
42 self.expected_success(204, resp.status)
43 return rest_client.ResponseBody(resp, body)
44
45 def show_resource(self, uri, **fields):
46 # fields is a dict which key is 'fields' and value is a
47 # list of field's name. An example:
48 # {'fields': ['id', 'name']}
49 req_uri = self.uri_prefix + uri
50 if fields:
51 req_uri += '?' + urllib.urlencode(fields, doseq=1)
52 resp, body = self.get(req_uri)
53 body = json.loads(body)
54 self.expected_success(200, resp.status)
55 return rest_client.ResponseBody(resp, body)
56
57 def create_resource(self, uri, post_data):
58 req_uri = self.uri_prefix + uri
59 req_post_data = json.dumps(post_data)
60 resp, body = self.post(req_uri, req_post_data)
61 body = json.loads(body)
62 self.expected_success(201, resp.status)
63 return rest_client.ResponseBody(resp, body)
64
65 def update_resource(self, uri, post_data):
66 req_uri = self.uri_prefix + uri
67 req_post_data = json.dumps(post_data)
68 resp, body = self.put(req_uri, req_post_data)
69 body = json.loads(body)
70 self.expected_success(200, resp.status)
71 return rest_client.ResponseBody(resp, body)