adding new container instance options (#54072)

This commit is contained in:
Zim Kalinowski 2019-03-21 17:01:12 +08:00 committed by GitHub
parent 8d62794f91
commit 0a2971dcf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 112 additions and 9 deletions

View file

@ -51,6 +51,11 @@ options:
- public
- none
default: 'none'
dns_name_label:
description:
- The Dns name label for the IP.
type: str
version_added: "2.8"
ports:
description:
- List of ports exposed within the container group.
@ -89,6 +94,40 @@ options:
ports:
description:
- List of ports exposed within the container group.
environment_variables:
description:
- List of container environment variables.
- When updating existing container all existing variables will be replaced by new ones.
type: dict
suboptions:
name:
description:
- Environment variable name.
type: str
value:
description:
- Environment variable value.
type: str
is_secure:
description:
- Is variable secure.
type: bool
version_added: "2.8"
commands:
description:
- List of commands to execute within the container instance in exec form.
- When updating existing container all existing commands will be replaced by new ones.
type: list
version_added: "2.8"
restart_policy:
description:
- Restart policy for all containers within the container group.
type: str
choices:
- always
- on_failure
- never
version_added: "2.8"
force_update:
description:
- Force update of existing container instance. Any update will result in deletion and recreation of existing containers.
@ -143,10 +182,11 @@ ip_address:
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.common.dict_transformations import _snake_to_camel
try:
from msrestazure.azure_exceptions import CloudError
from msrestazure.azure_operation import AzureOperationPoller
from msrest.polling import LROPoller
from azure.mgmt.containerinstance import ContainerInstanceManagementClient
except ImportError:
# This is handled in azure_rm_common
@ -222,6 +262,9 @@ class AzureRMContainerInstance(AzureRMModuleBase):
default='none',
choices=['public', 'none']
),
dns_name_label=dict(
type='str',
),
ports=dict(
type='list',
default=[]
@ -243,6 +286,10 @@ class AzureRMContainerInstance(AzureRMModuleBase):
type='list',
required=True
),
restart_policy=dict(
type='str',
choices=['always', 'on_failure', 'never']
),
force_update=dict(
type='bool',
default=False
@ -254,8 +301,9 @@ class AzureRMContainerInstance(AzureRMModuleBase):
self.location = None
self.state = None
self.ip_address = None
self.dns_name_label = None
self.containers = None
self.restart_policy = None
self.tags = None
@ -353,7 +401,7 @@ class AzureRMContainerInstance(AzureRMModuleBase):
ports = []
for port in self.ports:
ports.append(self.cgmodels.Port(port=port, protocol="TCP"))
ip_address = self.cgmodels.IpAddress(ports=ports, ip=self.ip_address)
ip_address = self.cgmodels.IpAddress(ports=ports, dns_name_label=self.dns_name_label, type='public')
containers = []
@ -362,24 +410,35 @@ class AzureRMContainerInstance(AzureRMModuleBase):
image = container_def.get("image")
memory = container_def.get("memory", 1.5)
cpu = container_def.get("cpu", 1)
commands = container_def.get("commands")
ports = []
variables = []
port_list = container_def.get("ports")
if port_list:
for port in port_list:
ports.append(self.cgmodels.ContainerPort(port=port))
variable_list = container_def.get("environment_variables")
if variable_list:
for variable in variable_list:
variables.append(self.cgmodels.EnvironmentVariable(name=variable.get('name'),
value=variable.get('value') if not variable.get('is_secure') else None,
secure_value=variable.get('value') if variable.get('is_secure') else None))
containers.append(self.cgmodels.Container(name=name,
image=image,
resources=self.cgmodels.ResourceRequirements(
requests=self.cgmodels.ResourceRequests(memory_in_gb=memory, cpu=cpu)
),
ports=ports))
ports=ports,
command=commands,
environment_variables=variables))
parameters = self.cgmodels.ContainerGroup(location=self.location,
containers=containers,
image_registry_credentials=registry_credentials,
restart_policy=None,
restart_policy=_snake_to_camel(self.restart_policy, True) if self.restart_policy else None,
ip_address=ip_address,
os_type=self.os_type,
volumes=None,
@ -389,7 +448,7 @@ class AzureRMContainerInstance(AzureRMModuleBase):
container_group_name=self.name,
container_group=parameters)
if isinstance(response, AzureOperationPoller):
if isinstance(response, LROPoller):
response = self.get_poller_result(response)
return response.as_dict()

