Debian Codegraph @

ansible_collections/netapp/cloudmanager/plugins/modules/na_cloudmanager_connector_aws.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_aws
8 '''
9
10 from __future__ import absolute_import, division, print_function
11 __metaclass__ = type
12
13 DOCUMENTATION = '''
14
15 module: na_cloudmanager_connector_aws
16 short_description: NetApp Cloud Manager connector for AWS
17 extends_documentation_fragment:
18 - netapp.cloudmanager.netapp.cloudmanager
19 version_added: '21.3.0'
20 author: NetApp Ansible Team (@carchi8py) <[email protected]>
21
22 description:
23 - Create or delete Cloud Manager connector for AWS.
24 - This module requires to be authenticated with AWS. This can be done with C(aws configure).
25
26 options:
27
28 state:
29 description:
30 - Whether the specified Cloud Manager connector for AWS should exist or not.
31 choices: ['present', 'absent']
32 default: 'present'
33 type: str
34
35 name:
36 required: true
37 description:
38 - The name of the Cloud Manager connector for AWS to manage.
39 type: str
40
41 instance_type:
42 description:
43 - The type of instance (for example, t3.xlarge). At least 4 CPU and 16 GB of memory are required.
44 type: str
45 default: t3.xlarge
46
47 key_name:
48 description:
49 - The name of the key pair to use for the Connector instance.
50 type: str
51
52 subnet_id:
53 description:
54 - The ID of the subnet for the instance.
55 type: str
56
57 region:
58 required: true
59 description:
60 - The region where the Cloud Manager Connector will be created.
61 type: str
62
63 instance_id:
64 description:
65 - The ID of the EC2 instance used for delete.
66 type: str
67
68 client_id:
69 description:
70 - The unique client ID of the Connector.
71 - The connector ID.
72 type: str
73
74 ami:
75 description:
76 - The image ID.
77 type: str
78
79 company:
80 description:
81 - The name of the company of the user.
82 type: str
83
84 security_group_ids:
85 description:
86 - The IDs of the security groups for the instance, multiple security groups can be provided separated by ','.
87 type: list
88 elements: str
89
90 iam_instance_profile_name:
91 description:
92 - The name of the instance profile for the Connector.
93 type: str
94
95 enable_termination_protection:
96 description:
97 - Indicates whether to enable termination protection on the instance.
98 type: bool
99 default: false
100
101 associate_public_ip_address:
102 description:
103 - Indicates whether to associate a public IP address to the instance. If not provided, the association will be done based on the subnet's configuration.
104 type: bool
105 default: true
106
107 account_id:
108 description:
109 - The NetApp tenancy account ID.
110 type: str
111
112 proxy_url:
113 description:
114 - The proxy URL, if using a proxy to connect to the internet.
115 type: str
116
117 proxy_user_name:
118 description:
119 - The proxy user name, if using a proxy to connect to the internet.
120 type: str
121
122 proxy_password:
123 description:
124 - The proxy password, if using a proxy to connect to the internet.
125 type: str
126
127 proxy_certificates:
128 description:
129 - The proxy certificates, a list of certificate file names.
130 type: list
131 elements: str
132 version_added: 21.5.0
133
134 aws_tag:
135 description:
136 - Additional tags for the AWS EC2 instance.
137 type: list
138 elements: dict
139 suboptions:
140 tag_key:
141 description: The key of the tag.
142 type: str
143 tag_value:
144 description: The tag value.
145 type: str
146
147 notes:
148 - Support check_mode.
149 '''
150
151 EXAMPLES = """
152 - name: Create NetApp Cloud Manager connector for AWS
153 netapp.cloudmanager.na_cloudmanager_connector_aws:
154 state: present
155 refresh_token: "{{ xxxxxxxxxxxxxxx }}"
156 name: bsuhas_ansible_occm
157 region: us-west-1
158 key_name: dev_automation
159 subnet_id: subnet-xxxxx
160 security_group_ids: [sg-xxxxxxxxxxx]
161 iam_instance_profile_name: OCCM_AUTOMATION
162 account_id: "{{ account-xxxxxxx }}"
163 company: NetApp
164 proxy_url: abc.com
165 proxy_user_name: xyz
166 proxy_password: abcxyz
167 proxy_certificates: [abc.crt.txt, xyz.crt.txt]
168 aws_tag: [
169 {tag_key: abc,
170 tag_value: a123}]
171
172 - name: Delete NetApp Cloud Manager connector for AWS
173 netapp.cloudmanager.na_cloudmanager_connector_aws:
174 state: absent
175 name: ansible
176 region: us-west-1
177 account_id: "{{ account-xxxxxxx }}"
178 instance_id: i-xxxxxxxxxxxxx
179 client_id: xxxxxxxxxxxxxxxxxxx
180 """
181
182 RETURN = """
183 ids:
184 description: Newly created AWS client ID in cloud manager, instance ID and account ID.
185 type: dict
186 returned: success
187 """
188
189 import traceback
190 import uuid
191 import time
192
193 from ansible.module_utils.basic import AnsibleModule
194 from ansible.module_utils._text import to_native
195 import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils
196 from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module import NetAppModule
197 from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp import CloudManagerRestAPI
198 IMPORT_EXCEPTION = None
199
200 try:
201 import boto3
202 from botocore.exceptions import ClientError
203 HAS_AWS_LIB = True
204 except ImportError as exc:
205 HAS_AWS_LIB = False
206 IMPORT_EXCEPTION = exc
207
208 UUID = str(uuid.uuid4())
209
210
211 class NetAppCloudManagerConnectorAWS(object):
212 ''' object initialize and class methods '''
213
214 def __init__(self):
215 self.use_rest = False
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 instance_type=dict(required=False, type='str', default='t3.xlarge'),
221 key_name=dict(required=False, type='str'),
222 subnet_id=dict(required=False, type='str'),
223 region=dict(required=True, type='str'),
224 instance_id=dict(required=False, type='str'),
225 client_id=dict(required=False, type='str'),
226 ami=dict(required=False, type='str'),
227 company=dict(required=False, type='str'),
228 security_group_ids=dict(required=False, type='list', elements='str'),
229 iam_instance_profile_name=dict(required=False, type='str'),
230 enable_termination_protection=dict(required=False, type='bool', default=False),
231 associate_public_ip_address=dict(required=False, type='bool', default=True),
232 account_id=dict(required=False, type='str'),
233 proxy_url=dict(required=False, type='str'),
234 proxy_user_name=dict(required=False, type='str'),
235 proxy_password=dict(required=False, type='str', no_log=True),
236 proxy_certificates=dict(required=False, type='list', elements='str'),
237 aws_tag=dict(required=False, type='list', elements='dict', options=dict(
238 tag_key=dict(type='str', no_log=False),
239 tag_value=dict(type='str')
240 )),
241 ))
242
243 self.module = AnsibleModule(
244 argument_spec=self.argument_spec,
245 required_if=[
246 ['state', 'present', ['company', 'iam_instance_profile_name', 'key_name', 'security_group_ids', 'subnet_id']],
247 ],
248 required_one_of=[['refresh_token', 'sa_client_id']],
249 required_together=[['sa_client_id', 'sa_secret_key']],
250 supports_check_mode=True
251 )
252
253 if HAS_AWS_LIB is False:
254 self.module.fail_json(msg="the python AWS packages boto3 and botocore are required. Command is pip install boto3."
255 "Import error: %s" % str(IMPORT_EXCEPTION))
256
257 self.na_helper = NetAppModule()
258 self.parameters = self.na_helper.set_parameters(self.module.params)
259
260 self.rest_api = CloudManagerRestAPI(self.module)
261
262 def get_instance(self):
263 """
264 Get Cloud Manager connector for AWS
265 :return:
266 Dictionary of current details if Cloud Manager connector for AWS
267 None if Cloud Manager connector for AWS is not found
268 """
269
270 response = None
271 client = boto3.client('ec2', region_name=self.parameters['region'])
272 filters = [{'Name': 'tag:Name', 'Values': [self.parameters['name']]},
273 {'Name': 'tag:OCCMInstance', 'Values': ['true']}]
274
275 kwargs = {'Filters': filters} if self.parameters.get('instance_id') is None else {'InstanceIds': [self.parameters['instance_id']]}
276
277 try:
278 response = client.describe_instances(**kwargs)
279
280 except ClientError as error:
281 self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
282
283 if len(response['Reservations']) == 0:
284 return None
285
286 actives = [instance for reservation in response['Reservations'] for instance in reservation['Instances'] if instance['State']['Name'] != 'terminated']
287 if len(actives) == 1:
288 return actives[0]
289 if not actives:
290 return None
291 self.module.fail_json(msg="Error: found multiple instances for name=%s: %s" % (self.parameters['name'], str(actives)))
292
293 def get_ami(self):
294 """
295 Get AWS EC2 Image
296 :return:
297 Latest AMI
298 """
299
300 instance_ami = None
301 client = boto3.client('ec2', region_name=self.parameters['region'])
302
303 try:
304 instance_ami = client.describe_images(
305 Filters=[
306 {
307 'Name': 'name',
308 'Values': [
309 self.rest_api.environment_data['AMI_FILTER'],
310 ]
311 },
312 ],
313 Owners=[
314 self.rest_api.environment_data['AWS_ACCOUNT'],
315 ],
316 )
317 except ClientError as error:
318 self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
319
320 latest_date = instance_ami['Images'][0]['CreationDate']
321 latest_ami = instance_ami['Images'][0]['ImageId']
322
323 for image in instance_ami['Images']:
324 if image['CreationDate'] > latest_date:
325 latest_date = image['CreationDate']
326 latest_ami = image['ImageId']
327
328 return latest_ami
329
330 def create_instance(self):
331 """
332 Create Cloud Manager connector for AWS
333 :return: client_id, instance_id
334 """
335
336 if self.parameters.get('ami') is None:
337 self.parameters['ami'] = self.get_ami()
338
339 user_data, client_id = self.register_agent_to_service()
340
341 ec2 = boto3.client('ec2', region_name=self.parameters['region'])
342
343 tags = [
344 {
345 'Key': 'Name',
346 'Value': self.parameters['name']
347 },
348 {
349 'Key': 'OCCMInstance',
350 'Value': 'true'
351 },
352 ]
353
354 if self.parameters.get('aws_tag') is not None:
355 for each_tag in self.parameters['aws_tag']:
356 tag = {
357 'Key': each_tag['tag_key'],
358 'Value': each_tag['tag_value']
359 }
360
361 tags.append(tag)
362
363 instance_input = {
364 'BlockDeviceMappings': [
365 {
366 'DeviceName': '/dev/sda1',
367 'Ebs': {
368 'Encrypted': True,
369 'VolumeSize': 100,
370 'VolumeType': 'gp2',
371 },
372 },
373 ],
374 'ImageId': self.parameters['ami'],
375 'MinCount': 1,
376 'MaxCount': 1,
377 'KeyName': self.parameters['key_name'],
378 'InstanceType': self.parameters['instance_type'],
379 'DisableApiTermination': self.parameters['enable_termination_protection'],
380 'TagSpecifications': [
381 {
382 'ResourceType': 'instance',
383 'Tags': tags
384 },
385 ],
386 'IamInstanceProfile': {
387 'Name': self.parameters['iam_instance_profile_name']
388 },
389 'UserData': user_data
390 }
391
392 if self.parameters.get('associate_public_ip_address') is True:
393 instance_input['NetworkInterfaces'] = [
394 {
395 'AssociatePublicIpAddress': self.parameters['associate_public_ip_address'],
396 'DeviceIndex': 0,
397 'SubnetId': self.parameters['subnet_id'],
398 'Groups': self.parameters['security_group_ids']
399 }
400 ]
401 else:
402 instance_input['SubnetId'] = self.parameters['subnet_id']
403 instance_input['SecurityGroupIds'] = self.parameters['security_group_ids']
404
405 try:
406 result = ec2.run_instances(**instance_input)
407 except ClientError as error:
408 self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
409
410 # Sleep for 2 minutes
411 time.sleep(120)
412 retries = 16
413 while retries > 0:
414 agent, error = self.na_helper.get_occm_agent_by_id(self.rest_api, client_id)
415 if error is not None:
416 self.module.fail_json(
417 msg="Error: not able to get occm status: %s, %s" % (str(error), str(agent)),
418 client_id=client_id, instance_id=result['Instances'][0]['InstanceId'])
419 if agent['status'] == "active":
420 break
421 else:
422 time.sleep(30)
423 retries -= 1
424 if retries == 0:
425 # Taking too long for status to be active
426 return self.module.fail_json(msg="Error: taking too long for OCCM agent to be active or not properly setup")
427
428 return client_id, result['Instances'][0]['InstanceId']
429
430 def get_vpc(self):
431 """
432 Get vpc
433 :return: vpc ID
434 """
435
436 vpc_result = None
437 ec2 = boto3.client('ec2', region_name=self.parameters['region'])
438
439 vpc_input = {'SubnetIds': [self.parameters['subnet_id']]}
440
441 try:
442 vpc_result = ec2.describe_subnets(**vpc_input)
443 except ClientError as error:
444 self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
445
446 return vpc_result['Subnets'][0]['VpcId']
447
448 def set_account_id(self):
449 if self.parameters.get('account_id') is None:
450 response, error = self.na_helper.get_or_create_account(self.rest_api)
451 if error is not None:
452 return error
453 self.parameters['account_id'] = response
454 return None
455
456 def register_agent_to_service(self):
457 """
458 Register agent to service and collect userdata by setting up connector
459 :return: UserData, ClientID
460 """
461
462 vpc = self.get_vpc()
463
464 if self.parameters.get('account_id') is None:
465 error = self.set_account_id()
466 if error is not None:
467 self.module.fail_json(msg="Error: failed to get account: %s." % str(error))
468
469 headers = {
470 "X-User-Token": self.rest_api.token_type + " " + self.rest_api.token,
471 "X-Service-Request-Id": "111"
472 }
473 body = {
474 "accountId": self.parameters['account_id'],
475 "name": self.parameters['name'],
476 "company": self.parameters['company'],
477 "placement": {
478 "provider": "AWS",
479 "region": self.parameters['region'],
480 "network": vpc,
481 "subnet": self.parameters['subnet_id'],
482 },
483 "extra": {
484 "proxy": {
485 "proxyUrl": self.parameters.get('proxy_url'),
486 "proxyUserName": self.parameters.get('proxy_user_name'),
487 "proxyPassword": self.parameters.get('proxy_password')
488 }
489 }
490 }
491
492 register_api = '/agents-mgmt/connector-setup'
493 response, error, dummy = self.rest_api.post(register_api, body, header=headers)
494 if error is not None:
495 self.module.fail_json(msg="Error: unexpected response on connector setup: %s, %s" % (str(error), str(response)))
496 client_id = response['clientId']
497 client_secret = response['clientSecret']
498
499 u_data = {
500 'instanceName': self.parameters['name'],
501 'company': self.parameters['company'],
502 'clientId': client_id,
503 'clientSecret': client_secret,
504 'systemId': UUID,
505 'tenancyAccountId': self.parameters['account_id'],
506 'proxySettings': {'proxyPassword': self.parameters.get('proxy_password'),
507 'proxyUserName': self.parameters.get('proxy_user_name'),
508 'proxyUrl': self.parameters.get('proxy_url'),
509 },
510 'localAgent': True
511 }
512
513 if self.parameters.get('proxy_certificates') is not None:
514 proxy_certificates = []
515 for certificate_file in self.parameters['proxy_certificates']:
516 encoded_certificate, error = self.na_helper.encode_certificates(certificate_file)
517 if error:
518 self.module.fail_json(msg="Error: could not open/read file '%s' of proxy_certificates: %s" % (certificate_file, error))
519 proxy_certificates.append(encoded_certificate)
520
521 if proxy_certificates:
522 u_data['proxySettings']['proxyCertificates'] = proxy_certificates
523
524 user_data = self.na_helper.convert_data_to_tabbed_jsonstring(u_data)
525
526 return user_data, client_id
527
528 def delete_instance(self):
529 """
530 Delete OCCM instance
531 :return:
532 None
533 """
534
535 ec2 = boto3.client('ec2', region_name=self.parameters['region'])
536 try:
537 ec2.terminate_instances(
538 InstanceIds=[
539 self.parameters['instance_id'],
540 ],
541 )
542 except ClientError as error:
543 self.module.fail_json(msg=to_native(error), exception=traceback.format_exc())
544
545 if 'client_id' not in self.parameters:
546 return None
547
548 retries = 30
549 while retries > 0:
550 agent, error = self.na_helper.get_occm_agent_by_id(self.rest_api, self.parameters['client_id'])
551 if error is not None:
552 return "Error: not able to get occm agent status after deleting instance: %s, %s." % (str(error), str(agent))
553 if agent['status'] != "active":
554 break
555 else:
556 time.sleep(10)
557 retries -= 1
558 if retries == 0:
559 # Taking too long for terminating OCCM
560 return "Error: taking too long for instance to finish terminating."
561 return None
562
563 def get_occm_agents(self):
564 if 'client_id' in self.parameters:
565 agent, error = self.na_helper.get_occm_agent_by_id(self.rest_api, self.parameters['client_id'])
566 if str(error) == '403' and 'Action not allowed for user' in str(agent):
567 # assume the agent does not exist anymore
568 agents, error = [], None
569 self.module.warn('Client Id %s was not found for this account.' % self.parameters['client_id'])
570 else:
571 agents = [agent]
572 else:
573 self.set_account_id()
574 if 'account_id' in self.parameters:
575 agents, error = self.na_helper.get_occm_agents_by_name(self.rest_api, self.parameters['account_id'],
576 self.parameters['name'], 'AWS')
577 else:
578 self.module.warn('Without account_id, some agents may still exist.')
579 agents, error = [], None
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):
586 agents = self.get_occm_agents()
587 client_id = self.parameters.get('client_id')
588 if client_id is None:
589 active_client_ids = [agent['agentId'] for agent in agents if 'agentId' in agent and agent['status'] == 'active']
590 if len(active_client_ids) == 1:
591 client_id = active_client_ids[0]
592 self.parameters['client_id'] = client_id
593 return client_id, agents
594
595 def delete_occm_agents(self, agents):
596 error = self.na_helper.delete_occm_agents(self.rest_api, agents)
597 if error:
598 return "Error: deleting OCCM agent(s): %s" % error
599 return None
600
601 def apply(self):
602 """
603 Apply action to the Cloud Manager connector for AWS
604 :return: None
605 """
606 results = {
607 'account_id': None,
608 'client_id': None,
609 'instance_id': None
610 }
611 agents = None
612 current = self.get_instance()
613 if current or self.parameters['state'] == 'absent':
614 if self.parameters.get('instance_id') is None and current:
615 self.parameters['instance_id'] = current['InstanceId']
616 results['instance_id'] = self.parameters.get('instance_id')
617 results['client_id'], agents = self.set_client_id()
618 if current is None and agents:
619 # it's possible the VM instance does not exist, but the clients are still present.
620 current = agents
621
622 cd_action = self.na_helper.get_cd_action(current, self.parameters)
623 if cd_action is None and self.parameters['state'] == 'present':
624 results['modify'] = 'Note: modifying an existing connector is not supported at this time.'
625
626 if not self.module.check_mode and self.na_helper.changed:
627 if cd_action == 'create':
628 results['client_id'], results['instance_id'] = self.create_instance()
629 elif cd_action == 'delete':
630 errors = []
631 if self.parameters.get('instance_id'):
632 errors.append(self.delete_instance())
633 if agents:
634 errors.append(self.delete_occm_agents(agents))
635 errors = [error for error in errors if error]
636 if errors:
637 self.module.fail_json(msg='Errors deleting instance or client: %s' % ', '.join(errors))
638
639 results['account_id'] = self.parameters.get('account_id')
640 results['changed'] = self.na_helper.changed
641 self.module.exit_json(**results)
642
643
644 def main():
645 """
646 Create Cloud Manager connector for AWS class instance and invoke apply
647 :return: None
648 """
649 obj_store = NetAppCloudManagerConnectorAWS()
650 obj_store.apply()
651
652
653 if __name__ == '__main__':
654 main()