aboutsummaryrefslogtreecommitdiffstats
path: root/python_moonclient/python_moonclient/cli/models.py
blob: 369d9027a9d2e00e21271123dc87529293b24aa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import logging
from importlib.machinery import SourceFileLoader
from cliff.lister import Lister
from cliff.command import Command
from python_moonclient.core import models, policies, pdp
from python_moonclient.cli.parser import Parser
from python_moonclient.cli.projects import ProjectsUtils

LOGGER = logging.getLogger("moonclient.cli.pdps")


class ModelUtils:
    def __init__(self):
        pass

    @staticmethod
    def get_model_id(model, parsed_id, parsed_name):
        modelz = models.check_model()
        for _model_key, _model_value in modelz["models"].items():
            if _model_key == parsed_id or _model_value['name'] == parsed_name:
                # LOGGER.info(
                # "Found pdp : [key='{}' , name='{}']".format(_pdp_key, _pdp_value['name']))
                return _model_key
        return None

    @staticmethod
    def get_model_name(pdp, parsed_id, parsed_name):
        modelz = models.check_model()
        for _model_key, _model_value in modelz["models"].items():
            if _model_key == parsed_id or _model_value['name'] == parsed_name:
                # LOGGER.info(
                # "Found pdp : [key='{}' , name='{}']".format(_pdp_key, _pdp_value['name']))
                return _model_value['name']
        return None


class Models(Lister):
    """show the list of existing pdps """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        Parser.add_common_options(parser)
        return parser

    def take_action(self, parsed_args):
        consul_host = parsed_args.consul_host
        consul_port = parsed_args.consul_port

        models.init(consul_host, consul_port)
        policies.init(consul_host, consul_port)
        pdp.init(consul_host, consul_port)

        modelz = models.check_model()

        return (('Key', 'Name'),
                ((_model_key, _model_value['name']) for _model_key, _model_value in
                 modelz["models"].items())
                )


class SubjectCategories(Lister):
    """show the list of existing categories """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        Parser.add_common_options(parser)

        return parser

    def take_action(self, parsed_args):
        consul_host = parsed_args.consul_host
        consul_port = parsed_args.consul_port

        models.init(consul_host, consul_port)
        policies.init(consul_host, consul_port)
        pdp.init(consul_host, consul_port)

        subject_categories = models.check_subject_category()
        print(subject_categories)
        return (('Key', 'Name'),
                ((_model_key, _model_value['name']) for _model_key, _model_value in
                 subject_categories["subject_categories"].items())
                )


class ObjectCategories(Lister):
    """show the list of existing categories """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        Parser.add_common_options(parser)

        return parser

    def take_action(self, parsed_args):
        consul_host = parsed_args.consul_host
        consul_port = parsed_args.consul_port

        models.init(consul_host, consul_port)
        policies.init(consul_host, consul_port)
        pdp.init(consul_host, consul_port)

        object_categories = models.check_object_category()
        print(object_categories)
        return (('Key', 'Name'),
                ((_model_key, _model_value['name']) for _model_key, _model_value in
                 object_categories["object_categories"].items())
                )


class ActionCategories(Lister):
    """show the list of existing categories """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        Parser.add_common_options(parser)

        return parser

    def take_action(self, parsed_args):
        consul_host = parsed_args.consul_host
        consul_port = parsed_args.consul_port

        models.init(consul_host, consul_port)
        policies.init(consul_host, consul_port)
        pdp.init(consul_host, consul_port)

        action_categories = models.check_action_category()
        print(action_categories)
        return (('Key', 'Name'),
                ((_model_key, _model_value['name']) for _model_key, _model_value in
                 action_categories["action_categories"].items())
                )


class SubjectCategoryAdd(Command):
    """show the list of existing categories """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        Parser.add_common_options(parser)
        Parser.add_name_argument(parser)

        return parser

    def take_action(self, parsed_args):
        consul_host = parsed_args.consul_host
        consul_port = parsed_args.consul_port

        models.init(consul_host, consul_port)
        policies.init(consul_host, consul_port)
        pdp.init(consul_host, consul_port)

        subject_category_id = models.add_subject_category(parsed_args.name)
        if subject_category_id is not None:
            print("Subject category created with id {}".format(subject_category_id))
        else:
            print("Error while creating subject category")
        # subject_categories = models.check_subject_category(subject_category_id)