# Copyright 2013 - Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import pecan
from pecan import rest
import wsme
import wsmeext.pecan as wsme_pecan

from solum.api.controllers.v1.datamodel import assembly
import solum.api.controllers.v1.userlog as userlog_controller
from solum.api.handlers import assembly_handler
from solum.common import exception
from solum.common import request
from solum import objects
from solum.openstack.common.gettextutils import _


class AssemblyController(rest.RestController):
    """Manages operations on a single assembly."""

    def __init__(self, assembly_id):
        super(AssemblyController, self).__init__()
        self._id = assembly_id

    @pecan.expose()
    def _lookup(self, primary_key, *remainder):
        if remainder and not remainder[-1]:
            remainder = remainder[:-1]
        if primary_key == 'logs':
            logs = userlog_controller.UserlogsController(self._id)
            return logs, remainder

    @exception.wrap_wsme_pecan_controller_exception
    @wsme_pecan.wsexpose(assembly.Assembly)
    def get(self):
        """Return this assembly."""
        request.check_request_for_https()
        handler = assembly_handler.AssemblyHandler(
            pecan.request.security_context)
        return assembly.Assembly.from_db_model(handler.get(self._id),
                                               pecan.request.host_url)

    @exception.wrap_wsme_pecan_controller_exception
    @wsme_pecan.wsexpose(assembly.Assembly, body=assembly.Assembly)
    def put(self, data):
        """Modify this assembly."""
        handler = assembly_handler.AssemblyHandler(
            pecan.request.security_context)
        res = handler.update(self._id,
                             data.as_dict(objects.registry.Assembly))
        return assembly.Assembly.from_db_model(res, pecan.request.host_url)

    @exception.wrap_wsme_pecan_controller_exception
    @wsme_pecan.wsexpose(status_code=204)
    def delete(self):
        """Delete this assembly."""
        handler = assembly_handler.AssemblyHandler(
            pecan.request.security_context)
        return handler.delete(self._id)


class AssembliesController(rest.RestController):
    """Manages operations on the assemblies collection."""

    @pecan.expose()
    def _lookup(self, assembly_id, *remainder):
        if remainder and not remainder[-1]:
            remainder = remainder[:-1]
        return AssemblyController(assembly_id), remainder

    @exception.wrap_wsme_pecan_controller_exception
    @wsme_pecan.wsexpose(assembly.Assembly, body=assembly.Assembly,
                         status_code=201)
    def post(self, data):
        """Create a new assembly."""
        js_data = data.as_dict(objects.registry.Assembly)
        if data.plan_uri is not wsme.Unset:
            plan_uri = data.plan_uri
            if plan_uri.startswith(pecan.request.host_url):
                pl_uuid = plan_uri.split('/')[-1]
                pl = objects.registry.Plan.get_by_uuid(
                    pecan.request.security_context, pl_uuid)
                js_data['plan_id'] = pl.id
            else:
                # TODO(asalkeld) we are not hosting the plan so
                # download the plan and insert it into our db.
                raise exception.BadRequest(reason=_(
                    'The plan was not hosted in solum'))

        if js_data.get('plan_id') is None:
            raise exception.BadRequest(reason=_(
                'The plan was not given or could not be found'))

        handler = assembly_handler.AssemblyHandler(
            pecan.request.security_context)
        return assembly.Assembly.from_db_model(
            handler.create(js_data), pecan.request.host_url)

    @exception.wrap_wsme_pecan_controller_exception
    @wsme_pecan.wsexpose([assembly.Assembly])
    def get_all(self):
        """Return all assemblies, based on the query provided."""
        request.check_request_for_https()
        handler = assembly_handler.AssemblyHandler(
            pecan.request.security_context)
        return [assembly.Assembly.from_db_model(assm, pecan.request.host_url)
                for assm in handler.get_all()]

