| 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 |
module: netapp_e_flashcache |
| 17 |
author: Kevin Hulquest (@hulquest) |
| 18 |
version_added: '2.2.0' |
| 19 |
short_description: NetApp E-Series manage SSD caches |
| 20 |
description: |
| 21 |
- Create or remove SSD caches on a NetApp E-Series storage array. |
| 22 |
options: |
| 23 |
api_username: |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
description: |
| 27 |
- The username to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 28 |
api_password: |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- The password to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 33 |
api_url: |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
description: |
| 37 |
- The url to the SANtricity WebServices Proxy or embedded REST API. |
| 38 |
validate_certs: |
| 39 |
required: false |
| 40 |
default: true |
| 41 |
description: |
| 42 |
- Should https certificates be validated? |
| 43 |
type: bool |
| 44 |
ssid: |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- The ID of the array to manage (as configured on the web services proxy). |
| 49 |
state: |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- Whether the specified SSD cache should exist or not. |
| 54 |
choices: ['present', 'absent'] |
| 55 |
name: |
| 56 |
required: true |
| 57 |
type: str |
| 58 |
description: |
| 59 |
- The name of the SSD cache to manage |
| 60 |
io_type: |
| 61 |
description: |
| 62 |
- The type of workload to optimize the cache for. |
| 63 |
choices: ['filesystem','database','media'] |
| 64 |
default: filesystem |
| 65 |
type: str |
| 66 |
disk_count: |
| 67 |
type: int |
| 68 |
description: |
| 69 |
- The minimum number of disks to use for building the cache. The cache will be expanded if this number exceeds the number of disks already in place |
| 70 |
disk_refs: |
| 71 |
description: |
| 72 |
- List of disk references |
| 73 |
type: list |
| 74 |
elements: str |
| 75 |
size_unit: |
| 76 |
description: |
| 77 |
- The unit to be applied to size arguments |
| 78 |
choices: ['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'] |
| 79 |
default: gb |
| 80 |
type: str |
| 81 |
cache_size_min: |
| 82 |
description: |
| 83 |
- The minimum size (in size_units) of the ssd cache. The cache will be expanded if this exceeds the current size of the cache. |
| 84 |
type: int |
| 85 |
criteria_disk_phy_type: |
| 86 |
description: |
| 87 |
- Type of physical disk |
| 88 |
choices: ['sas', 'sas4k', 'fibre', 'fibre520b', 'scsi', 'sata', 'pata'] |
| 89 |
type: str |
| 90 |
log_mode: |
| 91 |
type: str |
| 92 |
description: |
| 93 |
- Log mode |
| 94 |
log_path: |
| 95 |
type: str |
| 96 |
description: |
| 97 |
- Log path |
| 98 |
''' |
| 99 |
|
| 100 |
EXAMPLES = """ |
| 101 |
- name: Flash Cache |
| 102 |
netapp_e_flashcache: |
| 103 |
ssid: "{{ ssid }}" |
| 104 |
api_url: "{{ netapp_api_url }}" |
| 105 |
api_username: "{{ netapp_api_username }}" |
| 106 |
api_password: "{{ netapp_api_password }}" |
| 107 |
validate_certs: "{{ netapp_api_validate_certs }}" |
| 108 |
name: SSDCacheBuiltByAnsible |
| 109 |
""" |
| 110 |
|
| 111 |
RETURN = """ |
| 112 |
msg: |
| 113 |
description: Success message |
| 114 |
returned: success |
| 115 |
type: str |
| 116 |
sample: json for newly created flash cache |
| 117 |
""" |
| 118 |
import json |
| 119 |
import logging |
| 120 |
import sys |
| 121 |
import traceback |
| 122 |
|
| 123 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 124 |
from ansible.module_utils.basic import AnsibleModule |
| 125 |
from ansible.module_utils.six.moves import reduce |
| 126 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 127 |
from ansible.module_utils._text import to_native |
| 128 |
from ansible.module_utils.urls import open_url |
| 129 |
|
| 130 |
|
| 131 |
def request(url, data=None, headers=None, method='GET', use_proxy=True, |
| 132 |
force=False, last_mod_time=None, timeout=10, validate_certs=True, |
| 133 |
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False): |
| 134 |
try: |
| 135 |
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy, |
| 136 |
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, |
| 137 |
url_username=url_username, url_password=url_password, http_agent=http_agent, |
| 138 |
force_basic_auth=force_basic_auth) |
| 139 |
except HTTPError as err: |
| 140 |
r = err.fp |
| 141 |
|
| 142 |
try: |
| 143 |
raw_data = r.read() |
| 144 |
if raw_data: |
| 145 |
data = json.loads(raw_data) |
| 146 |
else: |
| 147 |
raw_data = None |
| 148 |
except Exception: |
| 149 |
if ignore_errors: |
| 150 |
pass |
| 151 |
else: |
| 152 |
raise Exception(raw_data) |
| 153 |
|
| 154 |
resp_code = r.getcode() |
| 155 |
|
| 156 |
if resp_code >= 400 and not ignore_errors: |
| 157 |
raise Exception(resp_code, data) |
| 158 |
else: |
| 159 |
return resp_code, data |
| 160 |
|
| 161 |
|
| 162 |
class NetAppESeriesFlashCache(object): |
| 163 |
def __init__(self): |
| 164 |
self.name = None |
| 165 |
self.log_mode = None |
| 166 |
self.log_path = None |
| 167 |
self.api_url = None |
| 168 |
self.api_username = None |
| 169 |
self.api_password = None |
| 170 |
self.ssid = None |
| 171 |
self.validate_certs = None |
| 172 |
self.disk_count = None |
| 173 |
self.size_unit = None |
| 174 |
self.cache_size_min = None |
| 175 |
self.io_type = None |
| 176 |
self.driveRefs = None |
| 177 |
self.state = None |
| 178 |
self._size_unit_map = dict( |
| 179 |
bytes=1, |
| 180 |
b=1, |
| 181 |
kb=1024, |
| 182 |
mb=1024 ** 2, |
| 183 |
gb=1024 ** 3, |
| 184 |
tb=1024 ** 4, |
| 185 |
pb=1024 ** 5, |
| 186 |
eb=1024 ** 6, |
| 187 |
zb=1024 ** 7, |
| 188 |
yb=1024 ** 8 |
| 189 |
) |
| 190 |
|
| 191 |
argument_spec = basic_auth_argument_spec() |
| 192 |
argument_spec.update(dict( |
| 193 |
api_username=dict(type='str', required=True), |
| 194 |
api_password=dict(type='str', required=True, no_log=True), |
| 195 |
api_url=dict(type='str', required=True), |
| 196 |
state=dict(required=True, choices=['present', 'absent'], type='str'), |
| 197 |
ssid=dict(required=True, type='str'), |
| 198 |
name=dict(required=True, type='str'), |
| 199 |
disk_count=dict(type='int'), |
| 200 |
disk_refs=dict(type='list', elements="str"), |
| 201 |
cache_size_min=dict(type='int'), |
| 202 |
io_type=dict(default='filesystem', choices=['filesystem', 'database', 'media']), |
| 203 |
size_unit=dict(default='gb', choices=['bytes', 'b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'], |
| 204 |
type='str'), |
| 205 |
criteria_disk_phy_type=dict(choices=['sas', 'sas4k', 'fibre', 'fibre520b', 'scsi', 'sata', 'pata'], |
| 206 |
type='str'), |
| 207 |
log_mode=dict(type='str'), |
| 208 |
log_path=dict(type='str'), |
| 209 |
)) |
| 210 |
self.module = AnsibleModule( |
| 211 |
argument_spec=argument_spec, |
| 212 |
required_if=[ |
| 213 |
|
| 214 |
], |
| 215 |
mutually_exclusive=[ |
| 216 |
|
| 217 |
], |
| 218 |
# TODO: update validation for various selection criteria |
| 219 |
supports_check_mode=True |
| 220 |
) |
| 221 |
|
| 222 |
self.__dict__.update(self.module.params) |
| 223 |
|
| 224 |
# logging setup |
| 225 |
self._logger = logging.getLogger(self.__class__.__name__) |
| 226 |
self.debug = self._logger.debug |
| 227 |
|
| 228 |
if self.log_mode == 'file' and self.log_path: |
| 229 |
logging.basicConfig(level=logging.DEBUG, filename=self.log_path) |
| 230 |
elif self.log_mode == 'stderr': |
| 231 |
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr) |
| 232 |
|
| 233 |
self.post_headers = dict(Accept="application/json") |
| 234 |
self.post_headers['Content-Type'] = 'application/json' |
| 235 |
|
| 236 |
def get_candidate_disks(self, disk_count, size_unit='gb', capacity=None): |
| 237 |
self.debug("getting candidate disks...") |
| 238 |
|
| 239 |
drives_req = dict( |
| 240 |
driveCount=disk_count, |
| 241 |
sizeUnit=size_unit, |
| 242 |
driveType='ssd', |
| 243 |
) |
| 244 |
|
| 245 |
if capacity: |
| 246 |
drives_req['targetUsableCapacity'] = capacity |
| 247 |
|
| 248 |
(rc, drives_resp) = request(self.api_url + "/storage-systems/%s/drives" % (self.ssid), |
| 249 |
data=json.dumps(drives_req), headers=self.post_headers, method='POST', |
| 250 |
url_username=self.api_username, url_password=self.api_password, |
| 251 |
validate_certs=self.validate_certs) |
| 252 |
|
| 253 |
if rc == 204: |
| 254 |
self.module.fail_json(msg='Cannot find disks to match requested criteria for ssd cache') |
| 255 |
|
| 256 |
disk_ids = [d['id'] for d in drives_resp] |
| 257 |
bytes = reduce(lambda s, d: s + int(d['usableCapacity']), drives_resp, 0) |
| 258 |
|
| 259 |
return (disk_ids, bytes) |
| 260 |
|
| 261 |
def create_cache(self): |
| 262 |
(disk_ids, bytes) = self.get_candidate_disks(disk_count=self.disk_count, size_unit=self.size_unit, |
| 263 |
capacity=self.cache_size_min) |
| 264 |
|
| 265 |
self.debug("creating ssd cache...") |
| 266 |
|
| 267 |
create_fc_req = dict( |
| 268 |
driveRefs=disk_ids, |
| 269 |
name=self.name |
| 270 |
) |
| 271 |
|
| 272 |
(rc, self.resp) = request(self.api_url + "/storage-systems/%s/flash-cache" % (self.ssid), |
| 273 |
data=json.dumps(create_fc_req), headers=self.post_headers, method='POST', |
| 274 |
url_username=self.api_username, url_password=self.api_password, |
| 275 |
validate_certs=self.validate_certs) |
| 276 |
|
| 277 |
def update_cache(self): |
| 278 |
self.debug('updating flash cache config...') |
| 279 |
update_fc_req = dict( |
| 280 |
name=self.name, |
| 281 |
configType=self.io_type |
| 282 |
) |
| 283 |
|
| 284 |
(rc, self.resp) = request(self.api_url + "/storage-systems/%s/flash-cache/configure" % (self.ssid), |
| 285 |
data=json.dumps(update_fc_req), headers=self.post_headers, method='POST', |
| 286 |
url_username=self.api_username, url_password=self.api_password, |
| 287 |
validate_certs=self.validate_certs) |
| 288 |
|
| 289 |
def delete_cache(self): |
| 290 |
self.debug('deleting flash cache...') |
| 291 |
(rc, self.resp) = request(self.api_url + "/storage-systems/%s/flash-cache" % (self.ssid), method='DELETE', |
| 292 |
url_username=self.api_username, url_password=self.api_password, |
| 293 |
validate_certs=self.validate_certs, ignore_errors=True) |
| 294 |
|
| 295 |
@property |
| 296 |
def needs_more_disks(self): |
| 297 |
if len(self.cache_detail['driveRefs']) < self.disk_count: |
| 298 |
self.debug("needs resize: current disk count %s < requested requested count %s", |
| 299 |
len(self.cache_detail['driveRefs']), self.disk_count) |
| 300 |
return True |
| 301 |
|
| 302 |
@property |
| 303 |
def needs_less_disks(self): |
| 304 |
if len(self.cache_detail['driveRefs']) > self.disk_count: |
| 305 |
self.debug("needs resize: current disk count %s < requested requested count %s", |
| 306 |
len(self.cache_detail['driveRefs']), self.disk_count) |
| 307 |
return True |
| 308 |
|
| 309 |
@property |
| 310 |
def current_size_bytes(self): |
| 311 |
return int(self.cache_detail['fcDriveInfo']['fcWithDrives']['usedCapacity']) |
| 312 |
|
| 313 |
@property |
| 314 |
def requested_size_bytes(self): |
| 315 |
if self.cache_size_min: |
| 316 |
return self.cache_size_min * self._size_unit_map[self.size_unit] |
| 317 |
else: |
| 318 |
return 0 |
| 319 |
|
| 320 |
@property |
| 321 |
def needs_more_capacity(self): |
| 322 |
if self.current_size_bytes < self.requested_size_bytes: |
| 323 |
self.debug("needs resize: current capacity %sb is less than requested minimum %sb", |
| 324 |
self.current_size_bytes, self.requested_size_bytes) |
| 325 |
return True |
| 326 |
|
| 327 |
@property |
| 328 |
def needs_resize(self): |
| 329 |
return self.needs_more_disks or self.needs_more_capacity or self.needs_less_disks |
| 330 |
|
| 331 |
def resize_cache(self): |
| 332 |
# increase up to disk count first, then iteratively add disks until we meet requested capacity |
| 333 |
|
| 334 |
# TODO: perform this calculation in check mode |
| 335 |
current_disk_count = len(self.cache_detail['driveRefs']) |
| 336 |
proposed_new_disks = 0 |
| 337 |
|
| 338 |
proposed_additional_bytes = 0 |
| 339 |
proposed_disk_ids = [] |
| 340 |
|
| 341 |
if self.needs_more_disks: |
| 342 |
proposed_disk_count = self.disk_count - current_disk_count |
| 343 |
|
| 344 |
(disk_ids, bytes) = self.get_candidate_disks(disk_count=proposed_disk_count) |
| 345 |
proposed_additional_bytes = bytes |
| 346 |
proposed_disk_ids = disk_ids |
| 347 |
|
| 348 |
while self.current_size_bytes + proposed_additional_bytes < self.requested_size_bytes: |
| 349 |
proposed_new_disks += 1 |
| 350 |
(disk_ids, bytes) = self.get_candidate_disks(disk_count=proposed_new_disks) |
| 351 |
proposed_disk_ids = disk_ids |
| 352 |
proposed_additional_bytes = bytes |
| 353 |
|
| 354 |
add_drives_req = dict( |
| 355 |
driveRef=proposed_disk_ids |
| 356 |
) |
| 357 |
|
| 358 |
self.debug("adding drives to flash-cache...") |
| 359 |
(rc, self.resp) = request(self.api_url + "/storage-systems/%s/flash-cache/addDrives" % (self.ssid), |
| 360 |
data=json.dumps(add_drives_req), headers=self.post_headers, method='POST', |
| 361 |
url_username=self.api_username, url_password=self.api_password, |
| 362 |
validate_certs=self.validate_certs) |
| 363 |
|
| 364 |
elif self.needs_less_disks and self.driveRefs: |
| 365 |
rm_drives = dict(driveRef=self.driveRefs) |
| 366 |
(rc, self.resp) = request(self.api_url + "/storage-systems/%s/flash-cache/removeDrives" % (self.ssid), |
| 367 |
data=json.dumps(rm_drives), headers=self.post_headers, method='POST', |
| 368 |
url_username=self.api_username, url_password=self.api_password, |
| 369 |
validate_certs=self.validate_certs) |
| 370 |
|
| 371 |
def apply(self): |
| 372 |
result = dict(changed=False) |
| 373 |
(rc, cache_resp) = request(self.api_url + "/storage-systems/%s/flash-cache" % (self.ssid), |
| 374 |
url_username=self.api_username, url_password=self.api_password, |
| 375 |
validate_certs=self.validate_certs, ignore_errors=True) |
| 376 |
|
| 377 |
if rc == 200: |
| 378 |
self.cache_detail = cache_resp |
| 379 |
else: |
| 380 |
self.cache_detail = None |
| 381 |
|
| 382 |
if rc not in [200, 404]: |
| 383 |
raise Exception( |
| 384 |
"Unexpected error code %s fetching flash cache detail. Response data was %s" % (rc, cache_resp)) |
| 385 |
|
| 386 |
if self.state == 'present': |
| 387 |
if self.cache_detail: |
| 388 |
# TODO: verify parameters against detail for changes |
| 389 |
if self.cache_detail['name'] != self.name: |
| 390 |
self.debug("CHANGED: name differs") |
| 391 |
result['changed'] = True |
| 392 |
if self.cache_detail['flashCacheBase']['configType'] != self.io_type: |
| 393 |
self.debug("CHANGED: io_type differs") |
| 394 |
result['changed'] = True |
| 395 |
if self.needs_resize: |
| 396 |
self.debug("CHANGED: resize required") |
| 397 |
result['changed'] = True |
| 398 |
else: |
| 399 |
self.debug("CHANGED: requested state is 'present' but cache does not exist") |
| 400 |
result['changed'] = True |
| 401 |
else: # requested state is absent |
| 402 |
if self.cache_detail: |
| 403 |
self.debug("CHANGED: requested state is 'absent' but cache exists") |
| 404 |
result['changed'] = True |
| 405 |
|
| 406 |
if not result['changed']: |
| 407 |
self.debug("no changes, exiting...") |
| 408 |
self.module.exit_json(**result) |
| 409 |
|
| 410 |
if self.module.check_mode: |
| 411 |
self.debug("changes pending in check mode, exiting early...") |
| 412 |
self.module.exit_json(**result) |
| 413 |
|
| 414 |
if self.state == 'present': |
| 415 |
if not self.cache_detail: |
| 416 |
self.create_cache() |
| 417 |
else: |
| 418 |
if self.needs_resize: |
| 419 |
self.resize_cache() |
| 420 |
|
| 421 |
# run update here as well, since io_type can't be set on creation |
| 422 |
self.update_cache() |
| 423 |
|
| 424 |
elif self.state == 'absent': |
| 425 |
self.delete_cache() |
| 426 |
|
| 427 |
# TODO: include other details about the storage pool (size, type, id, etc) |
| 428 |
self.module.exit_json(changed=result['changed'], **self.resp) |
| 429 |
|
| 430 |
|
| 431 |
def main(): |
| 432 |
sp = NetAppESeriesFlashCache() |
| 433 |
try: |
| 434 |
sp.apply() |
| 435 |
except Exception as e: |
| 436 |
sp.debug("Exception in apply(): \n%s", to_native(e)) |
| 437 |
sp.module.fail_json(msg="Failed to create flash cache. Error[%s]" % to_native(e), |
| 438 |
exception=traceback.format_exc()) |
| 439 |
|
| 440 |
|
| 441 |
if __name__ == '__main__': |
| 442 |
main() |