# Copyright 2014: Kylin Cloud
# All Rights Reserved.
#
#    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.

from rally import consts
from rally.plugins.openstack import scenario
from rally.plugins.openstack.scenarios.quotas import utils
from rally.task import validation

"""Scenarios for quotas."""


@validation.add("required_services", services=[consts.Service.NOVA])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["nova.quotas"]},
                    name="Quotas.nova_update",
                    platform="openstack")
class NovaUpdate(utils.QuotasScenario):

    def run(self, max_quota=1024):
        """Update quotas for Nova.

        :param max_quota: Max value to be updated for quota.
        """

        self._update_quotas("nova", self.context["tenant"]["id"],
                            max_quota)


@validation.add("required_services", services=[consts.Service.NOVA])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["nova.quotas"]},
                    name="Quotas.nova_update_and_delete", platform="openstack")
class NovaUpdateAndDelete(utils.QuotasScenario):

    def run(self, max_quota=1024):
        """Update and delete quotas for Nova.

        :param max_quota: Max value to be updated for quota.
        """

        self._update_quotas("nova", self.context["tenant"]["id"],
                            max_quota)
        self._delete_quotas("nova", self.context["tenant"]["id"])


@validation.add("required_services", services=[consts.Service.CINDER])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["cinder.quotas"]},
                    name="Quotas.cinder_update", platform="openstack")
class CinderUpdate(utils.QuotasScenario):

    def run(self, max_quota=1024):
        """Update quotas for Cinder.

        :param max_quota: Max value to be updated for quota.
        """

        self._update_quotas("cinder", self.context["tenant"]["id"],
                            max_quota)


@validation.add("required_services", services=[consts.Service.CINDER])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["cinder.quotas"]},
                    name="Quotas.cinder_get", platform="openstack")
class CinderGet(utils.QuotasScenario):

    def run(self):
        """Get quotas for Cinder.

        Measure the "cinder quota-show" command performance

        """
        self._get_quotas("cinder", self.context["tenant"]["id"])


@validation.add("required_services", services=[consts.Service.CINDER])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["cinder.quotas"]},
                    name="Quotas.cinder_update_and_delete",
                    platform="openstack")
class CinderUpdateAndDelete(utils.QuotasScenario):

    def run(self, max_quota=1024):
        """Update and Delete quotas for Cinder.

        :param max_quota: Max value to be updated for quota.
        """

        self._update_quotas("cinder", self.context["tenant"]["id"],
                            max_quota)
        self._delete_quotas("cinder", self.context["tenant"]["id"])


@validation.add("required_services",
                services=[consts.Service.NEUTRON])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["neutron.quota"]},
                    name="Quotas.neutron_update", platform="openstack")
class NeutronUpdate(utils.QuotasScenario):

    def run(self, max_quota=1024):
        """Update quotas for neutron.

        :param max_quota: Max value to be updated for quota.
        """

        quota_update_fn = self.admin_clients("neutron").update_quota
        self._update_quotas("neutron", self.context["tenant"]["id"],
                            max_quota, quota_update_fn)


@validation.add("required_services", services=[consts.Service.NOVA])
@validation.add("required_platform", platform="openstack",
                admin=True, users=True)
@scenario.configure(context={"admin_cleanup@openstack": ["nova.quotas"]},
                    name="Quotas.nova_get", platform="openstack")
class NovaGet(utils.QuotasScenario):

    def run(self):
        """Get quotas for nova."""

        self._get_quotas("nova", self.context["tenant"]["id"])
