ansible-collection-hetzner-.../plugins/modules/hcloud_server_network.py
renovate[bot] 3058995219
deps: update pre-commit hook ansible/ansible-lint to v6.21.0 (#365)
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ansible/ansible-lint](https://togithub.com/ansible/ansible-lint) |
repository | minor | `v6.20.3` -> `v6.21.0` |

Note: The `pre-commit` manager in Renovate is not supported by the
`pre-commit` maintainers or community. Please do not report any problems
there, instead [create a Discussion in the Renovate
repository](https://togithub.com/renovatebot/renovate/discussions/new)
if you have any questions.

---

### Release Notes

<details>
<summary>ansible/ansible-lint (ansible/ansible-lint)</summary>

###
[`v6.21.0`](https://togithub.com/ansible/ansible-lint/releases/tag/v6.21.0)

[Compare
Source](https://togithub.com/ansible/ansible-lint/compare/v6.20.3...v6.21.0)

#### Minor Changes

- Allow linting plugin EXAMPLES as playbooks
([#&#8203;3309](https://togithub.com/ansible/ansible-lint/issues/3309))
[@&#8203;Qalthos](https://togithub.com/Qalthos)

#### Bugfixes

- Add support for Rocky
([#&#8203;3843](https://togithub.com/ansible/ansible-lint/issues/3843))
[@&#8203;facorazza](https://togithub.com/facorazza)
- Update supported Ubuntu versions in `meta.json`
([#&#8203;3845](https://togithub.com/ansible/ansible-lint/issues/3845))
[@&#8203;mcdonnnj](https://togithub.com/mcdonnnj)
- Avoid false positives for handler in roles handlers directory
([#&#8203;3838](https://togithub.com/ansible/ansible-lint/issues/3838))
[@&#8203;ajinkyau](https://togithub.com/ajinkyau)
- Hide stacktrace when loading invalid yaml
([#&#8203;3844](https://togithub.com/ansible/ansible-lint/issues/3844))
[@&#8203;ajinkyau](https://togithub.com/ajinkyau)
- Add some platforms to `meta.json`
([#&#8203;3841](https://togithub.com/ansible/ansible-lint/issues/3841))
[@&#8203;mcdonnnj](https://togithub.com/mcdonnnj)
- Temporary avoid auto-fixing YAML files not owned by ansible
([#&#8203;3837](https://togithub.com/ansible/ansible-lint/issues/3837))
[@&#8203;ssbarnea](https://togithub.com/ssbarnea)
- Add environment variable for skipping schema update
([#&#8203;3835](https://togithub.com/ansible/ansible-lint/issues/3835))
[@&#8203;ajinkyau](https://togithub.com/ajinkyau)
- Avoid creating temporary YAML files inside source tree
([#&#8203;3819](https://togithub.com/ansible/ansible-lint/issues/3819))
[@&#8203;Qalthos](https://togithub.com/Qalthos)
- Document environment variables
([#&#8203;3833](https://togithub.com/ansible/ansible-lint/issues/3833))
[@&#8203;ssbarnea](https://togithub.com/ssbarnea)
- Update schemas
([#&#8203;3832](https://togithub.com/ansible/ansible-lint/issues/3832))
[@&#8203;ssbarnea](https://togithub.com/ssbarnea)
- Support complex requirements in argument_specs.yml
([#&#8203;3823](https://togithub.com/ansible/ansible-lint/issues/3823))
[@&#8203;tapetersen](https://togithub.com/tapetersen)
- Fix SARIF-formatter severity levels
([#&#8203;3824](https://togithub.com/ansible/ansible-lint/issues/3824))
[@&#8203;4ch1m](https://togithub.com/4ch1m)
- Add play level autofix for key-order rule
([#&#8203;3815](https://togithub.com/ansible/ansible-lint/issues/3815))
[@&#8203;ajinkyau](https://togithub.com/ajinkyau)
- Add support for python 3.12
([#&#8203;3813](https://togithub.com/ansible/ansible-lint/issues/3813))
[@&#8203;ssbarnea](https://togithub.com/ssbarnea)
- Update SPDX license list
([#&#8203;3814](https://togithub.com/ansible/ansible-lint/issues/3814))
[@&#8203;ssbarnea](https://togithub.com/ssbarnea)
- Use checkout action in install docs
([#&#8203;3810](https://togithub.com/ansible/ansible-lint/issues/3810))
[@&#8203;gma](https://togithub.com/gma)
- Fix actions-tagger arguments
([#&#8203;3808](https://togithub.com/ansible/ansible-lint/issues/3808))
[@&#8203;ssbarnea](https://togithub.com/ssbarnea)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/ansible-collections/hetzner.hcloud).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xOS4yIiwidXBkYXRlZEluVmVyIjoiMzcuMTkuMiIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: jo <ljonas@riseup.net>
2023-10-19 12:41:44 +02:00

250 lines
7.4 KiB
Python

#!/usr/bin/python
# Copyright: (c) 2019, Hetzner Cloud GmbH <info@hetzner-cloud.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = """
---
module: hcloud_server_network
short_description: Manage the relationship between Hetzner Cloud Networks and servers
description:
- Create and delete the relationship Hetzner Cloud Networks and servers
author:
- Lukas Kaemmerling (@lkaemmerling)
options:
network:
description:
- Name or ID of the Hetzner Cloud Networks.
type: str
required: true
server:
description:
- Name or ID of the Hetzner Cloud server.
type: str
required: true
ip:
description:
- The IP the server should have.
type: str
alias_ips:
description:
- Alias IPs the server has.
type: list
elements: str
state:
description:
- State of the server_network.
default: present
choices: [ absent, present ]
type: str
requirements:
- hcloud-python >= 1.3.0
extends_documentation_fragment:
- hetzner.hcloud.hcloud
"""
EXAMPLES = """
- name: Create a basic server network
hetzner.hcloud.hcloud_server_network:
network: my-network
server: my-server
state: present
- name: Create a server network and specify the ip address
hetzner.hcloud.hcloud_server_network:
network: my-network
server: my-server
ip: 10.0.0.1
state: present
- name: Create a server network and add alias ips
hetzner.hcloud.hcloud_server_network:
network: my-network
server: my-server
ip: 10.0.0.1
alias_ips:
- 10.1.0.1
- 10.2.0.1
state: present
- name: Ensure the server network is absent (remove if needed)
hetzner.hcloud.hcloud_server_network:
network: my-network
server: my-server
state: absent
"""
RETURN = """
hcloud_server_network:
description: The relationship between a server and a network
returned: always
type: complex
contains:
network:
description: Name of the Network
type: str
returned: always
sample: my-network
server:
description: Name of the server
type: str
returned: always
sample: my-server
ip:
description: IP of the server within the Network ip range
type: str
returned: always
sample: 10.0.0.8
alias_ips:
description: Alias IPs of the server within the Network ip range
type: list
elements: str
returned: always
sample: [10.1.0.1, ...]
"""
from typing import Optional
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native
from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import APIException, HCloudException
from ..module_utils.vendor.hcloud.networks import BoundNetwork
from ..module_utils.vendor.hcloud.servers import BoundServer, PrivateNet
class AnsibleHCloudServerNetwork(AnsibleHCloud):
represent = "hcloud_server_network"
hcloud_network: Optional[BoundNetwork] = None
hcloud_server: Optional[BoundServer] = None
hcloud_server_network: Optional[PrivateNet] = None
def _prepare_result(self):
return {
"network": to_native(self.hcloud_network.name),
"server": to_native(self.hcloud_server.name),
"ip": to_native(self.hcloud_server_network.ip),
"alias_ips": self.hcloud_server_network.alias_ips,
}
def _get_server_and_network(self):
try:
self.hcloud_network = self._client_get_by_name_or_id(
"networks",
self.module.params.get("network"),
)
self.hcloud_server = self._client_get_by_name_or_id(
"servers",
self.module.params.get("server"),
)
self.hcloud_server_network = None
except HCloudException as exception:
self.fail_json_hcloud(exception)
def _get_server_network(self):
for private_net in self.hcloud_server.private_net:
if private_net.network.id == self.hcloud_network.id:
self.hcloud_server_network = private_net
def _create_server_network(self):
params = {
"network": self.hcloud_network,
}
if self.module.params.get("ip") is not None:
params["ip"] = self.module.params.get("ip")
if self.module.params.get("alias_ips") is not None:
params["alias_ips"] = self.module.params.get("alias_ips")
if not self.module.check_mode:
try:
self.hcloud_server.attach_to_network(**params).wait_until_finished()
except HCloudException as exception:
self.fail_json_hcloud(exception)
self._mark_as_changed()
self._get_server_and_network()
self._get_server_network()
def _update_server_network(self):
params = {
"network": self.hcloud_network,
}
alias_ips = self.module.params.get("alias_ips")
if alias_ips is not None and sorted(self.hcloud_server_network.alias_ips) != sorted(alias_ips):
params["alias_ips"] = alias_ips
if not self.module.check_mode:
try:
self.hcloud_server.change_alias_ips(**params).wait_until_finished()
except APIException as exception:
self.fail_json_hcloud(exception)
self._mark_as_changed()
self._get_server_and_network()
self._get_server_network()
def present_server_network(self):
self._get_server_and_network()
self._get_server_network()
if self.hcloud_server_network is None:
self._create_server_network()
else:
self._update_server_network()
def delete_server_network(self):
self._get_server_and_network()
self._get_server_network()
if self.hcloud_server_network is not None and self.hcloud_server is not None:
if not self.module.check_mode:
try:
self.hcloud_server.detach_from_network(self.hcloud_server_network.network).wait_until_finished()
except HCloudException as exception:
self.fail_json_hcloud(exception)
self._mark_as_changed()
self.hcloud_server_network = None
@classmethod
def define_module(cls):
return AnsibleModule(
argument_spec=dict(
network={"type": "str", "required": True},
server={"type": "str", "required": True},
ip={"type": "str"},
alias_ips={"type": "list", "elements": "str"},
state={
"choices": ["absent", "present"],
"default": "present",
},
**super().base_module_arguments()
),
supports_check_mode=True,
)
def main():
module = AnsibleHCloudServerNetwork.define_module()
hcloud = AnsibleHCloudServerNetwork(module)
state = module.params["state"]
if state == "absent":
hcloud.delete_server_network()
elif state == "present":
hcloud.present_server_network()
module.exit_json(**hcloud.get_result())
if __name__ == "__main__":
main()