Debian Codegraph @

ansible_collections/netapp/cloudmanager/plugins/modules/na_cloudmanager_nss_account.py

raw
1 #!/usr/bin/python
2
3 # (c) 2021, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 '''
7 na_cloudmanager_nss_account
8 '''
9
10 from __future__ import absolute_import, division, print_function
11
12 __metaclass__ = type
13
14 DOCUMENTATION = '''
15
16 module: na_cloudmanager_nss_account
17 short_description: NetApp Cloud Manager nss account
18 extends_documentation_fragment:
19 - netapp.cloudmanager.netapp.cloudmanager
20 version_added: '21.3.0'
21 author: NetApp Ansible Team (@carchi8py) <[email protected]>
22
23 description:
24 - Create and Delete nss account.
25
26 options:
27 state:
28 description:
29 - Whether the specified nss account should exist or not.
30 choices: ['present', 'absent']
31 default: 'present'
32 type: str
33
34 client_id:
35 description:
36 - The connector ID of the Cloud Manager Connector.
37 required: true
38 type: str
39
40 public_id:
41 description:
42 - The ID of the NSS account.
43 type: str
44
45 name:
46 description:
47 - The name of the NSS account.
48 type: str
49
50 username:
51 description:
52 - The NSS username.
53 required: true
54 type: str
55
56 password:
57 description:
58 - The NSS password.
59 type: str
60
61 vsa_list:
62 description:
63 - The working environment list.
64 type: list
65 elements: str
66
67 notes:
68 - Support check_mode.
69 '''
70
71 EXAMPLES = '''
72 - name: Create nss account
73 netapp.cloudmanager.na_cloudmanager_nss_account:
74 state: present
75 name: test_cloud
76 username: test_cloud
77 password: password
78 client_id: your_client_id
79 refresh_token: your_refresh_token
80 '''
81
82 RETURN = r''' # '''
83
84 from ansible.module_utils.basic import AnsibleModule
85 import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils
86 from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module import NetAppModule
87
88
89 class NetAppCloudmanagerNssAccount(object):
90
91 def __init__(self):
92 """
93 Parse arguments, setup state variables,
94 check parameters and ensure request module is installed
95 """
96 self.argument_spec = netapp_utils.cloudmanager_host_argument_spec()
97 self.argument_spec.update(dict(
98 state=dict(required=False, choices=['present', 'absent'], default='present'),
99 name=dict(required=False, type='str'),
100 client_id=dict(required=True, type='str'),
101 username=dict(required=True, type='str'),
102 password=dict(required=False, type='str', no_log=True),
103 public_id=dict(required=False, type='str'),
104 vsa_list=dict(required=False, type='list', elements='str')
105 ))
106 self.module = AnsibleModule(
107 argument_spec=self.argument_spec,
108 required_one_of=[['refresh_token', 'sa_client_id']],
109 required_together=[['sa_client_id', 'sa_secret_key']],
110 required_if=[
111 ('state', 'present', ['password']),
112 ],
113 supports_check_mode=True
114 )
115 self.na_helper = NetAppModule()
116 # set up state variables
117 self.parameters = self.na_helper.set_parameters(self.module.params)
118 # Calling generic rest_api class
119 self.rest_api = netapp_utils.CloudManagerRestAPI(self.module)
120 self.rest_api.token_type, self.rest_api.token = self.rest_api.get_token()
121 self.rest_api.url += self.rest_api.environment_data['CLOUD_MANAGER_HOST']
122 self.rest_api.api_root_path = '/occm/api/'
123 self.headers = {
124 'X-Agent-Id': self.rest_api.format_client_id(self.parameters['client_id'])
125 }
126
127 def get_nss_account(self):
128 response, err, dummy = self.rest_api.send_request("GET", "%s/accounts" % (
129 self.rest_api.api_root_path), None, header=self.headers)
130 if err is not None:
131 self.module.fail_json(changed=False, msg="Error: unexpected response on getting nss account: %s, %s" % (str(err), str(response)))
132 if response is None:
133 return None
134 nss_accounts = []
135 if response.get('nssAccounts'):
136 nss_accounts = response['nssAccounts']
137 if len(nss_accounts) == 0:
138 return None
139 result = dict()
140 for account in nss_accounts:
141 if account['nssUserName'] == self.parameters['username']:
142 if self.parameters.get('public_id') and self.parameters['public_id'] != account['publicId']:
143 self.module.fail_json(changed=False, msg="Error: public_id '%s' does not match username."
144 % account['publicId'])
145 else:
146 self.parameters['public_id'] = account['publicId']
147 result['name'] = account['accountName']
148 result['user_name'] = account['nssUserName']
149 result['vsa_list'] = account['vsaList']
150 return result
151 return None
152
153 def create_nss_account(self):
154 account = dict()
155 if self.parameters.get('name'):
156 account['accountName'] = self.parameters['name']
157 account['providerKeys'] = {'nssUserName': self.parameters['username'],
158 'nssPassword': self.parameters['password']}
159 account['vsaList'] = []
160 if self.parameters.get('vsa_list'):
161 account['vsaList'] = self.parameters['vsa_list']
162 response, err, second_dummy = self.rest_api.send_request("POST", "%s/accounts/nss" % (
163 self.rest_api.api_root_path), None, account, header=self.headers)
164 if err is not None:
165 self.module.fail_json(changed=False, msg="Error: unexpected response on creating nss account: %s, %s" % (str(err), str(response)))
166
167 def delete_nss_account(self):
168 response, err, second_dummy = self.rest_api.send_request("DELETE", "%s/accounts/%s" % (
169 self.rest_api.api_root_path, self.parameters['public_id']), None, None, header=self.headers)
170 if err is not None:
171 self.module.fail_json(changed=False, msg="Error: unexpected response on deleting nss account: %s, %s" % (str(err), str(response)))
172 return None
173
174 def apply(self):
175 current = self.get_nss_account()
176 cd_action = self.na_helper.get_cd_action(current, self.parameters)
177 if self.na_helper.changed and not self.module.check_mode:
178 if cd_action == 'create':
179 self.create_nss_account()
180 elif cd_action == 'delete':
181 self.delete_nss_account()
182 self.module.exit_json(changed=self.na_helper.changed)
183
184
185 def main():
186 '''Main Function'''
187 account = NetAppCloudmanagerNssAccount()
188 account.apply()
189
190
191 if __name__ == '__main__':
192 main()