View file

@ -95,6 +95,12 @@ container_groups:
returned: always
type: str
sample: 173.15.18.1
dns_name_label:
description:
- The Dns name label for the IP.
returned: always
type: str
sample: mydomain
ports:
description:
- List of ports exposed by the container instance.
@ -140,6 +146,25 @@ container_groups:
returned: always
type: list
sample: [ 80, 81 ]
commands:
description:
- List of commands to execute within the container instance in exec form.
returned: always
type: list
sample: [ "pip install abc" ]
environment_variables:
description:
- List of container environment variables.
type: complex
contains:
name:
description:
- Environment variable name.
type: str
value:
description:
- Environment variable value.
type: str
tags:
description: Tags assigned to the resource. Dictionary of string:string pairs.
type: dict
@ -147,6 +172,7 @@ container_groups:
'''
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.common.dict_transformations import _camel_to_snake
try:
from msrestazure.azure_exceptions import CloudError
@ -257,7 +283,9 @@ class AzureRMContainerInstanceFacts(AzureRMModuleBase):
'image': old_container['image'],
'memory': old_container['resources']['requests']['memory_in_gb'],
'cpu': old_container['resources']['requests']['cpu'],
'ports': []
'ports': [],
'commands': old_container.get('command'),
'environment_variables': old_container.get('environment_variables')
}
for port_index in range(len(old_container['ports'])):
new_container['ports'].append(old_container['ports'][port_index]['port'])
@ -269,9 +297,11 @@ class AzureRMContainerInstanceFacts(AzureRMModuleBase):
'name': d['name'],
'os_type': d['os_type'],
'ip_address': 'public' if d['ip_address']['type'] == 'Public' else 'none',
'dns_name_label': d['ip_address'].get('dns_name_label'),
'ports': ports,
'location': d['location'],
'containers': containers,
'restart_policy': _camel_to_snake(d.get('restart_policy')) if d.get('restart_policy') else None,
'tags': d.get('tags', None)
}
return d

View file

@ -7,7 +7,7 @@ azure-mgmt-authorization==0.51.1
azure-mgmt-batch==4.1.0
azure-mgmt-cdn==3.0.0
azure-mgmt-compute==4.4.0
azure-mgmt-containerinstance==0.4.0
azure-mgmt-containerinstance==1.4.0
azure-mgmt-containerregistry==2.0.0
azure-mgmt-containerservice==4.4.0
azure-mgmt-dns==2.1.0

View file

@ -79,7 +79,9 @@
name: "aci{{ resource_group | hash('md5') | truncate(7, True, '') }}sec"
os_type: linux
ip_address: public
dns_name_label: mydnslabel{{ resource_group | hash('md5') | truncate(7, True, '') }}
location: eastus
restart_policy: on_failure
ports:
- 80
containers:
@ -89,6 +91,12 @@
ports:
- 80
- 81
commands:
- echo abc
- echo cdf
environment_variables:
- name: myvar
value: myvarvalue
register: output
- name: Gather facts for single Container Instance
@ -97,6 +105,9 @@
name: "aci{{ resource_group | hash('md5') | truncate(7, True, '') }}sec"
register: output
- debug:
var: output
- name: Assert that facts are returned
assert:
that:
@ -108,6 +119,9 @@
- output.containerinstances[0]['ip_address'] != None
- output.containerinstances[0]['ports'] != None
- output.containerinstances[0]['containers'] != None
- output.containerinstances[0]['containers'][0]['commands'] | length == 2
- output.containerinstances[0]['containers'][0]['environment_variables'] | length == 1
- output.containerinstances[0]['restart_policy'] == 'on_failure'
- name: Gather facts for all Container Instances in the resource group
azure_rm_containerinstance_facts:

View file

@ -7,7 +7,7 @@ azure-mgmt-authorization==0.51.1
azure-mgmt-batch==4.1.0
azure-mgmt-cdn==3.0.0
azure-mgmt-compute==4.4.0
azure-mgmt-containerinstance==0.4.0
azure-mgmt-containerinstance==1.4.0
azure-mgmt-containerregistry==2.0.0
azure-mgmt-containerservice==4.4.0
azure-mgmt-dns==2.1.0