|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# (c) 2020, Servers.com |
| 4 | +# GNU General Public License v3.0 |
| 5 | +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 | + |
| 7 | + |
| 8 | +from __future__ import absolute_import, division, print_function |
| 9 | + |
| 10 | + |
| 11 | +__metaclass__ = type |
| 12 | + |
| 13 | + |
| 14 | +ANSIBLE_METADATA = { |
| 15 | + "metadata_version": "1.1", |
| 16 | + "status": ["preview"], |
| 17 | + "supported_by": "community", |
| 18 | +} |
| 19 | + |
| 20 | +DOCUMENTATION = """ |
| 21 | +--- |
| 22 | +module: sc_dedicated_server_power |
| 23 | +version_added: "1.0.0" |
| 24 | +author: "Volodymyr Rudniev (@koef)" |
| 25 | +short_description: Power on/off Bare Metal server |
| 26 | +description: > |
| 27 | + Manage the power state of a Bare Metal server outlet. Power on will start |
| 28 | + the server only if OOB configured accordingly. |
| 29 | +
|
| 30 | +options: |
| 31 | + endpoint: |
| 32 | + type: str |
| 33 | + default: "https://api.servers.com/v1" |
| 34 | + description: |
| 35 | + - API endpoint to connect to. |
| 36 | +
|
| 37 | + token: |
| 38 | + type: str |
| 39 | + required: true |
| 40 | + description: |
| 41 | + - API bearer token. |
| 42 | + - Obtain from https://portal.servers.com under Profile → Public API. |
| 43 | +
|
| 44 | + server_id: |
| 45 | + type: str |
| 46 | + required: true |
| 47 | + description: |
| 48 | + - ID of the Bare Metal server. |
| 49 | + - Use I(serverscom.sc_api.sc_baremetal_servers_info) to retrieve servers. |
| 50 | +
|
| 51 | + state: |
| 52 | + type: str |
| 53 | + required: true |
| 54 | + choices: ['on', 'off', 'cycle'] |
| 55 | + description: |
| 56 | + - Desired power state. |
| 57 | + - "I(on): power on outlet." |
| 58 | + - "I(off): power off outlet." |
| 59 | + - "I(cycle): power cycle outlet." |
| 60 | +
|
| 61 | + fail_on_absent: |
| 62 | + type: bool |
| 63 | + default: true |
| 64 | + description: |
| 65 | + - Raise error if server is not found. |
| 66 | + - If set to false, absent (not found) server will have |
| 67 | + found=false in server info. |
| 68 | +
|
| 69 | + timeout: |
| 70 | + description: |
| 71 | + - | |
| 72 | + Maximum time in seconds to wait for the server to reach the desired state." |
| 73 | + required: false |
| 74 | + type: int |
| 75 | + default: 60 |
| 76 | +""" |
| 77 | + |
| 78 | +RETURN = """ |
| 79 | +id: |
| 80 | + description: Unique identifier of a server. |
| 81 | + type: str |
| 82 | + returned: on success |
| 83 | +
|
| 84 | +title: |
| 85 | + description: Displayed name of the server (defaults to hostname). |
| 86 | + type: str |
| 87 | + returned: on success |
| 88 | +
|
| 89 | +type: |
| 90 | + description: "Resource type (always 'dedicated_server')." |
| 91 | + type: str |
| 92 | + returned: on success |
| 93 | +
|
| 94 | +rack_id: |
| 95 | + description: Unique identifier of the rack, or null if provisioning. |
| 96 | + type: str |
| 97 | + returned: on success |
| 98 | +
|
| 99 | +status: |
| 100 | + description: Provisioning state of the server (init, pending, active). |
| 101 | + type: str |
| 102 | + returned: on success |
| 103 | +
|
| 104 | +operational_status: |
| 105 | + description: Detailed operational state (normal, provisioning, installation, entering_rescue_mode, rescue_mode, exiting_rescue_mode). |
| 106 | + type: str |
| 107 | + returned: on success |
| 108 | +
|
| 109 | +power_status: |
| 110 | + description: Power state indicator (unknown, powering_on, powered_on, powering_off, powered_off, power_cycling). |
| 111 | + type: str |
| 112 | + returned: on success |
| 113 | +
|
| 114 | +configuration: |
| 115 | + description: Chassis model, RAM, and disk details. |
| 116 | + type: str |
| 117 | + returned: on success |
| 118 | +
|
| 119 | +location_id: |
| 120 | + description: Numeric identifier of the server's location. |
| 121 | + type: int |
| 122 | + returned: on success |
| 123 | +
|
| 124 | +location_code: |
| 125 | + description: Technical code of the server's location. |
| 126 | + type: str |
| 127 | + returned: on success |
| 128 | +
|
| 129 | +private_ipv4_address: |
| 130 | + description: Private IPv4 address, or null if unassigned. |
| 131 | + type: str |
| 132 | + returned: on success |
| 133 | +
|
| 134 | +public_ipv4_address: |
| 135 | + description: Public IPv4 address, or null if unassigned. |
| 136 | + type: str |
| 137 | + returned: on success |
| 138 | +
|
| 139 | +lease_start_at: |
| 140 | + description: Date when leasing began, or null. |
| 141 | + type: str |
| 142 | + returned: on success |
| 143 | +
|
| 144 | +scheduled_release_at: |
| 145 | + description: Scheduled release date-time, or null. |
| 146 | + type: str |
| 147 | + returned: on success |
| 148 | +
|
| 149 | +configuration_details: |
| 150 | + description: Detailed configuration object. |
| 151 | + type: dict |
| 152 | + returned: on success |
| 153 | +
|
| 154 | +labels: |
| 155 | + description: Labels associated with the server resource. |
| 156 | + type: dict |
| 157 | + returned: on success |
| 158 | +
|
| 159 | +created_at: |
| 160 | + description: Timestamp when the server was created. |
| 161 | + type: str |
| 162 | + returned: on success |
| 163 | +
|
| 164 | +updated_at: |
| 165 | + description: Timestamp of the last update. |
| 166 | + type: str |
| 167 | + returned: on success |
| 168 | +
|
| 169 | +oob_ipv4_address: |
| 170 | + description: Out-of-band IPv4 address if OOB access is enabled, or null. |
| 171 | + type: str |
| 172 | + returned: on success |
| 173 | +""" |
| 174 | + |
| 175 | +EXAMPLES = """ |
| 176 | +- name: Power on server |
| 177 | + sc_dedicated_server_power: |
| 178 | + token: "{{ api_token }}" |
| 179 | + server_id: "0m592Zmn" |
| 180 | + state: on |
| 181 | +
|
| 182 | +- name: Power off server |
| 183 | + sc_dedicated_server_power: |
| 184 | + token: "{{ api_token }}" |
| 185 | + server_id: "0m592Zmn" |
| 186 | + state: off |
| 187 | +
|
| 188 | +- name: Cycle power on server |
| 189 | + sc_dedicated_server_power: |
| 190 | + token: "{{ api_token }}" |
| 191 | + server_id: "0m592Zmn" |
| 192 | + state: cycle |
| 193 | +""" |
| 194 | + |
| 195 | + |
| 196 | +from ansible.module_utils.basic import AnsibleModule |
| 197 | +from ansible_collections.serverscom.sc_api.plugins.module_utils.modules import ( |
| 198 | + DEFAULT_API_ENDPOINT, |
| 199 | + SCBaseError, |
| 200 | + ScDedicatedServerPower, |
| 201 | +) |
| 202 | + |
| 203 | +__metaclass__ = type |
| 204 | + |
| 205 | + |
| 206 | +def main(): |
| 207 | + module = AnsibleModule( |
| 208 | + argument_spec={ |
| 209 | + "endpoint": {"default": DEFAULT_API_ENDPOINT}, |
| 210 | + "token": {"type": "str", "no_log": True, "required": True}, |
| 211 | + "server_id": {"type": "str", "required": True}, |
| 212 | + "state": { |
| 213 | + "type": "str", |
| 214 | + "choices": ["on", "off", "cycle"], |
| 215 | + "required": True, |
| 216 | + }, |
| 217 | + "fail_on_absent": {"type": "bool", "default": True}, |
| 218 | + "timeout": {"type": "int", "default": 60}, |
| 219 | + }, |
| 220 | + supports_check_mode=True, |
| 221 | + ) |
| 222 | + |
| 223 | + try: |
| 224 | + power = ScDedicatedServerPower( |
| 225 | + endpoint=module.params["endpoint"], |
| 226 | + token=module.params["token"], |
| 227 | + server_id=module.params["server_id"], |
| 228 | + state=module.params["state"], |
| 229 | + fail_on_absent=module.params["fail_on_absent"], |
| 230 | + timeout=module.params["timeout"], |
| 231 | + checkmode=module.check_mode, |
| 232 | + ) |
| 233 | + module.exit_json(**power.run()) |
| 234 | + except SCBaseError as e: |
| 235 | + module.exit_json(**e.fail()) |
| 236 | + |
| 237 | + |
| 238 | +if __name__ == "__main__": |
| 239 | + main() |
0 commit comments