Skip to content

Commit

Permalink
cloudscale_server: add tags support (ansible#60396)
Browse files Browse the repository at this point in the history
  • Loading branch information
resmo authored Aug 14, 2019
1 parent 5298d31 commit f02f5c4
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/ansible/modules/cloud/cloudscale/cloudscale_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
default: no
type: bool
version_added: '2.8'
tags:
description:
- Tags assosiated with the servers. Set this to C({}) to clear any tags.
type: dict
version_added: '2.9'
extends_documentation_fragment: cloudscale
'''

Expand Down Expand Up @@ -245,6 +250,12 @@
type: list
sample: [ {"href": "https://api.cloudscale.ch/v1/server-groups/...", "uuid": "...", "name": "db-group"} ]
version_added: '2.8'
tags:
description: Tags assosiated with the volume.
returned: success
type: dict
sample: { 'project': 'my project' }
version_added: '2.9'
'''

from datetime import datetime, timedelta
Expand Down Expand Up @@ -453,6 +464,7 @@ def _update_server(self, server_info):

server_info = self._update_param('flavor', server_info, requires_stop=True)
server_info = self._update_param('name', server_info)
server_info = self._update_param('tags', server_info)

if previous_state == "running":
server_info = self._start_stop_server(server_info, target_state="running", ignore_diff=True)
Expand Down Expand Up @@ -508,7 +520,8 @@ def main():
anti_affinity_with=dict(removed_in_version='2.11'),
server_groups=dict(type='list'),
user_data=dict(),
force=dict(type='bool', default=False)
force=dict(type='bool', default=False),
tags=dict(type='dict'),
))

module = AnsibleModule(
Expand Down
137 changes: 137 additions & 0 deletions test/integration/targets/cloudscale_server/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags:
project: ansible-test
stage: production
sla: 24-7
register: server
check_mode: yes
- name: Verify create a running server in check mode
Expand All @@ -27,13 +31,20 @@
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags:
project: ansible-test
stage: production
sla: 24-7
register: server
- name: Verify create a running server
assert:
that:
- server is changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- server.tags.project == 'ansible-test'
- server.tags.stage == 'production'
- server.tags.sla == '24-7'

- name: Test create a running server idempotence
cloudscale_server:
Expand All @@ -42,13 +53,139 @@
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags:
project: ansible-test
stage: production
sla: 24-7
register: server
- name: Verify create a running server idempotence
assert:
that:
- server is not changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- server.tags.project == 'ansible-test'
- server.tags.stage == 'production'
- server.tags.sla == '24-7'

- name: Test update tags in check mode
cloudscale_server:
name: '{{ cloudscale_resource_prefix }}-test'
flavor: '{{ cloudscale_test_flavor }}'
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags:
project: ansible-test
stage: staging
sla: 8-5
register: server
check_mode: yes
- name: Verify update tags in check mode
assert:
that:
- server is changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- server.tags.project == 'ansible-test'
- server.tags.stage == 'production'
- server.tags.sla == '24-7'

- name: Test update tags
cloudscale_server:
name: '{{ cloudscale_resource_prefix }}-test'
flavor: '{{ cloudscale_test_flavor }}'
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags:
project: ansible-test
stage: staging
sla: 8-5
register: server
- name: Verify update tags
assert:
that:
- server is changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- server.tags.project == 'ansible-test'
- server.tags.stage == 'staging'
- server.tags.sla == '8-5'

- name: Test update tags idempotence
cloudscale_server:
name: '{{ cloudscale_resource_prefix }}-test'
flavor: '{{ cloudscale_test_flavor }}'
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags:
project: ansible-test
stage: staging
sla: 8-5
register: server
- name: Verify update tags idempotence
assert:
that:
- server is not changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- server.tags.project == 'ansible-test'
- server.tags.stage == 'staging'
- server.tags.sla == '8-5'

- name: Test omit tags idempotence
cloudscale_server:
name: '{{ cloudscale_resource_prefix }}-test'
flavor: '{{ cloudscale_test_flavor }}'
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
register: server
- name: Verify update tags idempotence
assert:
that:
- server is not changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- server.tags.project == 'ansible-test'
- server.tags.stage == 'staging'
- server.tags.sla == '8-5'

- name: Test delete tags
cloudscale_server:
name: '{{ cloudscale_resource_prefix }}-test'
flavor: '{{ cloudscale_test_flavor }}'
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags: {}
register: server
- name: Verify delete tags
assert:
that:
- server is changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- not server.tags

- name: Test delete tags idempotence
cloudscale_server:
name: '{{ cloudscale_resource_prefix }}-test'
flavor: '{{ cloudscale_test_flavor }}'
image: '{{ cloudscale_test_image }}'
ssh_keys: '{{ cloudscale_test_ssh_key }}'
server_groups: '{{ cloudscale_resource_prefix }}-group-1'
tags: {}
register: server
- name: Verify delete tags idempotence
assert:
that:
- server is not changed
- server.state == 'running'
- server.server_groups.0.name == '{{ cloudscale_resource_prefix }}-group-1'
- not server.tags

- name: Test update flavor of a running server without force in check mode
cloudscale_server:
Expand Down

0 comments on commit f02f5c4

Please sign in to comment.