# Source code for azure.mgmt.apimanagement.operations.tenant_configuration_operations

    # coding=utf-8
    # --------------------------------------------------------------------------
    # Copyright (c) Microsoft Corporation. All rights reserved.
    # Licensed under the MIT License. See License.txt in the project root for
    # license information.
    #
    # Code generated by Microsoft (R) AutoRest Code Generator.
    # Changes may cause incorrect behavior and will be lost if the code is
    # regenerated.
    # --------------------------------------------------------------------------

    import uuid
    from msrest.pipeline import ClientRawResponse
    from msrestazure.azure_exceptions import CloudError
    from msrest.polling import LROPoller, NoPolling
    from msrestazure.polling.arm_polling import ARMPolling

    from .. import models

    class TenantConfigurationOperations(object):
        """TenantConfigurationOperations operations.

        :param client: Client for service requests.
        :param config: Configuration of service client.
        :param serializer: An object model serializer.
        :param deserializer: An object model deserializer.
        :ivar api_version: Version of the API to be used with the client request. Constant value: "2019-01-01".
        :ivar configuration_name: The identifier of the Git Configuration Operation. Constant value: "configuration".
        """

        models = models

        def __init__(self, client, config, serializer, deserializer):

            self._client = client
            self._serialize = serializer
            self._deserialize = deserializer
            self.api_version = "2019-01-01"
            self.configuration_name = "configuration"

            self.config = config

        def _deploy_initial(
                self, resource_group_name, service_name, branch, force=None, custom_headers=None, raw=False, **operation_config):
            parameters = models.DeployConfigurationParameters(branch=branch, force=force)

            # Construct URL
            url = self.deploy.metadata['url']
            path_format_arguments = {
                'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
                'serviceName': self._serialize.url("service_name", service_name, 'str', max_length=50, min_length=1, pattern=r'^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$'),
                'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
                'configurationName': self._serialize.url("self.configuration_name", self.configuration_name, 'str')
            }
            url = self._client.format_url(url, **path_format_arguments)

            # Construct parameters
            query_parameters = {}
            query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')

            # Construct headers
            header_parameters = {}
            header_parameters['Accept'] = 'application/json'
            header_parameters['Content-Type'] = 'application/json; charset=utf-8'
            if self.config.generate_client_request_id:
                header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
            if custom_headers:
                header_parameters.update(custom_headers)
            if self.config.accept_language is not None:
                header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')

            # Construct body
            body_content = self._serialize.body(parameters, 'DeployConfigurationParameters')

            # Construct and send request
            request = self._client.post(url, query_parameters, header_parameters, body_content)
            response = self._client.send(request, stream=False, **operation_config)

            if response.status_code not in [200, 202]:
                raise models.ErrorResponseException(self._deserialize, response)

            deserialized = None

            if response.status_code == 200:
                deserialized = self._deserialize('OperationResultContract', response)

            if raw:
                client_raw_response = ClientRawResponse(deserialized, response)
                return client_raw_response

            return deserialized

        def deploy(
                self, resource_group_name, service_name, branch, force=None, custom_headers=None, raw=False, polling=True, **operation_config):
            """This operation applies changes from the specified Git branch to the
            configuration database. This is a long running operation and could take
            several minutes to complete.

            :param resource_group_name: The name of the resource group.
            :type resource_group_name: str
            :param service_name: The name of the API Management service.
            :type service_name: str
            :param branch: The name of the Git branch from which the configuration
             is to be deployed to the configuration database.
            :type branch: str
            :param force: The value enforcing deleting subscriptions to products
             that are deleted in this update.
            :type force: bool
            :param dict custom_headers: headers that will be added to the request
            :param bool raw: The poller return type is ClientRawResponse, the
             direct response alongside the deserialized response
            :param polling: True for ARMPolling, False for no polling, or a
             polling object for personal polling strategy
            :return: An instance of LROPoller that returns OperationResultContract
             or ClientRawResponse<OperationResultContract> if raw==True
            :rtype:
             ~msrestazure.azure_operation.AzureOperationPoller[~azure.mgmt.apimanagement.models.OperationResultContract]
             or
             ~msrestazure.azure_operation.AzureOperationPoller[~msrest.pipeline.ClientRawResponse[~azure.mgmt.apimanagement.models.OperationResultContract]]
            :raises:
             :class:`ErrorResponseException<azure.mgmt.apimanagement.models.ErrorResponseException>`
            """
            raw_result = self._deploy_initial(
                resource_group_name=resource_group_name,
                service_name=service_name,
                branch=branch,
                force=force,
                custom_headers=custom_headers,
                raw=True,
                **operation_config
            )

            def get_long_running_output(response):
                deserialized = self._deserialize('OperationResultContract', response)

                if raw:
                    client_raw_response = ClientRawResponse(deserialized, response)
                    return client_raw_response

                return deserialized

            lro_delay = operation_config.get(
                'long_running_operation_timeout',
                self.config.long_running_operation_timeout)
            if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'location'}, **operation_config)
            elif polling is False: polling_method = NoPolling()
            else: polling_method = polling
            return LROPoller(self._client, raw_result, get_long_running_output, polling_method)
        deploy.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/{configurationName}/deploy'}

        def _save_initial(
                self, resource_group_name, service_name, branch, force=None, custom_headers=None, raw=False, **operation_config):
            parameters = models.SaveConfigurationParameter(branch=branch, force=force)

            # Construct URL
            url = self.save.metadata['url']
            path_format_arguments = {
                'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
                'serviceName': self._serialize.url("service_name", service_name, 'str', max_length=50, min_length=1, pattern=r'^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$'),
                'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
                'configurationName': self._serialize.url("self.configuration_name", self.configuration_name, 'str')
            }
            url = self._client.format_url(url, **path_format_arguments)

            # Construct parameters
            query_parameters = {}
            query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')

            # Construct headers
            header_parameters = {}
            header_parameters['Accept'] = 'application/json'
            header_parameters['Content-Type'] = 'application/json; charset=utf-8'
            if self.config.generate_client_request_id:
                header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
            if custom_headers:
                header_parameters.update(custom_headers)
            if self.config.accept_language is not None:
                header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')

            # Construct body
            body_content = self._serialize.body(parameters, 'SaveConfigurationParameter')

            # Construct and send request
            request = self._client.post(url, query_parameters, header_parameters, body_content)
            response = self._client.send(request, stream=False, **operation_config)

            if response.status_code not in [200, 202]:
                raise models.ErrorResponseException(self._deserialize, response)

            deserialized = None

            if response.status_code == 200:
                deserialized = self._deserialize('OperationResultContract', response)

            if raw:
                client_raw_response = ClientRawResponse(deserialized, response)
                return client_raw_response

            return deserialized

        def save(
                self, resource_group_name, service_name, branch, force=None, custom_headers=None, raw=False, polling=True, **operation_config):
            """This operation creates a commit with the current configuration snapshot
            to the specified branch in the repository. This is a long running
            operation and could take several minutes to complete.

            :param resource_group_name: The name of the resource group.
            :type resource_group_name: str
            :param service_name: The name of the API Management service.
            :type service_name: str
            :param branch: The name of the Git branch in which to commit the
             current configuration snapshot.
            :type branch: str
            :param force: The value if true, the current configuration database is
             committed to the Git repository, even if the Git repository has newer
             changes that would be overwritten.
            :type force: bool
            :param dict custom_headers: headers that will be added to the request
            :param bool raw: The poller return type is ClientRawResponse, the
             direct response alongside the deserialized response
            :param polling: True for ARMPolling, False for no polling, or a
             polling object for personal polling strategy
            :return: An instance of LROPoller that returns OperationResultContract
             or ClientRawResponse<OperationResultContract> if raw==True
            :rtype:
             ~msrestazure.azure_operation.AzureOperationPoller[~azure.mgmt.apimanagement.models.OperationResultContract]
             or
             ~msrestazure.azure_operation.AzureOperationPoller[~msrest.pipeline.ClientRawResponse[~azure.mgmt.apimanagement.models.OperationResultContract]]
            :raises:
             :class:`ErrorResponseException<azure.mgmt.apimanagement.models.ErrorResponseException>`
            """
            raw_result = self._save_initial(
                resource_group_name=resource_group_name,
                service_name=service_name,
                branch=branch,
                force=force,
                custom_headers=custom_headers,
                raw=True,
                **operation_config
            )

            def get_long_running_output(response):
                deserialized = self._deserialize('OperationResultContract', response)

                if raw:
                    client_raw_response = ClientRawResponse(deserialized, response)
                    return client_raw_response

                return deserialized

            lro_delay = operation_config.get(
                'long_running_operation_timeout',
                self.config.long_running_operation_timeout)
            if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'location'}, **operation_config)
            elif polling is False: polling_method = NoPolling()
            else: polling_method = polling
            return LROPoller(self._client, raw_result, get_long_running_output, polling_method)
        save.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/{configurationName}/save'}

        def _validate_initial(
                self, resource_group_name, service_name, branch, force=None, custom_headers=None, raw=False, **operation_config):
            parameters = models.DeployConfigurationParameters(branch=branch, force=force)

            # Construct URL
            url = self.validate.metadata['url']
            path_format_arguments = {
                'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
                'serviceName': self._serialize.url("service_name", service_name, 'str', max_length=50, min_length=1, pattern=r'^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$'),
                'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
                'configurationName': self._serialize.url("self.configuration_name", self.configuration_name, 'str')
            }
            url = self._client.format_url(url, **path_format_arguments)

            # Construct parameters
            query_parameters = {}
            query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')

            # Construct headers
            header_parameters = {}
            header_parameters['Accept'] = 'application/json'
            header_parameters['Content-Type'] = 'application/json; charset=utf-8'
            if self.config.generate_client_request_id:
                header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
            if custom_headers:
                header_parameters.update(custom_headers)
            if self.config.accept_language is not None:
                header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')

            # Construct body
            body_content = self._serialize.body(parameters, 'DeployConfigurationParameters')

            # Construct and send request
            request = self._client.post(url, query_parameters, header_parameters, body_content)
            response = self._client.send(request, stream=False, **operation_config)

            if response.status_code not in [200, 202]:
                raise models.ErrorResponseException(self._deserialize, response)

            deserialized = None

            if response.status_code == 200:
                deserialized = self._deserialize('OperationResultContract', response)

            if raw:
                client_raw_response = ClientRawResponse(deserialized, response)
                return client_raw_response

            return deserialized

        def validate(
                self, resource_group_name, service_name, branch, force=None, custom_headers=None, raw=False, polling=True, **operation_config):
            """This operation validates the changes in the specified Git branch. This
            is a long running operation and could take several minutes to complete.

            :param resource_group_name: The name of the resource group.
            :type resource_group_name: str
            :param service_name: The name of the API Management service.
            :type service_name: str
            :param branch: The name of the Git branch from which the configuration
             is to be deployed to the configuration database.
            :type branch: str
            :param force: The value enforcing deleting subscriptions to products
             that are deleted in this update.
            :type force: bool
            :param dict custom_headers: headers that will be added to the request
            :param bool raw: The poller return type is ClientRawResponse, the
             direct response alongside the deserialized response
            :param polling: True for ARMPolling, False for no polling, or a
             polling object for personal polling strategy
            :return: An instance of LROPoller that returns OperationResultContract
             or ClientRawResponse<OperationResultContract> if raw==True
            :rtype:
             ~msrestazure.azure_operation.AzureOperationPoller[~azure.mgmt.apimanagement.models.OperationResultContract]
             or
             ~msrestazure.azure_operation.AzureOperationPoller[~msrest.pipeline.ClientRawResponse[~azure.mgmt.apimanagement.models.OperationResultContract]]
            :raises:
             :class:`ErrorResponseException<azure.mgmt.apimanagement.models.ErrorResponseException>`
            """
            raw_result = self._validate_initial(
                resource_group_name=resource_group_name,
                service_name=service_name,
                branch=branch,
                force=force,
                custom_headers=custom_headers,
                raw=True,
                **operation_config
            )

            def get_long_running_output(response):
                deserialized = self._deserialize('OperationResultContract', response)

                if raw:
                    client_raw_response = ClientRawResponse(deserialized, response)
                    return client_raw_response

                return deserialized

            lro_delay = operation_config.get(
                'long_running_operation_timeout',
                self.config.long_running_operation_timeout)
            if polling is True: polling_method = ARMPolling(lro_delay, lro_options={'final-state-via': 'location'}, **operation_config)
            elif polling is False: polling_method = NoPolling()
            else: polling_method = polling
            return LROPoller(self._client, raw_result, get_long_running_output, polling_method)
        validate.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/{configurationName}/validate'}

        def get_sync_state(
                self, resource_group_name, service_name, custom_headers=None, raw=False, **operation_config):
            """Gets the status of the most recent synchronization between the
            configuration database and the Git repository.

            :param resource_group_name: The name of the resource group.
            :type resource_group_name: str
            :param service_name: The name of the API Management service.
            :type service_name: str
            :param dict custom_headers: headers that will be added to the request
            :param bool raw: returns the direct response alongside the
             deserialized response
            :param operation_config: :ref:`Operation configuration
             overrides<msrest:optionsforoperations>`.
            :return: TenantConfigurationSyncStateContract or ClientRawResponse if
             raw=true
            :rtype:
             ~azure.mgmt.apimanagement.models.TenantConfigurationSyncStateContract
             or ~msrest.pipeline.ClientRawResponse
            :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
            """
            # Construct URL
            url = self.get_sync_state.metadata['url']
            path_format_arguments = {
                'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
                'serviceName': self._serialize.url("service_name", service_name, 'str', max_length=50, min_length=1, pattern=r'^[a-zA-Z](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$'),
                'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
                'configurationName': self._serialize.url("self.configuration_name", self.configuration_name, 'str')
            }
            url = self._client.format_url(url, **path_format_arguments)

            # Construct parameters
            query_parameters = {}
            query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')

            # Construct headers
            header_parameters = {}
            header_parameters['Accept'] = 'application/json'
            if self.config.generate_client_request_id:
                header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
            if custom_headers:
                header_parameters.update(custom_headers)
            if self.config.accept_language is not None:
                header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')

            # Construct and send request
            request = self._client.get(url, query_parameters, header_parameters)
            response = self._client.send(request, stream=False, **operation_config)

            if response.status_code not in [200]:
                exp = CloudError(response)
                exp.request_id = response.headers.get('x-ms-request-id')
                raise exp

            deserialized = None

            if response.status_code == 200:
                deserialized = self._deserialize('TenantConfigurationSyncStateContract', response)

            if raw:
                client_raw_response = ClientRawResponse(deserialized, response)
                return client_raw_response

            return deserialized
        get_sync_state.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/tenant/{configurationName}/syncState'}