Skip to content

Commit

Permalink
new module cvs pools
Browse files Browse the repository at this point in the history
  • Loading branch information
carchi8py committed Aug 26, 2019
1 parent e258ba7 commit 2c69d34
Show file tree
Hide file tree
Showing 3 changed files with 542 additions and 5 deletions.
277 changes: 277 additions & 0 deletions lib/ansible/modules/cloud/amazon/aws_netapp_cvs_pool.py
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()
8 changes: 3 additions & 5 deletions lib/ansible/plugins/doc_fragments/netapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class ModuleDocFragment(object):
- Auto -- will try to use the REST Api
default: Auto
choices: ['Never', 'Always', 'Auto']
type: str
requirements:
Expand Down Expand Up @@ -163,17 +162,14 @@ class ModuleDocFragment(object):
options:
api_username:
required: true
type: str
description:
- The username to authenticate with the SANtricity Web Services Proxy or Embedded Web Services API.
api_password:
required: true
type: str
description:
- The password to authenticate with the SANtricity Web Services Proxy or Embedded Web Services API.
api_url:
required: true
type: str
description:
- The url to the SANtricity Web Services Proxy or Embedded Web Services API.
Example https://prod-1.wahoo.acme.com/devmgr/v2
Expand All @@ -185,7 +181,6 @@ class ModuleDocFragment(object):
type: bool
ssid:
required: false
type: str
default: 1
description:
- The ID of the array to manage. This value must be unique for each array.
Expand All @@ -202,14 +197,17 @@ class ModuleDocFragment(object):
options:
api_key:
required: true
type: str
description:
- The access key to authenticate with the AWSCVS Web Services Proxy or Embedded Web Services API.
secret_key:
required: true
type: str
description:
- The secret_key to authenticate with the AWSCVS Web Services Proxy or Embedded Web Services API.
api_url:
required: true
type: str
description:
- The url to the AWSCVS Web Services Proxy or Embedded Web Services API.
validate_certs:
Expand Down
Loading

0 comments on commit 2c69d34

Please sign in to comment.