Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add support for defining interfaces in instance configs #247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions docs/modules/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,16 @@ Manage Linode Instances, Configs, and Disks.

| Field | Type | Required | Description |
|-----------|------|----------|------------------------------------------------------------------------------|
| [`devices` (sub-options)](#devices) | <center>`dict`</center> | <center>**Required**</center> | The devices to map to this configuration. |
| `label` | <center>`str`</center> | <center>**Required**</center> | The label to assign to this config. |
| `comments` | <center>`str`</center> | <center>Optional</center> | Arbitrary User comments on this Config. **(Updatable)** |
| [`devices` (sub-options)](#devices) | <center>`dict`</center> | <center>Optional</center> | The devices to map to this configuration. |
| [`helpers` (sub-options)](#helpers) | <center>`dict`</center> | <center>Optional</center> | Helpers enabled when booting to this Linode Config. |
| `kernel` | <center>`str`</center> | <center>Optional</center> | A Kernel ID to boot a Linode with. Defaults to “linode/latest-64bit”. **(Updatable)** |
| `memory_limit` | <center>`int`</center> | <center>Optional</center> | Defaults to the total RAM of the Linode. **(Updatable)** |
| `root_device` | <center>`str`</center> | <center>Optional</center> | The root device to boot. **(Updatable)** |
| `run_level` | <center>`str`</center> | <center>Optional</center> | Defines the state of your Linode after booting. **(Updatable)** |
| `virt_mode` | <center>`str`</center> | <center>Optional</center> | Controls the virtualization mode. **(Choices: `paravirt`, `fullvirt`; Updatable)** |
| [`interfaces` (sub-options)](#interfaces) | <center>`list`</center> | <center>Optional</center> | A list of network interfaces to apply to the Linode. See the [Linode API documentation](https://www.linode.com/docs/api/linode-instances/#configuration-profile-create__request-body-schema). **(Updatable)** |



Expand Down Expand Up @@ -244,6 +245,18 @@ Manage Linode Instances, Configs, and Disks.



### interfaces

| Field | Type | Required | Description |
|-----------|------|----------|------------------------------------------------------------------------------|
| `purpose` | <center>`str`</center> | <center>**Required**</center> | The type of interface. **(Choices: `public`, `vlan`)** |
| `label` | <center>`str`</center> | <center>Optional</center> | The name of this interface. Required for vlan purpose interfaces. Must be an empty string or null for public purpose interfaces. |
| `ipam_address` | <center>`str`</center> | <center>Optional</center> | This Network Interface’s private IP address in Classless Inter-Domain Routing (CIDR) notation. |





### disks

| Field | Type | Required | Description |
Expand All @@ -262,18 +275,6 @@ Manage Linode Instances, Configs, and Disks.



### interfaces

| Field | Type | Required | Description |
|-----------|------|----------|------------------------------------------------------------------------------|
| `purpose` | <center>`str`</center> | <center>**Required**</center> | The type of interface. **(Choices: `public`, `vlan`)** |
| `label` | <center>`str`</center> | <center>Optional</center> | The name of this interface. Required for vlan purpose interfaces. Must be an empty string or null for public purpose interfaces. |
| `ipam_address` | <center>`str`</center> | <center>Optional</center> | This Network Interface’s private IP address in Classless Inter-Domain Routing (CIDR) notation. |






## Return Values

Expand Down
45 changes: 37 additions & 8 deletions plugins/modules/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from ansible_collections.linode.cloud.plugins.module_utils.linode_common import \
LinodeModuleBase
from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import \
filter_null_values, paginated_list_to_json, drop_empty_strings, mapping_to_dict, request_retry
filter_null_values, paginated_list_to_json, drop_empty_strings, mapping_to_dict, \
request_retry, filter_null_values_recursive
from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import global_authors, \
global_requirements

Expand Down Expand Up @@ -149,7 +150,7 @@
description='Arbitrary User comments on this Config.'),

devices=dict(
type='dict', options=linode_instance_devices_spec,
type='dict', required=True, options=linode_instance_devices_spec,
description='The devices to map to this configuration.'),

helpers=dict(
Expand Down Expand Up @@ -182,7 +183,15 @@
choices=[
'paravirt',
'fullvirt'
])
]),
interfaces=dict(
type='list', elements='dict', options=linode_instance_interface_spec,
editable=True,
description=[
'A list of network interfaces to apply to the Linode.',
'See the [Linode API documentation](https://www.linode.com/docs/api/linode-instances'
'/#configuration-profile-create__request-body-schema).'
]),
)

