-
Notifications
You must be signed in to change notification settings - Fork 23.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
542 additions
and
5 deletions.
There are no files selected for viewing
277 changes: 277 additions & 0 deletions
277
lib/ansible/modules/cloud/amazon/aws_netapp_cvs_pool.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
#!/usr/bin/python | ||
|
||
# (c) 2019, NetApp Inc. | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
"""AWS Cloud Volumes Services - Manage Pools""" | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
__metaclass__ = type | ||
|
||
ANSIBLE_METADATA = {'metadata_version': '1.1', | ||
'status': ['preview'], | ||
'supported_by': 'community'} | ||
|
||
|
||
DOCUMENTATION = ''' | ||
module: aws_netapp_cvs_pool | ||
short_description: NetApp AWS Cloud Volumes Service Manage Pools. | ||
extends_documentation_fragment: | ||
- netapp.awscvs | ||
version_added: '2.9' | ||
author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com> | ||
description: | ||
- Create, Update, Delete Pool on AWS Cloud Volumes Service. | ||
options: | ||
state: | ||
description: | ||
- Whether the specified pool should exist or not. | ||
choices: ['present', 'absent'] | ||
required: true | ||
type: str | ||
region: | ||
description: | ||
- The region to which the Pool is associated. | ||
required: true | ||
type: str | ||
name: | ||
description: | ||
- pool name ( The human readable name of the Pool ) | ||
- name can be used for create, update and delete operations | ||
required: true | ||
type: str | ||
serviceLevel: | ||
description: | ||
- The service level of the Pool | ||
- can be used with pool create, update operations | ||
choices: ['basic', 'standard', 'extreme'] | ||
type: str | ||
sizeInBytes: | ||
description: | ||
- Size of the Pool in bytes | ||
- can be used with pool create, update operations | ||
- minimum value is 4000000000000 bytes | ||
type: int | ||
vendorID: | ||
description: | ||
- A vendor ID for the Pool. E.g. an ID allocated by a vendor service for the Pool. | ||
- can be used with pool create, update operations | ||
- must be unique | ||
type: str | ||
new_name: | ||
description: | ||
- rename the existing pool name ( The human readable name of the Pool ) | ||
- can be used with update operation | ||
type: str | ||
''' | ||
|
||
EXAMPLES = """ | ||
- name: Create a new Pool | ||
aws_netapp_cvs_pool: | ||
state: present | ||
name: TestPoolBB12 | ||
serviceLevel: extreme | ||
sizeInBytes: 4000000000000 | ||
vendorID: ansiblePoolTestVendorBB12 | ||
region: us-east-1 | ||
api_url: cds-aws-bundles.netapp.com | ||
api_key: MyAPiKey | ||
secret_key: MySecretKey | ||
- name: Delete a Pool | ||
aws_netapp_cvs_pool: | ||
state: absent | ||
name: TestPoolBB7 | ||
region: us-east-1 | ||
api_url: cds-aws-bundles.netapp.com | ||
api_key: MyAPiKey | ||
secret_key: MySecretKey | ||
- name: Update a Pool | ||
aws_netapp_cvs_pool: | ||
state: present | ||
name: TestPoolBB12 | ||
new_name: Mynewpool7 | ||
vendorID: ansibleVendorMynewpool15 | ||
serviceLevel: extreme | ||
sizeInBytes: 4000000000000 | ||
region: us-east-1 | ||
api_url: cds-aws-bundles.netapp.com | ||
api_key: MyAPiKey | ||
secret_key: MySecretKey | ||
""" | ||
|
||
RETURN = ''' | ||
''' | ||
|
||
import ansible.module_utils.netapp as netapp_utils | ||
from ansible.module_utils.basic import AnsibleModule | ||
from ansible.module_utils.netapp_module import NetAppModule | ||
from ansible.module_utils.netapp import AwsCvsRestAPI | ||
|
||
|
||
class NetAppAWSCVS(object): | ||
'''Class for Pool operations ''' | ||
|
||
def __init__(self): | ||
""" | ||
Parse arguments, setup state variables, | ||
""" | ||
self.argument_spec = netapp_utils.aws_cvs_host_argument_spec() | ||
self.argument_spec.update(dict( | ||
state=dict(required=True, choices=['present', 'absent']), | ||
region=dict(required=True, type='str'), | ||
name=dict(required=True, type='str'), | ||
new_name=dict(required=False, type='str'), | ||
serviceLevel=dict(required=False, choices=['basic', 'standard', 'extreme'], type='str'), | ||
sizeInBytes=dict(required=False, type='int'), | ||
vendorID=dict(required=False, type='str'), | ||
)) | ||
self.module = AnsibleModule( | ||
argument_spec=self.argument_spec, | ||
supports_check_mode=True | ||
) | ||
|
||
self.na_helper = NetAppModule() | ||
self.parameters = self.na_helper.set_parameters(self.module.params) | ||
self.restApi = AwsCvsRestAPI(self.module) | ||
self.sizeInBytes_min_value = 4000000000000 | ||
|
||
def get_aws_netapp_cvs_pool(self): | ||
""" | ||
Returns Pool object if exists else Return None | ||
""" | ||
pool_info = None | ||
new_name_exists = False | ||
is_new_name_check_needed = False | ||
|
||
if 'new_name' in self.parameters.keys(): | ||
is_new_name_check_needed = True | ||
|
||
pools, error = self.restApi.get('Pools') | ||
|
||
if error is None and pools is not None: | ||
for pool in pools: | ||
if 'name' in pool and pool['region'] == self.parameters['region']: | ||
if pool['name'] == self.parameters['name']: | ||
pool_info = pool | ||
|
||
if is_new_name_check_needed is True: | ||
if self.parameters['new_name'] == pool['name']: | ||
new_name_exists = True | ||
else: | ||
if pool_info is not None: | ||
break | ||
|
||
if pool_info is not None and new_name_exists is True: | ||
break | ||
|
||
return pool_info, new_name_exists | ||
|
||
def create_aws_netapp_cvs_pool(self): | ||
""" | ||
Create a pool | ||
""" | ||
api = 'Pools' | ||
|
||
for key in ['serviceLevel', 'sizeInBytes', 'vendorID']: | ||
if key not in self.parameters.keys() or self.parameters[key] is None: | ||
self.module.fail_json(changed=False, msg="Mandatory key '%s' required" % (key)) | ||
|
||
pool = { | ||
"name": self.parameters['name'], | ||
"region": self.parameters['region'], | ||
"serviceLevel": self.parameters['serviceLevel'], | ||
"sizeInBytes": self.parameters['sizeInBytes'], | ||
"vendorID": self.parameters['vendorID'] | ||
} | ||
if 'new_name' in self.parameters.keys(): | ||
self.module.exit_json(changed=False) | ||
|
||
response, error = self.restApi.post(api, pool) | ||
if error is not None: | ||
self.module.fail_json(changed=False, msg=error) | ||
|
||
def update_aws_netapp_cvs_pool(self, update_pool_info, pool_id): | ||
""" | ||
Update a pool | ||
""" | ||
api = 'Pools/' + pool_id | ||
|
||
pool = { | ||
"name": update_pool_info['name'], | ||
"region": self.parameters['region'], | ||
"serviceLevel": update_pool_info['serviceLevel'], | ||
"sizeInBytes": update_pool_info['sizeInBytes'], | ||
"vendorID": update_pool_info['vendorID'] | ||
} | ||
|
||
response, error = self.restApi.put(api, pool) | ||
if error is not None: | ||
self.module.fail_json(changed=False, msg=error) | ||
|
||
def delete_aws_netapp_cvs_pool(self, pool_id): | ||
""" | ||
Delete a pool | ||
""" | ||
api = 'Pools/' + pool_id | ||
data = None | ||
response, error = self.restApi.delete(api, data) | ||
|
||
if error is not None: | ||
self.module.fail_json(changed=False, msg=error) | ||
|
||
def apply(self): | ||
""" | ||
Perform pre-checks, call functions and exit | ||
""" | ||
update_required = False | ||
new_name_exists = False | ||
|
||
if 'sizeInBytes' in self.parameters.keys() and self.parameters['sizeInBytes'] < self.sizeInBytes_min_value: | ||
self.module.fail_json(changed=False, msg="sizeInBytes should be greater than or equal to %d" % (self.sizeInBytes_min_value)) | ||
|
||
current, new_name_exists = self.get_aws_netapp_cvs_pool() | ||
cd_action = self.na_helper.get_cd_action(current, self.parameters) | ||
|
||
if current and self.parameters['state'] != 'absent': | ||
keys_to_check = ['name', 'vendorID', 'sizeInBytes', 'serviceLevel'] | ||
update_pool_info, update_required = self.na_helper.compare_and_update_values(current, self.parameters, keys_to_check) | ||
|
||
if 'new_name' in self.parameters.keys(): | ||
if new_name_exists is True: | ||
self.module.fail_json(changed=False, msg="unable to rename pool '%s': already exists" % (self.parameters['new_name'])) | ||
else: | ||
update_pool_info['name'] = self.parameters['new_name'] | ||
update_required = True | ||
|
||
if update_required is True: | ||
self.na_helper.changed = True | ||
cd_action = 'update' | ||
|
||
if self.na_helper.changed: | ||
if self.module.check_mode: | ||
pass | ||
else: | ||
if cd_action == 'update': | ||
self.update_aws_netapp_cvs_pool(update_pool_info, current['poolId']) | ||
elif cd_action == 'create': | ||
self.create_aws_netapp_cvs_pool() | ||
elif cd_action == 'delete': | ||
self.delete_aws_netapp_cvs_pool(current['poolId']) | ||
|
||
self.module.exit_json(changed=self.na_helper.changed) | ||
|
||
|
||
def main(): | ||
'''Main Function''' | ||
aws_cvs_netapp_pool = NetAppAWSCVS() | ||
aws_cvs_netapp_pool.apply() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.