1# coding: utf-8
2# Copyright (c) 2016, 2021, Oracle and/or its affiliates.  All rights reserved.
3# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
4
5import oci  # noqa: F401
6from oci.util import WAIT_RESOURCE_NOT_FOUND  # noqa: F401
7
8
9class MysqlaasClientCompositeOperations(object):
10    """
11    This class provides a wrapper around :py:class:`~oci.mysql.MysqlaasClient` and offers convenience methods
12    for operations that would otherwise need to be chained together. For example, instead of performing an action
13    on a resource (e.g. launching an instance, creating a load balancer) and then using a waiter to wait for the resource
14    to enter a given state, you can call a single method in this class to accomplish the same functionality
15    """
16
17    def __init__(self, client, **kwargs):
18        """
19        Creates a new MysqlaasClientCompositeOperations object
20
21        :param MysqlaasClient client:
22            The service client which will be wrapped by this object
23        """
24        self.client = client
25
26    def create_configuration_and_wait_for_state(self, create_configuration_details, wait_for_states=[], operation_kwargs={}, waiter_kwargs={}):
27        """
28        Calls :py:func:`~oci.mysql.MysqlaasClient.create_configuration` and waits for the :py:class:`~oci.mysql.models.WorkRequest`
29        to enter the given state(s).
30
31        :param oci.mysql.models.CreateConfigurationDetails create_configuration_details: (required)
32            Request to create a Configuration.
33
34        :param list[str] wait_for_states:
35            An array of states to wait on. These should be valid values for :py:attr:`~oci.mysql.models.WorkRequest.status`
36
37        :param dict operation_kwargs:
38            A dictionary of keyword arguments to pass to :py:func:`~oci.mysql.MysqlaasClient.create_configuration`
39
40        :param dict waiter_kwargs:
41            A dictionary of keyword arguments to pass to the :py:func:`oci.wait_until` function. For example, you could pass ``max_interval_seconds`` or ``max_interval_seconds``
42            as dictionary keys to modify how long the waiter function will wait between retries and the maximum amount of time it will wait
43        """
44        operation_result = self.client.create_configuration(create_configuration_details, **operation_kwargs)
45        if not wait_for_states:
46            return operation_result
47
48        lowered_wait_for_states = [w.lower() for w in wait_for_states]
49        wait_for_resource_id = operation_result.headers['opc-work-request-id']
50
51        try:
52            waiter_result = oci.wait_until(
53                self.client,
54                self.client.get_work_request(wait_for_resource_id),
55                evaluate_response=lambda r: getattr(r.data, 'status') and getattr(r.data, 'status').lower() in lowered_wait_for_states,
56                **waiter_kwargs
57            )
58            result_to_return = waiter_result
59
60            return result_to_return
61        except Exception as e:
62            raise oci.exceptions.CompositeOperationError(partial_results=[operation_result], cause=e)
63
64    def delete_configuration_and_wait_for_state(self, configuration_id, wait_for_states=[], operation_kwargs={}, waiter_kwargs={}):
65        """
66        Calls :py:func:`~oci.mysql.MysqlaasClient.delete_configuration` and waits for the :py:class:`~oci.mysql.models.Configuration` acted upon
67        to enter the given state(s).
68
69        :param str configuration_id: (required)
70            The OCID of the Configuration.
71
72        :param list[str] wait_for_states:
73            An array of states to wait on. These should be valid values for :py:attr:`~oci.mysql.models.Configuration.lifecycle_state`
74
75        :param dict operation_kwargs:
76            A dictionary of keyword arguments to pass to :py:func:`~oci.mysql.MysqlaasClient.delete_configuration`
77
78        :param dict waiter_kwargs:
79            A dictionary of keyword arguments to pass to the :py:func:`oci.wait_until` function. For example, you could pass ``max_interval_seconds`` or ``max_interval_seconds``
80            as dictionary keys to modify how long the waiter function will wait between retries and the maximum amount of time it will wait
81        """
82        initial_get_result = self.client.get_configuration(configuration_id)
83        operation_result = None
84        try:
85            operation_result = self.client.delete_configuration(configuration_id, **operation_kwargs)
86        except oci.exceptions.ServiceError as e:
87            if e.status == 404:
88                return WAIT_RESOURCE_NOT_FOUND
89            else:
90                raise e
91
92        if not wait_for_states:
93            return operation_result
94
95        lowered_wait_for_states = [w.lower() for w in wait_for_states]
96
97        try:
98            waiter_result = oci.wait_until(
99                self.client,
100                initial_get_result,
101                evaluate_response=lambda r: getattr(r.data, 'lifecycle_state') and getattr(r.data, 'lifecycle_state').lower() in lowered_wait_for_states,
102                succeed_on_not_found=True,
103                **waiter_kwargs
104            )
105            result_to_return = waiter_result
106
107            return result_to_return
108        except Exception as e:
109            raise oci.exceptions.CompositeOperationError(partial_results=[operation_result], cause=e)
110
111    def update_configuration_and_wait_for_state(self, configuration_id, update_configuration_details, wait_for_states=[], operation_kwargs={}, waiter_kwargs={}):
112        """
113        Calls :py:func:`~oci.mysql.MysqlaasClient.update_configuration` and waits for the :py:class:`~oci.mysql.models.Configuration` acted upon
114        to enter the given state(s).
115
116        :param str configuration_id: (required)
117            The OCID of the Configuration.
118
119        :param oci.mysql.models.UpdateConfigurationDetails update_configuration_details: (required)
120            Request to update a Configuration.
121
122        :param list[str] wait_for_states:
123            An array of states to wait on. These should be valid values for :py:attr:`~oci.mysql.models.Configuration.lifecycle_state`
124
125        :param dict operation_kwargs:
126            A dictionary of keyword arguments to pass to :py:func:`~oci.mysql.MysqlaasClient.update_configuration`
127
128        :param dict waiter_kwargs:
129            A dictionary of keyword arguments to pass to the :py:func:`oci.wait_until` function. For example, you could pass ``max_interval_seconds`` or ``max_interval_seconds``
130            as dictionary keys to modify how long the waiter function will wait between retries and the maximum amount of time it will wait
131        """
132        operation_result = self.client.update_configuration(configuration_id, update_configuration_details, **operation_kwargs)
133        if not wait_for_states:
134            return operation_result
135
136        lowered_wait_for_states = [w.lower() for w in wait_for_states]
137        wait_for_resource_id = operation_result.data.id
138
139        try:
140            waiter_result = oci.wait_until(
141                self.client,
142                self.client.get_configuration(wait_for_resource_id),
143                evaluate_response=lambda r: getattr(r.data, 'lifecycle_state') and getattr(r.data, 'lifecycle_state').lower() in lowered_wait_for_states,
144                **waiter_kwargs
145            )
146            result_to_return = waiter_result
147
148            return result_to_return
149        except Exception as e:
150            raise oci.exceptions.CompositeOperationError(partial_results=[operation_result], cause=e)
151