# Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.
# All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#

import boto
import boto.jsonresponse
from boto.compat import json
from boto.regioninfo import RegionInfo
from boto.connection import AWSQueryConnection


class Layer1(AWSQueryConnection):

    APIVersion = '2010-12-01'
    DefaultRegionName = 'us-east-1'
    DefaultRegionEndpoint = 'elasticbeanstalk.us-east-1.amazonaws.com'

    def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
                 is_secure=True, port=None,
                 proxy=None, proxy_port=None,
                 proxy_user=None, proxy_pass=None, debug=0,
                 https_connection_factory=None, region=None, path='/',
                 api_version=None, security_token=None):
        if not region:
            region = RegionInfo(self, self.DefaultRegionName,
                                self.DefaultRegionEndpoint)
        self.region = region
        AWSQueryConnection.__init__(self, aws_access_key_id,
                                    aws_secret_access_key,
                                    is_secure, port, proxy, proxy_port,
                                    proxy_user, proxy_pass,
                                    self.region.endpoint, debug,
                                    https_connection_factory, path,
                                    security_token)

    def _required_auth_capability(self):
        return ['hmac-v4']

    def _encode_bool(self, v):
        v = bool(v)
        return {True: "true", False: "false"}[v]

    def _get_response(self, action, params, path='/', verb='GET'):
        params['ContentType'] = 'JSON'
        response = self.make_request(action, params, path, verb)
        body = response.read()
        boto.log.debug(body)
        if response.status == 200:
            return json.loads(body)
        else:
            raise self.ResponseError(response.status, response.reason, body)

    def check_dns_availability(self, cname_prefix):
        """Checks if the specified CNAME is available.

        :type cname_prefix: string
        :param cname_prefix: The prefix used when this CNAME is
            reserved.
        """
        params = {'CNAMEPrefix': cname_prefix}
        return self._get_response('CheckDNSAvailability', params)

    def create_application(self, application_name, description=None):
        """
        Creates an application that has one configuration template
        named default and no application versions.

        :type application_name: string
        :param application_name: The name of the application.
            Constraint: This name must be unique within your account. If the
            specified name already exists, the action returns an
            InvalidParameterValue error.

        :type description: string
        :param description: Describes the application.

        :raises: TooManyApplicationsException
        """
        params = {'ApplicationName': application_name}
        if description:
            params['Description'] = description
        return self._get_response('CreateApplication', params)

    def create_application_version(self, application_name, version_label,
                                   description=None, s3_bucket=None,
                                   s3_key=None, auto_create_application=None):
        """Creates an application version for the specified application.

        :type application_name: string
        :param application_name: The name of the application. If no
            application is found with this name, and AutoCreateApplication is
            false, returns an InvalidParameterValue error.

        :type version_label: string
        :param version_label: A label identifying this version. Constraint:
            Must be unique per application. If an application version already
            exists with this label for the specified application, AWS Elastic
            Beanstalk returns an InvalidParameterValue error.

        :type description: string
        :param description: Describes this version.

        :type s3_bucket: string
        :param s3_bucket: The Amazon S3 bucket where the data is located.

        :type s3_key: string
        :param s3_key: The Amazon S3 key where the data is located.  Both
            s3_bucket and s3_key must be specified in order to use a specific
            source bundle.  If both of these values are not specified the
            sample application will be used.

        :type auto_create_application: boolean
        :param auto_create_application: Determines how the system behaves if
            the specified application for this version does not already exist:
            true: Automatically creates the specified application for this
            version if it does not already exist.  false: Returns an
            InvalidParameterValue if the specified application for this version
            does not already exist.  Default: false  Valid Values: true | false

        :raises: TooManyApplicationsException,
                 TooManyApplicationVersionsException,
                 InsufficientPrivilegesException,
                 S3LocationNotInServiceRegionException

        """
        params = {'ApplicationName': application_name,
                  'VersionLabel': version_label}
        if description:
            params['Description'] = description
        if s3_bucket and s3_key:
            params['SourceBundle.S3Bucket'] = s3_bucket
            params['SourceBundle.S3Key'] = s3_key
        if auto_create_application:
            params['AutoCreateApplication'] = self._encode_bool(
                auto_create_application)
        return self._get_response('CreateApplicationVersion', params)

    def create_configuration_template(self, application_name, template_name,
                                      solution_stack_name=None,
                                      source_configuration_application_name=None,
                                      source_configuration_template_name=None,
                                      environment_id=None, description=None,
                                      option_settings=None):
        """Creates a configuration template.

        Templates are associated with a specific application and are used to
        deploy different versions of the application with the same
        configuration settings.

        :type application_name: string
        :param application_name: The name of the application to associate with
            this configuration template. If no application is found with this
            name, AWS Elastic Beanstalk returns an InvalidParameterValue error.

        :type template_name: string
        :param template_name: The name of the configuration template.
            Constraint: This name must be unique per application.  Default: If
            a configuration template already exists with this name, AWS Elastic
            Beanstalk returns an InvalidParameterValue error.

        :type solution_stack_name: string
        :param solution_stack_name: The name of the solution stack used by this
            configuration. The solution stack specifies the operating system,
            architecture, and application server for a configuration template.
            It determines the set of configuration options as well as the
            possible and default values.  Use ListAvailableSolutionStacks to
            obtain a list of available solution stacks.  Default: If the
            SolutionStackName is not specified and the source configuration
            parameter is blank, AWS Elastic Beanstalk uses the default solution
            stack. If not specified and the source configuration parameter is
            specified, AWS Elastic Beanstalk uses the same solution stack as
            the source configuration template.

        :type source_configuration_application_name: string
        :param source_configuration_application_name: The name of the
            application associated with the configuration.

        :type source_configuration_template_name: string
        :param source_configuration_template_name: The name of the
            configuration template.

        :type environment_id: string
        :param environment_id: The ID of the environment used with this
            configuration template.

        :type description: string
        :param description: Describes this configuration.

        :type option_settings: list
        :param option_settings: If specified, AWS Elastic Beanstalk sets the
            specified configuration option to the requested value. The new
            value overrides the value obtained from the solution stack or the
            source configuration template.

        :raises: InsufficientPrivilegesException,
                 TooManyConfigurationTemplatesException
        """
        params = {'ApplicationName': application_name,
                  'TemplateName': template_name}
        if solution_stack_name:
            params['SolutionStackName'] = solution_stack_name
        if source_configuration_application_name:
            params['SourceConfiguration.ApplicationName'] = source_configuration_application_name
        if source_configuration_template_name:
            params['SourceConfiguration.TemplateName'] = source_configuration_template_name
        if environment_id:
            params['EnvironmentId'] = environment_id
        if description:
            params['Description'] = description
        if option_settings:
            self._build_list_params(params, option_settings,
                                   'OptionSettings.member',
                                   ('Namespace', 'OptionName', 'Value'))
        return self._get_response('CreateConfigurationTemplate', params)

    def create_environment(self, application_name, environment_name,
                           version_label=None, template_name=None,
                           solution_stack_name=None, cname_prefix=None,
                           description=None, option_settings=None,
                           options_to_remove=None):
        """Launches an environment for the application using a configuration.

        :type application_name: string
        :param application_name: The name of the application that contains the
            version to be deployed.  If no application is found with this name,
            CreateEnvironment returns an InvalidParameterValue error.

        :type environment_name: string
        :param environment_name: A unique name for the deployment environment.
            Used in the application URL. Constraint: Must be from 4 to 23
            characters in length. The name can contain only letters, numbers,
            and hyphens. It cannot start or end with a hyphen. This name must
            be unique in your account. If the specified name already exists,
            AWS Elastic Beanstalk returns an InvalidParameterValue error.
            Default: If the CNAME parameter is not specified, the environment
            name becomes part of the CNAME, and therefore part of the visible
            URL for your application.

        :type version_label: string
        :param version_label: The name of the application version to deploy. If
            the specified application has no associated application versions,
            AWS Elastic Beanstalk UpdateEnvironment returns an
            InvalidParameterValue error.  Default: If not specified, AWS
            Elastic Beanstalk attempts to launch the most recently created
            application version.

        :type template_name: string
        :param template_name: The name of the configuration template to
            use in deployment. If no configuration template is found with this
            name, AWS Elastic Beanstalk returns an InvalidParameterValue error.
            Condition: You must specify either this parameter or a
            SolutionStackName, but not both. If you specify both, AWS Elastic
            Beanstalk returns an InvalidParameterCombination error. If you do
            not specify either, AWS Elastic Beanstalk returns a
            MissingRequiredParameter error.

        :type solution_stack_name: string
        :param solution_stack_name: This is an alternative to specifying a
            configuration name. If specified, AWS Elastic Beanstalk sets the
            configuration values to the default values associated with the
            specified solution stack.  Condition: You must specify either this
            or a TemplateName, but not both. If you specify both, AWS Elastic
            Beanstalk returns an InvalidParameterCombination error. If you do
            not specify either, AWS Elastic Beanstalk returns a
            MissingRequiredParameter error.

        :type cname_prefix: string
        :param cname_prefix: If specified, the environment attempts to use this
            value as the prefix for the CNAME. If not specified, the
            environment uses the environment name.

        :type description: string
        :param description: Describes this environment.

        :type option_settings: list
        :param option_settings: If specified, AWS Elastic Beanstalk sets the
            specified configuration options to the requested value in the
            configuration set for the new environment. These override the
            values obtained from the solution stack or the configuration
            template.  Each element in the list is a tuple of (Namespace,
            OptionName, Value), for example::

                [('aws:autoscaling:launchconfiguration',
                    'Ec2KeyName', 'mykeypair')]

        :type options_to_remove: list
        :param options_to_remove: A list of custom user-defined configuration
            options to remove from the configuration set for this new
            environment.

        :raises: TooManyEnvironmentsException, InsufficientPrivilegesException

        """
        params = {'ApplicationName': application_name,
                  'EnvironmentName': environment_name}
        if version_label:
            params['VersionLabel'] = version_label
        if template_name:
            params['TemplateName'] = template_name
        if solution_stack_name:
            params['SolutionStackName'] = solution_stack_name
        if cname_prefix:
            params['CNAMEPrefix'] = cname_prefix
        if description:
            params['Description'] = description
        if option_settings:
            self._build_list_params(params, option_settings,
                                   'OptionSettings.member',
                                   ('Namespace', 'OptionName', 'Value'))
        if options_to_remove:
            self.build_list_params(params, options_to_remove,
                                   'OptionsToRemove.member')
        return self._get_response('CreateEnvironment', params)

    def create_storage_location(self):
        """
        Creates the Amazon S3 storage location for the account.  This
        location is used to store user log files.

        :raises: TooManyBucketsException,
                 S3SubscriptionRequiredException,
                 InsufficientPrivilegesException

        """
        return self._get_response('CreateStorageLocation', params={})

    def delete_application(self, application_name,
                           terminate_env_by_force=None):
        """
        Deletes the specified application along with all associated
        versions and configurations. The application versions will not
        be deleted from your Amazon S3 bucket.

        :type application_name: string
        :param application_name: The name of the application to delete.

        :type terminate_env_by_force: boolean
        :param terminate_env_by_force: When set to true, running
            environments will be terminated before deleting the application.

        :raises: OperationInProgressException

        """
        params = {'ApplicationName': application_name}
        if terminate_env_by_force:
            params['TerminateEnvByForce'] = self._encode_bool(
                terminate_env_by_force)
        return self._get_response('DeleteApplication', params)

    def delete_application_version(self, application_name, version_label,
                                   delete_source_bundle=None):
        """Deletes the specified version from the specified application.

        :type application_name: string
        :param application_name: The name of the application to delete
            releases from.

        :type version_label: string
        :param version_label: The label of the version to delete.

        :type delete_source_bundle: boolean
        :param delete_source_bundle: Indicates whether to delete the
            associated source bundle from Amazon S3.  Valid Values: true |
            false

        :raises: SourceBundleDeletionException,
                 InsufficientPrivilegesException,
                 OperationInProgressException,
                 S3LocationNotInServiceRegionException
        """
        params = {'ApplicationName': application_name,
                  'VersionLabel': version_label}
        if delete_source_bundle:
            params['DeleteSourceBundle'] = self._encode_bool(
                delete_source_bundle)
        return self._get_response('DeleteApplicationVersion', params)

    def delete_configuration_template(self, application_name, template_name):
        """Deletes the specified configuration template.

        :type application_name: string
        :param application_name: The name of the application to delete
            the configuration template from.

        :type template_name: string
        :param template_name: The name of the configuration template to
            delete.

        :raises: OperationInProgressException

        """
        params = {'ApplicationName': application_name,
                  'TemplateName': template_name}
        return self._get_response('DeleteConfigurationTemplate', params)

    def delete_environment_configuration(self, application_name,
                                         environment_name):
        """
        Deletes the draft configuration associated with the running
        environment.  Updating a running environment with any
        configuration changes creates a draft configuration set. You can
        get the draft configuration using DescribeConfigurationSettings
        while the update is in progress or if the update fails. The
        DeploymentStatus for the draft configuration indicates whether
        the deployment is in process or has failed. The draft
        configuration remains in existence until it is deleted with this
        action.

        :type application_name: string
        :param application_name: The name of the application the
            environment is associated with.

        :type environment_name: string
        :param environment_name: The name of the environment to delete
            the draft configuration from.

        """
        params = {'ApplicationName': application_name,
                  'EnvironmentName': environment_name}
        return self._get_response('DeleteEnvironmentConfiguration', params)

    def describe_application_versions(self, application_name=None,
                                      version_labels=None):
        """Returns descriptions for existing application versions.

        :type application_name: string
        :param application_name: If specified, AWS Elastic Beanstalk restricts
            the returned descriptions to only include ones that are associated
            with the specified application.

        :type version_labels: list
        :param version_labels: If specified, restricts the returned
            descriptions to only include ones that have the specified version
            labels.

        """
        params = {}
        if application_name:
            params['ApplicationName'] = application_name
        if version_labels:
            self.build_list_params(params, version_labels,
                                   'VersionLabels.member')
        return self._get_response('DescribeApplicationVersions', params)

    def describe_applications(self, application_names=None):
        """Returns the descriptions of existing applications.

        :type application_names: list
        :param application_names: If specified, AWS Elastic Beanstalk restricts
            the returned descriptions to only include those with the specified
            names.

        """
        params = {}
        if application_names:
            self.build_list_params(params, application_names,
                                   'ApplicationNames.member')
        return self._get_response('DescribeApplications', params)

    def describe_configuration_options(self, application_name=None,
                                       template_name=None,
                                       environment_name=None,
                                       solution_stack_name=None, options=None):
        """Describes configuration options used in a template or environment.

        Describes the configuration options that are used in a
        particular configuration template or environment, or that a
        specified solution stack defines. The description includes the
        values the options, their default values, and an indication of
        the required action on a running environment if an option value
        is changed.

        :type application_name: string
        :param application_name: The name of the application associated with
            the configuration template or environment. Only needed if you want
            to describe the configuration options associated with either the
            configuration template or environment.

        :type template_name: string
        :param template_name: The name of the configuration template whose
            configuration options you want to describe.

        :type environment_name: string
        :param environment_name: The name of the environment whose
            configuration options you want to describe.

        :type solution_stack_name: string
        :param solution_stack_name: The name of the solution stack whose
            configuration options you want to describe.

        :type options: list
        :param options: If specified, restricts the descriptions to only
            the specified options.
        """
        params = {}
        if application_name:
            params['ApplicationName'] = application_name
        if template_name:
            params['TemplateName'] = template_name
        if environment_name:
            params['EnvironmentName'] = environment_name
        if solution_stack_name:
            params['SolutionStackName'] = solution_stack_name
        if options:
            self.build_list_params(params, options, 'Options.member')
        return self._get_response('DescribeConfigurationOptions', params)

    def describe_configuration_settings(self, application_name,
                                        template_name=None,
                                        environment_name=None):
        """
        Returns a description of the settings for the specified
        configuration set, that is, either a configuration template or
        the configuration set associated with a running environment.
        When describing the settings for the configuration set
        associated with a running environment, it is possible to receive
        two sets of setting descriptions. One is the deployed
        configuration set, and the other is a draft configuration of an
        environment that is either in the process of deployment or that
        failed to deploy.

        :type application_name: string
        :param application_name: The application for the environment or
            configuration template.

        :type template_name: string
        :param template_name: The name of the configuration template to
            describe.  Conditional: You must specify either this parameter or
            an EnvironmentName, but not both. If you specify both, AWS Elastic
            Beanstalk returns an InvalidParameterCombination error.  If you do
            not specify either, AWS Elastic Beanstalk returns a
            MissingRequiredParameter error.

        :type environment_name: string
        :param environment_name: The name of the environment to describe.
            Condition: You must specify either this or a TemplateName, but not
            both. If you specify both, AWS Elastic Beanstalk returns an
            InvalidParameterCombination error. If you do not specify either,
            AWS Elastic Beanstalk returns MissingRequiredParameter error.
        """
        params = {'ApplicationName': application_name}
        if template_name:
            params['TemplateName'] = template_name
        if environment_name:
            params['EnvironmentName'] = environment_name
        return self._get_response('DescribeConfigurationSettings', params)

    def describe_environment_resources(self, environment_id=None,
                                       environment_name=None):
        """Returns AWS resources for this environment.

        :type environment_id: string
        :param environment_id: The ID of the environment to retrieve AWS
            resource usage data.  Condition: You must specify either this or an
            EnvironmentName, or both. If you do not specify either, AWS Elastic
            Beanstalk returns MissingRequiredParameter error.

        :type environment_name: string
        :param environment_name: The name of the environment to retrieve
            AWS resource usage data.  Condition: You must specify either this
            or an EnvironmentId, or both. If you do not specify either, AWS
            Elastic Beanstalk returns MissingRequiredParameter error.

        :raises: InsufficientPrivilegesException
        """
        params = {}