| 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_subscription_info |
| 14 |
|
| 15 |
version_added: "1.2.0" |
| 16 |
|
| 17 |
short_description: Get Azure Subscription facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific subscription or all subscriptions. |
| 21 |
|
| 22 |
options: |
| 23 |
id: |
| 24 |
description: |
| 25 |
- Limit results to a specific subscription by id. |
| 26 |
- Mutually exclusive with I(name). |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Limit results to a specific subscription by name. |
| 31 |
- Mutually exclusive with I(id). |
| 32 |
aliases: |
| 33 |
- subscription_name |
| 34 |
type: str |
| 35 |
all: |
| 36 |
description: |
| 37 |
- If true, will return all subscriptions. |
| 38 |
- If false will omit disabled subscriptions (default). |
| 39 |
- Option has no effect when searching by id or name, and will be silently ignored. |
| 40 |
type: bool |
| 41 |
tags: |
| 42 |
description: |
| 43 |
- Limit results by providing a list of tags. Format tags as 'key:value'. |
| 44 |
- Option has no effect when searching by id or name, and will be silently ignored. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
|
| 48 |
extends_documentation_fragment: |
| 49 |
- azure.azcollection.azure |
| 50 |
|
| 51 |
author: |
| 52 |
- Paul Aiton (@paultaiton) |
| 53 |
''' |
| 54 |
|
| 55 |
EXAMPLES = ''' |
| 56 |
- name: Get facts for one subscription by id |
| 57 |
azure_rm_subscription_info: |
| 58 |
id: 00000000-0000-0000-0000-000000000000 |
| 59 |
|
| 60 |
- name: Get facts for one subscription by name |
| 61 |
azure_rm_subscription_info: |
| 62 |
name: "my-subscription" |
| 63 |
|
| 64 |
- name: Get facts for all subscriptions, including ones that are disabled. |
| 65 |
azure_rm_subscription_info: |
| 66 |
all: true |
| 67 |
|
| 68 |
- name: Get facts for subscriptions containing tags provided. |
| 69 |
azure_rm_subscription_info: |
| 70 |
tags: |
| 71 |
- testing |
| 72 |
- foo:bar |
| 73 |
''' |
| 74 |
|
| 75 |
RETURN = ''' |
| 76 |
subscriptions: |
| 77 |
description: |
| 78 |
- List of subscription dicts. |
| 79 |
returned: always |
| 80 |
type: list |
| 81 |
contains: |
| 82 |
display_name: |
| 83 |
description: Subscription display name. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: my-subscription |
| 87 |
fqid: |
| 88 |
description: Subscription fully qualified id. |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
sample: "/subscriptions/00000000-0000-0000-0000-000000000000" |
| 92 |
subscription_id: |
| 93 |
description: Subscription guid. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: "00000000-0000-0000-0000-000000000000" |
| 97 |
state: |
| 98 |
description: Subscription state. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: "'Enabled' or 'Disabled'" |
| 102 |
tags: |
| 103 |
description: Tags assigned to resource group. |
| 104 |
returned: always |
| 105 |
type: dict |
| 106 |
sample: { "tag1": "value1", "tag2": "value2" } |
| 107 |
tenant_id: |
| 108 |
description: Subscription tenant id |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: "00000000-0000-0000-0000-000000000000" |
| 112 |
''' |
| 113 |
|
| 114 |
try: |
| 115 |
from azure.core.exceptions import ResourceNotFoundError |
| 116 |
except Exception: |
| 117 |
|
| 118 |
pass |
| 119 |
|
| 120 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 121 |
|
| 122 |
|
| 123 |
AZURE_OBJECT_CLASS = 'Subscription' |
| 124 |
|
| 125 |
|
| 126 |
class AzureRMSubscriptionInfo(AzureRMModuleBase): |
| 127 |
|
| 128 |
def __init__(self): |
| 129 |
|
| 130 |
self.module_arg_spec = dict( |
| 131 |
name=dict(type='str', aliases=['subscription_name']), |
| 132 |
id=dict(type='str'), |
| 133 |
tags=dict(type='list', elements='str'), |
| 134 |
all=dict(type='bool') |
| 135 |
) |
| 136 |
|
| 137 |
self.results = dict( |
| 138 |
changed=False, |
| 139 |
subscriptions=[] |
| 140 |
) |
| 141 |
|
| 142 |
self.name = None |
| 143 |
self.id = None |
| 144 |
self.tags = None |
| 145 |
self.all = False |
| 146 |
|
| 147 |
mutually_exclusive = [['name', 'id']] |
| 148 |
|
| 149 |
super(AzureRMSubscriptionInfo, self).__init__(self.module_arg_spec, |
| 150 |
supports_check_mode=True, |
| 151 |
supports_tags=False, |
| 152 |
mutually_exclusive=mutually_exclusive, |
| 153 |
facts_module=True) |
| 154 |
|
| 155 |
def exec_module(self, **kwargs): |
| 156 |
for key in self.module_arg_spec: |
| 157 |
setattr(self, key, kwargs[key]) |
| 158 |
|
| 159 |
if self.id and self.name: |
| 160 |
self.fail("Parameter error: cannot search subscriptions by both name and id.") |
| 161 |
|
| 162 |
result = [] |
| 163 |
|
| 164 |
if self.id: |
| 165 |
result = self.get_item() |
| 166 |
else: |
| 167 |
result = self.list_items() |
| 168 |
|
| 169 |
self.results['subscriptions'] = result |
| 170 |
return self.results |
| 171 |
|
| 172 |
def get_item(self): |
| 173 |
self.log('Get properties for {0}'.format(self.id)) |
| 174 |
item = None |
| 175 |
result = [] |
| 176 |
|
| 177 |
try: |
| 178 |
item = self.subscription_client.subscriptions.get(self.id) |
| 179 |
except ResourceNotFoundError: |
| 180 |
pass |
| 181 |
|
| 182 |
result = self.to_dict(item) |
| 183 |
|
| 184 |
return result |
| 185 |
|
| 186 |
def list_items(self): |
| 187 |
self.log('List all items') |
| 188 |
try: |
| 189 |
response = self.subscription_client.subscriptions.list() |
| 190 |
except Exception as exc: |
| 191 |
self.fail("Failed to list all items - {0}".format(str(exc))) |
| 192 |
|
| 193 |
results = [] |
| 194 |
for item in response: |
| 195 |
|
| 196 |
|
| 197 |
if self.name and self.name.lower() == item.display_name.lower(): |
| 198 |
results.append(self.to_dict(item)) |
| 199 |
elif not self.name and (self.all or item.state == "Enabled"): |
| 200 |
results.append(self.to_dict(item)) |
| 201 |
|
| 202 |
return results |
| 203 |
|
| 204 |
def to_dict(self, subscription_object): |
| 205 |
if self.has_tags(subscription_object.tags, self.tags): |
| 206 |
return dict( |
| 207 |
display_name=subscription_object.display_name, |
| 208 |
fqid=subscription_object.id, |
| 209 |
state=subscription_object.state, |
| 210 |
subscription_id=subscription_object.subscription_id, |
| 211 |
tags=subscription_object.tags, |
| 212 |
tenant_id=subscription_object.tenant_id |
| 213 |
) |
| 214 |
else: |
| 215 |
return dict() |
| 216 |
|
| 217 |
|
| 218 |
def main(): |
| 219 |
AzureRMSubscriptionInfo() |
| 220 |
|
| 221 |
|
| 222 |
if __name__ == '__main__': |
| 223 |
main() |