Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Hostinger Collection Release Notes

.. contents:: Topics

v1.1.0
======

Breaking Changes
----------------
- `hostinger_vps_ssh_key_binding` now calls `POST /virtual-machines/{id}/ssh-keys` because the old `public-keys/attach` endpoint is deprecated. It takes key text in the new `keys` option, which replaces `public_key_ids`, and no longer takes `wait` (the API returns the key list, not an action).

v1.0.0
======

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This collection provides Ansible modules to manage Hostinger Virtual Private Ser

Every module accepts the token through the `token` option. When it is omitted, the `HOSTINGER_API_TOKEN` environment variable is used, which is also how the inventory plugin should receive it, because inventory files are not templated.

Modules that start an action on a virtual machine (power, recreate, snapshots, backups, hostname, PTR, firewall binding, SSH key binding and the malware scanner) return as soon as the action is accepted. Set `wait: true` to wait until it has finished; `wait_timeout` sets the limit in seconds (600 by default).
Modules that start an action on a virtual machine (power, recreate, snapshots, backups, hostname, PTR, firewall binding and the malware scanner) return as soon as the action is accepted. Set `wait: true` to wait until it has finished; `wait_timeout` sets the limit in seconds (600 by default).

Note: A valid payment method (such as Google Pay or PayPal) added to your Hostinger account is optional, and only required when provisioning new resources through the API. If you're using this collection to manage existing VPS instances, no payment method is needed.

Expand Down
2 changes: 1 addition & 1 deletion examples/ssh_key_attach_vps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
hostinger.vps.hostinger_vps_ssh_key_binding:
token: "{{ hostinger_token }}"
virtual_machine_id: "{{ vm_id }}"
public_key_ids: "{{ ssh_key_ids }}"
keys: "{{ ssh_keys }}"
register: result

- name: Show result
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace: hostinger
name: vps
version: 1.0.1
version: 1.1.0
authors: ["Hostinger Dev Team"]
description: Ansible collection to manage Hostinger VPS via Hostinger Public API.
license_file: GPL-3.0
Expand Down
30 changes: 14 additions & 16 deletions plugins/modules/hostinger_vps_ssh_key_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
module: hostinger_vps_ssh_key_binding
short_description: Attach public SSH keys to a Hostinger VPS
description:
- Attaches one or more existing public SSH keys to a specified virtual machine.
- Attaches one or more public SSH keys to a specified virtual machine.
options:
virtual_machine_id:
description: The ID of the VPS to attach the key(s) to
required: true
type: str
public_key_ids:
keys:
description:
- List of public SSH key IDs to attach to the VPS
- The actual public keys to attach to the VPS (e.g., ssh-ed25519 AAAAC3NzaC1... user@host)
- Each key must be in valid OpenSSH format.
required: true
type: list
elements: int
elements: str
version_added: 1.1.0
extends_documentation_fragment:
- hostinger.vps.api
- hostinger.vps.action_wait
author:
- Hostinger Dev Team (@hostinger)
'''
Expand All @@ -34,34 +35,32 @@
hostinger.vps.hostinger_vps_ssh_key_binding:
token: "{{ hostinger_token }}"
virtual_machine_id: "{{ vm_id }}"
public_key_ids:
- 237652
keys:
- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA... user@host"
'''

RETURN = '''
result:
description: API response
description: All SSH keys now attached to the VPS.
returned: always
type: dict
type: list
elements: dict
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.hostinger.vps.plugins.module_utils.api import (
HostingerApiError,
action_wait_argument_spec,
api_argument_spec,
client_from_module,
fail_on_api_error,
wait_for_action_if_requested,
)


def main():
module_args = api_argument_spec()
module_args.update(action_wait_argument_spec())
module_args.update(
virtual_machine_id=dict(type='str', required=True),
public_key_ids=dict(type='list', required=True, elements='int')
keys=dict(type='list', required=True, elements='str', no_log=False)
)

module = AnsibleModule(argument_spec=module_args)
Expand All @@ -71,10 +70,9 @@ def main():
try:
client = client_from_module(module)
result = client.post(
f"/api/vps/v1/public-keys/attach/{vm_id}",
body={"ids": module.params["public_key_ids"]},
f"/api/vps/v1/virtual-machines/{vm_id}/ssh-keys",
body={"keys": module.params["keys"]},
)
result = wait_for_action_if_requested(module, client, vm_id, result)
except HostingerApiError as error:
fail_on_api_error(module, error, "Attaching SSH keys")

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/hostinger_vps_ssh_key_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- name: Attach the key named "laptop" to a virtual machine
hostinger.vps.hostinger_vps_ssh_key_binding:
virtual_machine_id: 123456
public_key_ids: "{{ result.ssh_keys | selectattr('name', 'equalto', 'laptop') | map(attribute='id') | list }}"
keys: "{{ result.ssh_keys | selectattr('name', 'equalto', 'laptop') | map(attribute='key') | list }}"
'''

RETURN = r'''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function
__metaclass__ = type

from ansible_collections.hostinger.vps.plugins.modules import hostinger_vps_ssh_key_binding

SSH_KEYS_PATH = '/api/vps/v1/virtual-machines/592873/ssh-keys'
KEY = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA user@host'


def test_sends_key_text_to_the_ssh_keys_endpoint(api, run_module):
api.responses[('POST', SSH_KEYS_PATH)] = [{'key': KEY, 'name': 'user@host'}]

result = run_module(hostinger_vps_ssh_key_binding, {'virtual_machine_id': '592873', 'keys': [KEY]})

assert result['failed'] is False
assert result['result'] == [{'key': KEY, 'name': 'user@host'}]
assert api.calls == [('POST', SSH_KEYS_PATH, {'keys': [KEY]}, None)]
Loading