| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2019 Felix Fontein <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: server |
| 14 |
short_description: Update server information |
| 15 |
version_added: 1.2.0 |
| 16 |
author: |
| 17 |
- Felix Fontein (@felixfontein) |
| 18 |
description: |
| 19 |
- Allows to update server information. |
| 20 |
- Right now the API only supports updating the server's name. |
| 21 |
extends_documentation_fragment: |
| 22 |
- community.hrobot.robot |
| 23 |
- community.hrobot.attributes |
| 24 |
- community.hrobot.attributes.actiongroup_robot |
| 25 |
|
| 26 |
attributes: |
| 27 |
action_group: |
| 28 |
version_added: 1.6.0 |
| 29 |
check_mode: |
| 30 |
support: full |
| 31 |
diff_mode: |
| 32 |
support: none |
| 33 |
idempotent: |
| 34 |
support: full |
| 35 |
|
| 36 |
options: |
| 37 |
server_number: |
| 38 |
description: |
| 39 |
- The server number of the server to update. |
| 40 |
type: int |
| 41 |
required: true |
| 42 |
server_name: |
| 43 |
description: |
| 44 |
- The server's name. |
| 45 |
- If this option is not provided, it will not be adjusted. |
| 46 |
type: str |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
--- |
| 51 |
- name: Set server's name to foo.example.com |
| 52 |
community.hrobot.server: |
| 53 |
hetzner_user: foo |
| 54 |
hetzner_password: bar |
| 55 |
server_number: 123 |
| 56 |
server_name: foo.example.com |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
server: |
| 61 |
description: |
| 62 |
- Information on the server. |
| 63 |
returned: success |
| 64 |
type: dict |
| 65 |
contains: |
| 66 |
server_ip: |
| 67 |
description: |
| 68 |
- The server's main IP address. |
| 69 |
type: str |
| 70 |
sample: 123.123.123.123 |
| 71 |
returned: success |
| 72 |
server_ipv6_net: |
| 73 |
description: |
| 74 |
- The server's main IPv6 network address. |
| 75 |
type: str |
| 76 |
sample: '2a01:f48:111:4221::' |
| 77 |
returned: success |
| 78 |
server_number: |
| 79 |
description: |
| 80 |
- The server's numeric ID. |
| 81 |
type: int |
| 82 |
sample: 321 |
| 83 |
returned: success |
| 84 |
server_name: |
| 85 |
description: |
| 86 |
- The user-defined server's name. |
| 87 |
type: str |
| 88 |
sample: server1 |
| 89 |
returned: success |
| 90 |
product: |
| 91 |
description: |
| 92 |
- The server product name. |
| 93 |
type: str |
| 94 |
sample: EQ 8 |
| 95 |
returned: success |
| 96 |
dc: |
| 97 |
description: |
| 98 |
- The data center the server is located in. |
| 99 |
type: str |
| 100 |
sample: NBG1-DC1 |
| 101 |
returned: success |
| 102 |
traffic: |
| 103 |
description: |
| 104 |
- Free traffic quota. |
| 105 |
- V(unlimited) in case of unlimited traffic. |
| 106 |
type: str |
| 107 |
sample: 5 TB |
| 108 |
returned: success |
| 109 |
status: |
| 110 |
description: |
| 111 |
- Server status. |
| 112 |
type: str |
| 113 |
choices: |
| 114 |
- ready |
| 115 |
- in process |
| 116 |
sample: ready |
| 117 |
returned: success |
| 118 |
cancelled: |
| 119 |
description: |
| 120 |
- Whether the server is cancelled. |
| 121 |
type: bool |
| 122 |
sample: false |
| 123 |
returned: success |
| 124 |
paid_until: |
| 125 |
description: |
| 126 |
- The date until the server has been paid. |
| 127 |
type: str |
| 128 |
sample: "2018-08-04" |
| 129 |
returned: success |
| 130 |
ip: |
| 131 |
description: |
| 132 |
- List of assigned single IP addresses. |
| 133 |
type: list |
| 134 |
elements: str |
| 135 |
sample: |
| 136 |
- 123.123.123.123 |
| 137 |
returned: success |
| 138 |
subnet: |
| 139 |
description: |
| 140 |
- List of assigned subnets. |
| 141 |
type: list |
| 142 |
elements: dict |
| 143 |
sample: |
| 144 |
- ip: '2a01:4f8:111:4221::' |
| 145 |
mask: 64 |
| 146 |
contains: |
| 147 |
ip: |
| 148 |
description: |
| 149 |
- The first IP in the subnet. |
| 150 |
type: str |
| 151 |
sample: '2a01:4f8:111:4221::' |
| 152 |
mask: |
| 153 |
description: |
| 154 |
- The masks bitlength. |
| 155 |
type: str |
| 156 |
sample: "64" |
| 157 |
returned: success |
| 158 |
reset: |
| 159 |
description: |
| 160 |
- Whether the server can be automatically reset. |
| 161 |
type: bool |
| 162 |
sample: true |
| 163 |
returned: success |
| 164 |
rescue: |
| 165 |
description: |
| 166 |
- Whether the rescue system is available. |
| 167 |
type: bool |
| 168 |
sample: false |
| 169 |
returned: success |
| 170 |
vnc: |
| 171 |
description: |
| 172 |
- Flag of VNC installation availability. |
| 173 |
type: bool |
| 174 |
sample: true |
| 175 |
returned: success |
| 176 |
windows: |
| 177 |
description: |
| 178 |
- Flag of Windows installation availability. |
| 179 |
type: bool |
| 180 |
sample: true |
| 181 |
returned: success |
| 182 |
plesk: |
| 183 |
description: |
| 184 |
- Flag of Plesk installation availability. |
| 185 |
type: bool |
| 186 |
sample: true |
| 187 |
returned: success |
| 188 |
cpanel: |
| 189 |
description: |
| 190 |
- Flag of cPanel installation availability. |
| 191 |
type: bool |
| 192 |
sample: true |
| 193 |
returned: success |
| 194 |
wol: |
| 195 |
description: |
| 196 |
- Flag of Wake On Lan availability. |
| 197 |
type: bool |
| 198 |
sample: true |
| 199 |
returned: success |
| 200 |
hot_swap: |
| 201 |
description: |
| 202 |
- Flag of Hot Swap availability. |
| 203 |
type: bool |
| 204 |
sample: true |
| 205 |
returned: success |
| 206 |
linked_storagebox: |
| 207 |
description: |
| 208 |
- Linked Storage Box ID. |
| 209 |
type: int |
| 210 |
sample: 12345 |
| 211 |
returned: success |
| 212 |
""" |
| 213 |
|
| 214 |
from ansible.module_utils.basic import AnsibleModule |
| 215 |
|
| 216 |
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( |
| 217 |
BASE_URL, |
| 218 |
ROBOT_DEFAULT_ARGUMENT_SPEC, |
| 219 |
fetch_url_json, |
| 220 |
) |
| 221 |
|
| 222 |
try: |
| 223 |
from urllib.parse import urlencode |
| 224 |
except ImportError: |
| 225 |
# Python 2.x fallback: |
| 226 |
from urllib import urlencode |
| 227 |
|
| 228 |
|
| 229 |
def main(): |
| 230 |
argument_spec = dict( |
| 231 |
server_number=dict(type='int', required=True), |
| 232 |
server_name=dict(type='str'), |
| 233 |
) |
| 234 |
argument_spec.update(ROBOT_DEFAULT_ARGUMENT_SPEC) |
| 235 |
module = AnsibleModule( |
| 236 |
argument_spec=argument_spec, |
| 237 |
supports_check_mode=True, |
| 238 |
) |
| 239 |
|
| 240 |
server_number = module.params['server_number'] |
| 241 |
server_name = module.params['server_name'] |
| 242 |
|
| 243 |
url = "{0}/server/{1}".format(BASE_URL, server_number) |
| 244 |
server, error = fetch_url_json(module, url, accept_errors=['SERVER_NOT_FOUND']) |
| 245 |
if error: |
| 246 |
module.fail_json(msg='This server does not exist, or you do not have access rights for it') |
| 247 |
|
| 248 |
result = { |
| 249 |
'changed': False, |
| 250 |
'server': server['server'], |
| 251 |
} |
| 252 |
|
| 253 |
update = {} |
| 254 |
if server_name is not None: |
| 255 |
if server_name != result['server']['server_name']: |
| 256 |
update['server_name'] = server_name |
| 257 |
|
| 258 |
if update: |
| 259 |
result['changed'] = True |
| 260 |
if module.check_mode: |
| 261 |
result['server'].update(update) |
| 262 |
else: |
| 263 |
headers = {"Content-type": "application/x-www-form-urlencoded"} |
| 264 |
url = "{0}/server/{1}".format(BASE_URL, server_number) |
| 265 |
server, error = fetch_url_json( |
| 266 |
module, |
| 267 |
url, |
| 268 |
data=urlencode(update), |
| 269 |
headers=headers, |
| 270 |
method='POST', |
| 271 |
accept_errors=['INVALID_INPUT'], |
| 272 |
) |
| 273 |
if error: |
| 274 |
module.fail_json(msg='The values to update were invalid ({0})'.format(module.jsonify(update))) |
| 275 |
result['server'] = server['server'] |
| 276 |
|
| 277 |
module.exit_json(**result) |
| 278 |
|
| 279 |
|
| 280 |
if __name__ == '__main__': # pragma: no cover |
| 281 |
main() # pragma: no cover |