Debian Codegraph @

ansible_collections/netapp_eseries/santricity/plugins/modules/netapp_e_snapshot_volume.py

raw
1 #!/usr/bin/python
2
3 # (c) 2016, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9
10 ANSIBLE_METADATA = {'metadata_version': '1.1',
11 'status': ['deprecated'],
12 'supported_by': 'community'}
13
14
15 DOCUMENTATION = """
16 ---
17 module: netapp_e_snapshot_volume
18 short_description: NetApp E-Series manage snapshot volumes.
19 description:
20 - Create, update, remove snapshot volumes for NetApp E/EF-Series storage arrays.
21 version_added: '2.2.0'
22 author: Kevin Hulquest (@hulquest)
23 notes:
24 - Only I(full_threshold) is supported for update operations. If the snapshot volume already exists and the threshold matches, then an C(ok) status
25 will be returned, no other changes can be made to a pre-existing snapshot volume.
26 options:
27 api_username:
28 required: true
29 description:
30 - The username to authenticate with the SANtricity WebServices Proxy or embedded REST API.
31 type: str
32 api_password:
33 required: true
34 description:
35 - The password to authenticate with the SANtricity WebServices Proxy or embedded REST API.
36 type: str
37 api_url:
38 required: true
39 description:
40 - The url to the SANtricity WebServices Proxy or embedded REST API.
41 type: str
42 validate_certs:
43 required: false
44 default: true
45 description:
46 - Should https certificates be validated?
47 type: bool
48 ssid:
49 description:
50 - storage array ID
51 type: str
52 required: true
53 snapshot_image_id:
54 required: True
55 type: str
56 description:
57 - The identifier of the snapshot image used to create the new snapshot volume.
58 - "Note: You'll likely want to use the M(netapp_eseries.santricity.netapp_e_facts) module to find
59 the ID of the image you want."
60 full_threshold:
61 description:
62 - The repository utilization warning threshold percentage
63 default: 85
64 type: int
65 name:
66 required: True
67 description:
68 - The name you wish to give the snapshot volume
69 type: str
70 view_mode:
71 required: False
72 type: str
73 description:
74 - The snapshot volume access mode
75 choices: ['readOnly', 'readWrite', 'modeUnknown', '__Undefined']
76 default: 'readOnly'
77 repo_percentage:
78 description:
79 - The size of the view in relation to the size of the base volume
80 default: 20
81 type: int
82 storage_pool_name:
83 description:
84 - Name of the storage pool on which to allocate the repository volume.
85 type: str
86 required: True
87 state:
88 description:
89 - Whether to create or remove the snapshot volume
90 required: True
91 type: str
92 choices:
93 - absent
94 - present
95 """
96 EXAMPLES = """
97 - name: Snapshot volume
98 netapp_e_snapshot_volume:
99 ssid: "{{ ssid }}"
100 api_url: "{{ netapp_api_url }}/"
101 api_username: "{{ netapp_api_username }}"
102 api_password: "{{ netapp_api_password }}"
103 state: present
104 storage_pool_name: "{{ snapshot_volume_storage_pool_name }}"
105 snapshot_image_id: "{{ snapshot_volume_image_id }}"
106 name: "{{ snapshot_volume_name }}"
107 """
108 RETURN = """
109 msg:
110 description: Success message
111 returned: success
112 type: str
113 sample: Json facts for the volume that was created.
114 """
115 HEADERS = {
116 "Content-Type": "application/json",
117 "Accept": "application/json",
118 }
119 import json
120
121 from ansible.module_utils.api import basic_auth_argument_spec
122 from ansible.module_utils.basic import AnsibleModule
123
124 from ansible.module_utils.urls import open_url
125 from ansible.module_utils.six.moves.urllib.error import HTTPError
126
127
128 def request(url, data=None, headers=None, method='GET', use_proxy=True,
129 force=False, last_mod_time=None, timeout=10, validate_certs=True,
130 url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False):
131 try:
132 r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy,
133 force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs,
134 url_username=url_username, url_password=url_password, http_agent=http_agent,
135 force_basic_auth=force_basic_auth)
136 except HTTPError as err:
137 r = err.fp
138
139 try:
140 raw_data = r.read()
141 if raw_data:
142 data = json.loads(raw_data)
143 else:
144 raw_data = None
145 except Exception:
146 if ignore_errors:
147 pass
148 else:
149 raise Exception(raw_data)
150
151 resp_code = r.getcode()
152
153 if resp_code >= 400 and not ignore_errors:
154 raise Exception(resp_code, data)
155 else:
156 return resp_code, data
157
158
159 class SnapshotVolume(object):
160 def __init__(self):
161 argument_spec = basic_auth_argument_spec()
162 argument_spec.update(dict(
163 api_username=dict(type='str', required=True),
164 api_password=dict(type='str', required=True, no_log=True),
165 api_url=dict(type='str', required=True),
166 ssid=dict(type='str', required=True),
167 snapshot_image_id=dict(type='str', required=True),
168 full_threshold=dict(type='int', default=85),
169 name=dict(type='str', required=True),
170 view_mode=dict(type='str', required=False, default='readOnly',
171 choices=['readOnly', 'readWrite', 'modeUnknown', '__Undefined']),
172 repo_percentage=dict(type='int', default=20),
173 storage_pool_name=dict(type='str', required=True),
174 state=dict(type='str', required=True, choices=['absent', 'present'])
175 ))
176
177 self.module = AnsibleModule(argument_spec=argument_spec)
178 args = self.module.params
179 self.state = args['state']
180 self.ssid = args['ssid']
181 self.snapshot_image_id = args['snapshot_image_id']
182 self.full_threshold = args['full_threshold']
183 self.name = args['name']
184 self.view_mode = args['view_mode']
185 self.repo_percentage = args['repo_percentage']
186 self.storage_pool_name = args['storage_pool_name']
187 self.url = args['api_url']
188 self.user = args['api_username']
189 self.pwd = args['api_password']
190 self.certs = args['validate_certs']
191
192 if not self.url.endswith('/'):
193 self.url += '/'
194
195 @property
196 def pool_id(self):
197 pools = 'storage-systems/%s/storage-pools' % self.ssid
198 url = self.url + pools
199 (rc, data) = request(url, headers=HEADERS, url_username=self.user, url_password=self.pwd,
200 validate_certs=self.certs)
201
202 for pool in data:
203 if pool['name'] == self.storage_pool_name:
204 self.pool_data = pool
205 return pool['id']
206
207 self.module.fail_json(msg="No storage pool with the name: '%s' was found" % self.name)
208
209 @property
210 def ss_vol_exists(self):
211 rc, ss_vols = request(self.url + 'storage-systems/%s/snapshot-volumes' % self.ssid, headers=HEADERS,
212 url_username=self.user, url_password=self.pwd, validate_certs=self.certs)
213 if ss_vols:
214 for ss_vol in ss_vols:
215 if ss_vol['name'] == self.name:
216 self.ss_vol = ss_vol
217 return True
218 else:
219 return False
220
221 return False
222
223 @property
224 def ss_vol_needs_update(self):
225 if self.ss_vol['fullWarnThreshold'] != self.full_threshold:
226 return True
227 else:
228 return False
229
230 def create_ss_vol(self):
231 post_data = dict(
232 snapshotImageId=self.snapshot_image_id,
233 fullThreshold=self.full_threshold,
234 name=self.name,
235 viewMode=self.view_mode,
236 repositoryPercentage=self.repo_percentage,
237 repositoryPoolId=self.pool_id
238 )
239
240 rc, create_resp = request(self.url + 'storage-systems/%s/snapshot-volumes' % self.ssid,
241 data=json.dumps(post_data), headers=HEADERS, url_username=self.user,
242 url_password=self.pwd, validate_certs=self.certs, method='POST')
243
244 self.ss_vol = create_resp
245 # Doing a check after creation because the creation call fails to set the specified warning threshold
246 if self.ss_vol_needs_update:
247 self.update_ss_vol()
248 else:
249 self.module.exit_json(changed=True, **create_resp)
250
251 def update_ss_vol(self):
252 post_data = dict(
253 fullThreshold=self.full_threshold,
254 )
255
256 rc, resp = request(self.url + 'storage-systems/%s/snapshot-volumes/%s' % (self.ssid, self.ss_vol['id']),
257 data=json.dumps(post_data), headers=HEADERS, url_username=self.user, url_password=self.pwd,
258 method='POST', validate_certs=self.certs)
259
260 self.module.exit_json(changed=True, **resp)
261
262 def remove_ss_vol(self):
263 rc, resp = request(self.url + 'storage-systems/%s/snapshot-volumes/%s' % (self.ssid, self.ss_vol['id']),
264 headers=HEADERS, url_username=self.user, url_password=self.pwd, validate_certs=self.certs,
265 method='DELETE')
266 self.module.exit_json(changed=True, msg="Volume successfully deleted")
267
268 def apply(self):
269 if self.state == 'present':
270 if self.ss_vol_exists:
271 if self.ss_vol_needs_update:
272 self.update_ss_vol()
273 else:
274 self.module.exit_json(changed=False, **self.ss_vol)
275 else:
276 self.create_ss_vol()
277 else:
278 if self.ss_vol_exists:
279 self.remove_ss_vol()
280 else:
281 self.module.exit_json(changed=False, msg="Volume already absent")
282
283
284 def main():
285 sv = SnapshotVolume()
286 sv.apply()
287
288
289 if __name__ == '__main__':
290 main()