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