| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_galleryimage_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get Azure SIG Image info |
| 16 |
description: |
| 17 |
- Get info of Azure SIG Image. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
gallery_name: |
| 25 |
description: |
| 26 |
- The name of the shared image gallery from which the image definitions are to be retrieved. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
name: |
| 30 |
description: |
| 31 |
- Resource name. |
| 32 |
type: str |
| 33 |
extends_documentation_fragment: |
| 34 |
- azure.azcollection.azure |
| 35 |
author: |
| 36 |
- Liu Qingyi (@smile37773) |
| 37 |
|
| 38 |
''' |
| 39 |
|
| 40 |
EXAMPLES = ''' |
| 41 |
- name: List gallery images in a gallery. |
| 42 |
azure_rm_galleryimage_info: |
| 43 |
resource_group: myResourceGroup |
| 44 |
gallery_name: myGallery |
| 45 |
- name: Get a gallery image. |
| 46 |
azure_rm_galleryimage_info: |
| 47 |
resource_group: myResourceGroup |
| 48 |
gallery_name: myGallery |
| 49 |
name: myImage |
| 50 |
''' |
| 51 |
|
| 52 |
RETURN = ''' |
| 53 |
images: |
| 54 |
description: |
| 55 |
- A list of dict results where the key is the name of the image and the values are the info for that image. |
| 56 |
returned: always |
| 57 |
type: complex |
| 58 |
contains: |
| 59 |
id: |
| 60 |
description: |
| 61 |
- Resource ID. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup |
| 65 |
/providers/Microsoft.Compute/galleries/myGallery/images/myImage" |
| 66 |
name: |
| 67 |
description: |
| 68 |
- Resource name. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: myImage |
| 72 |
location: |
| 73 |
description: |
| 74 |
- Resource location. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: "eastus" |
| 78 |
tags: |
| 79 |
description: |
| 80 |
- Resource tags. |
| 81 |
returned: always |
| 82 |
type: dict |
| 83 |
sample: { "tag": "value" } |
| 84 |
os_state: |
| 85 |
description: |
| 86 |
- The allowed values for OS State are C(generalized). |
| 87 |
type: str |
| 88 |
sample: "Generalized" |
| 89 |
os_type: |
| 90 |
description: |
| 91 |
- This property allows you to specify the type of the OS that is included in the disk when creating a VM from a managed image. |
| 92 |
type: str |
| 93 |
sample: "linux/windows" |
| 94 |
identifier: |
| 95 |
description: |
| 96 |
- This is the gallery image definition identifier. |
| 97 |
type: dict |
| 98 |
contains: |
| 99 |
offer: |
| 100 |
description: |
| 101 |
- The name of the gallery image definition offer. |
| 102 |
type: str |
| 103 |
sample: "myOfferName" |
| 104 |
publisher: |
| 105 |
description: |
| 106 |
- The name of the gallery image definition publisher. |
| 107 |
type: str |
| 108 |
sample: "myPublisherName" |
| 109 |
sku: |
| 110 |
description: |
| 111 |
- The name of the gallery image definition sku. |
| 112 |
type: str |
| 113 |
sample: "mySkuName" |
| 114 |
|
| 115 |
''' |
| 116 |
|
| 117 |
import json |
| 118 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 119 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 120 |
|
| 121 |
|
| 122 |
class AzureRMGalleryImagesInfo(AzureRMModuleBase): |
| 123 |
def __init__(self): |
| 124 |
self.module_arg_spec = dict( |
| 125 |
resource_group=dict( |
| 126 |
type='str', |
| 127 |
required=True |
| 128 |
), |
| 129 |
gallery_name=dict( |
| 130 |
type='str', |
| 131 |
required=True |
| 132 |
), |
| 133 |
name=dict( |
| 134 |
type='str' |
| 135 |
) |
| 136 |
) |
| 137 |
|
| 138 |
self.resource_group = None |
| 139 |
self.gallery_name = None |
| 140 |
self.name = None |
| 141 |
|
| 142 |
self.results = dict(changed=False) |
| 143 |
self.mgmt_client = None |
| 144 |
self.state = None |
| 145 |
self.url = None |
| 146 |
self.status_code = [200] |
| 147 |
|
| 148 |
self.query_parameters = {} |
| 149 |
self.query_parameters['api-version'] = '2019-03-01' |
| 150 |
self.header_parameters = {} |
| 151 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 152 |
|
| 153 |
self.mgmt_client = None |
| 154 |
super(AzureRMGalleryImagesInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 155 |
|
| 156 |
def exec_module(self, **kwargs): |
| 157 |
|
| 158 |
for key in self.module_arg_spec: |
| 159 |
setattr(self, key, kwargs[key]) |
| 160 |
|
| 161 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 162 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 163 |
|
| 164 |
if (self.resource_group is not None and |
| 165 |
self.gallery_name is not None and |
| 166 |
self.name is not None): |
| 167 |
|
| 168 |
self.results['images'] = self.get() |
| 169 |
elif (self.resource_group is not None and |
| 170 |
self.gallery_name is not None): |
| 171 |
|
| 172 |
self.results['images'] = self.listbygallery() |
| 173 |
return self.results |
| 174 |
|
| 175 |
def get(self): |
| 176 |
response = None |
| 177 |
results = {} |
| 178 |
|
| 179 |
self.url = ('/subscriptions' + |
| 180 |
'/{{ subscription_id }}' + |
| 181 |
'/resourceGroups' + |
| 182 |
'/{{ resource_group }}' + |
| 183 |
'/providers' + |
| 184 |
'/Microsoft.Compute' + |
| 185 |
'/galleries' + |
| 186 |
'/{{ gallery_name }}' + |
| 187 |
'/images' + |
| 188 |
'/{{ image_name }}') |
| 189 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 190 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 191 |
self.url = self.url.replace('{{ gallery_name }}', self.gallery_name) |
| 192 |
self.url = self.url.replace('{{ image_name }}', self.name) |
| 193 |
|
| 194 |
try: |
| 195 |
response = self.mgmt_client.query(self.url, |
| 196 |
'GET', |
| 197 |
self.query_parameters, |
| 198 |
self.header_parameters, |
| 199 |
None, |
| 200 |
self.status_code, |
| 201 |
600, |
| 202 |
30) |
| 203 |
results = json.loads(response.body()) |
| 204 |
|
| 205 |
except Exception as e: |
| 206 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 207 |
|
| 208 |
return self.format_item(results) |
| 209 |
|
| 210 |
def listbygallery(self): |
| 211 |
response = None |
| 212 |
results = {} |
| 213 |
|
| 214 |
self.url = ('/subscriptions' + |
| 215 |
'/{{ subscription_id }}' + |
| 216 |
'/resourceGroups' + |
| 217 |
'/{{ resource_group }}' + |
| 218 |
'/providers' + |
| 219 |
'/Microsoft.Compute' + |
| 220 |
'/galleries' + |
| 221 |
'/{{ gallery_name }}' + |
| 222 |
'/images') |
| 223 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 224 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 225 |
self.url = self.url.replace('{{ gallery_name }}', self.gallery_name) |
| 226 |
|
| 227 |
try: |
| 228 |
response = self.mgmt_client.query(self.url, |
| 229 |
'GET', |
| 230 |
self.query_parameters, |
| 231 |
self.header_parameters, |
| 232 |
None, |
| 233 |
self.status_code, |
| 234 |
600, |
| 235 |
30) |
| 236 |
results = json.loads(response.body()) |
| 237 |
|
| 238 |
except Exception as e: |
| 239 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 240 |
|
| 241 |
return [self.format_item(x) for x in results['value']] if results['value'] else [] |
| 242 |
|
| 243 |
def format_item(self, item): |
| 244 |
d = { |
| 245 |
'id': item['id'], |
| 246 |
'name': item['name'], |
| 247 |
'location': item['location'], |
| 248 |
'tags': item.get('tags'), |
| 249 |
'os_state': item['properties']['osState'], |
| 250 |
'os_type': item['properties']['osType'], |
| 251 |
'identifier': item['properties']['identifier'] |
| 252 |
} |
| 253 |
return d |
| 254 |
|
| 255 |
|
| 256 |
def main(): |
| 257 |
AzureRMGalleryImagesInfo() |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
main() |