Debian Codegraph @

ansible_collections/netapp/cloudmanager/plugins/modules/na_cloudmanager_connector_gcp.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_connector_gcp
8 '''
9
10 from __future__ import absolute_import, division, print_function
11 __metaclass__ = type
12
13 DOCUMENTATION = '''
14
15 module: na_cloudmanager_connector_gcp
16 short_description: NetApp Cloud Manager connector for GCP.
17 extends_documentation_fragment:
18 - netapp.cloudmanager.netapp.cloudmanager
19 version_added: '21.4.0'
20 author: NetApp Ansible Team (@carchi8py) <[email protected]>
21
22 description:
23 - Create or delete Cloud Manager connector for GCP.
24
25 options:
26 state:
27 description:
28 - Whether the specified Cloud Manager connector for GCP should exist or not.
29 choices: ['present', 'absent']
30 default: 'present'
31 type: str
32
33 name:
34 required: true
35 description:
36 - The name of the Cloud Manager connector for GCP to manage.
37 type: str
38
39 project_id:
40 description:
41 - The GCP project_id where the connector will be created.
42 required: true
43 type: str
44
45 zone:
46 description:
47 - The GCP zone where the Connector will be created.
48 required: true
49 type: str
50
51 gcp_service_account_email:
52 description:
53 - The email of the service_account for the connector instance. This service account is used to allow the Connector to create Cloud Volume ONTAP.
54 required: true
55 type: str
56 aliases: ['service_account_email']
57 version_added: 21.7.0
58
59 company:
60 description:
61 - The name of the company of the user.
62 required: true
63 type: str
64
65 gcp_service_account_path:
66 description:
67 - The local path of the service_account JSON file for GCP authorization purposes. This service account is used to create the Connector in GCP.
68 type: str
69 aliases: ['service_account_path']
70 version_added: 21.7.0
71
72 subnet_id:
73 description:
74 - The name of the subnet for the virtual machine.
75 type: str
76 default: default
77
78 network_project_id:
79 description:
80 - The project id in GCP associated with the Subnet. If not provided, it is assumed that the Subnet is within the previously specified project id.
81 type: str
82
83 machine_type:
84 description:
85 - The machine_type for the Connector VM.
86 type: str
87 default: n2-standard-4
88
89 firewall_tags:
90 description:
91 - Indicates whether to add firewall_tags to the connector VM (HTTP and HTTP).
92 type: bool
93 default: true
94
95 associate_public_ip:
96 description:
97 - Indicates whether to associate a public IP address to the virtual machine.
98 type: bool
99 default: true
100
101 proxy_url:
102 description:
103 - The proxy URL, if using a proxy to connect to the internet.
104 type: str
105
106 proxy_user_name:
107 description:
108 - The proxy user name, if using a proxy to connect to the internet.
109 type: str
110
111 proxy_password:
112 description:
113 - The proxy password, if using a proxy to connect to the internet.
114 type: str
115
116 proxy_certificates:
117 description:
118 - The proxy certificates. A list of certificate file names.
119 type: list
120 elements: str
121
122 account_id:
123 description:
124 - The NetApp account ID that the Connector will be associated with.
125 - If not provided, Cloud Manager uses the first account. If no account exists, Cloud Manager creates a new account.
126 - You can find the account ID in the account tab of Cloud Manager at [https://cloudmanager.netapp.com](https://cloudmanager.netapp.com).
127 type: str
128
129 client_id:
130 description:
131 - The client ID of the Cloud Manager Connector.
132 - The connector ID.
133 - If state is absent, the client id is used to identify the agent and delete it.
134 - If state is absent and this parameter is not set, all agents associated with C(name) are deleted.
135 - Ignored when state is present.
136 type: str
137
138 '''
139
140 EXAMPLES = """
141 - name: Create NetApp Cloud Manager connector for GCP
142 netapp.cloudmanager.na_cloudmanager_connector_gcp:
143 state: present
144 name: ansible-occm-gcp
145 project_id: xxxxxxx-support
146 zone: us-east4-b
147 company: NetApp
148 gcp_service_account_email: [email protected]
149 gcp_service_account_path: gcp_creds.json
150 proxy_user_name: test
151 proxy_password: test
152 proxy_url: http://abcdefg.com
153 proxy_certificates: ["D-TRUST_Root_Class_3_CA_2_2009.crt", "DigiCertGlobalRootCA.crt", "DigiCertGlobalRootG2.crt"]
154 account_id: account-xxxxXXXX
155 refresh_token: "{{ xxxxxxxxxxxxxxx }}"
156
157 - name: Delete NetApp Cloud Manager connector for GCP
158 netapp.cloudmanager.na_cloudmanager_connector_gcp:
159 state: absent
160 name: ansible-occm-gcp
161 refresh_token: "{{ xxxxxxxxxxxxxxx }}"
162 client_id: "{{ wwwwwwwwww }}"
163 project_id: xxxxxxx-support
164 zone: us-east4-b
165 company: NetApp
166 gcp_service_account_email: [email protected]
167 gcp_service_account_path: gcp_creds.json
168 account_id: account-xxxxXXXX
169 """
170
171 RETURN = """
172 client_id:
173 description: Newly created GCP connector id on cloud manager.
174 type: str
175 returned: success
176 sample: 'FDQE8SwrbjVS6mqUgZoOHQmu2DvBNRRW'
177 client_ids:
178 description:
179 - a list of client ids matching the name and provider if the connector already exists.
180 - ideally the list should be empty, or contain a single element matching client_id.
181 type: list
182 elements: str
183 returned: success
184 sample: ['FDQE8SwrbjVS6mqUgZoOHQmu2DvBNRRW']
185 """
186 import uuid
187 import time
188 import base64
189 import json
190
191 from ansible.module_utils.basic import AnsibleModule
192 import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils
193 from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module import NetAppModule
194 from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp import CloudManagerRestAPI
195
196 IMPORT_ERRORS = []
197 HAS_GCP_COLLECTION = False
198
199 try:
200 import google.auth
201 from google.auth.transport import requests
202 from google.oauth2 import service_account
203 import yaml
204 HAS_GCP_COLLECTION = True
205 except ImportError as exc:
206 IMPORT_ERRORS.append(str(exc))
207
208 GCP_DEPLOYMENT_MANAGER = "www.googleapis.com"
209 UUID = str(uuid.uuid4())
210
211
212 class NetAppCloudManagerConnectorGCP(object):
213 ''' object initialize and class methods '''
214
215 def __init__(self):
216 self.argument_spec = netapp_utils.cloudmanager_host_argument_spec()
217 self.argument_spec.update(dict(
218 name=dict(required=True, type='str'),
219 state=dict(required=False, choices=['present', 'absent'], default='present'),
220 project_id=dict(required=True, type='str'),
221 zone=dict(required=True, type='str'),
222 company=dict(required=True, type='str'),
223 gcp_service_account_email=dict(required=True, type='str', aliases=['service_account_email']),
224 gcp_service_account_path=dict(required=False, type='str', aliases=['service_account_path']),
225 subnet_id=dict(required=False, type='str', default='default'),
226 network_project_id=dict(required=False, type='str'),
227 machine_type=dict(required=False, type='str', default='n2-standard-4'),
228 firewall_tags=dict(required=False, type='bool', default=True),
229 associate_public_ip=dict(required=False, type='bool', default=True),
230 proxy_url=dict(required=False, type='str'),
231 proxy_user_name=dict(required=False, type='str'),
232 proxy_password=dict(required=False, type='str', no_log=True),
233 proxy_certificates=dict(required=False, type='list', elements='str'),
234 account_id=dict(required=False, type='str'),
235 client_id=dict(required=False, type='str'),
236 ))
237
238 self.module = AnsibleModule(
239 argument_spec=self.argument_spec,
240 required_one_of=[['refresh_token', 'sa_client_id']],
241 required_together=[['sa_client_id', 'sa_secret_key']],
242 supports_check_mode=True
243 )
244 self.na_helper = NetAppModule()
245 self.parameters = self.na_helper.set_parameters(self.module.params)
246 self.rest_api = CloudManagerRestAPI(self.module)
247 self.gcp_common_suffix_name = "-vm-boot-deployment"
248 self.fail_when_import_errors(IMPORT_ERRORS, HAS_GCP_COLLECTION)
249 super(NetAppCloudManagerConnectorGCP, self).__init__()
250
251 self.rest_api.gcp_token, error = self.get_gcp_token()
252 if error:
253 self.module.fail_json(msg='Error getting gcp token: %s' % repr(error))
254
255 def get_gcp_token(self):
256 '''
257 get gcp token from gcp service account credential json file
258 '''
259 scopes = ["https://www.googleapis.com/auth/cloud-platform",
260 "https://www.googleapis.com/auth/compute",
261 "https://www.googleapis.com/auth/compute.readonly",
262 "https://www.googleapis.com/auth/ndev.cloudman",
263 "https://www.googleapis.com/auth/ndev.cloudman.readonly",
264 "https://www.googleapis.com/auth/devstorage.full_control",
265 "https://www.googleapis.com/auth/devstorage.read_write"]
266 if 'gcp_service_account_path' in self.parameters:
267 try:
268 fh = open(self.parameters['gcp_service_account_path'])
269 except (OSError, IOError) as error:
270 return None, "opening %s: got: %s" % (self.parameters['gcp_service_account_path'], repr(error))
271 with fh:
272 key_bytes = json.load(fh)
273 if key_bytes is None:
274 return None, "Error: gcp_service_account_path file is empty"
275 credentials = service_account.Credentials.from_service_account_file(self.parameters['gcp_service_account_path'], scopes=scopes)
276 else:
277 credentials, project = google.auth.default(scopes=scopes)
278
279 credentials.refresh(requests.Request())
280
281 return credentials.token, None
282
283 def fail_when_import_errors(self, import_errors, has_gcp_collection=True):
284 if has_gcp_collection and not import_errors:
285 return
286 msg = ''
287 if not has_gcp_collection:
288 msg = 'The python google-auth package is required. '
289 msg += 'Import errors: %s' % str(import_errors)
290 self.module.fail_json(msg=msg)
291
292 def get_deploy_vm(self):
293 '''
294 Get Cloud Manager connector for GCP
295 :return:
296 Dictionary of current details if Cloud Manager connector for GCP
297 None if Cloud Manager connector for GCP is not found
298 '''
299 api_url = GCP_DEPLOYMENT_MANAGER + '/deploymentmanager/v2/projects/%s/global/deployments/%s%s' % (
300 self.parameters['project_id'], self.parameters['name'], self.gcp_common_suffix_name)
301 headers = {
302 "X-User-Token": self.rest_api.token_type + " " + self.rest_api.token,
303 'Authorization': self.rest_api.token_type + " " + self.rest_api.gcp_token,
304 }
305
306 occm_status, error, dummy = self.rest_api.get(api_url, header=headers)
307 if error is not None:
308 if error == '404' and b'is not found' in occm_status:
309 return None
310 self.module.fail_json(
311 msg="Error: unexpected response on getting occm: %s, %s" % (str(error), str(occm_status)))
312
313 return occm_status
314
315 def get_custom_data_for_gcp(self, proxy_certificates):
316 '''
317 get custom data for GCP
318 '''
319 # get account ID
320 if 'account_id' not in self.parameters:
321 # get account ID
322 response, error = self.na_helper.get_or_create_account(self.rest_api)
323 if error is not None:
324 self.module.fail_json(
325 msg="Error: unexpected response on getting account: %s, %s" % (str(error), str(response)))
326 self.parameters['account_id'] = response
327 # registerAgentTOServiceForGCP
328 response, error = self.na_helper.register_agent_to_service(self.rest_api, "GCP", "")
329 if error is not None:
330 self.module.fail_json(
331 msg="Error: register agent to service for gcp failed: %s, %s" % (str(error), str(response)))
332 # add proxy_certificates as part of json data
333 client_id = response['clientId']
334 client_secret = response['clientSecret']
335 u_data = {
336 'instanceName': self.parameters['name'],
337 'company': self.parameters['company'],
338 'clientId': client_id,
339 'clientSecret': client_secret,
340 'systemId': UUID,
341 'tenancyAccountId': self.parameters['account_id'],
342 'proxySettings': {'proxyPassword': self.parameters.get('proxy_password'),
343 'proxyUserName': self.parameters.get('proxy_user_name'),
344 'proxyUrl': self.parameters.get('proxy_url'),
345 'proxyCertificates': proxy_certificates,
346 },
347 }
348 # convert response to json format
349 user_data = json.dumps(u_data)
350 return user_data, client_id, None
351
352 def deploy_gcp_vm(self, proxy_certificates):
353 '''
354 deploy GCP VM
355 '''
356 # getCustomDataForGCP
357 response, client_id, error = self.get_custom_data_for_gcp(proxy_certificates)
358 if error is not None:
359 self.module.fail_json(
360 msg="Error: Not able to get user data for GCP: %s, %s" % (str(error), str(response)))
361 # compose
362 user_data = response
363 gcp_custom_data = base64.b64encode(user_data.encode())
364 gcp_sa_scopes = ["https://www.googleapis.com/auth/cloud-platform",
365 "https://www.googleapis.com/auth/compute",
366 "https://www.googleapis.com/auth/compute.readonly",
367 "https://www.googleapis.com/auth/ndev.cloudman",
368 "https://www.googleapis.com/auth/ndev.cloudman.readonly"]
369
370 tags = []
371 if self.parameters['firewall_tags'] is True:
372 tags = {'items': ['firewall-tag-bvsu', 'http-server', 'https-server']}
373
374 # first resource
375 device_name = self.parameters['name'] + '-vm-disk-boot'
376 t = {
377 'name': self.parameters['name'] + '-vm',
378 'properties': {
379 'disks': [
380 {'autoDelete': True,
381 'boot': True,
382 'deviceName': device_name,
383 'name': device_name,
384 'source': "\\\"$(ref.%s.selfLink)\\\"" % device_name,
385 'type': "PERSISTENT",
386 },
387 ],
388 'machineType': "zones/%s/machineTypes/%s" % (self.parameters['zone'], self.parameters['machine_type']),
389 'metadata': {
390 'items': [
391 {'key': 'serial-port-enable',
392 'value': 1},
393 {'key': 'customData',
394 'value': gcp_custom_data}
395 ]
396 },
397 'serviceAccounts': [{'email': self.parameters['gcp_service_account_email'],
398 'scopes': gcp_sa_scopes, }],
399 'tags': tags,
400 'zone': self.parameters['zone']
401 },
402 'metadata': {'dependsOn': [device_name]},
403 'type': 'compute.v1.instance',
404 }
405
406 access_configs = []
407 if self.parameters['associate_public_ip'] is True:
408 access_configs = [{'kind': 'compute#accessConfig',
409 'name': 'External NAT',
410 'type': 'ONE_TO_ONE_NAT',
411 'networkTier': 'PREMIUM'
412 }]
413 project_id = self.parameters['project_id']
414 if self.parameters.get('network_project_id'):
415 project_id = self.parameters['network_project_id']
416
417 t['properties']['networkInterfaces'] = [
418 {'accessConfigs': access_configs,
419 'kind': 'compute#networkInterface',
420 'subnetwork': 'projects/%s/regions/%s/subnetworks/%s' % (
421 project_id, self.parameters['region'], self.parameters['subnet_id'])
422 }]
423
424 td = {
425 'name': device_name,
426 'properties': {'name': device_name,
427 'sizeGb': 100,
428 'sourceImage': 'projects/%s/global/images/family/%s' % (self.rest_api.environment_data['GCP_IMAGE_PROJECT'],
429 self.rest_api.environment_data['GCP_IMAGE_FAMILY']),
430 'type': 'zones/%s/diskTypes/pd-ssd' % (self.parameters['zone']),
431 'zone': self.parameters['zone']
432 },
433 'type': 'compute.v1.disks',
434 }
435 content = {
436 'resources': [t, td]
437 }
438 my_data = str(yaml.dump(content))
439 # The template must be in this format:
440 # {
441 # "name": "ansible-cycc-vm-boot-deployment",
442 # "target": {
443 # "config": {
444 # "content": "resources:
445 # - name: xxxx
446 # properties:
447 # ...
448 # "
449 # }
450 # }
451 # }
452 gcp_deployment_template = '{\n "name": "%s%s",\n "target": {\n "config": {\n "content": "%s"\n }\n}\n}' % (
453 self.parameters['name'], '-vm-boot-deployment', my_data)
454
455 # post
456 api_url = GCP_DEPLOYMENT_MANAGER + '/deploymentmanager/v2/projects/%s/global/deployments' % (
457 self.parameters['project_id'])
458
459 headers = {
460 'X-User-Token': self.rest_api.token_type + " " + self.rest_api.gcp_token,
461 'X-Tenancy-Account-Id': self.parameters['account_id'],
462 'Authorization': self.rest_api.token_type + " " + self.rest_api.gcp_token,
463 'Content-type': "application/json",
464 'Referer': "Ansible_NetApp",
465 'X-Agent-Id': self.rest_api.format_client_id(client_id)
466 }
467
468 response, error, dummy = self.rest_api.post(api_url, data=gcp_deployment_template, header=headers,
469 gcp_type=True)
470 if error is not None:
471 return response, client_id, error
472
473 # check occm status
474 # Sleep for 1 minutes
475 time.sleep(60)
476 retries = 16
477 while retries > 0:
478 agent, error = self.na_helper.get_occm_agent_by_id(self.rest_api, client_id)
479 if error is not None:
480 self.module.fail_json(
481 msg="Error: Not able to get occm status: %s, %s" % (str(error), str(agent)),
482 client_id=client_id, changed=True)
483 if agent['status'] == "active":
484 break
485 else:
486 time.sleep(30)
487 retries -= 1
488 if retries == 0:
489 # Taking too long for status to be active
490 msg = "Connector VM is created and registered. Taking too long for OCCM agent to be active or not properly setup."
491 msg += ' Latest status: %s' % agent
492 self.module.fail_json(msg=msg, client_id=client_id, changed=True)
493
494 return response, client_id, error
495
496 def create_occm_gcp(self):
497 '''
498 Create Cloud Manager connector for GCP
499 '''
500 # check proxy configuration
501 if 'proxy_user_name' in self.parameters and 'proxy_url' not in self.parameters:
502 self.module.fail_json(msg="Error: missing proxy_url")
503 if 'proxy_password' in self.parameters and 'proxy_url' not in self.parameters:
504 self.module.fail_json(msg="Error: missing proxy_url")
505
506 proxy_certificates = []
507 if 'proxy_certificates' in self.parameters:
508 for c_file in self.parameters['proxy_certificates']:
509 proxy_certificate, error = self.na_helper.encode_certificates(c_file)
510 # add to proxy_certificates list
511 if error is not None:
512 self.module.fail_json(msg="Error: not able to read certificate file %s" % c_file)
513 proxy_certificates.append(proxy_certificate)
514 # region is the super class of zone. For example, zone us-east4-b is one of the zone in region us-east4
515 self.parameters['region'] = self.parameters['zone'][:-2]
516 # deploy GCP VM
517 response, client_id, error = self.deploy_gcp_vm(proxy_certificates)
518 if error is not None:
519 self.module.fail_json(
520 msg="Error: create_occm_gcp: %s, %s" % (str(error), str(response)))
521 return client_id
522
523 def delete_occm_gcp(self):
524 '''
525 Delete Cloud Manager connector for GCP
526 '''
527 api_url = GCP_DEPLOYMENT_MANAGER + '/deploymentmanager/v2/projects/%s/global/deployments/%s%s' % (
528 self.parameters['project_id'],
529 self.parameters['name'],
530 self.gcp_common_suffix_name)
531 headers = {
532 "X-User-Token": self.rest_api.token_type + " " + self.rest_api.token,
533 'Authorization': self.rest_api.token_type + " " + self.rest_api.gcp_token,
534 'X-Tenancy-Account-Id': self.parameters['account_id'],
535 'Content-type': "application/json",
536 'Referer': "Ansible_NetApp",
537 }
538
539 response, error, dummy = self.rest_api.delete(api_url, None, header=headers)
540 if error is not None:
541 return "Error: unexpected response on deleting VM: %s, %s" % (str(error), str(response))
542 # sleep for 30 sec
543 time.sleep(30)
544 if 'client_id' not in self.parameters:
545 return None
546 # check occm status
547 retries = 30
548 while retries > 0:
549 agent, error = self.na_helper.get_occm_agent_by_id(self.rest_api, self.parameters['client_id'])
550 if error is not None:
551 return "Error: Not able to get occm status after deleting VM: %s, %s" % (str(error), str(agent))
552 if agent['status'] != ["active", "pending"]:
553 break
554 else:
555 time.sleep(10)
556 retries -= 1 if agent['status'] == "active" else 5
557 if retries == 0 and agent['status'] == "active":
558 # Taking too long for terminating OCCM
559 return "Taking too long for instance to finish terminating. Latest status: %s" % str(agent)
560 return None
561
562 def delete_occm_agents(self, agents):
563 error = self.na_helper.delete_occm_agents(self.rest_api, agents)
564 if error:
565 return "Error: deleting OCCM agent(s): %s" % error
566 return None
567
568 def get_occm_agents(self):
569 if 'client_id' in self.parameters and self.parameters['state'] == 'absent':
570 agent, error = self.na_helper.get_occm_agent_by_id(self.rest_api, self.parameters['client_id'])
571 if error == '403' and b'Action not allowed for user' in agent:
572 # assume the agent does not exist anymore
573 agents, error = [], None
574 self.module.warn('Client Id %s was not found for this account.' % self.parameters['client_id'])
575 else:
576 agents = [agent]
577 else:
578 agents, error = self.na_helper.get_occm_agents_by_name(self.rest_api, self.parameters['account_id'],
579 self.parameters['name'], 'GCP')
580 if error:
581 self.module.fail_json(
582 msg="Error: getting OCCM agents: %s, %s" % (str(error), str(agents)))
583 return agents
584
585 def set_client_id(self, agents):
586 client_id = ""
587 client_ids = [agent['agentId'] for agent in agents if 'agentId' in agent]
588 if len(client_ids) == 1:
589 client_id = client_ids[0]
590 self.parameters['client_id'] = client_ids[0]
591 elif 'client_id' in self.parameters and self.parameters['client_id'] in client_ids:
592 client_id = self.parameters['client_id']
593 return client_id, client_ids
594
595 def apply(self):
596 """
597 Apply action to the Cloud Manager connector for GCP
598 :return: None
599 """
600 client_id = ""
601 agents, client_ids = [], []
602 current_vm = self.get_deploy_vm()
603 if current_vm and current_vm['operation']['status'] == 'terminated':
604 current_vm = None
605 current = current_vm
606 if self.parameters['state'] == 'absent' or current:
607 agents = self.get_occm_agents()
608 client_id, client_ids = self.set_client_id(agents)
609 if agents and current is None:
610 current = {}
611 if agents:
612 current['agents'] = agents
613
614 cd_action = self.na_helper.get_cd_action(current, self.parameters)
615 if self.na_helper.changed and not self.module.check_mode:
616 if cd_action == 'create':
617 client_id = self.create_occm_gcp()
618 elif cd_action == 'delete':
619 errors = []
620 if current_vm:
621 error = self.delete_occm_gcp()
622 if error:
623 errors.append(error)
624 if agents:
625 error = self.delete_occm_agents(agents)
626 if error:
627 errors.append(error)
628 if errors:
629 self.module.fail_json(msg='. '.join(errors))
630
631 self.module.exit_json(changed=self.na_helper.changed, client_id=client_id, client_ids=client_ids)
632
633
634 def main():
635 """
636 Create Cloud Manager connector for GCP class instance and invoke apply
637 :return: None
638 """
639 obj_store = NetAppCloudManagerConnectorGCP()
640 obj_store.apply()
641
642
643 if __name__ == '__main__':
644 main()