ansible-collection-infomaniak/plugins/modules/public_cloud_config_info.py
Bertrand Lanson f5fc5bb845
Some checks failed
pull-requests-open / Check commit compliance (pull_request) Successful in 38s
pull-requests-open / Run tests (pull_request) Failing after 1m27s
feat: add unit tests for public_cloud_config_info module
2024-09-14 13:13:43 +02:00

111 lines
2.8 KiB
Python

#!/usr/bin/python
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = r"""
---
module: ednz_cloud.infomaniak.public_cloud_config_info
short_description: Retrieves the public cloud configuration from the Infomaniak API.
version_added: "0.1.0"
description:
- This module queries the Infomaniak API to retrieve the public cloud configuration for a given account.
- It uses the provided API token for authentication and the account ID as a query parameter.
requirements:
- C(requests)
options:
api_token:
description: The API token used to authenticate with the Infomaniak API.
required: true
type: str
account_id:
description: The account ID for which to query the public cloud configuration.
required: true
type: str
author:
- Bertrand Lanson (@ednxzu)
"""
EXAMPLES = r"""
# Example: Retrieve public cloud configuration for account ID 965060
- name: Get public cloud configuration
public_cloud_config_info:
api_token: "your_api_token"
account_id: "123456"
"""
RETURN = r"""
config:
description:
- The public cloud configuration for the specified account.
type: dict
returned: always
sample:
free_tier: 300
free_tier_used: 24.34
account_resource_level: 2
valid_from: 1707584356
valid_to: 1717192799
project_count: 2
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.ednz_cloud.infomaniak.plugins.module_utils.infomaniak_api_client import (
InfomaniakAPIClient,
)
def get_public_cloud_config(client: InfomaniakAPIClient, account_id: str):
endpoint = "/public_clouds/config"
params = {"account_id": account_id}
response = client.get(endpoint, params=params)
if response.get("result") != "success":
raise Exception(f"API request failed with result: {response.get('result')}")
return response.get("data", {})
def run_module():
module_args = dict(
api_token=dict(type="str", required=True, no_log=True),
account_id=dict(type="str", required=True),
)
result = dict(changed=False, config={})
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
api_token = module.params["api_token"]
account_id = module.params["account_id"]
if module.check_mode:
module.exit_json(
changed=False,
msg="Check mode: No changes made, would retrieve public cloud config.",
)
client = InfomaniakAPIClient(api_version="1", api_token=api_token)
try:
config_data = get_public_cloud_config(client, account_id)
result["config"] = config_data
module.exit_json(**result)
except Exception as e:
module.fail_json(msg=str(e))
def main():
run_module()
if __name__ == "__main__":
main()