| 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_azure |
| 8 |
''' |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = ''' |
| 15 |
|
| 16 |
module: na_cloudmanager_connector_azure |
| 17 |
short_description: NetApp Cloud Manager connector for Azure. |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp.cloudmanager.netapp.cloudmanager |
| 20 |
version_added: '21.4.0' |
| 21 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 22 |
|
| 23 |
description: |
| 24 |
- Create or delete Cloud Manager connector for Azure. |
| 25 |
|
| 26 |
options: |
| 27 |
|
| 28 |
state: |
| 29 |
description: |
| 30 |
- Whether the specified Cloud Manager connector for Azure 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 Azure to manage. |
| 39 |
type: str |
| 40 |
|
| 41 |
virtual_machine_size: |
| 42 |
description: |
| 43 |
- The virtual machine type. (for example, Standard_DS3_v2). |
| 44 |
- At least 4 CPU and 16 GB of memory are required. |
| 45 |
type: str |
| 46 |
default: Standard_DS3_v2 |
| 47 |
|
| 48 |
resource_group: |
| 49 |
required: true |
| 50 |
description: |
| 51 |
- The resource group in Azure where the resources will be created. |
| 52 |
type: str |
| 53 |
|
| 54 |
subnet_name: |
| 55 |
required: true |
| 56 |
description: |
| 57 |
- The name of the subnet for the virtual machine. |
| 58 |
- For example, in /subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/xxx/subnets/default, |
| 59 |
only default is needed. |
| 60 |
aliases: |
| 61 |
- subnet_id |
| 62 |
type: str |
| 63 |
version_added: '21.7.0' |
| 64 |
|
| 65 |
location: |
| 66 |
required: true |
| 67 |
description: |
| 68 |
- The location where the Cloud Manager Connector will be created. |
| 69 |
type: str |
| 70 |
|
| 71 |
client_id: |
| 72 |
description: |
| 73 |
- The unique client ID of the Connector. |
| 74 |
- The connector ID. |
| 75 |
type: str |
| 76 |
|
| 77 |
subscription_id: |
| 78 |
required: true |
| 79 |
description: |
| 80 |
- The ID of the Azure subscription. |
| 81 |
type: str |
| 82 |
|
| 83 |
company: |
| 84 |
required: true |
| 85 |
description: |
| 86 |
- The name of the company of the user. |
| 87 |
type: str |
| 88 |
|
| 89 |
vnet_name: |
| 90 |
required: true |
| 91 |
description: |
| 92 |
- The name of the virtual network. |
| 93 |
- for example, in /subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/virtualNetworks/default, |
| 94 |
only default is needed. |
| 95 |
aliases: |
| 96 |
- vnet_id |
| 97 |
type: str |
| 98 |
version_added: '21.7.0' |
| 99 |
|
| 100 |
vnet_resource_group: |
| 101 |
description: |
| 102 |
- The resource group in Azure associated with the virtual network. |
| 103 |
- If not provided, its assumed that the VNet is within the previously specified resource group. |
| 104 |
type: str |
| 105 |
|
| 106 |
network_security_resource_group: |
| 107 |
description: |
| 108 |
- The resource group in Azure associated with the security group. |
| 109 |
- If not provided, its assumed that the security group is within the previously specified resource group. |
| 110 |
type: str |
| 111 |
|
| 112 |
network_security_group_name: |
| 113 |
required: true |
| 114 |
description: |
| 115 |
- The name of the security group for the deployment. |
| 116 |
type: str |
| 117 |
|
| 118 |
proxy_certificates: |
| 119 |
description: |
| 120 |
- The proxy certificates, a list of certificate file names. |
| 121 |
type: list |
| 122 |
elements: str |
| 123 |
|
| 124 |
associate_public_ip_address: |
| 125 |
description: |
| 126 |
- Indicates whether to associate the public IP address to the virtual machine. |
| 127 |
type: bool |
| 128 |
default: true |
| 129 |
|
| 130 |
account_id: |
| 131 |
required: true |
| 132 |
description: |
| 133 |
- The NetApp tenancy account ID. |
| 134 |
type: str |
| 135 |
|
| 136 |
proxy_url: |
| 137 |
description: |
| 138 |
- The proxy URL, if using a proxy to connect to the internet. |
| 139 |
type: str |
| 140 |
|
| 141 |
proxy_user_name: |
| 142 |
description: |
| 143 |
- The proxy user name, if using a proxy to connect to the internet. |
| 144 |
type: str |
| 145 |
|
| 146 |
proxy_password: |
| 147 |
description: |
| 148 |
- The proxy password, if using a proxy to connect to the internet. |
| 149 |
type: str |
| 150 |
|
| 151 |
admin_username: |
| 152 |
required: true |
| 153 |
description: |
| 154 |
- The user name for the Connector. |
| 155 |
type: str |
| 156 |
|
| 157 |
admin_password: |
| 158 |
required: true |
| 159 |
description: |
| 160 |
- The password for the Connector. |
| 161 |
type: str |
| 162 |
|
| 163 |
storage_account: |
| 164 |
description: |
| 165 |
- The storage account can be created automatically. |
| 166 |
- When C(storage_account) is not set, the name is constructed by appending 'sa' to the connector C(name). |
| 167 |
- Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only. |
| 168 |
type: str |
| 169 |
version_added: '21.17.0' |
| 170 |
''' |
| 171 |
|
| 172 |
EXAMPLES = """ |
| 173 |
- name: Create NetApp Cloud Manager connector for Azure. |
| 174 |
netapp.cloudmanager.na_cloudmanager_connector_azure: |
| 175 |
state: present |
| 176 |
refresh_token: "{{ xxxxxxxxxxxxxxx }}" |
| 177 |
name: bsuhas_ansible_occm |
| 178 |
location: westus |
| 179 |
resource_group: occm_group_westus |
| 180 |
subnet_name: subnetxxxxx |
| 181 |
vnet_name: Vnetxxxxx |
| 182 |
subscription_id: "{{ xxxxxxxxxxxxxxxxx }}" |
| 183 |
account_id: "{{ account-xxxxxxx }}" |
| 184 |
company: NetApp |
| 185 |
admin_password: Netapp123456 |
| 186 |
admin_username: bsuhas |
| 187 |
network_security_group_name: OCCM_SG |
| 188 |
proxy_url: abc.com |
| 189 |
proxy_user_name: xyz |
| 190 |
proxy_password: abcxyz |
| 191 |
proxy_certificates: [abc.crt.txt, xyz.crt.txt] |
| 192 |
|
| 193 |
- name: Delete NetApp Cloud Manager connector for Azure. |
| 194 |
netapp.cloudmanager.na_cloudmanager_connector_azure: |
| 195 |
state: absent |
| 196 |
name: ansible |
| 197 |
location: westus |
| 198 |
resource_group: occm_group_westus |
| 199 |
network_security_group_name: OCCM_SG |
| 200 |
subnet_name: subnetxxxxx |
| 201 |
company: NetApp |
| 202 |
admin_password: Netapp123456 |
| 203 |
admin_username: bsuhas |
| 204 |
vnet_name: Vnetxxxxx |
| 205 |
subscription_id: "{{ xxxxxxxxxxxxxxxxx }}" |
| 206 |
account_id: "{{ account-xxxxxxx }}" |
| 207 |
refresh_token: "{{ xxxxxxxxxxxxxxx }}" |
| 208 |
client_id: xxxxxxxxxxxxxxxxxxx |
| 209 |
""" |
| 210 |
|
| 211 |
RETURN = """ |
| 212 |
msg: |
| 213 |
description: Newly created Azure connector id in cloud manager. |
| 214 |
type: str |
| 215 |
returned: success |
| 216 |
sample: 'xxxxxxxxxxxxxxxx' |
| 217 |
""" |
| 218 |
|
| 219 |
import traceback |
| 220 |
import time |
| 221 |
import base64 |
| 222 |
import json |
| 223 |
|
| 224 |
from ansible.module_utils.basic import AnsibleModule |
| 225 |
from ansible.module_utils._text import to_native |
| 226 |
import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils |
| 227 |
from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module import NetAppModule |
| 228 |
from ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp import CloudManagerRestAPI |
| 229 |
|
| 230 |
IMPORT_EXCEPTION = None |
| 231 |
|
| 232 |
try: |
| 233 |
from azure.mgmt.resource import ResourceManagementClient |
| 234 |
from azure.mgmt.compute import ComputeManagementClient |
| 235 |
from azure.mgmt.network import NetworkManagementClient |
| 236 |
from azure.mgmt.storage import StorageManagementClient |
| 237 |
from azure.mgmt.resource.resources.models import Deployment |
| 238 |
from azure.common.client_factory import get_client_from_cli_profile |
| 239 |
from msrestazure.azure_exceptions import CloudError |
| 240 |
HAS_AZURE_LIB = True |
| 241 |
except ImportError as exc: |
| 242 |
HAS_AZURE_LIB = False |
| 243 |
IMPORT_EXCEPTION = exc |
| 244 |
|
| 245 |
|
| 246 |
class NetAppCloudManagerConnectorAzure(object): |
| 247 |
''' object initialize and class methods ''' |
| 248 |
|
| 249 |
def __init__(self): |
| 250 |
self.use_rest = False |
| 251 |
self.argument_spec = netapp_utils.cloudmanager_host_argument_spec() |
| 252 |
self.argument_spec.update(dict( |
| 253 |
name=dict(required=True, type='str'), |
| 254 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 255 |
virtual_machine_size=dict(required=False, type='str', default='Standard_DS3_v2'), |
| 256 |
resource_group=dict(required=True, type='str'), |
| 257 |
subscription_id=dict(required=True, type='str'), |
| 258 |
subnet_name=dict(required=True, type='str', aliases=['subnet_id']), |
| 259 |
vnet_name=dict(required=True, type='str', aliases=['vnet_id']), |
| 260 |
vnet_resource_group=dict(required=False, type='str'), |
| 261 |
location=dict(required=True, type='str'), |
| 262 |
network_security_resource_group=dict(required=False, type='str'), |
| 263 |
network_security_group_name=dict(required=True, type='str'), |
| 264 |
client_id=dict(required=False, type='str'), |
| 265 |
company=dict(required=True, type='str'), |
| 266 |
proxy_certificates=dict(required=False, type='list', elements='str'), |
| 267 |
associate_public_ip_address=dict(required=False, type='bool', default=True), |
| 268 |
account_id=dict(required=True, type='str'), |
| 269 |
proxy_url=dict(required=False, type='str'), |
| 270 |
proxy_user_name=dict(required=False, type='str'), |
| 271 |
proxy_password=dict(required=False, type='str', no_log=True), |
| 272 |
admin_username=dict(required=True, type='str'), |
| 273 |
admin_password=dict(required=True, type='str', no_log=True), |
| 274 |
storage_account=dict(required=False, type='str'), |
| 275 |
)) |
| 276 |
|
| 277 |
self.module = AnsibleModule( |
| 278 |
argument_spec=self.argument_spec, |
| 279 |
required_if=[ |
| 280 |
['state', 'absent', ['client_id']] |
| 281 |
], |
| 282 |
required_one_of=[['refresh_token', 'sa_client_id']], |
| 283 |
required_together=[['sa_client_id', 'sa_secret_key']], |
| 284 |
supports_check_mode=True |
| 285 |
) |
| 286 |
|
| 287 |
if HAS_AZURE_LIB is False: |
| 288 |
self.module.fail_json(msg="the python AZURE library azure.mgmt and azure.common is required. Command is pip install azure-mgmt, azure-common." |
| 289 |
" Import error: %s" % str(IMPORT_EXCEPTION)) |
| 290 |
|
| 291 |
self.na_helper = NetAppModule() |
| 292 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 293 |
if 'storage_account' not in self.parameters or self.parameters['storage_account'] == "": |
| 294 |
self.parameters['storage_account'] = self.parameters['name'].lower() + 'sa' |
| 295 |
self.rest_api = CloudManagerRestAPI(self.module) |
| 296 |
|
| 297 |
def get_deploy_azure_vm(self): |
| 298 |
""" |
| 299 |
Get Cloud Manager connector for AZURE |
| 300 |
:return: |
| 301 |
Dictionary of current details if Cloud Manager connector for AZURE |
| 302 |
None if Cloud Manager connector for AZURE is not found |
| 303 |
""" |
| 304 |
|
| 305 |
exists = False |
| 306 |
|
| 307 |
resource_client = get_client_from_cli_profile(ResourceManagementClient) |
| 308 |
try: |
| 309 |
exists = resource_client.deployments.check_existence(self.parameters['resource_group'], self.parameters['name']) |
| 310 |
|
| 311 |
except CloudError as error: |
| 312 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 313 |
|
| 314 |
if not exists: |
| 315 |
return None |
| 316 |
|
| 317 |
return exists |
| 318 |
|
| 319 |
def deploy_azure(self): |
| 320 |
""" |
| 321 |
Create Cloud Manager connector for Azure |
| 322 |
:return: client_id |
| 323 |
""" |
| 324 |
|
| 325 |
user_data, client_id = self.register_agent_to_service() |
| 326 |
template = json.loads(self.na_helper.call_template()) |
| 327 |
params = json.loads(self.na_helper.call_parameters()) |
| 328 |
params['adminUsername']['value'] = self.parameters['admin_username'] |
| 329 |
params['adminPassword']['value'] = self.parameters['admin_password'] |
| 330 |
params['customData']['value'] = json.dumps(user_data) |
| 331 |
params['location']['value'] = self.parameters['location'] |
| 332 |
params['virtualMachineName']['value'] = self.parameters['name'] |
| 333 |
params['storageAccount']['value'] = self.parameters['storage_account'] |
| 334 |
if self.rest_api.environment == 'stage': |
| 335 |
params['environment']['value'] = self.rest_api.environment |
| 336 |
if '/subscriptions' in self.parameters['vnet_name']: |
| 337 |
network = self.parameters['vnet_name'] |
| 338 |
else: |
| 339 |
if self.parameters.get('vnet_resource_group') is not None: |
| 340 |
network = '/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s' % ( |
| 341 |
self.parameters['subscription_id'], self.parameters['vnet_resource_group'], self.parameters['vnet_name']) |
| 342 |
else: |
| 343 |
network = '/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s' % ( |
| 344 |
self.parameters['subscription_id'], self.parameters['resource_group'], self.parameters['vnet_name']) |
| 345 |
|
| 346 |
if '/subscriptions' in self.parameters['subnet_name']: |
| 347 |
subnet = self.parameters['subnet_name'] |
| 348 |
else: |
| 349 |
subnet = '%s/subnets/%s' % (network, self.parameters['subnet_name']) |
| 350 |
|
| 351 |
if self.parameters.get('network_security_resource_group') is not None: |
| 352 |
network_security_group_name = '/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s' % ( |
| 353 |
self.parameters['subscription_id'], self.parameters['network_security_resource_group'], self.parameters['network_security_group_name']) |
| 354 |
else: |
| 355 |
network_security_group_name = '/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s' % ( |
| 356 |
self.parameters['subscription_id'], self.parameters['resource_group'], self.parameters['network_security_group_name']) |
| 357 |
|
| 358 |
params['virtualNetworkId']['value'] = network |
| 359 |
params['networkSecurityGroupName']['value'] = network_security_group_name |
| 360 |
params['virtualMachineSize']['value'] = self.parameters['virtual_machine_size'] |
| 361 |
params['subnetId']['value'] = subnet |
| 362 |
|
| 363 |
try: |
| 364 |
resource_client = get_client_from_cli_profile(ResourceManagementClient) |
| 365 |
|
| 366 |
resource_client.resource_groups.create_or_update( |
| 367 |
self.parameters['resource_group'], |
| 368 |
{"location": self.parameters['location']}) |
| 369 |
|
| 370 |
deployment_properties = { |
| 371 |
'mode': 'Incremental', |
| 372 |
'template': template, |
| 373 |
'parameters': params |
| 374 |
} |
| 375 |
resource_client.deployments.begin_create_or_update( |
| 376 |
self.parameters['resource_group'], |
| 377 |
self.parameters['name'], |
| 378 |
Deployment(properties=deployment_properties) |
| 379 |
) |
| 380 |
|
| 381 |
except CloudError as error: |
| 382 |
self.module.fail_json(msg="Error in deploy_azure: %s" % to_native(error), exception=traceback.format_exc()) |
| 383 |
|
| 384 |
# Sleep for 2 minutes |
| 385 |
time.sleep(120) |
| 386 |
retries = 30 |
| 387 |
while retries > 0: |
| 388 |
occm_resp, error = self.na_helper.check_occm_status(self.rest_api, client_id) |
| 389 |
if error is not None: |
| 390 |
self.module.fail_json( |
| 391 |
msg="Error: Not able to get occm status: %s, %s" % (str(error), str(occm_resp))) |
| 392 |
if occm_resp['agent']['status'] == "active": |
| 393 |
break |
| 394 |
else: |
| 395 |
time.sleep(30) |
| 396 |
retries -= 1 |
| 397 |
if retries == 0: |
| 398 |
# Taking too long for status to be active |
| 399 |
return self.module.fail_json(msg="Taking too long for OCCM agent to be active or not properly setup") |
| 400 |
|
| 401 |
try: |
| 402 |
compute_client = get_client_from_cli_profile(ComputeManagementClient) |
| 403 |
vm = compute_client.virtual_machines.get(self.parameters['resource_group'], self.parameters['name']) |
| 404 |
except CloudError as error: |
| 405 |
return self.module.fail_json(msg="Error in deploy_azure (get identity): %s" % to_native(error), exception=traceback.format_exc()) |
| 406 |
|
| 407 |
principal_id = vm.identity.principal_id |
| 408 |
return client_id, principal_id |
| 409 |
|
| 410 |
def register_agent_to_service(self): |
| 411 |
""" |
| 412 |
Register agent to service and collect userdata by setting up connector |
| 413 |
:return: UserData, ClientID |
| 414 |
""" |
| 415 |
|
| 416 |
if '/subscriptions' in self.parameters['vnet_name']: |
| 417 |
network = self.parameters['vnet_name'] |
| 418 |
else: |
| 419 |
if self.parameters.get('vnet_resource_group') is not None: |
| 420 |
network = '/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s' % ( |
| 421 |
self.parameters['subscription_id'], self.parameters['vnet_resource_group'], self.parameters['vnet_name']) |
| 422 |
else: |
| 423 |
network = '/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/virtualNetworks/%s' % ( |
| 424 |
self.parameters['subscription_id'], self.parameters['resource_group'], self.parameters['vnet_name']) |
| 425 |
|
| 426 |
if '/subscriptions' in self.parameters['subnet_name']: |
| 427 |
subnet = self.parameters['subnet_name'] |
| 428 |
else: |
| 429 |
subnet = '%s/subnets/%s' % (network, self.parameters['subnet_name']) |
| 430 |
|
| 431 |
if self.parameters.get('account_id') is None: |
| 432 |
response, error = self.na_helper.get_or_create_account(self.rest_api) |
| 433 |
if error is not None: |
| 434 |
self.module.fail_json( |
| 435 |
msg="Error: unexpected response on getting account: %s, %s" % (str(error), str(response))) |
| 436 |
self.parameters['account_id'] = response |
| 437 |
|
| 438 |
headers = { |
| 439 |
"X-User-Token": self.rest_api.token_type + " " + self.rest_api.token, |
| 440 |
} |
| 441 |
body = { |
| 442 |
"accountId": self.parameters['account_id'], |
| 443 |
"name": self.parameters['name'], |
| 444 |
"company": self.parameters['company'], |
| 445 |
"placement": { |
| 446 |
"provider": "AZURE", |
| 447 |
"region": self.parameters['location'], |
| 448 |
"network": network, |
| 449 |
"subnet": subnet, |
| 450 |
}, |
| 451 |
"extra": { |
| 452 |
"proxy": { |
| 453 |
"proxyUrl": self.parameters.get('proxy_url'), |
| 454 |
"proxyUserName": self.parameters.get('proxy_user_name'), |
| 455 |
"proxyPassword": self.parameters.get('proxy_password') |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
register_url = "%s/agents-mgmt/connector-setup" % self.rest_api.environment_data['CLOUD_MANAGER_HOST'] |
| 461 |
response, error, dummy = self.rest_api.post(register_url, body, header=headers) |
| 462 |
if error is not None: |
| 463 |
self.module.fail_json(msg="Error: unexpected response on getting userdata for connector setup: %s, %s" % (str(error), str(response))) |
| 464 |
client_id = response['clientId'] |
| 465 |
|
| 466 |
proxy_certificates = [] |
| 467 |
if self.parameters.get('proxy_certificates') is not None: |
| 468 |
for each in self.parameters['proxy_certificates']: |
| 469 |
try: |
| 470 |
data = open(each, "r").read() |
| 471 |
except OSError: |
| 472 |
self.module.fail_json(msg="Error: Could not open/read file of proxy_certificates: %s" % str(each)) |
| 473 |
|
| 474 |
encoded_certificate = base64.b64encode(data) |
| 475 |
proxy_certificates.append(encoded_certificate) |
| 476 |
|
| 477 |
if proxy_certificates: |
| 478 |
response['proxySettings']['proxyCertificates'] = proxy_certificates |
| 479 |
|
| 480 |
return response, client_id |
| 481 |
|
| 482 |
def delete_azure_occm(self): |
| 483 |
""" |
| 484 |
Delete OCCM |
| 485 |
:return: |
| 486 |
None |
| 487 |
""" |
| 488 |
# delete vm deploy |
| 489 |
try: |
| 490 |
compute_client = get_client_from_cli_profile(ComputeManagementClient) |
| 491 |
vm_delete = compute_client.virtual_machines.begin_delete( |
| 492 |
self.parameters['resource_group'], |
| 493 |
self.parameters['name']) |
| 494 |
while not vm_delete.done(): |
| 495 |
vm_delete.wait(2) |
| 496 |
except CloudError as error: |
| 497 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 498 |
|
| 499 |
# delete interfaces deploy |
| 500 |
try: |
| 501 |
network_client = get_client_from_cli_profile(NetworkManagementClient) |
| 502 |
interface_delete = network_client.network_interfaces.begin_delete( |
| 503 |
self.parameters['resource_group'], |
| 504 |
self.parameters['name'] + '-nic') |
| 505 |
while not interface_delete.done(): |
| 506 |
interface_delete.wait(2) |
| 507 |
except CloudError as error: |
| 508 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 509 |
|
| 510 |
# delete storage account deploy |
| 511 |
try: |
| 512 |
storage_client = get_client_from_cli_profile(StorageManagementClient) |
| 513 |
storage_client.storage_accounts.delete( |
| 514 |
self.parameters['resource_group'], |
| 515 |
self.parameters['storage_account']) |
| 516 |
except CloudError as error: |
| 517 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 518 |
|
| 519 |
# delete storage account deploy |
| 520 |
try: |
| 521 |
network_client = get_client_from_cli_profile(NetworkManagementClient) |
| 522 |
public_ip_addresses_delete = network_client.public_ip_addresses.begin_delete( |
| 523 |
self.parameters['resource_group'], |
| 524 |
self.parameters['name'] + '-ip') |
| 525 |
while not public_ip_addresses_delete.done(): |
| 526 |
public_ip_addresses_delete.wait(2) |
| 527 |
except CloudError as error: |
| 528 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 529 |
|
| 530 |
# delete deployment |
| 531 |
try: |
| 532 |
resource_client = get_client_from_cli_profile(ResourceManagementClient) |
| 533 |
deployments_delete = resource_client.deployments.begin_delete( |
| 534 |
self.parameters['resource_group'], |
| 535 |
self.parameters['name'] + '-ip') |
| 536 |
while not deployments_delete.done(): |
| 537 |
deployments_delete.wait(5) |
| 538 |
except CloudError as error: |
| 539 |
self.module.fail_json(msg=to_native(error), exception=traceback.format_exc()) |
| 540 |
|
| 541 |
retries = 16 |
| 542 |
while retries > 0: |
| 543 |
occm_resp, error = self.na_helper.check_occm_status(self.rest_api, |
| 544 |
self.parameters['client_id']) |
| 545 |
if error is not None: |
| 546 |
self.module.fail_json( |
| 547 |
msg="Error: Not able to get occm status: %s, %s" % (str(error), str(occm_resp))) |
| 548 |
if occm_resp['agent']['status'] != "active": |
| 549 |
break |
| 550 |
else: |
| 551 |
time.sleep(10) |
| 552 |
retries -= 1 |
| 553 |
if retries == 0: |
| 554 |
# Taking too long for terminating OCCM |
| 555 |
return self.module.fail_json(msg="Taking too long for instance to finish terminating") |
| 556 |
client = self.rest_api.format_client_id(self.parameters['client_id']) |
| 557 |
error = self.na_helper.delete_occm_agents(self.rest_api, [{'agentId': client}]) |
| 558 |
if error: |
| 559 |
self.module.fail_json(msg="Error: unexpected response on deleting OCCM: %s" % (str(error))) |
| 560 |
|
| 561 |
def apply(self): |
| 562 |
""" |
| 563 |
Apply action to the Cloud Manager connector for AZURE |
| 564 |
:return: None |
| 565 |
""" |
| 566 |
client_id = None |
| 567 |
principal_id = None |
| 568 |
if not self.module.check_mode: |
| 569 |
if self.parameters['state'] == 'present': |
| 570 |
client_id, principal_id = self.deploy_azure() |
| 571 |
self.na_helper.changed = True |
| 572 |
elif self.parameters['state'] == 'absent': |
| 573 |
get_deploy = self.get_deploy_azure_vm() |
| 574 |
if get_deploy: |
| 575 |
self.delete_azure_occm() |
| 576 |
self.na_helper.changed = True |
| 577 |
|
| 578 |
self.module.exit_json(changed=self.na_helper.changed, msg={'client_id': client_id, 'principal_id': principal_id}) |
| 579 |
|
| 580 |
|
| 581 |
def main(): |
| 582 |
""" |
| 583 |
Create Cloud Manager connector for AZURE class instance and invoke apply |
| 584 |
:return: None |
| 585 |
""" |
| 586 |
obj_store = NetAppCloudManagerConnectorAzure() |
| 587 |
obj_store.apply() |
| 588 |
|
| 589 |
|
| 590 |
if __name__ == '__main__': |
| 591 |
main() |