1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

add azure app service plan facts (#44156)

* add app service plan facts

* fix lint

* fix missed tags

* remove  azure_

* fix test random
This commit is contained in:
Yunge Zhu 2018-08-16 16:13:32 +08:00 committed by Zim Kalinowski
parent 6a395d8e24
commit aef5099695
3 changed files with 257 additions and 4 deletions

View file

@ -191,6 +191,8 @@ class AzureRMAppServicePlans(AzureRMModuleBase):
self.is_linux = None self.is_linux = None
self.number_of_workers = 1 self.number_of_workers = 1
self.tags = None
self.results = dict( self.results = dict(
changed=False, changed=False,
ansible_facts=dict(azure_appserviceplan=None) ansible_facts=dict(azure_appserviceplan=None)
@ -237,11 +239,11 @@ class AzureRMAppServicePlans(AzureRMModuleBase):
if self.state == 'present': if self.state == 'present':
self.log('Result: {0}'.format(old_response)) self.log('Result: {0}'.format(old_response))
update_tags, old_response['tags'] = self.update_tags( update_tags, newtags = self.update_tags(old_response.get('tags', dict()))
old_response.get('tags', dict()))
if update_tags: if update_tags:
to_be_updated = True to_be_updated = True
self.tags = newtags
# check if sku changed # check if sku changed
if self.sku and _normalize_sku(self.sku) != old_response['sku']['size']: if self.sku and _normalize_sku(self.sku) != old_response['sku']['size']:
@ -312,7 +314,7 @@ class AzureRMAppServicePlans(AzureRMModuleBase):
sku_def = SkuDescription(tier=get_sku_name( sku_def = SkuDescription(tier=get_sku_name(
sku), name=sku, capacity=self.number_of_workers) sku), name=sku, capacity=self.number_of_workers)
plan_def = AppServicePlan( plan_def = AppServicePlan(
location=self.location, app_service_plan_name=self.name, sku=sku_def, reserved=self.is_linux) location=self.location, app_service_plan_name=self.name, sku=sku_def, reserved=self.is_linux, tags=self.tags if self.tags else None)
poller = self.web_client.app_service_plans.create_or_update(self.resource_group, self.name, plan_def) poller = self.web_client.app_service_plans.create_or_update(self.resource_group, self.name, plan_def)

View file

@ -0,0 +1,239 @@
#!/usr/bin/python
#
# Copyright (c) 2018 Yunge Zhu, <yungez@microsoft.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: azure_rm_appserviceplan_facts
version_added: "2.7"
short_description: Get azure app service plan facts.
description:
- 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.
options:
name:
description:
- Only show results for a specific app service plan.
resource_group:
description:
- Limit results by resource group.
tags:
description:
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
extends_documentation_fragment:
- azure
author:
- "Yunge Zhu (@yungezz)"
'''
EXAMPLES = '''
- name: Get facts for app service plan by name
azure_rm_appserviceplan_facts:
resource_group: testrg
name: winwebapp1
- name: Get azure_rm_appserviceplan_facts for app service plan in resource group
azure_rm_webapp_facts:
resource_group: testrg
- name: Get facts for app service plan with tags
azure_rm_appserviceplan_facts:
tags:
- testtag
- foo:bar
'''
RETURN = '''
appserviceplans:
description: List of app service plans.
returned: always
type: complex
contains:
id:
description: Id of the app service plan.
returned: always
type: str
sample: /subscriptions/xxxx/resourceGroupsxxx/providers/Microsoft.Web/serverfarms/xxx
name:
description: Name of the app service plan.
returned: always
type: str
resource_group:
description: Resource group of the app service plan.
returned: always
type: str
location:
description: Location of the app service plan.
returned: always
type: str
kind:
description: Kind of the app service plan.
returned: always
type: str
sample: app
sku:
description: Sku of the app service plan.
returned: always
type: complex
contains:
name:
description: Name of sku.
returned: always
type: str
sample: S1
family:
description: Family of sku.
returned: always
type: str
sample: S
size:
description: Size of sku.
returned: always
type: str
sample: S1
tier:
description: Tier of sku.
returned: always
type: str
sample: Standard
capacity:
description: Capacity of sku.
returned: always
type: int
sample: 1
'''
try:
from msrestazure.azure_exceptions import CloudError
from azure.common import AzureMissingResourceHttpError, AzureHttpError
except:
# This is handled in azure_rm_common
pass
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
AZURE_OBJECT_CLASS = 'AppServicePlan'
class AzureRMAppServicePlanFacts(AzureRMModuleBase):
def __init__(self):
self.module_arg_spec = dict(
name=dict(type='str'),
resource_group=dict(type='str'),
tags=dict(type='list')
)
self.results = dict(
changed=False,
ansible_facts=dict(azure_appserviceplans=[])
)
self.name = None
self.resource_group = None
self.tags = None
self.info_level = None
super(AzureRMAppServicePlanFacts, self).__init__(self.module_arg_spec,
supports_tags=False,
facts_module=True)
def exec_module(self, **kwargs):
for key in self.module_arg_spec:
setattr(self, key, kwargs[key])
if self.name:
self.results['appserviceplans'] = self.list_by_name()
elif self.resource_group:
self.results['appserviceplans'] = self.list_by_resource_group()
else:
self.results['appserviceplans'] = self.list_all()
return self.results
def list_by_name(self):
self.log('Get app service plan {0}'.format(self.name))
item = None
result = []
try:
item = self.web_client.app_service_plans.get(self.resource_group, self.name)
except CloudError:
pass
if item and self.has_tags(item.tags, self.tags):
curated_result = self.construct_curated_plan(item)
result = [curated_result]
return result
def list_by_resource_group(self):
self.log('List app service plans in resource groups {0}'.format(self.resource_group))
try:
response = list(self.web_client.app_service_plans.list_by_resource_group(self.resource_group))
except CloudError as exc:
self.fail("Error listing app service plan in resource groups {0} - {1}".format(self.resource_group, str(exc)))
results = []
for item in response:
if self.has_tags(item.tags, self.tags):
curated_output = self.construct_curated_plan(item)
results.append(curated_output)
return results
def list_all(self):
self.log('List app service plans in current subscription')
try:
response = list(self.web_client.app_service_plans.list())
except CloudError as exc:
self.fail("Error listing app service plans: {1}".format(str(exc)))
results = []
for item in response:
if self.has_tags(item.tags, self.tags):
curated_output = self.construct_curated_plan(item)
results.append(curated_output)
return results
def construct_curated_plan(self, plan):
plan_info = self.serialize_obj(plan, AZURE_OBJECT_CLASS)
curated_output = dict()
curated_output['id'] = plan_info['id']
curated_output['name'] = plan_info['name']
curated_output['resource_group'] = plan_info['properties']['resourceGroup']
curated_output['location'] = plan_info['location']
curated_output['tags'] = plan_info.get('tags', None)
curated_output['is_linux'] = False
curated_output['kind'] = plan_info['kind']
curated_output['sku'] = plan_info['sku']
if plan_info['properties'].get('reserved', None):
curated_output['is_linux'] = True
return curated_output
def main():
AzureRMAppServicePlanFacts()
if __name__ == '__main__':
main()

View file

@ -2,7 +2,7 @@
set_fact: set_fact:
linux_plan_resource_group: "{{ resource_group_secondary }}" linux_plan_resource_group: "{{ resource_group_secondary }}"
win_plan_name: "{{ (resource_prefix | replace('-','x'))[-8:] }}winplan" win_plan_name: "{{ (resource_prefix | replace('-','x'))[-8:] }}winplan"
linux_plan_name: "{{ (resource_group_secondary | replace('-','x'))[-8:] }}linplan" linux_plan_name: "{{ (resource_prefix | replace('-','x'))[-8:] }}linplan"
- name: create a windows plan - name: create a windows plan
azure_rm_appserviceplan: azure_rm_appserviceplan:
@ -32,6 +32,18 @@
- output.changed - output.changed
- output.id - output.id
- name: get app service plan by name
azure_rm_appserviceplan_facts:
resource_group: "{{ linux_plan_resource_group }}"
name: "{{ linux_plan_name }}1"
register: output
- name: assert is_linux is True
assert:
that:
- output.appserviceplans | length == 1
- output.appserviceplans[0].is_linux == True
- name: create linux app service plan idempotent - name: create linux app service plan idempotent
azure_rm_appserviceplan: azure_rm_appserviceplan:
resource_group: "{{ linux_plan_resource_group }}" resource_group: "{{ linux_plan_resource_group }}"