linode_instance_spec = dict(
Expand Down Expand Up @@ -342,7 +351,8 @@
'memory_limit',
'root_device',
'run_level',
'virt_mode'
'virt_mode',
'interfaces'
}


Expand All @@ -355,6 +365,8 @@ def __init__(self) -> None:
self.mutually_exclusive = [
('image', 'disks'),
('image', 'configs'),
('interfaces', 'configs'),
('interfaces', 'disks')
]

self.results: dict = dict(
Expand Down Expand Up @@ -503,11 +515,20 @@ def _create_config_register(self, config_params: Dict[str, Any]) -> None:
device_params = config_params.pop('devices')
devices = []

for device_suffix in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']:
device_dict = device_params.get('sd{0}'.format(device_suffix))
devices.append(self._param_device_to_device(device_dict))
if device_params is not None:
for device_suffix in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of code could use a comment.

device_name = 'sd{0}'.format(device_suffix)
if device_name not in device_params:
continue

device_dict = device_params.get(device_name)
devices.append(self._param_device_to_device(device_dict))

try:
self._instance.config_create(devices=devices, **filter_null_values(config_params))
except ValueError as err:
self.fail(msg=';'.join(err.args))

self._instance.config_create(devices=devices, **filter_null_values(config_params))
self.register_action('Created config {0}'.format(config_params.get('label')))

def _delete_config_register(self, config: Config) -> None:
Expand Down Expand Up @@ -591,6 +612,14 @@ def _update_config(self, config: Config, config_params: Dict[str, Any]) -> None:

old_value = mapping_to_dict(getattr(config, key))

# Special handling for the ConfigInterface type
if key == 'interfaces':
old_value = filter_null_values_recursive([
drop_empty_strings(v._serialize()) for v in old_value])
new_value = filter_null_values_recursive([
drop_empty_strings(v) for v in new_value
])

# Special diffing due to handling in linode_api4-python
if key == 'devices':
for device_key, device in old_value.items():
Expand Down
117 changes: 117 additions & 0 deletions tests/integration/targets/instance_config_vlan/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
- name: instance_config_vlan
block:
- set_fact:
r: "{{ 1000000000 | random }}"

- name: Create a Linode instance with interface
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}'
region: us-southeast
type: g6-standard-1
disks:
- label: test-disk
filesystem: ext4
size: 10
configs:
- label: cool-config
devices:
sda:
disk_label: test-disk
interfaces:
- purpose: vlan
label: really-cool-vlan
wait: false
booted: false
state: present
register: create_instance

- name: Assert instance created
assert:
that:
- create_instance.changed
- create_instance.instance.ipv4|length == 1

- create_instance.configs[0].interfaces[0].purpose == 'vlan'
- create_instance.configs[0].interfaces[0].label == 'really-cool-vlan'

- name: Update the instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}'
region: us-southeast
type: g6-standard-1
disks:
- label: test-disk
filesystem: ext4
size: 10
configs:
- label: cool-config
devices:
sda:
disk_label: test-disk
interfaces:
- purpose: public
- purpose: vlan
label: really-cool-vlan
wait: false
booted: false
state: present
register: update_instance

- assert:
that:
- update_instance.changed
- update_instance.instance.ipv4|length == 1

- update_instance.configs[0].interfaces[0].purpose == 'public'
- update_instance.configs[0].interfaces[1].purpose == 'vlan'
- update_instance.configs[0].interfaces[1].label == 'really-cool-vlan'

- name: Don't change the instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: 'ansible-test-{{ r }}'
region: us-southeast
type: g6-standard-1
disks:
- label: test-disk
filesystem: ext4
size: 10
configs:
- label: cool-config
devices:
sda:
disk_label: test-disk
interfaces:
- purpose: public
- purpose: vlan
label: really-cool-vlan
wait: false
booted: false
state: present
register: unchanged_instance

- assert:
that:
- unchanged_instance.changed == False

always:
- ignore_errors: yes
block:
- name: Delete a Linode instance
linode.cloud.instance:
api_token: '{{ api_token }}'
ua_prefix: '{{ ua_prefix }}'
label: '{{ create_instance.instance.label }}'
state: absent
register: delete_instance

- name: Assert instance delete succeeded
assert:
that:
- delete_instance.changed
- delete_instance.instance.id == create_instance.instance.id