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

INFRA-9588: Fix spawning of public cloud instances with private interface #76

Merged
merged 1 commit into from
Mar 12, 2024
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
18 changes: 10 additions & 8 deletions plugins/modules/public_cloud_instance_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
interface_ip:
required: true
description: The fixed IP to set to the interface
interface_network_id:
interface_openstack_id:
required: true
description: The network id to attache the interface to
description:
- The network's openstack id to attache the interface to
- This is returned by a call to public_cloud_private_network_info.
'''

EXAMPLES = r'''
Expand All @@ -45,7 +47,7 @@
service_name: "{{ service_name }}"
instance_id: "{{ instance_id }}"
interface_ip: "{{ network_vrack.ip }}"
interface_network_id: "{{ network_vrack.network_id }}"
interface_openstack_id: "{{ network_info.openstack_id }}"
delegate_to: localhost
register: interface_metadata

Expand All @@ -63,7 +65,7 @@ def run_module():
instance_id=dict(required=True),
state=dict(choices=['present', 'absent'], default='present'),
interface_ip=dict(required=True),
interface_network_id=dict(required=True)
interface_openstack_id=dict(required=True)
))

module = AnsibleModule(
Expand All @@ -76,21 +78,21 @@ def run_module():
instance_id = module.params['instance_id']
state = module.params['state']
interface_ip = module.params['interface_ip']
interface_network_id = module.params['interface_network_id']
interface_openstack_id = module.params['interface_openstack_id']

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

if state == 'absent':
# Need to get the interface id (via /cloud/project/{serviceName}/instance/{instanceId}/interface).
# How to manage multiple interfaces ?
module.fail_json(msg="Removing an interface is not yet implemented")
if state == 'present':
result = client.wrap_call("POST", f"/cloud/project/{service_name}/instance/{isinstance}/interface",
tortuegenialez marked this conversation as resolved.
Show resolved Hide resolved
result = client.wrap_call("POST", f"/cloud/project/{service_name}/instance/{instance_id}/interface",
ip=interface_ip,
networkId=interface_network_id)
networkId=interface_openstack_id)
module.exit_json(
changed=True,
msg="Interface has been attached to instance id {}".format(
Expand Down
92 changes: 92 additions & 0 deletions plugins/modules/public_cloud_private_network_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function

from ansible.module_utils.basic import AnsibleModule

__metaclass__ = type

DOCUMENTATION = r"""
---
module: public_cloud_private_network_info

short_description: Get information about private networks for a given region.

description:
- Get the openstack id for a private network depending on the region.

requirements:
- ovh >= 0.5.0

options:
service_name:
required: true
description: The service name
private_network:
required: true
description: The OVH private network
region:
required: true
description: The region where to lookup for network
"""

EXAMPLES = r"""
- name: Get the openstack id for the private network in the region
synthesio.ovh.public_cloud_private_network_info:
service_name: "{{ service_name }}"
private_network: "{{ network }}"
region: "GRA11"
delegate_to: localhost
register: network_info

"""

RETURN = r"""
openstack_id:
description: Openstack alpha numeric identifier of the network
returned: when matching region is found
type: str
sample: 54e97ee2-407c-4dbc-a833-39d2910514d4
# """

from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import (
OVH,
ovh_argument_spec,
)


def run_module():
module_args = ovh_argument_spec()
module_args.update(
dict(
service_name=dict(required=True),
private_network=dict(required=True),
region=dict(required=True),
)
)

module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
client = OVH(module)

service_name = module.params["service_name"]
private_network = module.params["private_network"]
region = module.params["region"]

network_list = client.wrap_call(
"GET", f"/cloud/project/{service_name}/network/private/{private_network}"
)

for network in network_list["regions"]:
if network["region"] == region:
module.exit_json(changed=False, openstack_id=network["openstackId"])

module.fail_json(msg=f"No network found for {region}", changed=False)


def main():
run_module()


if __name__ == "__main__":
main()
Loading