| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_sqlmidblongtermretentionpolicy |
| 14 |
version_added: "2.4.0" |
| 15 |
short_description: Manage Azure SQL Managed Instance long-term backup retention |
| 16 |
description: |
| 17 |
- Manage Azure SQL Managed Instance long-term backup retention. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group that contains the resource. |
| 23 |
type: str |
| 24 |
required: true |
| 25 |
managed_instance_name: |
| 26 |
description: |
| 27 |
- The name of the SQL managed instance. |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
database_name: |
| 31 |
description: |
| 32 |
- The name of the SQL managed instance database. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
policy_name: |
| 36 |
description: |
| 37 |
- The name of the SQL managed instance long term retention policy. |
| 38 |
type: str |
| 39 |
required: true |
| 40 |
choices: |
| 41 |
- default |
| 42 |
monthly_retention: |
| 43 |
description: |
| 44 |
- The monthly retention policy for an LTR backup in an ISO 8601 format. |
| 45 |
type: str |
| 46 |
yearly_retention: |
| 47 |
description: |
| 48 |
- The yearly retention policy for an LTR backup in an ISO 8601 format. |
| 49 |
type: str |
| 50 |
weekly_retention: |
| 51 |
description: |
| 52 |
- The weekly retention policy for an LTR backup in an ISO 8601 format. |
| 53 |
type: str |
| 54 |
week_of_year: |
| 55 |
description: |
| 56 |
- The week of year to take the yearly backup in an ISO 8601 format. |
| 57 |
type: int |
| 58 |
|
| 59 |
extends_documentation_fragment: |
| 60 |
- azure.azcollection.azure |
| 61 |
|
| 62 |
author: |
| 63 |
- xuzhang3 (@xuzhang3) |
| 64 |
- Fred-sun (@Fred-sun) |
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = ''' |
| 68 |
- name: Update SQL managed instance long term retention policy's retention_days |
| 69 |
azure_rm_sqlmidblongtermretentionpolicy: |
| 70 |
resource_group: testrg |
| 71 |
managed_instance_name: testinstancename |
| 72 |
database_name: newdatabase |
| 73 |
policy_name: default |
| 74 |
monthly_retention: P3M |
| 75 |
week_of_year: 17 |
| 76 |
weekly_retention: P13W |
| 77 |
yearly_retention: P6Y |
| 78 |
''' |
| 79 |
|
| 80 |
RETURN = ''' |
| 81 |
long_term_retention_policy: |
| 82 |
description: |
| 83 |
- A dictionary containing facts for SQL Managed Instance Short Term Retention Policies. |
| 84 |
returned: always |
| 85 |
type: complex |
| 86 |
contains: |
| 87 |
id: |
| 88 |
description: |
| 89 |
- Resource ID. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Sql/ |
| 93 |
managedInstances/fredsqlmi/databases/newdatabase/backupShortTermRetentionPolicies/default" |
| 94 |
database_name: |
| 95 |
description: |
| 96 |
- SQL managed instance database name. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: newdatabase |
| 100 |
policy_name: |
| 101 |
description: |
| 102 |
- SQL managed instance long term retentioni policy name. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: default |
| 106 |
managed_instance_name: |
| 107 |
description: |
| 108 |
- SQL managed instance name. |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: testmanagedinstance |
| 112 |
type: |
| 113 |
description: |
| 114 |
- The SQL managed instance type. |
| 115 |
type: str |
| 116 |
returned: always |
| 117 |
sample: "Microsoft.Sql/managedInstances" |
| 118 |
resource_group: |
| 119 |
description: |
| 120 |
- The resource relate resource group. |
| 121 |
type: str |
| 122 |
returned: always |
| 123 |
sample: testRG |
| 124 |
week_of_year: |
| 125 |
description: |
| 126 |
- The week of year to take the yearly backup in an ISO 8601 format. |
| 127 |
type: int |
| 128 |
sample: 7 |
| 129 |
returned: always |
| 130 |
weekly_retention: |
| 131 |
description: |
| 132 |
- The weekly retention policy for an LTR backup in an ISO 8601 format. |
| 133 |
type: str |
| 134 |
sample: P13W |
| 135 |
returned: always |
| 136 |
monthly_retention: |
| 137 |
description: |
| 138 |
- The monthly retention policy for an LTR backup in an ISO 8601 format. |
| 139 |
type: str |
| 140 |
sample: P3M |
| 141 |
returned: always |
| 142 |
yearly_retention: |
| 143 |
description: |
| 144 |
- The yearly retention policy for an LTR backup in an ISO 8601 format. |
| 145 |
type: str |
| 146 |
sample: P6Y |
| 147 |
returned: always |
| 148 |
''' |
| 149 |
|
| 150 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 151 |
|
| 152 |
try: |
| 153 |
from azure.core.exceptions import HttpResponseError |
| 154 |
except ImportError: |
| 155 |
|
| 156 |
pass |
| 157 |
|
| 158 |
|
| 159 |
class AzureRMSqMILongTermRetentionPolicy(AzureRMModuleBase): |
| 160 |
def __init__(self): |
| 161 |
|
| 162 |
self.module_arg_spec = dict( |
| 163 |
resource_group=dict( |
| 164 |
type='str', |
| 165 |
required=True, |
| 166 |
), |
| 167 |
managed_instance_name=dict( |
| 168 |
type='str', |
| 169 |
required=True, |
| 170 |
), |
| 171 |
database_name=dict( |
| 172 |
type='str', |
| 173 |
required=True, |
| 174 |
), |
| 175 |
policy_name=dict( |
| 176 |
type='str', |
| 177 |
required=True, |
| 178 |
choices=['default'] |
| 179 |
), |
| 180 |
weekly_retention=dict( |
| 181 |
type='str', |
| 182 |
), |
| 183 |
monthly_retention=dict( |
| 184 |
type='str' |
| 185 |
), |
| 186 |
yearly_retention=dict( |
| 187 |
type='str' |
| 188 |
), |
| 189 |
week_of_year=dict( |
| 190 |
type='int' |
| 191 |
) |
| 192 |
) |
| 193 |
|
| 194 |
self.parameters = dict() |
| 195 |
self.results = dict( |
| 196 |
changed=False, |
| 197 |
diff=[] |
| 198 |
) |
| 199 |
self.resource_group = None |
| 200 |
self.managed_instance_name = None |
| 201 |
self.database_name = None |
| 202 |
self.policy_name = None |
| 203 |
|
| 204 |
super(AzureRMSqMILongTermRetentionPolicy, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 205 |
|
| 206 |
def exec_module(self, **kwargs): |
| 207 |
for key in self.module_arg_spec: |
| 208 |
if hasattr(self, key): |
| 209 |
setattr(self, key, kwargs[key]) |
| 210 |
else: |
| 211 |
self.parameters[key] = kwargs.get(key) |
| 212 |
|
| 213 |
old_response = self.get() |
| 214 |
|
| 215 |
if old_response is not None: |
| 216 |
for key in self.parameters.keys(): |
| 217 |
if self.parameters[key] is not None and old_response[key] != self.parameters[key]: |
| 218 |
self.results['changed'] = True |
| 219 |
self.results['diff'].append(key) |
| 220 |
if self.results['changed'] and not self.check_mode: |
| 221 |
self.results['long_term_retention_policy'] = self.create_or_update_policy() |
| 222 |
else: |
| 223 |
self.results['changed'] = True |
| 224 |
if not self.check_mode: |
| 225 |
self.results['long_term_retention_policy'] = self.create_or_update_policy() |
| 226 |
return self.results |
| 227 |
|
| 228 |
def get(self): |
| 229 |
response = None |
| 230 |
try: |
| 231 |
response = self.sql_client.managed_instance_long_term_retention_policies.get(resource_group_name=self.resource_group, |
| 232 |
managed_instance_name=self.managed_instance_name, |
| 233 |
database_name=self.database_name, |
| 234 |
policy_name=self.policy_name) |
| 235 |
self.log("Response : {0}".format(response)) |
| 236 |
except HttpResponseError: |
| 237 |
self.log('Could not get facts for SQL managed instance long term retention policyes.') |
| 238 |
|
| 239 |
return self.format_item(response) if response is not None else None |
| 240 |
|
| 241 |
def create_or_update_policy(self): |
| 242 |
response = None |
| 243 |
try: |
| 244 |
response = self.sql_client.managed_instance_long_term_retention_policies.begin_create_or_update(resource_group_name=self.resource_group, |
| 245 |
managed_instance_name=self.managed_instance_name, |
| 246 |
database_name=self.database_name, |
| 247 |
policy_name=self.policy_name, |
| 248 |
parameters=self.parameters) |
| 249 |
self.log("Response : {0}".format(response)) |
| 250 |
except HttpResponseError as ec: |
| 251 |
self.fail('Could not create SQL managed instance long term retention policyes. Exception info as {0}'.format(ec)) |
| 252 |
|
| 253 |
return self.format_item(self.get_poller_result(response)) |
| 254 |
|
| 255 |
def format_item(self, item): |
| 256 |
d = item.as_dict() |
| 257 |
d = { |
| 258 |
'resource_group': self.resource_group, |
| 259 |
'managed_instance_name': self.managed_instance_name, |
| 260 |
'database_name': self.database_name, |
| 261 |
'id': d.get('id', None), |
| 262 |
'name': d.get('name', None), |
| 263 |
'type': d.get('type', None), |
| 264 |
"monthly_retention": d.get("monthly_retention"), |
| 265 |
"week_of_year": d.get("week_of_year"), |
| 266 |
"weekly_retention": d.get("weekly_retention"), |
| 267 |
"yearly_retention": d.get("yearly_retention") |
| 268 |
} |
| 269 |
return d |
| 270 |
|
| 271 |
|
| 272 |
def main(): |
| 273 |
AzureRMSqMILongTermRetentionPolicy() |
| 274 |
|
| 275 |
|
| 276 |
if __name__ == '__main__': |
| 277 |
main() |