mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 05:40:23 -07:00
Add scw_compute_private_network (#4727)
* Add scw_compute_private_network * fix argument required and BOTMETA * little fix in commentary/doc * test with link for ansible-doc check * remove unwanted file * fix entry missing in meta/runtime.yml * scaleway_compute_private_network add some check in test and some fic in doc * a=add missing del os.environ * fix whitespace * test_scaleway_compute_private_network : fix test * test_scaleway_compute_private_network : fix pep8 * scaleway_compute_private_network add . in description * scaleway_compute_private_network: fix var name * [scaleway_compute_private_network] add name for the example's task * Update plugins/modules/cloud/scaleway/scaleway_compute_private_network.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/cloud/scaleway/scaleway_compute_private_network.py Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
739ca737f1
commit
7f4c11cd64
4 changed files with 392 additions and 0 deletions
|
@ -0,0 +1,179 @@
|
|||
# Copyright: (c) 2019, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
import os
|
||||
import json
|
||||
import pytest
|
||||
|
||||
|
||||
from ansible_collections.community.general.plugins.modules.cloud.scaleway import scaleway_compute_private_network
|
||||
from ansible_collections.community.general.plugins.module_utils.scaleway import Scaleway, Response
|
||||
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
|
||||
from ansible_collections.community.general.tests.unit.compat.mock import patch
|
||||
|
||||
|
||||
def response_without_nics():
|
||||
info = {"status": 200,
|
||||
"body": '{ "private_nics": []}'
|
||||
}
|
||||
return Response(None, info)
|
||||
|
||||
|
||||
def response_with_nics():
|
||||
info = {"status": 200,
|
||||
"body": ('{ "private_nics": [{'
|
||||
'"id": "c123b4cd-ef5g-678h-90i1-jk2345678l90",'
|
||||
'"private_network_id": "b589b4cd-ef5g-678h-90i1-jk2345678l90",'
|
||||
'"server_id": "c004b4cd-ef5g-678h-90i1-jk2345678l90",'
|
||||
'"mac_address": "02:00:00:00:12:23",'
|
||||
'"state": "available",'
|
||||
'"creation_date": "2022-03-30T06:25:28.155973+00:00",'
|
||||
'"modification_date": "2022-03-30T06:25:28.155973+00:00",'
|
||||
'"zone": "fr-par-1"'
|
||||
'}]}'
|
||||
)
|
||||
}
|
||||
return Response(None, info)
|
||||
|
||||
|
||||
def response_when_add_nics():
|
||||
info = {"status": 200,
|
||||
"body": ('{ "private_nics": {'
|
||||
'"id": "c123b4cd-ef5g-678h-90i1-jk2345678l90",'
|
||||
'"private_network_id": "b589b4cd-ef5g-678h-90i1-jk2345678l90",'
|
||||
'"server_id": "c004b4cd-ef5g-678h-90i1-jk2345678l90",'
|
||||
'"mac_address": "02:00:00:00:12:23",'
|
||||
'"state": "available",'
|
||||
'"creation_date": "2022-03-30T06:25:28.155973+00:00",'
|
||||
'"modification_date": "2022-03-30T06:25:28.155973+00:00",'
|
||||
'"zone": "fr-par-1"'
|
||||
'}}'
|
||||
)
|
||||
}
|
||||
return Response(None, info)
|
||||
|
||||
|
||||
def response_remove_nics():
|
||||
info = {"status": 200}
|
||||
return Response(None, info)
|
||||
|
||||
|
||||
def test_scaleway_private_network_without_arguments(capfd):
|
||||
set_module_args({})
|
||||
with pytest.raises(SystemExit) as results:
|
||||
scaleway_compute_private_network.main()
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
assert not err
|
||||
assert json.loads(out)['failed']
|
||||
|
||||
|
||||
def test_scaleway_add_nic(capfd):
|
||||
os.environ['SCW_API_TOKEN'] = 'notrealtoken'
|
||||
pnid = 'b589b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
cid = 'c004b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
url = 'servers/' + cid + '/private_nics'
|
||||
|
||||
set_module_args({"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
|
||||
"state": "present",
|
||||
"region": "par1",
|
||||
"compute_id": cid,
|
||||
"private_network_id": pnid
|
||||
})
|
||||
|
||||
with patch.object(Scaleway, 'get') as mock_scw_get:
|
||||
mock_scw_get.return_value = response_without_nics()
|
||||
with patch.object(Scaleway, 'post') as mock_scw_post:
|
||||
mock_scw_post.return_value = response_when_add_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_post.assert_any_call(path=url, data={"private_network_id": pnid})
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
del os.environ['SCW_API_TOKEN']
|
||||
assert not err
|
||||
assert json.loads(out)['changed']
|
||||
|
||||
|
||||
def test_scaleway_add_existing_nic(capfd):
|
||||
os.environ['SCW_API_TOKEN'] = 'notrealtoken'
|
||||
pnid = 'b589b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
cid = 'c004b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
url = 'servers/' + cid + '/private_nics'
|
||||
|
||||
set_module_args({"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
|
||||
"state": "present",
|
||||
"region": "par1",
|
||||
"compute_id": cid,
|
||||
"private_network_id": pnid
|
||||
})
|
||||
|
||||
with patch.object(Scaleway, 'get') as mock_scw_get:
|
||||
mock_scw_get.return_value = response_with_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
del os.environ['SCW_API_TOKEN']
|
||||
assert not err
|
||||
assert not json.loads(out)['changed']
|
||||
|
||||
|
||||
def test_scaleway_remove_existing_nic(capfd):
|
||||
os.environ['SCW_API_TOKEN'] = 'notrealtoken'
|
||||
pnid = 'b589b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
cid = 'c004b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
nicid = 'c123b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
url = 'servers/' + cid + '/private_nics'
|
||||
urlremove = 'servers/' + cid + '/private_nics/' + nicid
|
||||
|
||||
set_module_args({"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
|
||||
"state": "absent",
|
||||
"region": "par1",
|
||||
"compute_id": cid,
|
||||
"private_network_id": pnid
|
||||
})
|
||||
|
||||
with patch.object(Scaleway, 'get') as mock_scw_get:
|
||||
mock_scw_get.return_value = response_with_nics()
|
||||
with patch.object(Scaleway, 'delete') as mock_scw_delete:
|
||||
mock_scw_delete.return_value = response_remove_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_delete.assert_any_call(urlremove)
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
|
||||
del os.environ['SCW_API_TOKEN']
|
||||
assert not err
|
||||
assert json.loads(out)['changed']
|
||||
|
||||
|
||||
def test_scaleway_remove_absent_nic(capfd):
|
||||
os.environ['SCW_API_TOKEN'] = 'notrealtoken'
|
||||
pnid = 'b589b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
cid = 'c004b4cd-ef5g-678h-90i1-jk2345678l90'
|
||||
url = 'servers/' + cid + '/private_nics'
|
||||
|
||||
set_module_args({"project": "a123b4cd-ef5g-678h-90i1-jk2345678l90",
|
||||
"state": "absent",
|
||||
"region": "par1",
|
||||
"compute_id": cid,
|
||||
"private_network_id": pnid
|
||||
})
|
||||
|
||||
with patch.object(Scaleway, 'get') as mock_scw_get:
|
||||
mock_scw_get.return_value = response_without_nics()
|
||||
with pytest.raises(SystemExit) as results:
|
||||
scaleway_compute_private_network.main()
|
||||
mock_scw_get.assert_any_call(url)
|
||||
|
||||
out, err = capfd.readouterr()
|
||||
del os.environ['SCW_API_TOKEN']
|
||||
assert not err
|
||||
assert not json.loads(out)['changed']
|
Loading…
Add table
Add a link
Reference in a new issue