Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Yunge Zhu, <[email protected]>
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_applicationsecuritygroup_info
14 version_added: "0.1.2"
15 short_description: Get Azure Application Security Group facts
16 description:
17 - Get facts of Azure Application Security Group.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 name:
25 description:
26 - The name of the application security group.
27 type: str
28 tags:
29 description:
30 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
31 type: list
32 elements: str
33
34 extends_documentation_fragment:
35 - azure.azcollection.azure
36
37 author:
38 - Yunge Zhu (@yungezz)
39
40 '''
41
42 EXAMPLES = '''
43 - name: List application security groups in specific resource group
44 azure_rm_applicationsecuritygroup_info:
45 resource_group: myResourceGroup
46
47 - name: List application security groups in specific subscription
48 azure_rm_applicationsecuritygroup_info:
49
50 - name: Get application security group by name
51 azure_rm_applicationsecuritygroup_info:
52 resource_group: myResourceGroup
53 name: myApplicationSecurityGroup
54 tags:
55 - foo
56 '''
57
58 RETURN = '''
59 applicationsecuritygroups:
60 description:
61 - List of application security groups.
62 returned: always
63 type: complex
64 contains:
65 id:
66 description: Id of the application security group.
67 type: str
68 returned: always
69 sample:
70 "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/applicationSecurityGroups/MyAsg"
71 location:
72 description:
73 - Location of the application security group.
74 type: str
75 returned: always
76 sample: eastus
77 name:
78 description:
79 - Name of the resource.
80 type: str
81 returned: always
82 sample: myAsg
83 provisioning_state:
84 description:
85 - Provisioning state of application security group.
86 type: str
87 returned: always
88 sample: Succeeded
89 '''
90
91 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
92
93 try:
94 from azure.core.exceptions import ResourceNotFoundError
95 except ImportError:
96 # This is handled in azure_rm_common
97 pass
98
99
100 def applicationsecuritygroup_to_dict(asg):
101 return dict(
102 id=asg.id,
103 location=asg.location,
104 name=asg.name,
105 tags=asg.tags,
106 provisioning_state=asg.provisioning_state
107 )
108
109
110 class AzureRMApplicationSecurityGroupInfo(AzureRMModuleBase):
111
112 def __init__(self):
113 self.module_arg_spec = dict(
114 resource_group=dict(
115 type='str'
116 ),
117 name=dict(
118 type='str'
119 ),
120 tags=dict(type='list', elements='str')
121 )
122
123 self.resource_group = None
124 self.name = None
125 self.tags = None
126
127 self.results = dict(changed=False)
128
129 super(AzureRMApplicationSecurityGroupInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
130 supports_check_mode=True,
131 supports_tags=False,
132 facts_module=True)
133
134 def exec_module(self, **kwargs):
135 """Main module execution method"""
136
137 for key in list(self.module_arg_spec.keys()) + ['tags']:
138 if hasattr(self, key):
139 setattr(self, key, kwargs[key])
140
141 if self.name:
142 if self.resource_group:
143 self.results['applicationsecuritygroups'] = self.get()
144 else:
145 self.fail("resource_group is required when filtering by name")
146 elif self.resource_group:
147 self.results['applicationsecuritygroups'] = self.list_by_resource_group()
148 else:
149 self.results['applicationsecuritygroups'] = self.list_all()
150
151 return self.results
152
153 def get(self):
154 '''
155 Gets the properties of the specified Application Security Group.
156
157 :return: deserialized Application Security Group instance state dictionary
158 '''
159 self.log("Get the Application Security Group instance {0}".format(self.name))
160
161 results = []
162 try:
163 response = self.network_client.application_security_groups.get(resource_group_name=self.resource_group,
164 application_security_group_name=self.name)
165 self.log("Response : {0}".format(response))
166
167 if response and self.has_tags(response.tags, self.tags):
168 results.append(applicationsecuritygroup_to_dict(response))
169 except ResourceNotFoundError as e:
170 self.fail('Did not find the Application Security Group instance.')
171 return results
172
173 def list_by_resource_group(self):
174 '''
175 Lists the properties of Application Security Groups in specific resource group.
176
177 :return: deserialized Application Security Group instance state dictionary
178 '''
179 self.log("Get the Application Security Groups in resource group {0}".format(self.resource_group))
180
181 results = []
182 try:
183 response = list(self.network_client.application_security_groups.list(resource_group_name=self.resource_group))
184 self.log("Response : {0}".format(response))
185
186 if response:
187 for item in response:
188 if self.has_tags(item.tags, self.tags):
189 results.append(applicationsecuritygroup_to_dict(item))
190 except ResourceNotFoundError as e:
191 self.log('Did not find the Application Security Group instance.')
192 return results
193
194 def list_all(self):
195 '''
196 Lists the properties of Application Security Groups in specific subscription.
197
198 :return: deserialized Application Security Group instance state dictionary
199 '''
200 self.log("Get the Application Security Groups in current subscription")
201
202 results = []
203 try:
204 response = list(self.network_client.application_security_groups.list_all())
205 self.log("Response : {0}".format(response))
206
207 if response:
208 for item in response:
209 if self.has_tags(item.tags, self.tags):
210 results.append(applicationsecuritygroup_to_dict(item))
211 except ResourceNotFoundError as e:
212 self.log('Did not find the Application Security Group instance.')
213 return results
214
215
216 def main():
217 """Main execution"""
218 AzureRMApplicationSecurityGroupInfo()
219
220
221 if __name__ == '__main__':
222 main()