|
| 1 | +from __future__ import (absolute_import, division, print_function) |
| 2 | + |
| 3 | +from ansible.module_utils.basic import AnsibleModule |
| 4 | + |
| 5 | +__metaclass__ = type |
| 6 | + |
| 7 | +DOCUMENTATION = ''' |
| 8 | +--- |
| 9 | +module: dedicated_server_engagement_strategy |
| 10 | +short_description: Sets the engagement strategy for a dedicated server |
| 11 | +description: |
| 12 | + - This module sets the engagement strategy for a dedicated server. |
| 13 | + - the engagement strategy is the rule that will be applied at the end of the current engagement period, if any. |
| 14 | + - Possible values are: |
| 15 | + - REACTIVATE_ENGAGEMENT |
| 16 | + - STOP_ENGAGEMENT_FALLBACK_DEFAULT_PRICE |
| 17 | + - CANCEL_SERVICE |
| 18 | +author: Marco Sarti <[email protected]> |
| 19 | +requirements: |
| 20 | + - ovh >= 0.5.0 |
| 21 | +options: |
| 22 | + engagement_strategy: |
| 23 | + required: true |
| 24 | + description: |
| 25 | + - The engagement strategy rule to apply |
| 26 | + service_name: |
| 27 | + required: true |
| 28 | + description: |
| 29 | + - The service name |
| 30 | +''' |
| 31 | + |
| 32 | +EXAMPLES = r''' |
| 33 | +- name: "Changes the engagement strategy for the service" |
| 34 | + synthesio.ovh.dedicated_server_engagement_strategy: |
| 35 | + engagement_strategy: "{{ engagement_strategy }}" |
| 36 | + service_name: "{{ service_name }}" |
| 37 | + delegate_to: localhost |
| 38 | +''' |
| 39 | + |
| 40 | +RETURN = ''' # ''' |
| 41 | + |
| 42 | +from ansible_collections.synthesio.ovh.plugins.module_utils.ovh import OVH, ovh_argument_spec |
| 43 | + |
| 44 | + |
| 45 | +def run_module(): |
| 46 | + module_args = ovh_argument_spec() |
| 47 | + module_args.update(dict( |
| 48 | + engagement_strategy=dict(required=True), |
| 49 | + service_name=dict(required=True) |
| 50 | + )) |
| 51 | + |
| 52 | + module = AnsibleModule( |
| 53 | + argument_spec=module_args, |
| 54 | + supports_check_mode=True |
| 55 | + ) |
| 56 | + client = OVH(module) |
| 57 | + |
| 58 | + engagement_strategy = module.params['engagement_strategy'] |
| 59 | + service_name = module.params['service_name'] |
| 60 | + |
| 61 | + if module.check_mode: |
| 62 | + module.exit_json(msg=f"engagement_strategy has been set to {engagement_strategy} ! - (dry run mode)", changed=True) |
| 63 | + |
| 64 | + result = client.wrap_call("GET", f"/dedicated/server/{service_name}/serviceInfos") |
| 65 | + |
| 66 | + service_id = result["serviceId"] |
| 67 | + |
| 68 | + service = client.wrap_call("GET", f"/services/{service_id}") |
| 69 | + |
| 70 | + if service['billing']['engagement'] is None: |
| 71 | + module.exit_json(msg=f"No engagement for server {service_name}", changed=False) |
| 72 | + |
| 73 | + if service['billing']['engagement']['endRule']['strategy'] == engagement_strategy: |
| 74 | + module.exit_json(msg=f"Engagement strategy is already {engagement_strategy} on {service_name}", changed=False) |
| 75 | + |
| 76 | + if engagement_strategy not in service['billing']['engagement']['endRule']['possibleStrategies']: |
| 77 | + module.fail_json(msg=f"Strategy {engagement_strategy} not available for service") |
| 78 | + |
| 79 | + resource = {'strategy': engagement_strategy} |
| 80 | + |
| 81 | + client.wrap_call( |
| 82 | + "PUT", |
| 83 | + f"/services/{service_id}/billing/engagement/endRule", |
| 84 | + **resource |
| 85 | + ) |
| 86 | + module.exit_json( |
| 87 | + msg=f"engagement_strategy succesfully set to {engagement_strategy} for {service_name} !", changed=True) |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + run_module() |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + main() |
0 commit comments