Skip to content

Commit d16c1ef

Browse files
INFRA-9588: Fix spawning of public cloud instances with private interface (#76)
Add a module to retrieve private network and return the openstack id of the network matching a given region.
1 parent 2b6b879 commit d16c1ef

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

plugins/modules/public_cloud_instance_interface.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
interface_ip:
3535
required: true
3636
description: The fixed IP to set to the interface
37-
interface_network_id:
37+
interface_openstack_id:
3838
required: true
39-
description: The network id to attache the interface to
39+
description:
40+
- The network's openstack id to attache the interface to
41+
- This is returned by a call to public_cloud_private_network_info.
4042
'''
4143

4244
EXAMPLES = r'''
@@ -45,7 +47,7 @@
4547
service_name: "{{ service_name }}"
4648
instance_id: "{{ instance_id }}"
4749
interface_ip: "{{ network_vrack.ip }}"
48-
interface_network_id: "{{ network_vrack.network_id }}"
50+
interface_openstack_id: "{{ network_info.openstack_id }}"
4951
delegate_to: localhost
5052
register: interface_metadata
5153
@@ -63,7 +65,7 @@ def run_module():
6365
instance_id=dict(required=True),
6466
state=dict(choices=['present', 'absent'], default='present'),
6567
interface_ip=dict(required=True),
66-
interface_network_id=dict(required=True)
68+
interface_openstack_id=dict(required=True)
6769
))
6870

6971
module = AnsibleModule(
@@ -76,21 +78,21 @@ def run_module():
7678
instance_id = module.params['instance_id']
7779
state = module.params['state']
7880
interface_ip = module.params['interface_ip']
79-
interface_network_id = module.params['interface_network_id']
81+
interface_openstack_id = module.params['interface_openstack_id']
8082

8183
if module.check_mode:
8284
module.exit_json(msg="Ensure interface {} on {} is {} on instance id {} - (dry run mode)"
83-
.format(interface_ip, interface_network_id, state, instance_id),
85+
.format(interface_ip, interface_openstack_id, state, instance_id),
8486
changed=True)
8587

8688
if state == 'absent':
8789
# Need to get the interface id (via /cloud/project/{serviceName}/instance/{instanceId}/interface).
8890
# How to manage multiple interfaces ?
8991
module.fail_json(msg="Removing an interface is not yet implemented")
9092
if state == 'present':
91-
result = client.wrap_call("POST", f"/cloud/project/{service_name}/instance/{isinstance}/interface",
93+
result = client.wrap_call("POST", f"/cloud/project/{service_name}/instance/{instance_id}/interface",
9294
ip=interface_ip,
93-
networkId=interface_network_id)
95+
networkId=interface_openstack_id)
9496
module.exit_json(
9597
changed=True,
9698
msg="Interface has been attached to instance id {}".format(
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
from __future__ import absolute_import, division, print_function
5+
6+
from ansible.module_utils.basic import AnsibleModule
7+
8+
__metaclass__ = type
9+
10+
DOCUMENTATION = r"""
11+
---
12+
module: public_cloud_private_network_info
13+
14+
short_description: Get information about private networks for a given region.
15+
16+
description:
17+
- Get the openstack id for a private network depending on the region.
18+
19+
requirements:
20+
- ovh >= 0.5.0
21+
22+
options:
23+
service_name:
24+
required: true
25+
description: The service name
26+
private_network:
27+
required: true
28+
description: The OVH private network
29+
region:
30+
required: true
31+
description: The region where to lookup for network
32+
"""
33+
34+
EXAMPLES = r"""
35+
- name: Get the openstack id for the private network in the region
36+
synthesio.ovh.public_cloud_private_network_info:
37+
service_name: "{{ service_name }}"
38+
private_network: "{{ network }}"
39+
region: "GRA11"
40+
delegate_to: localhost
41+
register: network_info
42+
43+
"""
44+
45+
RETURN = r"""
46+
openstack_id:
47+
description: Openstack alpha numeric identifier of the network
48+
returned: when matching region is found
49+
type: str
50+
sample: 54e97ee2-407c-4dbc-a833-39d2910514d4
51+
# """
52+
53+
from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import (
54+
OVH,
55+
ovh_argument_spec,
56+
)
57+
58+
59+
def run_module():
60+
module_args = ovh_argument_spec()
61+
module_args.update(
62+
dict(
63+
service_name=dict(required=True),
64+
private_network=dict(required=True),
65+
region=dict(required=True),
66+
)
67+
)
68+
69+
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
70+
client = OVH(module)
71+
72+
service_name = module.params["service_name"]
73+
private_network = module.params["private_network"]
74+
region = module.params["region"]
75+
76+
network_list = client.wrap_call(
77+
"GET", f"/cloud/project/{service_name}/network/private/{private_network}"
78+
)
79+
80+
for network in network_list["regions"]:
81+
if network["region"] == region:
82+
module.exit_json(changed=False, openstack_id=network["openstackId"])
83+
84+
module.fail_json(msg=f"No network found for {region}", changed=False)
85+
86+
87+
def main():
88+
run_module()
89+
90+
91+
if __name__ == "__main__":
92+
main()

0 commit comments

Comments
 (0)