Debian Codegraph @

ansible_collections/community/general/plugins/modules/ovh_monthly_billing.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2019, Francois Lallart (@fraff)
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: ovh_monthly_billing
11 author: Francois Lallart (@fraff)
12 version_added: '0.2.0'
13 short_description: Manage OVH monthly billing
14 description:
15 - Enable monthly billing on OVH cloud instances (be aware OVH does not allow to disable it).
16 requirements: ["ovh"]
17 extends_documentation_fragment:
18 - community.general._attributes
19 attributes:
20 check_mode:
21 support: full
22 diff_mode:
23 support: none
24 options:
25 project_id:
26 required: true
27 type: str
28 description:
29 - ID of the project, get it with U(https://api.ovh.com/console/#/cloud/project#GET).
30 instance_id:
31 required: true
32 type: str
33 description:
34 - ID of the instance, get it with U(https://api.ovh.com/console/#/cloud/project/%7BserviceName%7D/instance#GET).
35 endpoint:
36 type: str
37 description:
38 - The endpoint to use (for instance V(ovh-eu)).
39 application_key:
40 type: str
41 description:
42 - The applicationKey to use.
43 application_secret:
44 type: str
45 description:
46 - The application secret to use.
47 consumer_key:
48 type: str
49 description:
50 - The consumer key to use.
51 """
52
53 EXAMPLES = r"""
54 - name: Basic usage, using auth from /etc/ovh.conf
55 community.general.ovh_monthly_billing:
56 project_id: 0c727a20aa144485b70c44dee9123b46
57 instance_id: 8fa89ad2-8f08-4220-9fa4-9695ea23e948
58
59 # Get openstack cloud ID and instance ID, OVH use them in its API
60 - name: Get openstack cloud ID and instance ID
61 os_server_info:
62 cloud: myProjectName
63 region_name: myRegionName
64 server: myServerName
65 register: openstack_servers
66
67 - name: Use IDs
68 community.general.ovh_monthly_billing:
69 project_id: "{{ openstack_servers.0.tenant_id }}"
70 instance_id: "{{ openstack_servers.0.id }}"
71 application_key: yourkey
72 application_secret: yoursecret
73 consumer_key: yourconsumerkey
74 """
75
76 RETURN = r"""
77 """
78
79 import traceback
80
81 try:
82 import ovh
83 import ovh.exceptions
84 from ovh.exceptions import APIError
85
86 HAS_OVH = True
87 except ImportError:
88 HAS_OVH = False
89 OVH_IMPORT_ERROR = traceback.format_exc()
90
91 from ansible.module_utils.basic import AnsibleModule
92
93
94 def main():
95 module = AnsibleModule(
96 argument_spec=dict(
97 project_id=dict(required=True),
98 instance_id=dict(required=True),
99 endpoint=dict(),
100 application_key=dict(no_log=True),
101 application_secret=dict(no_log=True),
102 consumer_key=dict(no_log=True),
103 ),
104 supports_check_mode=True,
105 )
106
107 # Get parameters
108 project_id = module.params.get("project_id")
109 instance_id = module.params.get("instance_id")
110 endpoint = module.params.get("endpoint")
111 application_key = module.params.get("application_key")
112 application_secret = module.params.get("application_secret")
113 consumer_key = module.params.get("consumer_key")
114 instance = ""
115 ovh_billing_status = ""
116
117 if not HAS_OVH:
118 module.fail_json(msg="python-ovh is required to run this module, see https://github.com/ovh/python-ovh")
119
120 # Connect to OVH API
121 client = ovh.Client(
122 endpoint=endpoint,
123 application_key=application_key,
124 application_secret=application_secret,
125 consumer_key=consumer_key,
126 )
127
128 # Check that the instance exists
129 try:
130 client.get(f"/cloud/project/{project_id}")
131 except ovh.exceptions.ResourceNotFoundError:
132 module.fail_json(msg=f"project {project_id} does not exist")
133
134 # Check that the instance exists
135 try:
136 instance = client.get(f"/cloud/project/{project_id}/instance/{instance_id}")
137 except ovh.exceptions.ResourceNotFoundError:
138 module.fail_json(msg=f"instance {instance_id} does not exist in project {project_id}")
139
140 # Is monthlyBilling already enabled or pending ?
141 if instance["monthlyBilling"] is not None:
142 if instance["monthlyBilling"]["status"] in ["ok", "activationPending"]:
143 module.exit_json(changed=False, ovh_billing_status=instance["monthlyBilling"])
144
145 if module.check_mode:
146 module.exit_json(changed=True, msg="Dry Run!")
147
148 try:
149 ovh_billing_status = client.post(f"/cloud/project/{project_id}/instance/{instance_id}/activeMonthlyBilling")
150 module.exit_json(changed=True, ovh_billing_status=ovh_billing_status["monthlyBilling"])
151 except APIError as apiError:
152 module.fail_json(changed=False, msg=f"Failed to call OVH API: {apiError}")
153
154 # We should never reach here
155 module.fail_json(msg="Internal ovh_monthly_billing module error")
156
157
158 if __name__ == "__main__":
159 main()