From f9e3e229ddfc9ffca9871b292c3be9e825244446 Mon Sep 17 00:00:00 2001 From: Fabio Sangiovanni <4040184+sanjioh@users.noreply.github.com> Date: Sun, 18 Apr 2021 09:45:26 +0200 Subject: [PATCH] linode_v4: add support for `private_ip` option. (#2249) * linode_v4: add support for `private_ip` option. * linode_v4: remove `required` attribute from `private_ip` parameter. * linode_v4: add changelog fragment. * linode_v4: add PR link to changelog fragment. Co-authored-by: Amin Vakil * linode_v4: add the `version_added` attribute to the `private_ip` section of module documentation Co-authored-by: Felix Fontein * linode_v4: improve styling of `private_ip` docs Co-authored-by: Felix Fontein Co-authored-by: Amin Vakil Co-authored-by: Felix Fontein --- ...9-linode_v4-support-private_ip-option.yaml | 2 + plugins/modules/cloud/linode/linode_v4.py | 9 ++++ .../modules/cloud/linode/test_linode_v4.py | 49 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 changelogs/fragments/2249-linode_v4-support-private_ip-option.yaml diff --git a/changelogs/fragments/2249-linode_v4-support-private_ip-option.yaml b/changelogs/fragments/2249-linode_v4-support-private_ip-option.yaml new file mode 100644 index 0000000000..e5d6ca02d7 --- /dev/null +++ b/changelogs/fragments/2249-linode_v4-support-private_ip-option.yaml @@ -0,0 +1,2 @@ +minor_changes: + - linode_v4 - add support for ``private_ip`` option (https://github.com/ansible-collections/community.general/pull/2249). diff --git a/plugins/modules/cloud/linode/linode_v4.py b/plugins/modules/cloud/linode/linode_v4.py index 34d33871bd..0f1133bac0 100644 --- a/plugins/modules/cloud/linode/linode_v4.py +++ b/plugins/modules/cloud/linode/linode_v4.py @@ -53,6 +53,13 @@ options: group labelling is deprecated but still supported. The encouraged method for marking instances is to use tags. type: str + private_ip: + description: + - If C(true), the created Linode will have private networking enabled and + assigned a private IPv4 address. + type: bool + default: false + version_added: 3.0.0 tags: description: - The tags that the instance should be marked under. See @@ -238,6 +245,7 @@ def initialise_module(): authorized_keys=dict(type='list', elements='str', no_log=False), group=dict(type='str'), image=dict(type='str'), + private_ip=dict(type='bool', default=False), region=dict(type='str'), root_pass=dict(type='str', no_log=True), tags=dict(type='list', elements='str'), @@ -283,6 +291,7 @@ def main(): group=module.params['group'], image=module.params['image'], label=module.params['label'], + private_ip=module.params['private_ip'], region=module.params['region'], root_pass=module.params['root_pass'], tags=module.params['tags'], diff --git a/tests/unit/plugins/modules/cloud/linode/test_linode_v4.py b/tests/unit/plugins/modules/cloud/linode/test_linode_v4.py index fece341431..c966f79d5b 100644 --- a/tests/unit/plugins/modules/cloud/linode/test_linode_v4.py +++ b/tests/unit/plugins/modules/cloud/linode/test_linode_v4.py @@ -175,6 +175,55 @@ def test_optional_image_is_validated(default_args, capfd, access_token): )) +@pytest.mark.parametrize('value', [True, False]) +def test_private_ip_valid_values(default_args, access_token, value): + default_args.update({'private_ip': value}) + set_module_args(default_args) + + module = linode_v4.initialise_module() + + assert module.params['private_ip'] is value + + +@pytest.mark.parametrize('value', ['not-a-bool', 42]) +def test_private_ip_invalid_values(default_args, capfd, access_token, value): + default_args.update({'private_ip': value}) + set_module_args(default_args) + + with pytest.raises(SystemExit): + linode_v4.initialise_module() + + out, err = capfd.readouterr() + results = json.loads(out) + + assert results['failed'] is True + assert 'not a valid boolean' in results['msg'] + + +def test_private_ip_default_value(default_args, access_token): + default_args.pop('private_ip', None) + set_module_args(default_args) + + module = linode_v4.initialise_module() + + assert module.params['private_ip'] is False + + +def test_private_ip_is_forwarded_to_linode(default_args, mock_linode, access_token): + default_args.update({'private_ip': True}) + set_module_args(default_args) + + target = 'linode_api4.linode_client.LinodeGroup.instances' + with mock.patch(target, return_value=[]): + with pytest.raises(SystemExit): + target = 'linode_api4.linode_client.LinodeGroup.instance_create' + with mock.patch(target, return_value=(mock_linode, 'passw0rd')) as instance_create_mock: + linode_v4.main() + + args, kwargs = instance_create_mock.call_args + assert kwargs['private_ip'] is True + + def test_instance_already_created(default_args, mock_linode, capfd,