From da9ac0da02c8e65f85072ac1d6e05d55b48d4c3c Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Tue, 17 Jun 2025 11:35:47 -0400 Subject: [PATCH 1/2] add account settings --- README.md | 1 + requirements.txt | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 28dea3cf..067d7058 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Modules for managing Linode infrastructure. Name | Description | --- | ------------ | +[linode.cloud.account_settings](./docs/modules/account_settings.md)|Returns information related to your Account settings.| [linode.cloud.api_request](./docs/modules/api_request.md)|Make an arbitrary Linode API request.| [linode.cloud.database_mysql](./docs/modules/database_mysql.md)|Manage a Linode MySQL database.| [linode.cloud.database_mysql_v2](./docs/modules/database_mysql_v2.md)|Create, read, and update a Linode MySQL database.| diff --git a/requirements.txt b/requirements.txt index 880fb53f..52441599 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ -linode-api4>=5.32.0 +# TODO: Revert once the enhanced Linode interfaces support is released in linode_api4 +# linode-api4>=5.32.0 +git+https://github.com/linode/linode_api4-python@proj/enhanced-interfaces polling==0.3.2 ansible-specdoc>=0.0.19 From 303c444b1d21af3db285f992129e45e487ad9efb Mon Sep 17 00:00:00 2001 From: Jacob Riddle Date: Tue, 17 Jun 2025 11:42:12 -0400 Subject: [PATCH 2/2] add code files --- docs/modules/account_settings.md | 53 ++++++ .../doc_fragments/account_settings.py | 15 ++ plugins/modules/account_settings.py | 151 ++++++++++++++++++ .../targets/account_settings/tasks/main.yaml | 42 +++++ 4 files changed, 261 insertions(+) create mode 100644 docs/modules/account_settings.md create mode 100644 plugins/module_utils/doc_fragments/account_settings.py create mode 100644 plugins/modules/account_settings.py create mode 100644 tests/integration/targets/account_settings/tasks/main.yaml diff --git a/docs/modules/account_settings.md b/docs/modules/account_settings.md new file mode 100644 index 00000000..35b7a72a --- /dev/null +++ b/docs/modules/account_settings.md @@ -0,0 +1,53 @@ +# account_settings + +Returns information related to your Account settings. + +- [Minimum Required Fields](#minimum-required-fields) +- [Examples](#examples) +- [Parameters](#parameters) +- [Return Values](#return-values) + +## Minimum Required Fields +| Field | Type | Required | Description | +|-------------|-------|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `api_token` | `str` | **Required** | The Linode account personal access token. It is necessary to run the module.
It can be exposed by the environment variable `LINODE_API_TOKEN` instead.
See details in [Usage](https://github.com/linode/ansible_linode?tab=readme-ov-file#usage). | + +## Examples + +```yaml +- name: Retrieve + linode.cloud.account_settings: + state: present +``` + + +## Parameters + +| Field | Type | Required | Description | +|-----------|------|----------|------------------------------------------------------------------------------| +| `state` |
`str`
|
**Required**
| The state of Account Settings. **(Choices: `present`)** | +| `backups_enabled` |
`bool`
|
Optional
| Account-wide backups default. If true, all Linodes created will automatically be enrolled in the Backups service. If false, Linodes will not be enrolled by default, but may still be enrolled on creation or later. | +| `interfaces_for_new_linodes` |
`str`
|
Optional
| Defines if new Linodes can use legacy configuration interfaces. | +| `longview_subscription` |
`str`
|
Optional
| The Longview Pro tier you are currently subscribed to. The value must be a Longview subscription ID or null for Longview Free. | +| `managed` |
`bool`
|
Optional
| Our 24/7 incident response service. This robust, multi-homed monitoring system distributes monitoring checks to ensure that your servers remain online and available at all times. Linode Managed can monitor any service or software stack reachable over TCP or HTTP. Once you add a service to Linode Managed, we'll monitor it for connectivity, response, and total request time. | +| `network_helper` |
`bool`
|
Optional
| Enables network helper across all users by default for new Linodes and Linode Configs. | +| `object_storage` |
`str`
|
Optional
| A string describing the status of this account's Object Storage service enrollment. | + +## Return Values + +- `account_settings` - Account Settings in JSON serialized form. + + - Sample Response: + ```json + { + "backups_enabled": true, + "interfaces_for_new_linodes": "linode_only", + "longview_subscription": "longview-3", + "managed": true, + "network_helper": false, + "object_storage": "active", + } + ``` + - See the [Linode API response documentation](https://techdocs.akamai.com/linode-api/reference/get-account-settings) for a list of returned fields + + diff --git a/plugins/module_utils/doc_fragments/account_settings.py b/plugins/module_utils/doc_fragments/account_settings.py new file mode 100644 index 00000000..4d6d1448 --- /dev/null +++ b/plugins/module_utils/doc_fragments/account_settings.py @@ -0,0 +1,15 @@ +"""Documentation fragments for the account settings module""" + +specdoc_examples = [''' +- name: Retrieve + linode.cloud.account_settings: + state: present'''] + +result_account_settings_samples = ['''{ + "backups_enabled": true, + "interfaces_for_new_linodes": "linode_only", + "longview_subscription": "longview-3", + "managed": true, + "network_helper": false, + "object_storage": "active", +}'''] diff --git a/plugins/modules/account_settings.py b/plugins/modules/account_settings.py new file mode 100644 index 00000000..6d3d13e5 --- /dev/null +++ b/plugins/modules/account_settings.py @@ -0,0 +1,151 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +"""This module allows users to retrieve information about the Linode account settings.""" + +from __future__ import absolute_import, division, print_function + +from typing import Any, Optional, List + +import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.account_settings as docs + +from ansible_collections.linode.cloud.plugins.module_utils.linode_common import ( + LinodeModuleBase, +) +from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import ( + global_authors, + global_requirements, +) +from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import ( + filter_null_values, + handle_updates, +) +from ansible_specdoc.objects import ( + FieldType, + SpecDocMeta, + SpecField, + SpecReturnValue, +) +from linode_api4 import AccountSettings + +SPEC = { + "state": SpecField( + type=FieldType.string, + choices=["present"], + required=True, + description=["The state of Account Settings."], + ), + "backups_enabled": SpecField( + type=FieldType.bool, + description=[ + "Account-wide backups default. If true, all Linodes created " + "will automatically be enrolled in the Backups service. " + "If false, Linodes will not be enrolled by default, " + "but may still be enrolled on creation or later." + ], + ), + "interfaces_for_new_linodes": SpecField( + type=FieldType.string, + description=["Defines if new Linodes can use legacy configuration interfaces."], + ), + "longview_subscription": SpecField( + type=FieldType.string, + description=[ + "The Longview Pro tier you are currently subscribed to. " + "The value must be a Longview subscription ID or null for Longview Free." + ], + ), + "managed": SpecField( + type=FieldType.bool, + description=[ + "Our 24/7 incident response service. This robust, multi-homed " + "monitoring system distributes monitoring checks to ensure that " + "your servers remain online and available at all times. " + "Linode Managed can monitor any service or software stack " + "reachable over TCP or HTTP. Once you add a service to Linode Managed, " + "we'll monitor it for connectivity, response, and total request time." + ], + ), + "network_helper": SpecField( + type=FieldType.bool, + description=[ + "Enables network helper across all users by default " + "for new Linodes and Linode Configs." + ], + ), + "object_storage": SpecField( + type=FieldType.string, + description=[ + "A string describing the status of this account's " + "Object Storage service enrollment." + ], + ), +} + +SPECDOC_META = SpecDocMeta( + description=["Returns information related to your Account settings."], + requirements=global_requirements, + author=global_authors, + options=SPEC, + examples=docs.specdoc_examples, + return_values={ + "account_settings": SpecReturnValue( + description="Account Settings in JSON serialized form.", + docs_url="https://techdocs.akamai.com/linode-api/reference/get-account-settings", + type=FieldType.dict, + sample=docs.result_account_settings_samples, + ) + }, +) + +MUTABLE_FIELDS = {"interfaces_for_new_linodes", "backups_enabled", "network_helper"} + +DOCUMENTATION = r""" +""" +EXAMPLES = r""" +""" +RETURN = r""" +""" + +class Module(LinodeModuleBase): + """Module for viewing and updating Account Settings""" + + def __init__(self) -> None: + self.module_arg_spec = SPECDOC_META.ansible_spec + self.required_one_of: List[str] = [] + self.results = { + "changed": False, + "actions": [], + "account_settings": None, + } + + self._account_settings: Optional[AccountSettings] = None + + super().__init__( + module_arg_spec=self.module_arg_spec, + required_one_of=self.required_one_of, + ) + + def _update(self) -> None: + """Handles all updates for Account Settings""" + handle_updates( + self._account_settings, + filter_null_values(self.module.params), + MUTABLE_FIELDS, + self.register_action, + ) + + def _present(self) -> None: + self._account_settings = self.client.account.settings() + self._update() + self._account_settings._api_get() + self.results["account_settings"] = self._account_settings._raw_json + + def exec_module(self, **kwargs: Any) -> Optional[dict]: + """Entrypoint for Account Settings module""" + if kwargs.get("state") == "present": + self._present() + return self.results + +if __name__ == "__main__": + Module() diff --git a/tests/integration/targets/account_settings/tasks/main.yaml b/tests/integration/targets/account_settings/tasks/main.yaml new file mode 100644 index 00000000..6ec7de38 --- /dev/null +++ b/tests/integration/targets/account_settings/tasks/main.yaml @@ -0,0 +1,42 @@ +- name: account_settings + block: + - set_fact: + r: "{{ 1000000000 | random }}" + + - name: Get account_settings + linode.cloud.account_settings: + state: 'present' + register: info + + - name: Assert account_settings response + assert: + that: + - info.account_settings.longview_subscription is none + - info.account_settings.managed == false + - info.account_settings.object_storage == 'active' + + - name: Update account_settings + linode.cloud.account_settings: + state: 'present' + interfaces_for_new_linodes: 'legacy_config_only' + register: update + + - name: Assert account_settings updates + assert: + that: + - update.account_settings.interfaces_for_new_linodes == 'legacy_config_only' + + always: + - ignore_errors: yes + block: + - name: Change account_settings back + linode.cloud.account_settings: + state: 'present' + interfaces_for_new_linodes: 'linode_only' + + environment: + LINODE_UA_PREFIX: '{{ ua_prefix }}' + LINODE_API_TOKEN: '{{ api_token }}' + LINODE_API_URL: '{{ api_url }}' + LINODE_API_VERSION: '{{ api_version }}' + LINODE_CA: '{{ ca_file or "" }}'