| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway Security Group management module |
| 4 |
# |
| 5 |
# Copyright (C) 2018 Antoine Barbare ([email protected]). |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: scaleway_security_group |
| 14 |
short_description: Scaleway Security Group management module |
| 15 |
author: Antoine Barbare (@abarbare) |
| 16 |
description: |
| 17 |
- This module manages Security Group on Scaleway account 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 |
description: |
| 34 |
- Indicate desired state of the Security Group. |
| 35 |
type: str |
| 36 |
choices: [absent, present] |
| 37 |
default: present |
| 38 |
|
| 39 |
organization: |
| 40 |
type: str |
| 41 |
description: |
| 42 |
- Organization identifier. |
| 43 |
- Exactly one of O(project) and O(organization) must be specified. |
| 44 |
|
| 45 |
region: |
| 46 |
description: |
| 47 |
- Scaleway region to use (for example V(par1)). |
| 48 |
type: str |
| 49 |
required: true |
| 50 |
choices: |
| 51 |
- ams1 |
| 52 |
- EMEA-NL-EVS |
| 53 |
- ams2 |
| 54 |
- ams3 |
| 55 |
- par1 |
| 56 |
- EMEA-FR-PAR1 |
| 57 |
- par2 |
| 58 |
- EMEA-FR-PAR2 |
| 59 |
- par3 |
| 60 |
- waw1 |
| 61 |
- EMEA-PL-WAW1 |
| 62 |
- waw2 |
| 63 |
- waw3 |
| 64 |
|
| 65 |
name: |
| 66 |
description: |
| 67 |
- Name of the Security Group. |
| 68 |
type: str |
| 69 |
required: true |
| 70 |
|
| 71 |
description: |
| 72 |
description: |
| 73 |
- Description of the Security Group. |
| 74 |
type: str |
| 75 |
|
| 76 |
stateful: |
| 77 |
description: |
| 78 |
- Create a stateful security group which allows established connections in and out. |
| 79 |
type: bool |
| 80 |
required: true |
| 81 |
|
| 82 |
inbound_default_policy: |
| 83 |
description: |
| 84 |
- Default policy for incoming traffic. |
| 85 |
type: str |
| 86 |
choices: [accept, drop] |
| 87 |
|
| 88 |
outbound_default_policy: |
| 89 |
description: |
| 90 |
- Default policy for outcoming traffic. |
| 91 |
type: str |
| 92 |
choices: [accept, drop] |
| 93 |
|
| 94 |
organization_default: |
| 95 |
description: |
| 96 |
- Create security group to be the default one. |
| 97 |
type: bool |
| 98 |
|
| 99 |
project: |
| 100 |
type: str |
| 101 |
description: |
| 102 |
- Project identifier. |
| 103 |
- Exactly one of O(project) and O(organization) must be specified. |
| 104 |
version_added: 12.3.0 |
| 105 |
""" |
| 106 |
|
| 107 |
EXAMPLES = r""" |
| 108 |
- name: Create a Security Group using a project ID |
| 109 |
community.general.scaleway_security_group: |
| 110 |
state: present |
| 111 |
region: par1 |
| 112 |
name: security_group |
| 113 |
description: "my security group description" |
| 114 |
stateful: false |
| 115 |
inbound_default_policy: accept |
| 116 |
outbound_default_policy: accept |
| 117 |
organization_default: false |
| 118 |
project: 951df375-e094-4d26-97c1-ba548eeb9c42 |
| 119 |
register: security_group_creation_task |
| 120 |
|
| 121 |
- name: Create a Security Group in the default project using an organization ID (deprecated) |
| 122 |
community.general.scaleway_security_group: |
| 123 |
state: present |
| 124 |
region: par1 |
| 125 |
name: security_group |
| 126 |
description: "my security group description" |
| 127 |
organization: 43a3b6c8-916f-477b-b7ec-ff1898f5fdd9 |
| 128 |
stateful: false |
| 129 |
inbound_default_policy: accept |
| 130 |
outbound_default_policy: accept |
| 131 |
organization_default: false |
| 132 |
register: security_group_creation_task |
| 133 |
""" |
| 134 |
|
| 135 |
RETURN = r""" |
| 136 |
data: |
| 137 |
description: This is only present when O(state=present). |
| 138 |
returned: when O(state=present) |
| 139 |
type: dict |
| 140 |
sample: |
| 141 |
{ |
| 142 |
"scaleway_security_group": { |
| 143 |
"description": "my security group description", |
| 144 |
"enable_default_security": true, |
| 145 |
"id": "0168fb1f-cc46-4f69-b4be-c95d2a19bcae", |
| 146 |
"inbound_default_policy": "accept", |
| 147 |
"name": "security_group", |
| 148 |
"organization": "43a3b6c8-916f-477b-b7ec-ff1898f5fdd9", |
| 149 |
"organization_default": false, |
| 150 |
"outbound_default_policy": "accept", |
| 151 |
"servers": [], |
| 152 |
"stateful": false |
| 153 |
} |
| 154 |
} |
| 155 |
""" |
| 156 |
|
| 157 |
from uuid import uuid4 |
| 158 |
|
| 159 |
from ansible.module_utils.basic import AnsibleModule |
| 160 |
|
| 161 |
from ansible_collections.community.general.plugins.module_utils._scaleway import ( |
| 162 |
SCALEWAY_LOCATION, |
| 163 |
Scaleway, |
| 164 |
scaleway_argument_spec, |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
def payload_from_security_group(security_group): |
| 169 |
return {k: v for k, v in security_group.items() if k != "id" and v is not None} |
| 170 |
|
| 171 |
|
| 172 |
def present_strategy(api, security_group): |
| 173 |
ret = {"changed": False} |
| 174 |
|
| 175 |
response = api.get("security_groups") |
| 176 |
if not response.ok: |
| 177 |
api.module.fail_json( |
| 178 |
msg=f'Error getting security groups "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 179 |
) |
| 180 |
|
| 181 |
security_group_lookup = {sg["name"]: sg for sg in response.json["security_groups"]} |
| 182 |
|
| 183 |
if security_group["name"] not in security_group_lookup.keys(): |
| 184 |
ret["changed"] = True |
| 185 |
if api.module.check_mode: |
| 186 |
# Help user when check mode is enabled by defining id key |
| 187 |
ret["scaleway_security_group"] = {"id": str(uuid4())} |
| 188 |
return ret |
| 189 |
|
| 190 |
# Create Security Group |
| 191 |
response = api.post("/security_groups", data=payload_from_security_group(security_group)) |
| 192 |
|
| 193 |
if not response.ok: |
| 194 |
msg = f'Error during security group creation: "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 195 |
api.module.fail_json(msg=msg) |
| 196 |
ret["scaleway_security_group"] = response.json["security_group"] |
| 197 |
|
| 198 |
else: |
| 199 |
ret["scaleway_security_group"] = security_group_lookup[security_group["name"]] |
| 200 |
|
| 201 |
return ret |
| 202 |
|
| 203 |
|
| 204 |
def absent_strategy(api, security_group): |
| 205 |
response = api.get("security_groups") |
| 206 |
ret = {"changed": False} |
| 207 |
|
| 208 |
if not response.ok: |
| 209 |
api.module.fail_json( |
| 210 |
msg=f'Error getting security groups "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 211 |
) |
| 212 |
|
| 213 |
security_group_lookup = {sg["name"]: sg for sg in response.json["security_groups"]} |
| 214 |
if security_group["name"] not in security_group_lookup.keys(): |
| 215 |
return ret |
| 216 |
|
| 217 |
ret["changed"] = True |
| 218 |
if api.module.check_mode: |
| 219 |
return ret |
| 220 |
|
| 221 |
response = api.delete(f"/security_groups/{security_group_lookup[security_group['name']]['id']}") |
| 222 |
if not response.ok: |
| 223 |
api.module.fail_json( |
| 224 |
msg=f'Error deleting security group "{response.info["msg"]}": "{response.json["message"]}" ({response.json})' |
| 225 |
) |
| 226 |
|
| 227 |
return ret |
| 228 |
|
| 229 |
|
| 230 |
def core(module): |
| 231 |
security_group = { |
| 232 |
"organization": module.params["organization"], |
| 233 |
"name": module.params["name"], |
| 234 |
"description": module.params["description"], |
| 235 |
"stateful": module.params["stateful"], |
| 236 |
"inbound_default_policy": module.params["inbound_default_policy"], |
| 237 |
"outbound_default_policy": module.params["outbound_default_policy"], |
| 238 |
"organization_default": module.params["organization_default"], |
| 239 |
"project": module.params["project"], |
| 240 |
} |
| 241 |
|
| 242 |
region = module.params["region"] |
| 243 |
module.params["api_url"] = SCALEWAY_LOCATION[region]["api_endpoint"] |
| 244 |
|
| 245 |
api = Scaleway(module=module) |
| 246 |
if module.params["state"] == "present": |
| 247 |
summary = present_strategy(api=api, security_group=security_group) |
| 248 |
else: |
| 249 |
summary = absent_strategy(api=api, security_group=security_group) |
| 250 |
module.exit_json(**summary) |
| 251 |
|
| 252 |
|
| 253 |
def main(): |
| 254 |
argument_spec = scaleway_argument_spec() |
| 255 |
argument_spec.update( |
| 256 |
dict( |
| 257 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 258 |
organization=dict(type="str"), |
| 259 |
name=dict(type="str", required=True), |
| 260 |
description=dict(type="str"), |
| 261 |
region=dict(type="str", required=True, choices=list(SCALEWAY_LOCATION.keys())), |
| 262 |
stateful=dict(type="bool", required=True), |
| 263 |
inbound_default_policy=dict(type="str", choices=["accept", "drop"]), |
| 264 |
outbound_default_policy=dict(type="str", choices=["accept", "drop"]), |
| 265 |
organization_default=dict(type="bool"), |
| 266 |
project=dict(type="str"), |
| 267 |
) |
| 268 |
) |
| 269 |
module = AnsibleModule( |
| 270 |
argument_spec=argument_spec, |
| 271 |
supports_check_mode=True, |
| 272 |
required_if=[["stateful", True, ["inbound_default_policy", "outbound_default_policy"]]], |
| 273 |
mutually_exclusive=[ |
| 274 |
("organization", "project"), |
| 275 |
], |
| 276 |
required_one_of=[ |
| 277 |
("organization", "project"), |
| 278 |
], |
| 279 |
) |
| 280 |
|
| 281 |
core(module) |
| 282 |
|
| 283 |
|
| 284 |
if __name__ == "__main__": |
| 285 |
main() |