blob: fe8b244eaeee6b46eb7de116decbcc4ae5897d48 [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
Felipe Monteiro8aa94cb2017-06-16 03:02:33 +010057 def create_resource(self, uri, post_data, expect_empty_body=False,
58 expect_response_code=201):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050059 req_uri = self.uri_prefix + uri
60 req_post_data = json.dumps(post_data)
61 resp, body = self.post(req_uri, req_post_data)
Hirofumi Ichihara31a054a2016-06-02 20:37:13 +090062 # NOTE: RFC allows both a valid non-empty body and an empty body for
63 # response of POST API. If a body is expected not empty, we decode the
64 # body. Otherwise we returns the body as it is.
65 if not expect_empty_body:
66 body = json.loads(body)
Hirofumi Ichihara1950d222016-08-10 09:54:14 +090067 else:
68 body = None
Felipe Monteiro8aa94cb2017-06-16 03:02:33 +010069 self.expected_success(expect_response_code, resp.status)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050070 return rest_client.ResponseBody(resp, body)
71
Felipe Monteiro8aa94cb2017-06-16 03:02:33 +010072 def update_resource(self, uri, post_data, expect_empty_body=False,
73 expect_response_code=200):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050074 req_uri = self.uri_prefix + uri
75 req_post_data = json.dumps(post_data)
76 resp, body = self.put(req_uri, req_post_data)
Hirofumi Ichihara31a054a2016-06-02 20:37:13 +090077 # NOTE: RFC allows both a valid non-empty body and an empty body for
78 # response of PUT API. If a body is expected not empty, we decode the
79 # body. Otherwise we returns the body as it is.
80 if not expect_empty_body:
81 body = json.loads(body)
Hirofumi Ichihara1950d222016-08-10 09:54:14 +090082 else:
83 body = None
Felipe Monteiro8aa94cb2017-06-16 03:02:33 +010084 self.expected_success(expect_response_code, resp.status)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050085 return rest_client.ResponseBody(resp, body)