adding container instance facts (#43326)
This commit is contained in:
parent
f465a251e1
commit
9b28319020
3 changed files with 359 additions and 0 deletions
|
@ -0,0 +1,280 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright (c) 2017 Zim Kalinowski, <zikalino@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_containerinstance_facts
|
||||
version_added: "2.8"
|
||||
short_description: Get Azure Container Instance facts.
|
||||
description:
|
||||
- Get facts of Container Instance.
|
||||
|
||||
options:
|
||||
resource_group:
|
||||
description:
|
||||
- The name of the resource group.
|
||||
required: True
|
||||
name:
|
||||
description:
|
||||
- The name of the container instance.
|
||||
tags:
|
||||
description:
|
||||
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
|
||||
|
||||
extends_documentation_fragment:
|
||||
- azure
|
||||
|
||||
author:
|
||||
- "Zim Kalinowski (@zikalino)"
|
||||
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Get specific Container Instance facts
|
||||
azure_rm_containerinstance_facts:
|
||||
resource_group: resource_group_name
|
||||
name: container_group_name
|
||||
|
||||
- name: List Container Instances in a specified resource group name
|
||||
azure_rm_containerinstance_facts:
|
||||
resource_group: resource_group_name
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
container_groups:
|
||||
description: A list of Container Instance dictionaries.
|
||||
returned: always
|
||||
type: complex
|
||||
contains:
|
||||
id:
|
||||
description:
|
||||
- The resource id.
|
||||
returned: always
|
||||
type: str
|
||||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/demo/providers/Microsoft.ContainerInstance/containerGroups/my
|
||||
containers"
|
||||
resource_group:
|
||||
description:
|
||||
- Resource group where the container exists.
|
||||
returned: always
|
||||
type: str
|
||||
sample: testrg
|
||||
name:
|
||||
description:
|
||||
- The resource name.
|
||||
returned: always
|
||||
type: str
|
||||
sample: mycontainers
|
||||
location:
|
||||
description:
|
||||
- The resource location.
|
||||
returned: always
|
||||
type: str
|
||||
sample: westus
|
||||
os_type:
|
||||
description:
|
||||
- The OS type of containers.
|
||||
returned: always
|
||||
type: str
|
||||
sample: linux
|
||||
ip_address:
|
||||
description:
|
||||
- IP address of the container instance.
|
||||
returned: always
|
||||
type: str
|
||||
sample: 173.15.18.1
|
||||
ports:
|
||||
description:
|
||||
- List of ports exposed by the container instance.
|
||||
returned: always
|
||||
type: list
|
||||
sample: [ 80, 81 ]
|
||||
containers:
|
||||
description:
|
||||
- The containers within the container group.
|
||||
returned: always
|
||||
type: complex
|
||||
sample: containers
|
||||
contains:
|
||||
name:
|
||||
description:
|
||||
- The name of the container instance.
|
||||
returned: always
|
||||
type: str
|
||||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/demo/providers/Microsoft.ContainerInstance/containerGroups/my
|
||||
containers"
|
||||
image:
|
||||
description:
|
||||
- The container image name.
|
||||
returned: always
|
||||
type: str
|
||||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/demo/providers/Microsoft.ContainerInstance/containerGroups/my
|
||||
containers"
|
||||
memory:
|
||||
description:
|
||||
- The required memory of the containers in GB.
|
||||
returned: always
|
||||
type: float
|
||||
sample: 1.5
|
||||
cpu:
|
||||
description:
|
||||
- The required number of CPU cores of the containers.
|
||||
returned: always
|
||||
type: int
|
||||
sample: 1
|
||||
ports:
|
||||
description:
|
||||
- List of ports exposed within the container group.
|
||||
returned: always
|
||||
type: list
|
||||
sample: [ 80, 81 ]
|
||||
'''
|
||||
|
||||
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
|
||||
|
||||
try:
|
||||
from msrestazure.azure_exceptions import CloudError
|
||||
from msrestazure.azure_operation import AzureOperationPoller
|
||||
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
|
||||
from msrest.serialization import Model
|
||||
except ImportError:
|
||||
# This is handled in azure_rm_common
|
||||
pass
|
||||
|
||||
|
||||
class AzureRMContainerInstanceFacts(AzureRMModuleBase):
|
||||
def __init__(self):
|
||||
# define user inputs into argument
|
||||
self.module_arg_spec = dict(
|
||||
resource_group=dict(
|
||||
type='str',
|
||||
required=True
|
||||
),
|
||||
name=dict(
|
||||
type='str'
|
||||
),
|
||||
tags=dict(
|
||||
type='list'
|
||||
)
|
||||
)
|
||||
# store the results of the module operation
|
||||
self.results = dict(
|
||||
changed=False,
|
||||
ansible_facts=dict()
|
||||
)
|
||||
self.resource_group = None
|
||||
self.name = None
|
||||
super(AzureRMContainerInstanceFacts, self).__init__(self.module_arg_spec, supports_tags=False)
|
||||
|
||||
def exec_module(self, **kwargs):
|
||||
for key in self.module_arg_spec:
|
||||
setattr(self, key, kwargs[key])
|
||||
|
||||
if (self.name is not None):
|
||||
self.results['containerinstances'] = self.get()
|
||||
elif (self.resource_group is not None):
|
||||
self.results['containerinstances'] = self.list_by_resource_group()
|
||||
else:
|
||||
self.results['containerinstances'] = self.list_all()
|
||||
return self.results
|
||||
|
||||
def get(self):
|
||||
response = None
|
||||
results = []
|
||||
try:
|
||||
response = self.containerinstance_client.container_groups.get(resource_group_name=self.resource_group,
|
||||
container_group_name=self.name)
|
||||
self.log("Response : {0}".format(response))
|
||||
except CloudError as e:
|
||||
self.log('Could not get facts for Container Instances.')
|
||||
|
||||
if response is not None and self.has_tags(response.tags, self.tags):
|
||||
results.append(self.format_item(response))
|
||||
|
||||
return results
|
||||
|
||||
def list_by_resource_group(self):
|
||||
response = None
|
||||
results = []
|
||||
try:
|
||||
response = self.containerinstance_client.container_groups.list_by_resource_group(resource_group_name=self.resource_group)
|
||||
self.log("Response : {0}".format(response))
|
||||
except CloudError as e:
|
||||
self.fail('Could not list facts for Container Instances.')
|
||||
|
||||
if response is not None:
|
||||
for item in response:
|
||||
if self.has_tags(item.tags, self.tags):
|
||||
results.append(self.format_item(item))
|
||||
|
||||
return results
|
||||
|
||||
def list_all(self):
|
||||
response = None
|
||||
results = []
|
||||
try:
|
||||
response = self.containerinstance_client.container_groups.list()
|
||||
self.log("Response : {0}".format(response))
|
||||
except CloudError as e:
|
||||
self.fail('Could not list facts for Container Instances.')
|
||||
|
||||
if response is not None:
|
||||
for item in response:
|
||||
if self.has_tags(item.tags, self.tags):
|
||||
results.append(self.format_item(item))
|
||||
|
||||
return results
|
||||
|
||||
def format_item(self, item):
|
||||
d = item.as_dict()
|
||||
containers = d['containers']
|
||||
ports = d['ip_address']['ports']
|
||||
resource_group = d['id'].split('resourceGroups/')[1].split('/')[0]
|
||||
|
||||
for port_index in range(len(ports)):
|
||||
ports[port_index] = ports[port_index]['port']
|
||||
|
||||
for container_index in range(len(containers)):
|
||||
old_container = containers[container_index]
|
||||
new_container = {
|
||||
'name': old_container['name'],
|
||||
'image': old_container['image'],
|
||||
'memory': old_container['resources']['requests']['memory_in_gb'],
|
||||
'cpu': old_container['resources']['requests']['cpu'],
|
||||
'ports': []
|
||||
}
|
||||
for port_index in range(len(old_container['ports'])):
|
||||
new_container['ports'].append(old_container['ports'][port_index]['port'])
|
||||
containers[container_index] = new_container
|
||||
|
||||
d = {
|
||||
'id': d['id'],
|
||||
'resource_group': resource_group,
|
||||
'name': d['name'],
|
||||
'os_type': d['os_type'],
|
||||
'ip_address': 'public' if d['ip_address']['type'] == 'Public' else 'none',
|
||||
'ports': ports,
|
||||
'location': d['location'],
|
||||
'containers': containers
|
||||
}
|
||||
return d
|
||||
|
||||
|
||||
def main():
|
||||
AzureRMContainerInstanceFacts()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -1,3 +1,4 @@
|
|||
cloud/azure
|
||||
destructive
|
||||
shippable/azure/group1
|
||||
azure_rm_containerinstance_facts
|
||||
|
|
|
@ -73,6 +73,66 @@
|
|||
- output.changed
|
||||
- output.provisioning_state == 'Succeeded'
|
||||
|
||||
- name: Create second container instance for testing purposes
|
||||
azure_rm_containerinstance:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "aci{{ resource_group | hash('md5') | truncate(7, True, '') }}sec"
|
||||
os_type: linux
|
||||
ip_address: public
|
||||
location: eastus
|
||||
ports:
|
||||
- 80
|
||||
containers:
|
||||
- name: mycontainer1
|
||||
image: httpd
|
||||
memory: 1.5
|
||||
ports:
|
||||
- 80
|
||||
- 81
|
||||
register: output
|
||||
|
||||
- name: Gather facts for single Container Instance
|
||||
azure_rm_containerinstance_facts:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "aci{{ resource_group | hash('md5') | truncate(7, True, '') }}sec"
|
||||
register: output
|
||||
|
||||
- name: Assert that facts are returned
|
||||
assert:
|
||||
that:
|
||||
- output.changed == False
|
||||
- output.containerinstances[0]['resource_group'] != None
|
||||
- output.containerinstances[0]['name'] != None
|
||||
- output.containerinstances[0]['os_type'] != None
|
||||
- output.containerinstances[0]['location'] != None
|
||||
- output.containerinstances[0]['ip_address'] != None
|
||||
- output.containerinstances[0]['ports'] != None
|
||||
- output.containerinstances[0]['containers'] != None
|
||||
|
||||
- name: Gather facts for all Container Instances in the resource group
|
||||
azure_rm_containerinstance_facts:
|
||||
resource_group: "{{ resource_group }}"
|
||||
register: output
|
||||
|
||||
- name: Assert that facts are returned
|
||||
assert:
|
||||
that:
|
||||
- output.changed == False
|
||||
- output.containerinstances[0]['resource_group'] != None
|
||||
- output.containerinstances[0]['name'] != None
|
||||
- output.containerinstances[0]['os_type'] != None
|
||||
- output.containerinstances[0]['location'] != None
|
||||
- output.containerinstances[0]['ip_address'] != None
|
||||
- output.containerinstances[0]['ports'] != None
|
||||
- output.containerinstances[0]['containers'] != None
|
||||
- output.containerinstances[1]['resource_group'] != None
|
||||
- output.containerinstances[1]['name'] != None
|
||||
- output.containerinstances[1]['os_type'] != None
|
||||
- output.containerinstances[1]['location'] != None
|
||||
- output.containerinstances[1]['ip_address'] != None
|
||||
- output.containerinstances[1]['ports'] != None
|
||||
- output.containerinstances[1]['containers'] != None
|
||||
|
||||
- name: Remove container instance
|
||||
azure_rm_containerinstance:
|
||||
resource_group: "{{ resource_group }}"
|
||||
|
@ -97,6 +157,24 @@
|
|||
that:
|
||||
- output.changed
|
||||
|
||||
- name: Remove container instance
|
||||
azure_rm_containerinstance:
|
||||
resource_group: "{{ resource_group }}"
|
||||
name: "aci{{ resource_group | hash('md5') | truncate(7, True, '') }}sec"
|
||||
os_type: linux
|
||||
ip_address: public
|
||||
location: eastus
|
||||
ports:
|
||||
- 80
|
||||
containers:
|
||||
- name: mycontainer1
|
||||
image: httpd
|
||||
memory: 1.5
|
||||
ports:
|
||||
- 80
|
||||
- 81
|
||||
state: absent
|
||||
|
||||
- name: Remove container instance again
|
||||
azure_rm_containerinstance:
|
||||
resource_group: "{{ resource_group }}"
|
||||
|
|
Loading…
Reference in a new issue