Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_postgresqlflexibledatabase_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_postgresqlflexibledatabase_info
14 version_added: "2.2.0"
15 short_description: Get Azure PostgreSQL Flexible Database facts
16 description:
17 - Get facts of PostgreSQL Flexible Database.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal.
23 type: str
24 required: True
25 server_name:
26 description:
27 - The name of the post gresql server.
28 type: str
29 required: True
30 name:
31 description:
32 - The name of the post gresql database.
33 type: str
34
35 extends_documentation_fragment:
36 - azure.azcollection.azure
37
38 author:
39 - xuzhang3 (@xuzhang3)
40 - Fred-sun (@Fred-sun)
41
42 '''
43
44 EXAMPLES = '''
45 - name: List instance of PostgreSQL Flexible Database by server name
46 azure_rm_postgresqlflexibledatabase_info:
47 resource_group: myResourceGroup
48 server_name: server_name
49
50 - name: Get instances of PostgreSQL Flexible Database
51 azure_rm_postgresqlflexibledatabase_info:
52 resource_group: myResourceGroup
53 server_name: server_name
54 name: database_name
55 '''
56
57 RETURN = '''
58 database:
59 description:
60 - A list of dictionaries containing facts for PostgreSQL Flexible Database.
61 returned: always
62 type: complex
63 contains:
64 id:
65 description:
66 - Resource ID of the postgresql flexible database.
67 returned: always
68 type: str
69 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/postfle9/databases/freddatabase"
70 name:
71 description:
72 - Resource name.
73 returned: always
74 type: str
75 sample: freddatabase
76 charset:
77 description:
78 - The charset of the database.
79 returned: always
80 type: str
81 sample: UTF-8
82 collation:
83 description:
84 - The collation of the database.
85 returned: always
86 type: str
87 sample: en_US.utf8
88 type:
89 description:
90 - The type of the resource.
91 returned: always
92 type: str
93 sample: Microsoft.DBforPostgreSQL/flexibleServers/databases
94 system_data:
95 description:
96 - The system metadata relating to this resource.
97 type: complex
98 returned: always
99 contains:
100 created_by:
101 description:
102 - The identity that created the resource.
103 type: str
104 returned: always
105 sample: null
106 created_by_type:
107 description:
108 - The type of identity that created the resource.
109 returned: always
110 type: str
111 sample: null
112 created_at:
113 description:
114 - The timestamp of resource creation (UTC).
115 returned: always
116 sample: null
117 type: str
118 last_modified_by:
119 description:
120 - The identity that last modified the resource.
121 type: str
122 returned: always
123 sample: null
124 last_modified_by_type:
125 description:
126 - The type of identity that last modified the resource.
127 returned: always
128 sample: null
129 type: str
130 last_modified_at:
131 description:
132 - The timestamp of resource last modification (UTC).
133 returned: always
134 sample: null
135 type: str
136 '''
137
138
139 try:
140 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
141 from azure.core.exceptions import ResourceNotFoundError
142 except ImportError:
143 # This is handled in azure_rm_common
144 pass
145
146
147 class AzureRMPostgreSqlFlexibleDatabaseInfo(AzureRMModuleBase):
148 def __init__(self):
149 # define user inputs into argument
150 self.module_arg_spec = dict(
151 resource_group=dict(
152 type='str',
153 required=True
154 ),
155 server_name=dict(
156 type='str',
157 required=True
158 ),
159 name=dict(
160 type='str'
161 ),
162 )
163 # store the results of the module operation
164 self.results = dict(
165 changed=False
166 )
167 self.resource_group = None
168 self.name = None
169 self.server_name = None
170 super(AzureRMPostgreSqlFlexibleDatabaseInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
171
172 def exec_module(self, **kwargs):
173 for key in self.module_arg_spec:
174 setattr(self, key, kwargs[key])
175
176 if self.name is not None:
177 self.results['databases'] = self.get()
178 else:
179 self.results['databases'] = self.list_all()
180 return self.results
181
182 def get(self):
183 response = None
184 results = []
185 try:
186 response = self.postgresql_flexible_client.databases.get(resource_group_name=self.resource_group,
187 server_name=self.server_name,
188 database_name=self.name)
189 self.log("Response : {0}".format(response))
190 except ResourceNotFoundError:
191 self.log('Could not get facts for PostgreSQL Flexible Server.')
192
193 if response is not None:
194 results.append(self.format_item(response))
195
196 return results
197
198 def list_all(self):
199 response = None
200 results = []
201 try:
202 response = self.postgresql_flexible_client.databases.list_by_server(resource_group_name=self.resource_group,
203 server_name=self.server_name)
204 self.log("Response : {0}".format(response))
205 except Exception as ec:
206 self.log('Could not get facts for PostgreSQL Flexible Servers.')
207
208 if response is not None:
209 for item in response:
210 results.append(self.format_item(item))
211
212 return results
213
214 def format_item(self, item):
215 result = dict(
216 id=item.id,
217 name=item.name,
218 system_data=dict(),
219 type=item.type,
220 charset=item.charset,
221 collation=item.collation
222 )
223 if item.system_data is not None:
224 result['system_data']['created_by'] = item.system_data.created_by
225 result['system_data']['created_by_type'] = item.system_data.created_by_type
226 result['system_data']['created_at'] = item.system_data.created_at
227 result['system_data']['last_modified_by'] = item.system_data.last_modified_by
228 result['system_data']['last_modified_by_type'] = item.system_data.last_modified_by_type
229 result['system_data']['last_modified_at'] = item.system_data.last_modified_at
230
231 return result
232
233
234 def main():
235 AzureRMPostgreSqlFlexibleDatabaseInfo()
236
237
238 if __name__ == '__main__':
239 main()