| 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_group |
| 18 |
short_description: NetApp E-Series manage snapshot groups |
| 19 |
description: |
| 20 |
- Create, update, delete snapshot groups for NetApp E-series storage arrays |
| 21 |
version_added: '2.2.0' |
| 22 |
author: Kevin Hulquest (@hulquest) |
| 23 |
options: |
| 24 |
ssid: |
| 25 |
description: |
| 26 |
- Storage system identifier |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
api_username: |
| 30 |
required: true |
| 31 |
description: |
| 32 |
- The username to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 33 |
type: str |
| 34 |
api_password: |
| 35 |
required: true |
| 36 |
description: |
| 37 |
- The password to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 38 |
type: str |
| 39 |
api_url: |
| 40 |
required: true |
| 41 |
description: |
| 42 |
- The url to the SANtricity WebServices Proxy or embedded REST API. |
| 43 |
type: str |
| 44 |
validate_certs: |
| 45 |
required: false |
| 46 |
default: true |
| 47 |
description: |
| 48 |
- Should https certificates be validated? |
| 49 |
type: bool |
| 50 |
state: |
| 51 |
description: |
| 52 |
- Whether to ensure the group is present or absent. |
| 53 |
required: True |
| 54 |
type: str |
| 55 |
choices: |
| 56 |
- present |
| 57 |
- absent |
| 58 |
name: |
| 59 |
description: |
| 60 |
- The name to give the snapshot group |
| 61 |
type: str |
| 62 |
required: True |
| 63 |
base_volume_name: |
| 64 |
description: |
| 65 |
- The name of the base volume or thin volume to use as the base for the new snapshot group. |
| 66 |
- If a snapshot group with an identical C(name) already exists but with a different base volume |
| 67 |
an error will be returned. |
| 68 |
type: str |
| 69 |
required: True |
| 70 |
repo_pct: |
| 71 |
description: |
| 72 |
- The size of the repository in relation to the size of the base volume |
| 73 |
required: False |
| 74 |
type: int |
| 75 |
default: 20 |
| 76 |
warning_threshold: |
| 77 |
description: |
| 78 |
- The repository utilization warning threshold, as a percentage of the repository volume capacity. |
| 79 |
required: False |
| 80 |
type: int |
| 81 |
default: 80 |
| 82 |
delete_limit: |
| 83 |
description: |
| 84 |
- The automatic deletion indicator. |
| 85 |
- If non-zero, the oldest snapshot image will be automatically deleted when creating a new snapshot image to keep the total number of |
| 86 |
snapshot images limited to the number specified. |
| 87 |
- This value is overridden by the consistency group setting if this snapshot group is associated with a consistency group. |
| 88 |
required: False |
| 89 |
type: int |
| 90 |
default: 30 |
| 91 |
full_policy: |
| 92 |
description: |
| 93 |
- The behavior on when the data repository becomes full. |
| 94 |
- This value is overridden by consistency group setting if this snapshot group is associated with a consistency group |
| 95 |
required: False |
| 96 |
default: purgepit |
| 97 |
type: str |
| 98 |
choices: ['unknown', 'failbasewrites', 'purgepit'] |
| 99 |
storage_pool_name: |
| 100 |
required: True |
| 101 |
description: |
| 102 |
- The name of the storage pool on which to allocate the repository volume. |
| 103 |
type: str |
| 104 |
rollback_priority: |
| 105 |
required: False |
| 106 |
description: |
| 107 |
- The importance of the rollback operation. |
| 108 |
- This value is overridden by consistency group setting if this snapshot group is associated with a consistency group |
| 109 |
choices: ['highest', 'high', 'medium', 'low', 'lowest'] |
| 110 |
type: str |
| 111 |
default: medium |
| 112 |
""" |
| 113 |
|
| 114 |
EXAMPLES = """ |
| 115 |
- name: Configure Snapshot group |
| 116 |
netapp_e_snapshot_group: |
| 117 |
ssid: "{{ ssid }}" |
| 118 |
api_url: "{{ netapp_api_url }}" |
| 119 |
api_username: "{{ netapp_api_username }}" |
| 120 |
api_password: "{{ netapp_api_password }}" |
| 121 |
validate_certs: "{{ netapp_api_validate_certs }}" |
| 122 |
base_volume_name: SSGroup_test |
| 123 |
name=: OOSS_Group |
| 124 |
repo_pct: 20 |
| 125 |
warning_threshold: 85 |
| 126 |
delete_limit: 30 |
| 127 |
full_policy: purgepit |
| 128 |
storage_pool_name: Disk_Pool_1 |
| 129 |
rollback_priority: medium |
| 130 |
""" |
| 131 |
RETURN = """ |
| 132 |
msg: |
| 133 |
description: Success message |
| 134 |
returned: success |
| 135 |
type: str |
| 136 |
sample: json facts for newly created snapshot group. |
| 137 |
""" |
| 138 |
HEADERS = { |
| 139 |
"Content-Type": "application/json", |
| 140 |
"Accept": "application/json", |
| 141 |
} |
| 142 |
import json |
| 143 |
|
| 144 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 145 |
from ansible.module_utils.basic import AnsibleModule |
| 146 |
|
| 147 |
from ansible.module_utils._text import to_native |
| 148 |
from ansible.module_utils.urls import open_url |
| 149 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 150 |
|
| 151 |
|
| 152 |
def request(url, data=None, headers=None, method='GET', use_proxy=True, |
| 153 |
force=False, last_mod_time=None, timeout=10, validate_certs=True, |
| 154 |
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False): |
| 155 |
try: |
| 156 |
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy, |
| 157 |
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, |
| 158 |
url_username=url_username, url_password=url_password, http_agent=http_agent, |
| 159 |
force_basic_auth=force_basic_auth) |
| 160 |
except HTTPError as err: |
| 161 |
r = err.fp |
| 162 |
|
| 163 |
try: |
| 164 |
raw_data = r.read() |
| 165 |
if raw_data: |
| 166 |
data = json.loads(raw_data) |
| 167 |
else: |
| 168 |
raw_data = None |
| 169 |
except Exception: |
| 170 |
if ignore_errors: |
| 171 |
pass |
| 172 |
else: |
| 173 |
raise Exception(raw_data) |
| 174 |
|
| 175 |
resp_code = r.getcode() |
| 176 |
|
| 177 |
if resp_code >= 400 and not ignore_errors: |
| 178 |
raise Exception(resp_code, data) |
| 179 |
else: |
| 180 |
return resp_code, data |
| 181 |
|
| 182 |
|
| 183 |
class SnapshotGroup(object): |
| 184 |
def __init__(self): |
| 185 |
|
| 186 |
argument_spec = basic_auth_argument_spec() |
| 187 |
argument_spec.update( |
| 188 |
api_username=dict(type='str', required=True), |
| 189 |
api_password=dict(type='str', required=True, no_log=True), |
| 190 |
api_url=dict(type='str', required=True), |
| 191 |
state=dict(required=True, choices=['present', 'absent']), |
| 192 |
base_volume_name=dict(required=True), |
| 193 |
name=dict(required=True), |
| 194 |
repo_pct=dict(default=20, type='int'), |
| 195 |
warning_threshold=dict(default=80, type='int'), |
| 196 |
delete_limit=dict(default=30, type='int'), |
| 197 |
full_policy=dict(default='purgepit', choices=['unknown', 'failbasewrites', 'purgepit']), |
| 198 |
rollback_priority=dict(default='medium', choices=['highest', 'high', 'medium', 'low', 'lowest']), |
| 199 |
storage_pool_name=dict(type='str', required=True), |
| 200 |
ssid=dict(required=True), |
| 201 |
) |
| 202 |
|
| 203 |
self.module = AnsibleModule(argument_spec=argument_spec) |
| 204 |
|
| 205 |
self.post_data = dict() |
| 206 |
self.warning_threshold = self.module.params['warning_threshold'] |
| 207 |
self.base_volume_name = self.module.params['base_volume_name'] |
| 208 |
self.name = self.module.params['name'] |
| 209 |
self.repo_pct = self.module.params['repo_pct'] |
| 210 |
self.delete_limit = self.module.params['delete_limit'] |
| 211 |
self.full_policy = self.module.params['full_policy'] |
| 212 |
self.rollback_priority = self.module.params['rollback_priority'] |
| 213 |
self.storage_pool_name = self.module.params['storage_pool_name'] |
| 214 |
self.state = self.module.params['state'] |
| 215 |
|
| 216 |
self.url = self.module.params['api_url'] |
| 217 |
self.user = self.module.params['api_username'] |
| 218 |
self.pwd = self.module.params['api_password'] |
| 219 |
self.certs = self.module.params['validate_certs'] |
| 220 |
self.ssid = self.module.params['ssid'] |
| 221 |
|
| 222 |
if not self.url.endswith('/'): |
| 223 |
self.url += '/' |
| 224 |
|
| 225 |
self.changed = False |
| 226 |
|
| 227 |
@property |
| 228 |
def pool_id(self): |
| 229 |
pools = 'storage-systems/%s/storage-pools' % self.ssid |
| 230 |
url = self.url + pools |
| 231 |
try: |
| 232 |
(rc, data) = request(url, headers=HEADERS, url_username=self.user, url_password=self.pwd) |
| 233 |
except Exception as err: |
| 234 |
self.module.fail_json(msg="Snapshot group module - Failed to fetch storage pools. " + |
| 235 |
"Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 236 |
|
| 237 |
for pool in data: |
| 238 |
if pool['name'] == self.storage_pool_name: |
| 239 |
self.pool_data = pool |
| 240 |
return pool['id'] |
| 241 |
|
| 242 |
self.module.fail_json(msg="No storage pool with the name: '%s' was found" % self.name) |
| 243 |
|
| 244 |
@property |
| 245 |
def volume_id(self): |
| 246 |
Id = None |
| 247 |
volumes = 'storage-systems/%s/volumes' % self.ssid |
| 248 |
url = self.url + volumes |
| 249 |
try: |
| 250 |
rc, data = request(url, headers=HEADERS, url_username=self.user, url_password=self.pwd, |
| 251 |
validate_certs=self.certs) |
| 252 |
except Exception as err: |
| 253 |
self.module.fail_json(msg="Snapshot group module - Failed to fetch volumes. " + |
| 254 |
"Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 255 |
qty = 0 |
| 256 |
for volume in data: |
| 257 |
if volume['name'] == self.base_volume_name: |
| 258 |
qty += 1 |
| 259 |
|
| 260 |
if qty > 1: |
| 261 |
self.module.fail_json(msg="More than one volume with the name: %s was found, " |
| 262 |
"please ensure your volume has a unique name" % self.base_volume_name) |
| 263 |
else: |
| 264 |
Id = volume['id'] |
| 265 |
self.volume = volume |
| 266 |
|
| 267 |
try: |
| 268 |
return Id |
| 269 |
except NameError: |
| 270 |
self.module.fail_json(msg="No volume with the name: %s, was found" % self.base_volume_name) |
| 271 |
|
| 272 |
@property |
| 273 |
def snapshot_group_id(self): |
| 274 |
url = self.url + 'storage-systems/%s/snapshot-groups' % self.ssid |
| 275 |
try: |
| 276 |
rc, data = request(url, headers=HEADERS, url_username=self.user, url_password=self.pwd, |
| 277 |
validate_certs=self.certs) |
| 278 |
except Exception as err: |
| 279 |
self.module.fail_json(msg="Failed to fetch snapshot groups. " + |
| 280 |
"Id [%s]. Error [%s]." % (self.ssid, to_native(err))) |
| 281 |
for ssg in data: |
| 282 |
if ssg['name'] == self.name: |
| 283 |
self.ssg_data = ssg |
| 284 |
return ssg['id'] |
| 285 |
|
| 286 |
return None |
| 287 |
|
| 288 |
@property |
| 289 |
def ssg_needs_update(self): |
| 290 |
if self.ssg_data['fullWarnThreshold'] != self.warning_threshold or \ |
| 291 |
self.ssg_data['autoDeleteLimit'] != self.delete_limit or \ |
| 292 |
self.ssg_data['repFullPolicy'] != self.full_policy or \ |
| 293 |
self.ssg_data['rollbackPriority'] != self.rollback_priority: |
| 294 |
return True |
| 295 |
else: |
| 296 |
return False |
| 297 |
|
| 298 |
def create_snapshot_group(self): |
| 299 |
self.post_data = dict( |
| 300 |
baseMappableObjectId=self.volume_id, |
| 301 |
name=self.name, |
| 302 |
repositoryPercentage=self.repo_pct, |
| 303 |
warningThreshold=self.warning_threshold, |
| 304 |
autoDeleteLimit=self.delete_limit, |
| 305 |
fullPolicy=self.full_policy, |
| 306 |
storagePoolId=self.pool_id, |
| 307 |
) |
| 308 |
snapshot = 'storage-systems/%s/snapshot-groups' % self.ssid |
| 309 |
url = self.url + snapshot |
| 310 |
try: |
| 311 |
rc, self.ssg_data = request(url, data=json.dumps(self.post_data), method='POST', headers=HEADERS, |
| 312 |
url_username=self.user, url_password=self.pwd, validate_certs=self.certs) |
| 313 |
except Exception as err: |
| 314 |
self.module.fail_json(msg="Failed to create snapshot group. " + |
| 315 |
"Snapshot group [%s]. Id [%s]. Error [%s]." % (self.name, |
| 316 |
self.ssid, |
| 317 |
to_native(err))) |
| 318 |
|
| 319 |
if not self.snapshot_group_id: |
| 320 |
self.snapshot_group_id = self.ssg_data['id'] |
| 321 |
|
| 322 |
if self.ssg_needs_update: |
| 323 |
self.update_ssg() |
| 324 |
else: |
| 325 |
self.module.exit_json(changed=True, **self.ssg_data) |
| 326 |
|
| 327 |
def update_ssg(self): |
| 328 |
self.post_data = dict( |
| 329 |
warningThreshold=self.warning_threshold, |
| 330 |
autoDeleteLimit=self.delete_limit, |
| 331 |
fullPolicy=self.full_policy, |
| 332 |
rollbackPriority=self.rollback_priority |
| 333 |
) |
| 334 |
|
| 335 |
url = self.url + "storage-systems/%s/snapshot-groups/%s" % (self.ssid, self.snapshot_group_id) |
| 336 |
try: |
| 337 |
rc, self.ssg_data = request(url, data=json.dumps(self.post_data), method='POST', headers=HEADERS, |
| 338 |
url_username=self.user, url_password=self.pwd, validate_certs=self.certs) |
| 339 |
except Exception as err: |
| 340 |
self.module.fail_json(msg="Failed to update snapshot group. " + |
| 341 |
"Snapshot group [%s]. Id [%s]. Error [%s]." % (self.name, |
| 342 |
self.ssid, |
| 343 |
to_native(err))) |
| 344 |
|
| 345 |
def apply(self): |
| 346 |
if self.state == 'absent': |
| 347 |
if self.snapshot_group_id: |
| 348 |
try: |
| 349 |
rc, resp = request( |
| 350 |
self.url + 'storage-systems/%s/snapshot-groups/%s' % (self.ssid, self.snapshot_group_id), |
| 351 |
method='DELETE', headers=HEADERS, url_password=self.pwd, url_username=self.user, |
| 352 |
validate_certs=self.certs) |
| 353 |
except Exception as err: |
| 354 |
self.module.fail_json(msg="Failed to delete snapshot group. " + |
| 355 |
"Snapshot group [%s]. Id [%s]. Error [%s]." % (self.name, |
| 356 |
self.ssid, |
| 357 |
to_native(err))) |
| 358 |
self.module.exit_json(changed=True, msg="Snapshot group removed", **self.ssg_data) |
| 359 |
else: |
| 360 |
self.module.exit_json(changed=False, msg="Snapshot group absent") |
| 361 |
|
| 362 |
elif self.snapshot_group_id: |
| 363 |
if self.ssg_needs_update: |
| 364 |
self.update_ssg() |
| 365 |
self.module.exit_json(changed=True, **self.ssg_data) |
| 366 |
else: |
| 367 |
self.module.exit_json(changed=False, **self.ssg_data) |
| 368 |
else: |
| 369 |
self.create_snapshot_group() |
| 370 |
|
| 371 |
|
| 372 |
def main(): |
| 373 |
vg = SnapshotGroup() |
| 374 |
vg.apply() |
| 375 |
|
| 376 |
|
| 377 |
if __name__ == '__main__': |
| 378 |
main() |