Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024
4 # Hen Yaish <[email protected]>
5 #
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 DOCUMENTATION = '''
13 ---
14 module: azure_rm_imagesku_info
15
16 version_added: "2.4.0"
17
18 short_description: Get compute-related Image SKUs list
19
20 description:
21 - Get details for compute-related resource Image SKUs.
22
23 options:
24 location:
25 description:
26 - A region supported by current subscription.
27 type: str
28 offer:
29 description:
30 - The `Offer` refers to the specific product line of the operating system or software.
31 - The offer usually includes multiple versions or configurations of the product.
32 type: str
33 publisher:
34 description:
35 - The `Publisher` is the entity that provides the OS image. It could be Microsoft, Canonical (for Ubuntu), Red Hat, etc.
36 - It is a mandatory field that identifies the provider of the image.
37 type: str
38
39 extends_documentation_fragment:
40 - azure.azcollection.azure
41
42 author:
43 - Hen Yaish (@hyaish)
44
45 '''
46
47 EXAMPLES = '''
48 - name: Gather Resource Group info
49 azure.azcollection.azure_rm_resourcegroup_info:
50 name: "{{ resource_group }}"
51 register: rg_info
52
53 - name: List available Image SKUs for 0001-com-ubuntu-server-focal
54 azure.azcollection.azure_rm_imagesku_info:
55 location: westus
56 offer: 0001-com-ubuntu-server-focal
57 publisher: Canonical
58 register: available_image_skus_results
59 '''
60
61 RETURN = '''
62 available_skus:
63 description:
64 - List of Azure image SKUs for provisioning virtual machines.
65 returned: always
66 type: complex
67 contains:
68 name:
69 description:
70 - The specific SKU name or version.
71 returned: always
72 type: str
73 sample: "20_04-lts"
74 id:
75 description:
76 - The full Azure resource ID for the SKU.
77 returned: always
78 type: str
79 sample: "0001-com-ubuntu-server-focal/Skus/20_04-lts"
80 location:
81 description:
82 - The Azure region where the SKU is available.
83 returned: always
84 type: str
85 sample: "westus"
86 automatic_os_upgrade_supported:
87 description:
88 - Whether automatic OS upgrades are supported for this SKU.
89 returned: always
90 type: bool
91 sample: false
92 restrictions:
93 description:
94 - Restrictions that may apply to the use of this image SKU, such as region limitations or feature incompatibility.
95 returned: always
96 type: complex
97 contains:
98 type:
99 description:
100 - The type of restriction, which could include location-based restrictions or hardware compatibility issues.
101 type: str
102 returned: always
103 sample: "location"
104 restriction_values:
105 description:
106 - A list of restricted locations, regions, or zones where the image SKU cannot be used.
107 type: list
108 returned: always
109 sample: ["eastus", "westeurope"]
110 values:
111 description:
112 - A list of restricted locations, regions, or zones where the image SKU cannot be used.
113 - This return value has been deprecated and will be removed in a release after
114 2027-05-01. Use C(restriction_values) instead.
115 type: list
116 returned: always
117 sample: ["eastus", "westeurope"]
118 reason_code:
119 description:
120 - The reason for the restriction, such as quota limitations or specific hardware requirements.
121 type: str
122 sample: "NotSupported"
123 restriction_info:
124 description:
125 - Additional information about the restrictions, such as unsupported regions or features.
126 returned: always
127 type: complex
128 contains:
129 locations:
130 description:
131 - Locations where this SKU is restricted or unavailable.
132 type: list
133 sample: ["eastus"]
134 zones:
135 description:
136 - Availability zones within the region where this SKU is restricted.
137 type: list
138 sample: ["1", "2"]
139 '''
140
141
142 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
143 try:
144 from azure.mgmt.compute import ComputeManagementClient
145 from azure.core.exceptions import HttpResponseError
146 except ImportError:
147 # This is handled in azure_rm_common
148 pass
149
150
151 class AzureRMImageskuInfo(AzureRMModuleBase):
152 def __init__(self):
153
154 self.module_arg_spec = dict(
155 location=dict(type='str'),
156 publisher=dict(type='str'),
157 offer=dict(type='str'),
158 )
159
160 self.results = dict(
161 available_skus=[],
162 count=0
163 )
164 self.location = None
165 self.publisher = None
166 self.offer = None
167 super(AzureRMImageskuInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
168 supports_check_mode=True,
169 supports_tags=False)
170
171 def exec_module(self, **kwargs):
172 for key in self.module_arg_spec:
173 setattr(self, key, kwargs[key])
174
175 available_skus = self.list_skus()
176 self.results['available_skus'] = available_skus
177 self.results['count'] = len(available_skus)
178 self.module.deprecate(
179 "The 'values' return key in 'restrictions' is deprecated. Use 'restriction_values' instead.",
180 date="2027-05-01",
181 collection_name="azure.azcollection",
182 )
183 return self.results
184
185 def list_skus(self):
186 try:
187 compute_client = self.get_mgmt_svc_client(ComputeManagementClient,
188 base_url=self._cloud_environment.endpoints.resource_manager,
189 api_version='2021-07-01')
190 skus_result = compute_client.virtual_machine_images.list_skus(location=self.location,
191 publisher_name=self.publisher,
192 offer=self.offer)
193 available_skus = []
194
195 for sku_info in skus_result:
196 sku_dict = sku_info.as_dict()
197 for restriction in sku_dict.get('restrictions', []):
198 if 'values' in restriction:
199 restriction['restriction_values'] = restriction['values']
200 available_skus.append(sku_dict)
201 return available_skus
202
203 except HttpResponseError as e:
204 # Handle exceptions
205 raise e
206
207
208 def _match_location(loc, locations):
209 return next((x for x in locations if x.lower() == loc.lower()), None)
210
211
212 def _is_sku_available(sku_info, zone):
213 """
214 The SKU is unavailable in the following cases:
215 1. regional restriction and the region is restricted
216 2. parameter "zone" is input which indicates only showing skus with availability zones.
217 Meanwhile, zonal restriction and all zones are restricted
218 """
219 is_available = True
220 is_restrict_zone = False
221 is_restrict_location = False
222 if not sku_info.restrictions:
223 return is_available
224 for restriction in sku_info.restrictions:
225 if restriction.reason_code == 'NotAvailableForSubscription':
226 if restriction.type == 'Zone' and not (
227 set(sku_info.location_info[0].zones or []) - set(restriction.restriction_info.zones or [])):
228 is_restrict_zone = True
229 if restriction.type == 'Location' and (
230 sku_info.location_info[0].location in (restriction.restriction_info.locations or [])):
231 is_restrict_location = True
232 if is_restrict_location or (is_restrict_zone and zone):
233 is_available = False
234 break
235 return is_available
236
237
238 def main():
239 AzureRMImageskuInfo()
240
241
242 if __name__ == '__main__':
243 main()