Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2025 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_diskaccess_info
14 version_added: '3.3.0'
15 short_description: Show the details for the disk access
16 description:
17 - Get or list the details for the disk access.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 name:
25 description:
26 - The disk access name.
27 type: str
28 tags:
29 description:
30 - The tags of the disk access.
31 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
32 type: list
33 elements: str
34
35 extends_documentation_fragment:
36 - azure.azcollection.azure
37
38 author:
39 - xuzhang3 (@xuzhang3)
40 - Fred Sun (@Fred-sun)
41 '''
42
43 EXAMPLES = '''
44 - name: Get disk access by name
45 azure_rm_diskaccess_info:
46 resource_group: myRG
47 name: testaccess
48
49 - name: List disk access by resource group
50 azure_rm_diskaccess_info:
51 resource_group: myRG
52
53 - name: List disk access in same subscription and filter by tags
54 azure_rm_diskaccess_info:
55 resource_group: myRG
56 tags:
57 - key1
58 - key2
59 '''
60
61 RETURN = '''
62 disk_accesses:
63 description:
64 - Details for the disk access.
65 returned: always
66 type: complex
67 contains:
68 id:
69 description:
70 - Resource ID.
71 type: str
72 returned: always
73 sample: "/subscriptions/xxxx/resourceGroups/myRG/providers/Microsoft.Compute/diskAccesses/diskacc"
74 resource_group:
75 description:
76 - Resource group name.
77 type: str
78 returned: always
79 sample: myRG
80 name:
81 description:
82 - Resource name.
83 type: str
84 returned: always
85 sample: testaccess
86 type:
87 description:
88 - Resource Type.
89 type: str
90 returned: always
91 sample: "Microsoft.Compute/diskAccesses"
92 tags:
93 description:
94 - The tags of the disk access.
95 type: dict
96 returned: always
97 sample: {key1: value1, key2: value2}
98 provisioning_state:
99 description:
100 - The disk access resource provisioning state.
101 type: str
102 returned: always
103 sample: Successed
104 extended_location:
105 description:
106 - The extended location where the disk access will be created.
107 type: dict
108 returned: always
109 sample: {"name": "portland", "type": "EdgeZone"}
110 '''
111
112 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
113
114 try:
115 from azure.core.exceptions import ResourceNotFoundError
116 except ImportError:
117 pass
118
119
120 class AzureRMDiskAccessInfo(AzureRMModuleBase):
121 def __init__(self):
122 # define user inputs into argument
123 self.module_arg_spec = dict(
124 resource_group=dict(
125 type='str',
126 ),
127 name=dict(
128 type='str'
129 ),
130 tags=dict(
131 type='list',
132 elements='str',
133 )
134 )
135 # store the results of the module operation
136 self.results = dict()
137 self.resource_group = None
138 self.name = None
139 self.tags = None
140
141 super(AzureRMDiskAccessInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
142
143 def exec_module(self, **kwargs):
144
145 for key in list(self.module_arg_spec):
146 setattr(self, key, kwargs[key])
147
148 if self.name is not None:
149 if self.resource_group is not None:
150 disk_accesses = self.get_diskaccess()
151 else:
152 self.fail("Missing configuration, When configure the nae, you must configure the resource group name")
153 elif self.resource_group is not None:
154 disk_accesses = self.list_by_resourcegroup()
155 else:
156 disk_accesses = self.list_all()
157
158 self.results['disk_accesses'] = [self.to_dict(x) for x in disk_accesses if self.has_tags(x.tags, self.tags)]
159 return self.results
160
161 def get_diskaccess(self):
162 try:
163 return [self.disk_client.disk_accesses.get(self.resource_group, self.name)]
164 except ResourceNotFoundError:
165 return []
166
167 def list_by_resourcegroup(self):
168 try:
169 return self.disk_client.disk_accesses.list_by_resource_group(self.resource_group)
170 except Exception:
171 pass
172
173 def list_all(self):
174 result = []
175 try:
176 return self.disk_client.disk_accesses.list()
177 except Exception as ec:
178 pass
179
180 def to_dict(self, item):
181 if item is None:
182 return None
183 disk_access = dict(
184 resource_group=item.id.split('/')[4],
185 name=item.name,
186 id=item.id,
187 type=item.type,
188 location=item.location,
189 provisioning_state=item.provisioning_state,
190 extended_location=dict(),
191 tags=item.tags
192 )
193 if item.extended_location is not None:
194 disk_access['extended_location']['name'] = item.extended_location.name
195 disk_access['extended_location']['type'] = item.extended_location.type
196 return disk_access
197
198
199 def main():
200 AzureRMDiskAccessInfo()
201
202
203 if __name__ == '__main__':
204 main()