|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""This module allows users to retrieve and modify Linode account settings.""" |
| 5 | + |
| 6 | +from __future__ import absolute_import, division, print_function |
| 7 | + |
| 8 | +from typing import Any, List, Optional |
| 9 | + |
| 10 | +import ansible_collections.linode.cloud.plugins.module_utils.doc_fragments.account_settings as docs |
| 11 | +from ansible_collections.linode.cloud.plugins.module_utils.linode_common import ( |
| 12 | + LinodeModuleBase, |
| 13 | +) |
| 14 | +from ansible_collections.linode.cloud.plugins.module_utils.linode_docs import ( |
| 15 | + global_authors, |
| 16 | + global_requirements, |
| 17 | +) |
| 18 | +from ansible_collections.linode.cloud.plugins.module_utils.linode_helper import ( |
| 19 | + filter_null_values, |
| 20 | + handle_updates, |
| 21 | +) |
| 22 | +from ansible_specdoc.objects import ( |
| 23 | + FieldType, |
| 24 | + SpecDocMeta, |
| 25 | + SpecField, |
| 26 | + SpecReturnValue, |
| 27 | +) |
| 28 | +from linode_api4 import AccountSettings |
| 29 | + |
| 30 | +SPEC = { |
| 31 | + "state": SpecField( |
| 32 | + type=FieldType.string, |
| 33 | + choices=["present"], |
| 34 | + required=True, |
| 35 | + description=["The state of Account Settings."], |
| 36 | + ), |
| 37 | + "backups_enabled": SpecField( |
| 38 | + type=FieldType.bool, |
| 39 | + description=[ |
| 40 | + "Account-wide backups default. If true, all Linodes created " |
| 41 | + "will automatically be enrolled in the Backups service. " |
| 42 | + "If false, Linodes will not be enrolled by default, " |
| 43 | + "but may still be enrolled on creation or later." |
| 44 | + ], |
| 45 | + ), |
| 46 | + "longview_subscription": SpecField( |
| 47 | + type=FieldType.string, |
| 48 | + description=[ |
| 49 | + "The Longview Pro tier you are currently subscribed to. " |
| 50 | + "The value must be a Longview subscription ID or null for Longview Free." |
| 51 | + ], |
| 52 | + ), |
| 53 | + "network_helper": SpecField( |
| 54 | + type=FieldType.bool, |
| 55 | + description=[ |
| 56 | + "Enables network helper across all users by default " |
| 57 | + "for new Linodes and Linode Configs." |
| 58 | + ], |
| 59 | + ), |
| 60 | +} |
| 61 | + |
| 62 | +SPECDOC_META = SpecDocMeta( |
| 63 | + description=["Returns information related to your Account settings."], |
| 64 | + requirements=global_requirements, |
| 65 | + author=global_authors, |
| 66 | + options=SPEC, |
| 67 | + examples=docs.specdoc_examples, |
| 68 | + return_values={ |
| 69 | + "account_settings": SpecReturnValue( |
| 70 | + description="Account Settings in JSON serialized form.", |
| 71 | + docs_url="https://techdocs.akamai.com/linode-api/reference/get-account-settings", |
| 72 | + type=FieldType.dict, |
| 73 | + sample=docs.result_account_settings_samples, |
| 74 | + ) |
| 75 | + }, |
| 76 | +) |
| 77 | + |
| 78 | +MUTABLE_FIELDS = {"backups_enabled", "network_helper"} |
| 79 | + |
| 80 | +DOCUMENTATION = r""" |
| 81 | +""" |
| 82 | +EXAMPLES = r""" |
| 83 | +""" |
| 84 | +RETURN = r""" |
| 85 | +""" |
| 86 | + |
| 87 | + |
| 88 | +class Module(LinodeModuleBase): |
| 89 | + """Module for viewing and updating Account Settings""" |
| 90 | + |
| 91 | + def __init__(self) -> None: |
| 92 | + self.module_arg_spec = SPECDOC_META.ansible_spec |
| 93 | + self.required_one_of: List[str] = [] |
| 94 | + self.results = { |
| 95 | + "changed": False, |
| 96 | + "actions": [], |
| 97 | + "account_settings": None, |
| 98 | + } |
| 99 | + |
| 100 | + self._account_settings: Optional[AccountSettings] = None |
| 101 | + |
| 102 | + super().__init__( |
| 103 | + module_arg_spec=self.module_arg_spec, |
| 104 | + required_one_of=self.required_one_of, |
| 105 | + ) |
| 106 | + |
| 107 | + def _update(self) -> None: |
| 108 | + """Handles all updates for Account Settings""" |
| 109 | + handle_updates( |
| 110 | + self._account_settings, |
| 111 | + filter_null_values(self.module.params), |
| 112 | + MUTABLE_FIELDS, |
| 113 | + self.register_action, |
| 114 | + ) |
| 115 | + |
| 116 | + def _present(self) -> None: |
| 117 | + self._account_settings = self.client.account.settings() |
| 118 | + self._update() |
| 119 | + self._account_settings._api_get() |
| 120 | + self.results["account_settings"] = self._account_settings._raw_json |
| 121 | + |
| 122 | + def exec_module(self, **kwargs: Any) -> Optional[dict]: |
| 123 | + """Entrypoint for Account Settings module""" |
| 124 | + if kwargs.get("state") == "present": |
| 125 | + self._present() |
| 126 | + return self.results |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + Module() |
0 commit comments