Debian Codegraph @

ansible_collections/netapp/ontap/plugins/module_utils/rest_application.py

raw
1 # This code is part of Ansible, but is an independent component.
2 # This particular file snippet, and this file snippet only, is BSD licensed.
3 # Modules you write using this snippet, which is embedded dynamically by Ansible
4 # still belong to the author of the module, and may assign their own license
5 # to the complete work.
6 #
7 # Copyright (c) 2020-2022, Laurent Nicolas <[email protected]>
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without modification,
11 # are permitted provided that the following conditions are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above copyright notice,
16 # this list of conditions and the following disclaimer in the documentation
17 # and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 """ Support class for NetApp ansible modules
30
31 Provides accesss to application resources using REST calls
32 """
33
34 from __future__ import (absolute_import, division, print_function)
35 __metaclass__ = type
36
37 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
38
39
40 class RestApplication():
41 """Helper methods to manage application and application components"""
42 def __init__(self, rest_api, svm_name, app_name):
43 self.svm_name = svm_name
44 self.app_name = app_name
45 self.app_uuid = None
46 self.rest_api = rest_api
47
48 def _set_application_uuid(self):
49 """Use REST application/applications to get application uuid"""
50 api = 'application/applications'
51 query = {'svm.name': self.svm_name, 'name': self.app_name}
52 record, error = rest_generic.get_one_record(self.rest_api, api, query)
53 if error is None and record is not None:
54 self.app_uuid = record['uuid']
55 return None, error
56
57 def get_application_uuid(self):
58 """Use REST application/applications to get application uuid"""
59 error = None
60 if self.app_uuid is None:
61 dummy, error = self._set_application_uuid()
62 return self.app_uuid, error
63
64 def get_application_details(self, template=None):
65 """Use REST application/applications to get application details"""
66 uuid, error = self.get_application_uuid()
67 if error:
68 return uuid, error
69 if uuid is None: # not found
70 return None, None
71 query = dict(fields='name,%s,statistics' % template) if template else None
72 api = 'application/applications/%s' % uuid
73 return rest_generic.get_one_record(self.rest_api, api, query)
74
75 def create_application(self, body):
76 """Use REST application/applications san template to create one or more LUNs"""
77 dummy, error = self.fail_if_uuid('create_application')
78 if error is not None:
79 return dummy, error
80 api = 'application/applications'
81 query = {'return_records': 'true'}
82 response, error = rest_generic.post_async(self.rest_api, api, body, query)
83 if error and 'Unexpected argument' in error and 'exclude_aggregates' in error:
84 error += ' "exclude_aggregates" requires ONTAP 9.9.1 GA or later.'
85 return response, error
86
87 def patch_application(self, body):
88 """Use REST application/applications san template to add one or more LUNs"""
89 dummy, error = self.fail_if_no_uuid()
90 if error is not None:
91 return dummy, error
92 api = 'application/applications'
93 query = {'return_records': 'true'}
94 return rest_generic.patch_async(self.rest_api, api, self.app_uuid, body, query)
95
96 def create_application_body(self, template_name, template_body, smart_container=True):
97 if not isinstance(smart_container, bool):
98 error = "expecting bool value for smart_container, got: %s" % smart_container
99 return None, error
100 body = {
101 'name': self.app_name,
102 'svm': {'name': self.svm_name},
103 'smart_container': smart_container,
104 template_name: template_body
105 }
106 return body, None
107
108 def delete_application(self):
109 """Use REST application/applications to delete app"""
110 dummy, error = self.fail_if_no_uuid()
111 if error is not None:
112 return dummy, error
113 api = 'application/applications'
114 response, error = rest_generic.delete_async(self.rest_api, api, self.app_uuid)
115 self.app_uuid = None
116 return response, error
117
118 def get_application_components(self):
119 """Use REST application/applications to get application components"""
120 dummy, error = self.fail_if_no_uuid()
121 if error is not None:
122 return dummy, error
123 api = 'application/applications/%s/components' % self.app_uuid
124 return rest_generic.get_0_or_more_records(self.rest_api, api)
125
126 def get_application_component_uuid(self):
127 """Use REST application/applications to get component uuid
128 Assume a single component per application
129 """
130 dummy, error = self.fail_if_no_uuid()
131 if error is not None:
132 return dummy, error
133 api = 'application/applications/%s/components' % self.app_uuid
134 record, error = rest_generic.get_one_record(self.rest_api, api, fields='uuid')
135 if error is None and record is not None:
136 return record['uuid'], None
137 return None, error
138
139 def get_application_component_details(self, comp_uuid=None):
140 """Use REST application/applications to get application components"""
141 dummy, error = self.fail_if_no_uuid()
142 if error is not None:
143 return dummy, error
144 if comp_uuid is None:
145 # assume a single component
146 comp_uuid, error = self.get_application_component_uuid()
147 if error:
148 return comp_uuid, error
149 if comp_uuid is None:
150 error = 'no component for application %s' % self.app_name
151 return None, error
152 api = 'application/applications/%s/components/%s' % (self.app_uuid, comp_uuid)
153 return rest_generic.get_one_record(self.rest_api, api)
154
155 def get_application_component_backing_storage(self):
156 """Use REST application/applications to get component uuid.
157
158 Assume a single component per application
159 """
160 dummy, error = self.fail_if_no_uuid()
161 if error is not None:
162 return dummy, error
163 response, error = self.get_application_component_details()
164 if error or response is None:
165 return response, error
166 return response['backing_storage'], None
167
168 def fail_if_no_uuid(self):
169 """Prevent a logic error."""
170 if self.app_uuid is None:
171 msg = 'function should not be called before application uuid is set.'
172 return None, msg
173 return None, None
174
175 def fail_if_uuid(self, fname):
176 """Prevent a logic error."""
177 if self.app_uuid is not None:
178 msg = 'function %s should not be called when application uuid is set: %s.' % (fname, self.app_uuid)
179 return None, msg
180 return None, None