Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_sqlmidblongtermretentionpolicy_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_sqlmidblongtermretentionpolicy_info
14 version_added: "2.4.0"
15 short_description: Get Azure SQL managed instance facts
16 description:
17 - Get facts of Azure SQL managed instance facts.
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 short term retention policy.
38 type: str
39 choices:
40 - default
41
42 extends_documentation_fragment:
43 - azure.azcollection.azure
44
45 author:
46 - xuzhang3 (@xuzhang3)
47 - Fred-sun (@Fred-sun)
48 '''
49
50 EXAMPLES = '''
51 - name: Get SQL managed instance long term retention policy by name
52 azure_rm_sqlmidblongtermretentionpolicy_info:
53 resource_group: testrg
54 managed_instance_name: testinstancename
55 database_name: newdatabase
56 policy_name: default
57 '''
58
59 RETURN = '''
60 short_term_retention_policy:
61 description:
62 - A dictionary containing facts for SQL Managed Instance Short Term Retention Policies.
63 returned: always
64 type: complex
65 contains:
66 id:
67 description:
68 - Resource ID.
69 returned: always
70 type: str
71 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Sql/
72 managedInstances/fredsqlmi/databases/newdatabase/backupShortTermRetentionPolicies/default"
73 database_name:
74 description:
75 - SQL managed instance database name.
76 returned: always
77 type: str
78 sample: newdatabase
79 policy_name:
80 description:
81 - SQL managed instance short term retentioni policy name.
82 returned: always
83 type: str
84 sample: default
85 managed_instance_name:
86 description:
87 - SQL managed instance name.
88 returned: always
89 type: str
90 sample: testmanagedinstance
91 type:
92 description:
93 - The SQL managed instance short term retention policy type.
94 type: str
95 returned: always
96 sample: "Microsoft.Sql/managedInstances/databases/backupShortTermRetentionPolicies"
97 resource_group:
98 description:
99 - The resource relate resource group.
100 type: str
101 returned: always
102 sample: testRG
103 week_of_year:
104 description:
105 - The week of year to take the yearly backup in an ISO 8601 format.
106 type: int
107 sample: 7
108 returned: always
109 weekly_retention:
110 description:
111 - The weekly retention policy for an LTR backup in an ISO 8601 format.
112 type: str
113 sample: P13W
114 returned: always
115 monthly_retention:
116 description:
117 - The monthly retention policy for an LTR backup in an ISO 8601 format.
118 type: str
119 sample: P3M
120 returned: always
121 yearly_retention:
122 description:
123 - The yearly retention policy for an LTR backup in an ISO 8601 format.
124 type: str
125 sample: P6Y
126 returned: always
127 '''
128
129 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
130
131 try:
132 from azure.core.exceptions import HttpResponseError
133 except ImportError:
134 # This is handled in azure_rm_common
135 pass
136
137
138 class AzureRMSqMILongTermRetentionPolicyInfo(AzureRMModuleBase):
139 def __init__(self):
140 # define user inputs into argument
141 self.module_arg_spec = dict(
142 resource_group=dict(
143 type='str',
144 required=True,
145 ),
146 managed_instance_name=dict(
147 type='str',
148 required=True,
149 ),
150 database_name=dict(
151 type='str',
152 required=True,
153 ),
154 policy_name=dict(
155 type='str',
156 choices=['default']
157 ),
158 )
159 # store the results of the module operation
160 self.results = dict(
161 changed=False
162 )
163 self.resource_group = None
164 self.managed_instance_name = None
165 self.database_name = None
166 self.policy_name = None
167
168 super(AzureRMSqMILongTermRetentionPolicyInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
169
170 def exec_module(self, **kwargs):
171 for key in self.module_arg_spec:
172 setattr(self, key, kwargs[key])
173
174 if self.policy_name is not None:
175 self.results['long_term_retention_policy'] = self.get()
176 else:
177 self.results['long_term_retention_policy'] = self.list_by_database()
178 return self.results
179
180 def list_by_database(self):
181 response = None
182 try:
183 response = self.sql_client.managed_instance_long_term_retention_policies.list_by_database(resource_group_name=self.resource_group,
184 managed_instance_name=self.managed_instance_name,
185 database_name=self.database_name)
186 self.log("Response : {0}".format(response))
187 except HttpResponseError:
188 self.log('Could not get facts for SQL managed instance short term retention policyes.')
189
190 return [self.format_item(item) for item in response] if response is not None else []
191
192 def get(self):
193 response = None
194 try:
195 response = self.sql_client.managed_instance_long_term_retention_policies.get(resource_group_name=self.resource_group,
196 managed_instance_name=self.managed_instance_name,
197 database_name=self.database_name,
198 policy_name=self.policy_name)
199 self.log("Response : {0}".format(response))
200 except HttpResponseError as ec:
201 self.fail(ec)
202 self.log('Could not get facts for SQL managed instance short term retention policyes.')
203
204 return [self.format_item(response)] if response is not None else None
205
206 def format_item(self, item):
207 d = item.as_dict()
208 d = {
209 'resource_group': self.resource_group,
210 'managed_instance_name': self.managed_instance_name,
211 'database_name': self.database_name,
212 'id': d.get('id', None),
213 'name': d.get('name', None),
214 'type': d.get('type', None),
215 "monthly_retention": d.get("monthly_retention"),
216 "week_of_year": d.get("week_of_year"),
217 "weekly_retention": d.get("weekly_retention"),
218 "yearly_retention": d.get("yearly_retention")
219 }
220 return d
221
222
223 def main():
224 AzureRMSqMILongTermRetentionPolicyInfo()
225
226
227 if __name__ == '__main__':
228 main()