# 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 cliff import lister
from cliff import show

from vitrageclient.common import utils


# noinspection PyAbstractClass
class ResourceShow(show.ShowOne):
    """Show a resource"""

    def get_parser(self, prog_name):
        parser = super(ResourceShow, self).get_parser(prog_name)
        parser.add_argument('resource_id', help='ID of a resource')
        return parser

    def take_action(self, parsed_args):
        resource_id = parsed_args.resource_id
        resource = utils.get_client(self).resource.get(resource_id=resource_id)
        return self.dict2columns(resource)


class ResourceList(lister.Lister):
    """List resources"""

    def get_parser(self, prog_name):
        parser = super(ResourceList, self).get_parser(prog_name)
        parser.add_argument('--type',
                            dest='resource_type',
                            metavar='<resource type>',
                            help='Type of resource')
        parser.add_argument('--all-tenants',
                            default=False,
                            dest='all_tenants',
                            action='store_true',
                            help='Shows resources of all the tenants')

        return parser

    def take_action(self, parsed_args):
        resource_type = parsed_args.resource_type
        all_tenants = parsed_args.all_tenants
        resources = utils.get_client(self).resource.list(
            resource_type=resource_type,
            all_tenants=all_tenants)
        return utils.list2cols(('vitrage_id',
                                'type',
                                'name',
                                'id',
                                'state',
                                'project_id'), resources)

@@ -1182,24 +1182,198 @@
 sources%22%22%22%0A%0A
+    RESOURCE_PROPERTIES = %5C%0A        ('vitrage_id', 'type', 'id', 'state',%0A         'aggregated_state', 'metadata')%0A    METADATA = ('name', 'project_id', 'update_timestamp')%0A%0A
     def get_
@@ -2222,250 +2222,394 @@
     
-return utils.list2cols(('vitrage_id',%0A                                'type',%0A                                'name',%0A                                'id',%0A                                'state',%0A                                'project_id')
+# cluster, zone and host don't have %22project_id%22 property%0A        # neutron.port don't have %22name%22 property%0A        # cluster don't have %22update_timestamp%22%0A        for resource in resources:%0A            resource%5B'metadata'%5D = %5C%0A                %7Bitem: resource%5Bitem%5D for item in self.METADATA%0A                 if item in resource%7D%0A%0A        return utils.list2cols(self.RESOURCE_PROPERTIES
 , re

Add a 'metadata' column to show resource optional properties