| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_vmbackuppolicy_info |
| 13 |
version_added: '1.1.0' |
| 14 |
short_description: Fetch Backup Policy Details |
| 15 |
description: |
| 16 |
- Get Backup Policy Details. |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Policy Name. |
| 21 |
required: true |
| 22 |
type: str |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- The name of the resource group. |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
vault_name: |
| 29 |
description: |
| 30 |
- Recovery Service Vault Name. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
extends_documentation_fragment: |
| 34 |
- azure.azcollection.azure |
| 35 |
- azure.azcollection.azure_tags |
| 36 |
author: |
| 37 |
- Suyeb Ansari (@suyeb786) |
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = ''' |
| 41 |
- name: Get backvm policy facts |
| 42 |
azure_rm_backvmuppolicy_info: |
| 43 |
name: 'myBackupPolicy' |
| 44 |
vault_name: 'myVault' |
| 45 |
resource_group: 'myResourceGroup' |
| 46 |
''' |
| 47 |
|
| 48 |
RETURN = ''' |
| 49 |
response: |
| 50 |
description: |
| 51 |
- The response about the current state of the backup policy. |
| 52 |
returned: always |
| 53 |
type: complex |
| 54 |
contains: |
| 55 |
id: |
| 56 |
description: |
| 57 |
- Resource ID. |
| 58 |
returned: always |
| 59 |
type: str |
| 60 |
sample: "/subscriptions/xxxxxxx/resourceGroups/resourcegroup_name/ \ |
| 61 |
providers/Microsoft.RecoveryServices/vaults/myVault/backupPolicies/myBackup" |
| 62 |
name: |
| 63 |
description: |
| 64 |
- Backup Policy Name. |
| 65 |
returned: always |
| 66 |
type: str |
| 67 |
sample: "myBackup" |
| 68 |
properties: |
| 69 |
description: |
| 70 |
- The backup policy properties. |
| 71 |
returned: always |
| 72 |
type: dict |
| 73 |
sample: { |
| 74 |
"backupManagementType": "AzureIaasVM", |
| 75 |
"schedulePolicy": { |
| 76 |
"schedulePolicyType": "SimpleSchedulePolicy", |
| 77 |
"scheduleRunFrequency": "Weekly", |
| 78 |
"scheduleRunDays": [ |
| 79 |
"Monday", |
| 80 |
"Wednesday", |
| 81 |
"Thursday" |
| 82 |
], |
| 83 |
"scheduleRunTimes": [ |
| 84 |
"2018-01-24T10:00:00Z" |
| 85 |
], |
| 86 |
"scheduleWeeklyFrequency": 0 |
| 87 |
}, |
| 88 |
"retentionPolicy": { |
| 89 |
"retentionPolicyType": "LongTermRetentionPolicy", |
| 90 |
"weeklySchedule": { |
| 91 |
"daysOfTheWeek": [ |
| 92 |
"Monday", |
| 93 |
"Wednesday", |
| 94 |
"Thursday" |
| 95 |
], |
| 96 |
"retentionTimes": [ |
| 97 |
"2018-01-24T10:00:00Z" |
| 98 |
], |
| 99 |
"retentionDuration": { |
| 100 |
"count": 1, |
| 101 |
"durationType": "Weeks" |
| 102 |
} |
| 103 |
}, |
| 104 |
"monthlySchedule": { |
| 105 |
"retentionScheduleFormatType": "Weekly", |
| 106 |
"retentionScheduleWeekly": { |
| 107 |
"daysOfTheWeek": [ |
| 108 |
"Wednesday", |
| 109 |
"Thursday" |
| 110 |
], |
| 111 |
"weeksOfTheMonth": [ |
| 112 |
"First", |
| 113 |
"Third" |
| 114 |
] |
| 115 |
}, |
| 116 |
"retentionTimes": [ |
| 117 |
"2018-01-24T10:00:00Z" |
| 118 |
], |
| 119 |
"retentionDuration": { |
| 120 |
"count": 2, |
| 121 |
"durationType": "Months" |
| 122 |
} |
| 123 |
}, |
| 124 |
"yearlySchedule": { |
| 125 |
"retentionScheduleFormatType": "Weekly", |
| 126 |
"monthsOfYear": [ |
| 127 |
"February", |
| 128 |
"November" |
| 129 |
], |
| 130 |
"retentionScheduleWeekly": { |
| 131 |
"daysOfTheWeek": [ |
| 132 |
"Monday", |
| 133 |
"Thursday" |
| 134 |
], |
| 135 |
"weeksOfTheMonth": [ |
| 136 |
"Fourth" |
| 137 |
] |
| 138 |
}, |
| 139 |
"retentionTimes": [ |
| 140 |
"2018-01-24T10:00:00Z" |
| 141 |
], |
| 142 |
"retentionDuration": { |
| 143 |
"count": 4, |
| 144 |
"durationType": "Years" |
| 145 |
} |
| 146 |
} |
| 147 |
}, |
| 148 |
"timeZone": "Pacific Standard Time", |
| 149 |
"protectedItemsCount": 0 |
| 150 |
} |
| 151 |
type: |
| 152 |
description: |
| 153 |
- Resource type. |
| 154 |
returned: always |
| 155 |
type: str |
| 156 |
sample: "Microsoft.RecoveryServices/vaults/backupPolicies" |
| 157 |
''' |
| 158 |
|
| 159 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 160 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 161 |
import json |
| 162 |
|
| 163 |
|
| 164 |
class BackupPolicyVMInfo(AzureRMModuleBaseExt): |
| 165 |
def __init__(self): |
| 166 |
self.module_arg_spec = dict( |
| 167 |
resource_group=dict( |
| 168 |
type='str', |
| 169 |
required=True |
| 170 |
), |
| 171 |
name=dict( |
| 172 |
type='str', |
| 173 |
required=True |
| 174 |
), |
| 175 |
vault_name=dict( |
| 176 |
type='str', |
| 177 |
required=True |
| 178 |
) |
| 179 |
) |
| 180 |
|
| 181 |
self.resource_group = None |
| 182 |
self.name = None |
| 183 |
self.vault_name = None |
| 184 |
|
| 185 |
self.results = dict(changed=False) |
| 186 |
self.mgmt_client = None |
| 187 |
self.url = None |
| 188 |
self.status_code = [200, 202] |
| 189 |
|
| 190 |
self.query_parameters = {} |
| 191 |
self.query_parameters['api-version'] = '2019-05-13' |
| 192 |
self.header_parameters = {} |
| 193 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 194 |
|
| 195 |
super(BackupPolicyVMInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 196 |
supports_check_mode=True, |
| 197 |
supports_tags=True |
| 198 |
) |
| 199 |
|
| 200 |
def get_url(self): |
| 201 |
return '/subscriptions/' \ |
| 202 |
+ self.subscription_id \ |
| 203 |
+ '/resourceGroups/' \ |
| 204 |
+ self.resource_group \ |
| 205 |
+ '/providers/Microsoft.RecoveryServices' \ |
| 206 |
+ '/vaults' + '/' \ |
| 207 |
+ self.vault_name + '/' \ |
| 208 |
+ "backupPolicies/" \ |
| 209 |
+ self.name |
| 210 |
|
| 211 |
def exec_module(self, **kwargs): |
| 212 |
for key in list(self.module_arg_spec.keys()): |
| 213 |
if hasattr(self, key): |
| 214 |
setattr(self, key, kwargs[key]) |
| 215 |
|
| 216 |
self.url = self.get_url() |
| 217 |
|
| 218 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 219 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 220 |
|
| 221 |
response = self.get_resource() |
| 222 |
changed = False |
| 223 |
self.results['response'] = response |
| 224 |
self.results['changed'] = changed |
| 225 |
|
| 226 |
return self.results |
| 227 |
|
| 228 |
def get_resource(self): |
| 229 |
|
| 230 |
try: |
| 231 |
response = self.mgmt_client.query( |
| 232 |
self.url, |
| 233 |
'GET', |
| 234 |
self.query_parameters, |
| 235 |
self.header_parameters, |
| 236 |
None, |
| 237 |
self.status_code, |
| 238 |
600, |
| 239 |
30, |
| 240 |
) |
| 241 |
found = True |
| 242 |
except Exception as e: |
| 243 |
self.log('Backup policy does not exist.') |
| 244 |
self.fail('Error in fetching VM Backup Policy {0}'.format(str(e))) |
| 245 |
if hasattr(response, 'body'): |
| 246 |
response = json.loads(response.body()) |
| 247 |
elif hasattr(response, 'context'): |
| 248 |
response = response.context['deserialized_data'] |
| 249 |
else: |
| 250 |
self.fail("Create or Updating fail, no match message return, return info as {0}".format(response)) |
| 251 |
|
| 252 |
return response |
| 253 |
|
| 254 |
|
| 255 |
def main(): |
| 256 |
BackupPolicyVMInfo() |
| 257 |
|
| 258 |
|
| 259 |
if __name__ == '__main__': |
| 260 |
main() |