1# Copyright 2016 NTT DATA
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import testtools
16
17
18class BaseController(testtools.TestCase):
19    def __init__(self, api, schema_api, controller_class):
20        self.controller = controller_class(api, schema_api)
21
22    def _assertRequestId(self, obj):
23        self.assertIsNotNone(getattr(obj, 'request_ids', None))
24        self.assertEqual(['req-1234'], obj.request_ids)
25
26    def list(self, *args, **kwargs):
27        gen_obj = self.controller.list(*args, **kwargs)
28        # For generator cases the request_ids property will be an empty list
29        # until the underlying generator is invoked at-least once.
30        resources = list(gen_obj)
31        if len(resources) > 0:
32            self._assertRequestId(gen_obj)
33        else:
34            # If list is empty that means geneator object has raised
35            # StopIteration for first iteration and will not contain the
36            # request_id in it.
37            self.assertEqual([], gen_obj.request_ids)
38
39        return resources
40
41    def get_associated_image_tasks(self, *args, **kwargs):
42        resource = self.controller.get_associated_image_tasks(
43            *args, **kwargs)
44
45        self._assertRequestId(resource)
46        return resource
47
48    def get(self, *args, **kwargs):
49        resource = self.controller.get(*args, **kwargs)
50
51        self._assertRequestId(resource)
52        return resource
53
54    def create(self, *args, **kwargs):
55        resource = self.controller.create(*args, **kwargs)
56        self._assertRequestId(resource)
57        return resource
58
59    def create_multiple(self, *args, **kwargs):
60        tags = self.controller.create_multiple(*args, **kwargs)
61        actual = [tag.name for tag in tags]
62        self._assertRequestId(tags)
63        return actual
64
65    def update(self, *args, **properties):
66        resource = self.controller.update(*args, **properties)
67        self._assertRequestId(resource)
68        return resource
69
70    def delete(self, *args):
71        resp = self.controller.delete(*args)
72        self._assertRequestId(resp)
73
74    def delete_all(self, *args):
75        resp = self.controller.delete_all(*args)
76        self._assertRequestId(resp)
77
78    def deactivate(self, *args):
79        resp = self.controller.deactivate(*args)
80        self._assertRequestId(resp)
81
82    def reactivate(self, *args):
83        resp = self.controller.reactivate(*args)
84        self._assertRequestId(resp)
85
86    def upload(self, *args, **kwargs):
87        resp = self.controller.upload(*args, **kwargs)
88        self._assertRequestId(resp)
89
90    def data(self, *args, **kwargs):
91        body = self.controller.data(*args, **kwargs)
92        self._assertRequestId(body)
93        return body
94
95    def delete_locations(self, *args):
96        resp = self.controller.delete_locations(*args)
97        self._assertRequestId(resp)
98
99    def add_location(self, *args, **kwargs):
100        resp = self.controller.add_location(*args, **kwargs)
101        self._assertRequestId(resp)
102
103    def update_location(self, *args, **kwargs):
104        resp = self.controller.update_location(*args, **kwargs)
105        self._assertRequestId(resp)
106
107    def associate(self, *args, **kwargs):
108        resource_types = self.controller.associate(*args, **kwargs)
109        self._assertRequestId(resource_types)
110        return resource_types
111
112    def deassociate(self, *args):
113        resp = self.controller.deassociate(*args)
114        self._assertRequestId(resp)
115
116    def image_import(self, *args):
117        resp = self.controller.image_import(*args)
118        self._assertRequestId(resp)
119
120
121class BaseResourceTypeController(BaseController):
122    def __init__(self, api, schema_api, controller_class):
123        super(BaseResourceTypeController, self).__init__(api, schema_api,
124                                                         controller_class)
125
126    def get(self, *args, **kwargs):
127        resource_types = self.controller.get(*args)
128        names = [rt.name for rt in resource_types]
129        self._assertRequestId(resource_types)
130        return names
131