| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = \ |
| 11 |
''' |
| 12 |
--- |
| 13 |
module: azure_rm_backupazurevm_info |
| 14 |
version_added: '1.1.0' |
| 15 |
short_description: Back up an Azure Virtual Machine using Azure Backup Information |
| 16 |
description: |
| 17 |
- Get Recovery point details for protected items. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
recovery_vault_name: |
| 25 |
description: |
| 26 |
- The name of the Azure Recovery Service Vault. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
resource_id: |
| 30 |
description: |
| 31 |
- Azure Virtual Machine Resource ID. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
extends_documentation_fragment: |
| 35 |
- azure.azcollection.azure |
| 36 |
- azure.azcollection.azure_tags |
| 37 |
author: |
| 38 |
- Suyeb Ansari (@suyeb786) |
| 39 |
- Pallavi Chaudhari (@PallaviC2510) |
| 40 |
|
| 41 |
''' |
| 42 |
|
| 43 |
EXAMPLES = \ |
| 44 |
''' |
| 45 |
- name: Get Recovery Point Details |
| 46 |
azure_rm_backupazurevm_info: |
| 47 |
resource_group: 'my-rg' |
| 48 |
recovery_vault_name: 'testVault' |
| 49 |
resource_id: "/subscriptions/<sub-id>/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/testVM" |
| 50 |
''' |
| 51 |
|
| 52 |
RETURN = \ |
| 53 |
''' |
| 54 |
id: |
| 55 |
description: |
| 56 |
- VM Protection details. |
| 57 |
returned: always |
| 58 |
type: str |
| 59 |
sample: '{"response":{"id":"protection_id","name":"protection_item_name","properties":{}}}' |
| 60 |
''' |
| 61 |
|
| 62 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 63 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 64 |
import json |
| 65 |
|
| 66 |
|
| 67 |
class Actions: |
| 68 |
(NoAction, Create, Update, Delete) = range(4) |
| 69 |
|
| 70 |
|
| 71 |
class BackupAzureVMInfo(AzureRMModuleBaseExt): |
| 72 |
def __init__(self): |
| 73 |
self.module_arg_spec = dict( |
| 74 |
resource_group=dict( |
| 75 |
type='str', |
| 76 |
required=True |
| 77 |
), |
| 78 |
recovery_vault_name=dict( |
| 79 |
type='str', |
| 80 |
required=True |
| 81 |
), |
| 82 |
resource_id=dict( |
| 83 |
type='str', |
| 84 |
required=True |
| 85 |
) |
| 86 |
) |
| 87 |
|
| 88 |
self.resource_group = None |
| 89 |
self.recovery_vault_name = None |
| 90 |
self.resource_id = None |
| 91 |
|
| 92 |
self.results = dict(changed=False) |
| 93 |
self.mgmt_client = None |
| 94 |
self.url = None |
| 95 |
self.status_code = [200, 201, 202, 204] |
| 96 |
self.to_do = Actions.NoAction |
| 97 |
|
| 98 |
self.body = {} |
| 99 |
self.query_parameters = {} |
| 100 |
self.query_parameters['api-version'] = '2019-05-13' |
| 101 |
self.header_parameters = {} |
| 102 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 103 |
|
| 104 |
super(BackupAzureVMInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 105 |
supports_check_mode=True, |
| 106 |
supports_tags=True) |
| 107 |
|
| 108 |
def get_url(self): |
| 109 |
return '/subscriptions' + '/' + self.subscription_id \ |
| 110 |
+ '/resourceGroups' + '/' + self.resource_group + '/providers' \ |
| 111 |
+ '/Microsoft.RecoveryServices' + '/vaults' + '/' \ |
| 112 |
+ self.recovery_vault_name \ |
| 113 |
+ '/backupFabrics/Azure/protectionContainers/' \ |
| 114 |
+ 'iaasvmcontainer;iaasvmcontainerv2;' + self.parse_resource_to_dict(self.resource_id)['resource_group'] \ |
| 115 |
+ ';' + self.parse_resource_to_dict(self.resource_id)['name'] + '/protectedItems/' \ |
| 116 |
+ 'vm;iaasvmcontainerv2;' + self.parse_resource_to_dict(self.resource_id)['resource_group'] + ';' \ |
| 117 |
+ self.parse_resource_to_dict(self.resource_id)['name'] |
| 118 |
|
| 119 |
def exec_module(self, **kwargs): |
| 120 |
for key in list(self.module_arg_spec.keys()): |
| 121 |
if hasattr(self, key): |
| 122 |
setattr(self, key, kwargs[key]) |
| 123 |
elif kwargs[key] is not None: |
| 124 |
self.body[key] = kwargs[key] |
| 125 |
|
| 126 |
self.inflate_parameters(self.module_arg_spec, self.body, 0) |
| 127 |
self.url = self.get_url() |
| 128 |
|
| 129 |
response = None |
| 130 |
|
| 131 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 132 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 133 |
|
| 134 |
response = self.get_recovery_point_info() |
| 135 |
self.results['response'] = response |
| 136 |
return self.results |
| 137 |
|
| 138 |
def get_recovery_point_info(self): |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
try: |
| 143 |
response = self.mgmt_client.query( |
| 144 |
self.url, |
| 145 |
'GET', |
| 146 |
self.query_parameters, |
| 147 |
self.header_parameters, |
| 148 |
None, |
| 149 |
self.status_code, |
| 150 |
600, |
| 151 |
30, |
| 152 |
) |
| 153 |
except Exception as e: |
| 154 |
self.log('Error in fetching recovery point.') |
| 155 |
self.fail('Error in fetching recovery point {0}'.format(str(e))) |
| 156 |
|
| 157 |
if hasattr(response, 'body'): |
| 158 |
response = json.loads(response.body()) |
| 159 |
elif hasattr(response, 'context'): |
| 160 |
response = response.context['deserialized_data'] |
| 161 |
else: |
| 162 |
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response)) |
| 163 |
|
| 164 |
return response |
| 165 |
|
| 166 |
|
| 167 |
def main(): |
| 168 |
BackupAzureVMInfo() |
| 169 |
|
| 170 |
|
| 171 |
if __name__ == '__main__': |
| 172 |
main() |