| 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_appserviceplan_info |
| 14 |
|
| 15 |
version_added: "0.1.2" |
| 16 |
|
| 17 |
short_description: Get azure app service plan facts |
| 18 |
|
| 19 |
description: |
| 20 |
- Get facts for a specific app service plan or all app service plans in a resource group, or all app service plan in current subscription. |
| 21 |
|
| 22 |
options: |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Only show results for a specific app service plan. |
| 26 |
type: str |
| 27 |
resource_group: |
| 28 |
description: |
| 29 |
- Limit results by resource group. |
| 30 |
type: str |
| 31 |
tags: |
| 32 |
description: |
| 33 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 34 |
type: list |
| 35 |
elements: str |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- azure.azcollection.azure |
| 39 |
|
| 40 |
author: |
| 41 |
- Yunge Zhu (@yungezz) |
| 42 |
''' |
| 43 |
|
| 44 |
EXAMPLES = ''' |
| 45 |
- name: Get facts for app service plan by name |
| 46 |
azure_rm_appserviceplan_info: |
| 47 |
resource_group: myResourceGroup |
| 48 |
name: myAppServicePlan |
| 49 |
|
| 50 |
- name: Get azure_rm_appserviceplan_facts for app service plan in resource group |
| 51 |
azure_rm_appserviceplan_info: |
| 52 |
resource_group: myResourceGroup |
| 53 |
|
| 54 |
- name: Get facts for app service plan with tags |
| 55 |
azure_rm_appserviceplan_info: |
| 56 |
tags: |
| 57 |
- testtag |
| 58 |
- foo:bar |
| 59 |
''' |
| 60 |
|
| 61 |
RETURN = ''' |
| 62 |
appserviceplans: |
| 63 |
description: List of app service plans. |
| 64 |
returned: always |
| 65 |
type: complex |
| 66 |
contains: |
| 67 |
id: |
| 68 |
description: Id of the app service plan. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myPlan |
| 72 |
name: |
| 73 |
description: Name of the app service plan. |
| 74 |
returned: always |
| 75 |
type: str |
| 76 |
resource_group: |
| 77 |
description: Resource group of the app service plan. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: myResourceGroup |
| 81 |
location: |
| 82 |
description: Location of the app service plan. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
kind: |
| 86 |
description: Kind of the app service plan. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: app |
| 90 |
sku: |
| 91 |
description: Sku of the app service plan. |
| 92 |
returned: always |
| 93 |
type: complex |
| 94 |
contains: |
| 95 |
name: |
| 96 |
description: Name of sku. |
| 97 |
returned: always |
| 98 |
type: str |
| 99 |
sample: S1 |
| 100 |
family: |
| 101 |
description: Family of sku. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
sample: S |
| 105 |
size: |
| 106 |
description: Size of sku. |
| 107 |
returned: always |
| 108 |
type: str |
| 109 |
sample: S1 |
| 110 |
tier: |
| 111 |
description: Tier of sku. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
sample: Standard |
| 115 |
capacity: |
| 116 |
description: Capacity of sku. |
| 117 |
returned: always |
| 118 |
type: int |
| 119 |
sample: 1 |
| 120 |
''' |
| 121 |
try: |
| 122 |
from azure.core.exceptions import ResourceNotFoundError |
| 123 |
except Exception: |
| 124 |
|
| 125 |
pass |
| 126 |
|
| 127 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 128 |
|
| 129 |
AZURE_OBJECT_CLASS = 'AppServicePlan' |
| 130 |
|
| 131 |
|
| 132 |
class AzureRMAppServicePlanInfo(AzureRMModuleBase): |
| 133 |
|
| 134 |
def __init__(self): |
| 135 |
|
| 136 |
self.module_arg_spec = dict( |
| 137 |
name=dict(type='str'), |
| 138 |
resource_group=dict(type='str'), |
| 139 |
tags=dict(type='list', elements='str') |
| 140 |
) |
| 141 |
|
| 142 |
self.results = dict(changed=False) |
| 143 |
|
| 144 |
self.name = None |
| 145 |
self.resource_group = None |
| 146 |
self.tags = None |
| 147 |
self.info_level = None |
| 148 |
|
| 149 |
super(AzureRMAppServicePlanInfo, self).__init__(self.module_arg_spec, |
| 150 |
supports_check_mode=True, |
| 151 |
supports_tags=False, |
| 152 |
facts_module=True) |
| 153 |
|
| 154 |
def exec_module(self, **kwargs): |
| 155 |
|
| 156 |
for key in self.module_arg_spec: |
| 157 |
setattr(self, key, kwargs[key]) |
| 158 |
|
| 159 |
if self.name: |
| 160 |
self.results['appserviceplans'] = self.list_by_name() |
| 161 |
elif self.resource_group: |
| 162 |
self.results['appserviceplans'] = self.list_by_resource_group() |
| 163 |
else: |
| 164 |
self.results['appserviceplans'] = self.list_all() |
| 165 |
|
| 166 |
return self.results |
| 167 |
|
| 168 |
def list_by_name(self): |
| 169 |
self.log('Get app service plan {0}'.format(self.name)) |
| 170 |
item = None |
| 171 |
result = [] |
| 172 |
|
| 173 |
try: |
| 174 |
item = self.web_client.app_service_plans.get(resource_group_name=self.resource_group, name=self.name) |
| 175 |
except ResourceNotFoundError: |
| 176 |
pass |
| 177 |
|
| 178 |
if item and self.has_tags(item.tags, self.tags): |
| 179 |
curated_result = self.construct_curated_plan(item) |
| 180 |
result = [curated_result] |
| 181 |
|
| 182 |
return result |
| 183 |
|
| 184 |
def list_by_resource_group(self): |
| 185 |
self.log('List app service plans in resource groups {0}'.format(self.resource_group)) |
| 186 |
try: |
| 187 |
response = list(self.web_client.app_service_plans.list_by_resource_group(resource_group_name=self.resource_group)) |
| 188 |
except Exception as exc: |
| 189 |
self.fail("Error listing app service plan in resource groups {0} - {1}".format(self.resource_group, str(exc))) |
| 190 |
|
| 191 |
results = [] |
| 192 |
for item in response: |
| 193 |
if self.has_tags(item.tags, self.tags): |
| 194 |
curated_output = self.construct_curated_plan(item) |
| 195 |
results.append(curated_output) |
| 196 |
return results |
| 197 |
|
| 198 |
def list_all(self): |
| 199 |
self.log('List app service plans in current subscription') |
| 200 |
try: |
| 201 |
response = list(self.web_client.app_service_plans.list()) |
| 202 |
except Exception as exc: |
| 203 |
self.fail("Error listing app service plans: {0}".format(str(exc))) |
| 204 |
|
| 205 |
results = [] |
| 206 |
for item in response: |
| 207 |
if self.has_tags(item.tags, self.tags): |
| 208 |
curated_output = self.construct_curated_plan(item) |
| 209 |
results.append(curated_output) |
| 210 |
return results |
| 211 |
|
| 212 |
def construct_curated_plan(self, plan): |
| 213 |
plan_facts = self.serialize_obj(plan, AZURE_OBJECT_CLASS) |
| 214 |
|
| 215 |
curated_output = dict() |
| 216 |
curated_output['id'] = plan_facts['id'] |
| 217 |
curated_output['name'] = plan_facts['name'] |
| 218 |
curated_output['resource_group'] = plan_facts['resource_group'] |
| 219 |
curated_output['location'] = plan_facts['location'] |
| 220 |
curated_output['tags'] = plan_facts.get('tags', None) |
| 221 |
curated_output['is_linux'] = False |
| 222 |
curated_output['kind'] = plan_facts['kind'] |
| 223 |
curated_output['sku'] = plan_facts['sku'] |
| 224 |
|
| 225 |
if plan_facts.get('reserved', None): |
| 226 |
curated_output['is_linux'] = True |
| 227 |
|
| 228 |
return curated_output |
| 229 |
|
| 230 |
|
| 231 |
def main(): |
| 232 |
AzureRMAppServicePlanInfo() |
| 233 |
|
| 234 |
|
| 235 |
if __name__ == '__main__': |
| 236 |
main() |