#!/usr/bin/python
#
# Copyright (c) 2018 Yunge Zhu, <yungez@microsoft.com>
#
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}


DOCUMENTATION = '''
---
module: azure_rm_webappslot
version_added: "2.8"
short_description: Manage Azure Web App slot.
description:
    - Create, update and delete Azure Web App slot.

options:
    resource_group:
        description:
            - Name of the resource group to which the resource belongs.
        required: True
    name:
        description:
            - Unique name of the deployment slot to create or update.
        required: True
    webapp_name:
        description:
            - Web app name which this deployment slot belongs to.
        required: True
    location:
        description:
            - Resource location. If not set, location from the resource group will be used as default.
    configuration_source:
        description:
            - Source slot to clone configurations from when creating slot. Use webapp's name to refer to the production slot.
    auto_swap_slot_name:
        description:
            - Used to configure target slot name to auto swap, or disable auto swap.
            - Set it target slot name to auto swap.
            - Set it to False to disable auto slot swap.
    swap:
        description:
            - Swap deployment slots of a web app.
        suboptions:
            action:
                description:
                    - Swap types.
                    - preview is to apply target slot settings on source slot first.
                    - swap is to complete swapping.
                    - reset is to reset the swap.
                choices:
                    - preview
                    - swap
                    - reset
                default: preview
            target_slot:
                description:
                    - Name of target slot to swap. If set to None, then swap with production slot.
            preserve_vnet:
                description:
                    - True to preserve virtual network to the slot during swap. Otherwise False.
                type: bool
                default: True
    frameworks:
        description:
            - Set of run time framework settings. Each setting is a dictionary.
            - See U(https://docs.microsoft.com/en-us/azure/app-service/app-service-web-overview) for more info.
        suboptions:
            name:
                description:
                    - Name of the framework.
                    - Supported framework list for Windows web app and Linux web app is different.
                    - For Windows web app, supported names(June 2018) java, net_framework, php, python, node. Multiple framework can be set at same time.
                    - For Linux web app, supported names(June 2018) java, ruby, php, dotnetcore, node. Only one framework can be set.
                    - Java framework is mutually exclusive with others.
                choices:
                    - java
                    - net_framework
                    - php
                    - python
                    - ruby
                    - dotnetcore
                    - node
            version:
                description:
                    - Version of the framework. For Linux web app supported value, see U(https://aka.ms/linux-stacks) for more info.
                    - net_framework supported value sample, 'v4.0' for .NET 4.6 and 'v3.0' for .NET 3.5.
                    - php supported value sample, 5.5, 5.6, 7.0.
                    - python supported value sampl
e, e.g., 5.5, 5.
6, 7.0.
                    - node supported value sample, 6.6, 6.9.
                    - dotnetcore supported value sample, 1.0, 1,1, 1.2.
                    - ruby supported value sample, 2.3.
                    - java supported value sample, 1.8, 1.9 for windows web app. 8 for linux web app.
            settings:
                description:
                    - List of settings of the framework.
                suboptions:
                    java_container:
                        description: Name of Java container. This is supported by specific framework C(java) only. e.g. Tomcat, Jetty.
                    java_container_version:
                        description:
                            - Version of Java container. This is supported by specific framework C(java) only.
                            - For Tomcat, e.g. 8.0, 8.5, 9.0. For Jetty, e.g. 9.1, 9.3.
    container_settings:
        description: Web app slot container settings.
        suboptions:
            name:
                description: Name of container. eg. "imagename:tag"
            registry_server_url:
                description: Container registry server url. eg. mydockerregistry.io
            registry_server_user:
                description: The container registry server user name.
            registry_server_password:
                description:
                    - The container registry server password.
    startup_file:
        description:
            - The slot startup file.
            - This only applies for linux web app slot.
    app_settings:
        description:
            - Configure web app slot application settings. Suboptions are in key value pair format.
    purge_app_settings:
        description:
            - Purge any existing application settings. Replace slot application settings with app_settings.
        type: bool
    deployment_source:
        description:
            - Deployment source for git
        suboptions:
            url:
                description:
                    - Repository url of deployment source.
            branch:
                description:
                    - The branch name of the repository.
    app_state:
        description:
            - Start/Stop/Restart the slot.
        type: str
        choices:
            - started
            - stopped
            - restarted
        default: started
    state:
      description:
        - Assert the state of the Web App deployment slot.
        - Use 'present' to create or update a  slot and 'absent' to delete it.
      default: present
      choices:
        - absent
        - present

extends_documentation_fragment:
    - azure
    - azure_tags

author:
    - "Yunge Zhu(@yungezz)"

'''

EXAMPLES = '''
  - name: Create a webapp slot
    azure_rm_webapp_slot:
      resource_group: myRG
      webapp_name: myJavaWebApp
      name: stage
      configuration_source: myJavaWebApp
      app_settings:
        testkey: testvalue

  - name: swap the slot with production slot
    azure_rm_webapp_slot:
      resource_group: myRG
      webapp_name: myJavaWebApp
      name: stage
      swap:
        action: swap

  - name: stop the slot
    azure_rm_webapp_slot:
      resource_group: myRG
      webapp_name: myJavaWebApp
      name: stage
      app_state: stopped

  - name: udpate a webapp slot app settings
    azure_rm_webapp_slot:
      resource_group: myRG
      webapp_name: myJavaWebApp
      name: stage
      app_settings:
        testkey: testvalue2

  - name: udpate a webapp slot frameworks
    azure_rm_webapp_slot:
      resource_group: myRG
      webapp_name: myJavaWebApp
      name: stage
      frameworks:
        - name: "node"
          version: "10.1"
'''

RETURN = '''
id:
    description: Id of current slot.
    returned: always
    type: str
    sample: /subscriptions/<subsid>/resourceGroups/myRG/providers/Microsoft.Web/sites/testapp/slots/stage1
'''

import time
from ansible.module_utils.azure_rm_common import AzureRMModuleBase

try:
    from msrestazure.azure_exceptions import CloudError
    from msrestazure.azure_operation import AzureOperationPoller
    from msrest.serialization import Model
    from azure.mgmt.web.models import (
        site_config, app_service_plan, Site,
        AppServicePlan, SkuDescription, NameValuePair
    )
except Imp