From 0a2971dcf50f337f1ae4ca257dfc58f95ff9c6d8 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Thu, 21 Mar 2019 17:01:12 +0800 Subject: [PATCH] adding new container instance options (#54072) --- .../cloud/azure/azure_rm_containerinstance.py | 71 +++++++++++++++++-- .../azure/azure_rm_containerinstance_facts.py | 32 ++++++++- packaging/requirements/requirements-azure.txt | 2 +- .../azure_rm_containerinstance/tasks/main.yml | 14 ++++ .../requirements/integration.cloud.azure.txt | 2 +- 5 files changed, 112 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py index f958230a0b..220538b05a 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance.py @@ -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() diff --git a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_facts.py index 488f3b81ce..2680e5379c 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_facts.py @@ -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 diff --git a/packaging/requirements/requirements-azure.txt b/packaging/requirements/requirements-azure.txt index 57ba4ad259..59485beb47 100644 --- a/packaging/requirements/requirements-azure.txt +++ b/packaging/requirements/requirements-azure.txt @@ -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 diff --git a/test/integration/targets/azure_rm_containerinstance/tasks/main.yml b/test/integration/targets/azure_rm_containerinstance/tasks/main.yml index 032cd63e53..13ce1a7a7b 100644 --- a/test/integration/targets/azure_rm_containerinstance/tasks/main.yml +++ b/test/integration/targets/azure_rm_containerinstance/tasks/main.yml @@ -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: diff --git a/test/runner/requirements/integration.cloud.azure.txt b/test/runner/requirements/integration.cloud.azure.txt index 57ba4ad259..59485beb47 100644 --- a/test/runner/requirements/integration.cloud.azure.txt +++ b/test/runner/requirements/integration.cloud.azure.txt @@ -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