[((36, 5, 36, 19), 'pecan.expose', 'pecan.expose', ({}, {}), '()', False, 'import pecan\n'), ((45, 5, 45, 43), 'wsmeext.pecan.wsexpose', 'wsme_pecan.wsexpose', ({(45, 25, 45, 42): 'assembly.Assembly'}, {}), '(assembly.Assembly)', True, 'import wsmeext.pecan as wsme_pecan\n'), ((55, 5, 55, 67), 'wsmeext.pecan.wsexpose', 'wsme_pecan.wsexpose', (), '', True, 'import wsmeext.pecan as wsme_pecan\n'), ((65, 5, 65, 41), 'wsmeext.pecan.wsexpose', 'wsme_pecan.wsexpose', (), '', True, 'import wsmeext.pecan as wsme_pecan\n'), ((76, 5, 76, 19), 'pecan.expose', 'pecan.expose', ({}, {}), '()', False, 'import pecan\n'), ((83, 5, 84, 41), 'wsmeext.pecan.wsexpose', 'wsme_pecan.wsexpose', (), '', True, 'import wsmeext.pecan as wsme_pecan\n'), ((111, 5, 111, 45), 'wsmeext.pecan.wsexpose', 'wsme_pecan.wsexpose', ({(111, 25, 111, 44): '[assembly.Assembly]'}, {}), '([assembly.Assembly])', True, 'import wsmeext.pecan as wsme_pecan\n'), ((48, 8, 48, 41), 'solum.common.request.check_request_for_https', 'request.check_request_for_https', ({}, {}), '()', False, 'from solum.common import request\n'), ((49, 18, 50, 43), 'solum.api.handlers.assembly_handler.AssemblyHandler', 'assembly_handler.AssemblyHandler', ({(50, 12, 50, 42): 'pecan.request.security_context'}, {}), '(pecan.request.security_context)', False, 'from solum.api.handlers import assembly_handler\n'), ((58, 18, 59, 43), 'solum.api.handlers.assembly_handler.AssemblyHandler', 'assembly_handler.AssemblyHandler', ({(59, 12, 59, 42): 'pecan.request.security_context'}, {}), '(pecan.request.security_context)', False, 'from solum.api.handlers import assembly_handler\n'), ((62, 15, 62, 75), 'solum.api.controllers.v1.datamodel.assembly.Assembly.from_db_model', 'assembly.Assembly.from_db_model', ({(62, 47, 62, 50): 'res', (62, 52, 62, 74): 'pecan.request.host_url'}, {}), '(res, pecan.request.host_url)', False, 'from solum.api.controllers.v1.datamodel import assembly\n'), ((68, 18, 69, 43), 'solum.api.handlers.assembly_handler.AssemblyHandler', 'assembly_handler.AssemblyHandler', ({(69, 12, 69, 42): 'pecan.request.security_context'}, {}), '(pecan.request.security_context)', False, 'from solum.api.handlers import assembly_handler\n'), ((105, 18, 106, 43), 'solum.api.handlers.assembly_handler.AssemblyHandler', 'assembly_handler.AssemblyHandler', ({(106, 12, 106, 42): 'pecan.request.security_context'}, {}), '(pecan.request.security_context)', False, 'from solum.api.handlers import assembly_handler\n'), ((114, 8, 114, 41), 'solum.common.request.check_request_for_https', 'request.check_request_for_https', ({}, {}), '()', False, 'from solum.common import request\n'), ((115, 18, 116, 43), 'solum.api.handlers.assembly_handler.AssemblyHandler', 'assembly_handler.AssemblyHandler', ({(116, 12, 116, 42): 'pecan.request.security_context'}, {}), '(pecan.request.security_context)', False, 'from solum.api.handlers import assembly_handler\n'), ((41, 19, 41, 66), 'solum.api.controllers.v1.userlog.UserlogsController', 'userlog_controller.UserlogsController', ({(41, 57, 41, 65): 'self._id'}, {}), '(self._id)', True, 'import solum.api.controllers.v1.userlog as userlog_controller\n'), ((117, 16, 117, 77), 'solum.api.controllers.v1.datamodel.assembly.Assembly.from_db_model', 'assembly.Assembly.from_db_model', ({(117, 48, 117, 52): 'assm', (117, 54, 117, 76): 'pecan.request.host_url'}, {}), '(assm, pecan.request.host_url)', False, 'from solum.api.controllers.v1.datamodel import assembly\n'), ((92, 21, 93, 60), 'solum.objects.registry.Plan.get_by_uuid', 'objects.registry.Plan.get_by_uuid', ({(93, 20, 93, 50): 'pecan.request.security_context', (93, 52, 93, 59): 'pl_uuid'}, {}), '(pecan.request.security_context, pl_uuid)', False, 'from solum import objects\n'), ((102, 46, 103, 63), 'solum.openstack.common.gettextutils._', '_', ({(103, 16, 103, 62): '"""The plan was not given or could not be found"""'}, {}), "('The plan was not given or could not be found')", False, 'from solum.openstack.common.gettextutils import _\n'), ((98, 50, 99, 55), 'solum.openstack.common.gettextutils._', '_', ({(99, 20, 99, 54): '"""The plan was not hosted in solum"""'}, {}), "('The plan was not hosted in solum')", False, 'from solum.openstack.common.gettextutils import _\n')]