| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway VPC management module |
| 4 |
# |
| 5 |
# Copyright (c) Ansible project |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: scaleway_compute_private_network |
| 13 |
short_description: Scaleway compute - private network management |
| 14 |
version_added: 5.2.0 |
| 15 |
author: Pascal MANGIN (@pastral) |
| 16 |
description: |
| 17 |
- This module add or remove a private network to a compute instance (U(https://developer.scaleway.com)). |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._scaleway |
| 20 |
- community.general._attributes |
| 21 |
- community.general._scaleway.actiongroup_scaleway |
| 22 |
|
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
action_group: |
| 29 |
version_added: 11.3.0 |
| 30 |
|
| 31 |
options: |
| 32 |
state: |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- Indicate desired state of the VPC. |
| 36 |
default: present |
| 37 |
choices: |
| 38 |
- present |
| 39 |
- absent |
| 40 |
|
| 41 |
project: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- Project identifier. |
| 45 |
required: true |
| 46 |
|
| 47 |
region: |
| 48 |
type: str |
| 49 |
description: |
| 50 |
- Scaleway region to use (for example V(par1)). |
| 51 |
required: true |
| 52 |
choices: |
| 53 |
- ams1 |
| 54 |
- EMEA-NL-EVS |
| 55 |
- ams2 |
| 56 |
- ams3 |
| 57 |
- par1 |
| 58 |
- EMEA-FR-PAR1 |
| 59 |
- par2 |
| 60 |
- EMEA-FR-PAR2 |
| 61 |
- par3 |
| 62 |
- waw1 |
| 63 |
- EMEA-PL-WAW1 |
| 64 |
- waw2 |
| 65 |
- waw3 |
| 66 |
|
| 67 |
compute_id: |
| 68 |
type: str |
| 69 |
description: |
| 70 |
- ID of the compute instance (see M(community.general.scaleway_compute)). |
| 71 |
required: true |
| 72 |
|
| 73 |
private_network_id: |
| 74 |
type: str |
| 75 |
description: |
| 76 |
- ID of the private network (see M(community.general.scaleway_private_network)). |
| 77 |
required: true |
| 78 |
""" |
| 79 |
|
| 80 |
EXAMPLES = r""" |
| 81 |
- name: Plug a VM to a private network |
| 82 |
community.general.scaleway_compute_private_network: |
| 83 |
project: '{{ scw_project }}' |
| 84 |
state: present |
| 85 |
region: par1 |
| 86 |
compute_id: "12345678-f1e6-40ec-83e5-12345d67ed89" |
| 87 |
private_network_id: "22345678-f1e6-40ec-83e5-12345d67ed89" |
| 88 |
register: nicsvpc_creation_task |
| 89 |
|
| 90 |
- name: Unplug a VM from a private network |
| 91 |
community.general.scaleway_compute_private_network: |
| 92 |
project: '{{ scw_project }}' |
| 93 |
state: absent |
| 94 |
region: par1 |
| 95 |
compute_id: "12345678-f1e6-40ec-83e5-12345d67ed89" |
| 96 |
private_network_id: "22345678-f1e6-40ec-83e5-12345d67ed89" |
| 97 |
""" |
| 98 |
|
| 99 |
RETURN = r""" |
| 100 |
scaleway_compute_private_network: |
| 101 |
description: Information on the VPC. |
| 102 |
returned: success when O(state=present) |
| 103 |
type: dict |
| 104 |
sample: |
| 105 |
{ |
| 106 |
"created_at": "2022-01-15T11:11:12.676445Z", |
| 107 |
"id": "12345678-f1e6-40ec-83e5-12345d67ed89", |
| 108 |
"name": "network", |
| 109 |
"organization_id": "a123b4cd-ef5g-678h-90i1-jk2345678l90", |
| 110 |
"project_id": "a123b4cd-ef5g-678h-90i1-jk2345678l90", |
| 111 |
"tags": [ |
| 112 |
"tag1", |
| 113 |
"tag2", |
| 114 |
"tag3", |
| 115 |
"tag4", |
| 116 |
"tag5" |
| 117 |
], |
| 118 |
"updated_at": "2022-01-15T11:12:04.624837Z", |
| 119 |
"zone": "fr-par-2" |
| 120 |
} |
| 121 |
""" |
| 122 |
|
| 123 |
from ansible.module_utils.basic import AnsibleModule |
| 124 |
|
| 125 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 126 |
SCALEWAY_LOCATION, |
| 127 |
Scaleway, |
| 128 |
scaleway_argument_spec, |
| 129 |
) |
| 130 |
|
| 131 |
|
| 132 |
def get_nics_info(api, compute_id, private_network_id): |
| 133 |
response = api.get(f"servers/{compute_id}/private_nics") |
| 134 |
if not response.ok: |
| 135 |
msg = f"Error during get servers information: {response.info['msg']}: '{response.json['message']}' ({response.json})" |
| 136 |
api.module.fail_json(msg=msg) |
| 137 |
|
| 138 |
i = 0 |
| 139 |
list_nics = response.json["private_nics"] |
| 140 |
|
| 141 |
while i < len(list_nics): |
| 142 |
if list_nics[i]["private_network_id"] == private_network_id: |
| 143 |
return list_nics[i] |
| 144 |
i += 1 |
| 145 |
|
| 146 |
return None |
| 147 |
|
| 148 |
|
| 149 |
def present_strategy(api, compute_id, private_network_id): |
| 150 |
changed = False |
| 151 |
nic = get_nics_info(api, compute_id, private_network_id) |
| 152 |
if nic is not None: |
| 153 |
return changed, nic |
| 154 |
|
| 155 |
data = {"private_network_id": private_network_id} |
| 156 |
changed = True |
| 157 |
if api.module.check_mode: |
| 158 |
return changed, {"status": "a private network would be add to a server"} |
| 159 |
|
| 160 |
response = api.post(path=f"servers/{compute_id}/private_nics", data=data) |
| 161 |
|
| 162 |
if not response.ok: |
| 163 |
api.module.fail_json( |
| 164 |
msg=f"Error when adding a private network to a server [{response.status_code}: {response.json}]" |
| 165 |
) |
| 166 |
|
| 167 |
return changed, response.json |
| 168 |
|
| 169 |
|
| 170 |
def absent_strategy(api, compute_id, private_network_id): |
| 171 |
changed = False |
| 172 |
nic = get_nics_info(api, compute_id, private_network_id) |
| 173 |
if nic is None: |
| 174 |
return changed, {} |
| 175 |
|
| 176 |
changed = True |
| 177 |
if api.module.check_mode: |
| 178 |
return changed, {"status": "private network would be destroyed"} |
| 179 |
|
| 180 |
response = api.delete(f"servers/{compute_id}/private_nics/{nic['id']}") |
| 181 |
|
| 182 |
if not response.ok: |
| 183 |
api.module.fail_json( |
| 184 |
msg=f"Error deleting private network from server [{response.status_code}: {response.json}]" |
| 185 |
) |
| 186 |
|
| 187 |
return changed, response.json |
| 188 |
|
| 189 |
|
| 190 |
def core(module): |
| 191 |
compute_id = module.params["compute_id"] |
| 192 |
pn_id = module.params["private_network_id"] |
| 193 |
|
| 194 |
region = module.params["region"] |
| 195 |
module.params["api_url"] = SCALEWAY_LOCATION[region]["api_endpoint"] |
| 196 |
|
| 197 |
api = Scaleway(module=module) |
| 198 |
if module.params["state"] == "absent": |
| 199 |
changed, summary = absent_strategy(api=api, compute_id=compute_id, private_network_id=pn_id) |
| 200 |
else: |
| 201 |
changed, summary = present_strategy(api=api, compute_id=compute_id, private_network_id=pn_id) |
| 202 |
module.exit_json(changed=changed, scaleway_compute_private_network=summary) |
| 203 |
|
| 204 |
|
| 205 |
def main(): |
| 206 |
argument_spec = scaleway_argument_spec() |
| 207 |
argument_spec.update( |
| 208 |
dict( |
| 209 |
state=dict(default="present", choices=["absent", "present"]), |
| 210 |
project=dict(required=True), |
| 211 |
region=dict(required=True, choices=list(SCALEWAY_LOCATION.keys())), |
| 212 |
compute_id=dict(required=True), |
| 213 |
private_network_id=dict(required=True), |
| 214 |
) |
| 215 |
) |
| 216 |
module = AnsibleModule( |
| 217 |
argument_spec=argument_spec, |
| 218 |
supports_check_mode=True, |
| 219 |
) |
| 220 |
|
| 221 |
core(module) |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
main() |