aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/moon_manager/api/data.py
blob: 570bb9cd75c8d5b556da75609e3a383332ba0fc2 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.


"""
Data are elements used to create rules

* Subjects are the source of an action on an object
  (examples : users, virtual machines)
* Objects are the destination of an action
  (examples virtual machines, virtual Routers)
* Actions are what subject wants to do on an object
"""

import hug
import logging
import requests
from moon_manager import db_driver as driver
from moon_utilities.security_functions import validate_input
from moon_utilities.auth_functions import api_key_authentication, connect_from_env
from moon_utilities.invalided_functions import invalidate_data_in_slaves
from moon_manager.api import slave as slave_class
from moon_manager.api import configuration
from moon_manager.api import policy
from moon_manager.api import meta_data

LOGGER = logging.getLogger("moon.manager.api." + __name__)


class SubjectData(object):
    """
    Endpoint for subject data requests
    """

    @staticmethod
    @hug.local()
    @hug.get("/policies/{uuid}/subject_data", requires=api_key_authentication)
    @hug.get("/policies/{uuid}/subject_data/{category_id}", requires=api_key_authentication)
    @hug.get("/policies/{uuid}/subject_data/{category_id}/{data_id}",
             requires=api_key_authentication)
    def get(uuid: hug.types.text, category_id: hug.types.text = None,
            data_id: hug.types.text = None, authed_user: hug.directives.user = None):
        """Retrieve all subject categories or a specific one if data_id is given
        for a given policy

        :param uuid: uuid of the policy
        :param category_id: uuid of the subject category
        :param data_id: uuid of the subject data
        :param authed_user: user ID who do the request
        :return: [{
            "policy_id": "policy_id1",
            "category_id": "category_id1",
            "data": {
                "subject_data_id": {
                    "name": "name of the data",
                    "description": "description of the data (optional)"
                }
            }
        }]
        :internal_api: get_subject_data
        """
        data = driver.PolicyManager.get_subject_data(moon_user_id=authed_user, policy_id=uuid,
                                                     category_id=category_id, data_id=data_id)

        return {"subject_data": data}

    @staticmethod
    @hug.local()
    @hug.post("/policies/{uuid}/subject_data/{category_id}", requires=api_key_authentication)
    def post(body: validate_input("name"), uuid: hug.types.text, category_id: hug.types.text,
             authed_user: hug.directives.user = None):
        """Create or update a subject.

        :param body: body of the request
        :param uuid: uuid of the policy
        :param category_id: uuid of the subject category
        :param authed_user: user ID who do the request
        :request body: {
            "name": "name of the data (mandatory)",
            "description": "description of the data (optional)"
        }
        :return: {
            "policy_id": "policy_id1",
            "category_id": "category_id1",
            "data": {
                "subject_data_id": {
                    "name": "name of the data (mandatory)",
                    "description": "description of the data (optional)"
                }
            }
        }
        :internal_api: add_subject_data
        """
        data = driver.PolicyManager.set_subject_data(moon_user_id=authed_user, policy_id=uuid,
                                                     category_id=category_id, value=body)

        slaves = slave_class.Slaves.get().get("slaves")
        invalidate_data_in_slaves(slaves=slaves, policy_id=uuid, category_id=category_id,
                                  data_id=None, type="subject")

        return {"subject_data": data}

    @staticmethod
    @hug.local()
    @hug.delete("/policies/{uuid}/subject_data/{category_id}/{data_id}",
                requires=api_key_authentication)
    def delete(uuid: hug.types.text, data_id: hug.types.text,
               category_id: hug.types.text = None, authed_user: hug.directives.user = None):
        """Delete a subject for a given policy

        :param uuid: uuid of the policy
        :param category_id: uuid of the subject category
        :param data_id: uuid of the subject data
        :param authed_user: user ID who do the request
        :return: [{
            "result": "True or False",
            "message": "optional message (optional)"
        }]
        :internal_api: delete_subject_data
        """
        LOGGER.info("api.delete {} {}".format(uuid, data_id))
        driver.PolicyManager.delete_subject_data(moon_user_id=authed_user, policy_id=uuid,
                                                 category_id=category_id, data_id=data_id)

        slaves = slave_class.Slaves.get().get("slaves")

        invalidate_data_in_slaves(slaves=slaves, policy_id=None, category_id=None,
                                  data_id=data_id, type="subject")
        return {"result": True}


class ObjectData(object):
    """
    Endpoint for object data requests
    """

    @staticmethod
    @hug.local()
    @hug.get("/policies/{uuid}/object_data", requires=api_key_authentication)
    @hug.get("/policies/{uuid}/object_data/{category_id}", requires=api_key_authentication)
    @hug.get("/policies/{uuid}/object_data/{category_id}/{data_id}",
             requires=api_key_authentication)
    def get(uuid: hug.types.text, category_id: hug.types.text = None,
            data_id: hug.types.text = None, authed_user: hug.directives.user = None):
        """Retrieve all object categories or a specific one if sid is given
        for a given policy

        :param uuid: uuid of the policy
        :param category_id: uuid of the object category
        :param data_id: uuid of the object data
        :param authed_user: user ID who do the request
        :return: [{
            "policy_id": "policy_id1",
            "category_id": "category_id1",
            "data": {
                "object_data_id": {
                    "name": "name of the data",
                    "description": "description of the data (optional)"
                }
            }
        }]
        :internal_api: get_object_data
        """
        data = driver.PolicyManager.get_object_data(moon_user_id=authed_user, policy_id=uuid,
                                                    category_id=category_id, data_id=data_id)
        return {"object_data": data}

    @staticmethod
    @hug.local()
    @hug.post("/policies/{uuid}/object_data/{category_id}", requires=api_key_authentication)
    def post(body: validate_input("name"), uuid: hug.types.text, category_id: hug.types.text,
             authed_user: hug.directives.user = None):
        """Create or update a object.

        :param body: body of the request
        :param uuid: uuid of the policy
        :param category_id: uuid of the object category
        :param authed_user: user ID who do the request
        :request body: {
            "name": "name of the data (mandatory)",
            "description": "description of the data (optional)"
        }
        :return: {
            "policy_id": "policy_id1",
            "category_id": "category_id1",
            "data": {
                "object_data_id": {
                    "name": "name of the data",
                    "description": "description of the data (optional)"
                }
            }
        }
        :internal_api: add_object_data
        """
        data = driver.PolicyManager.add_object_data(moon_user_id=authed_user, policy_id=uuid,
                                                    category_id=category_id, value=body)

        slaves = slave_class.Slaves.get().get("slaves")
        invalidate_data_in_slaves(slaves=slaves, policy_id=uuid, category_id=category_id,
                                  data_id=None, type="object")


        return {"object_data": data}

    @staticmethod
    @hug.local()
    @hug.delete("/policies/{uuid}/object_data/{category_id}/{data_id}",
                requires=api_key_authentication)
    def delete(uuid: hug.types.text, data_id: hug.types.text,
               category_id: hug.types.text = None, authed_user: hug.directives.user = None):
        """Delete a object for a given policy

        :param uuid: uuid of the policy
        :param category_id: uuid of the object category
        :param data_id: uuid of the object data
        :param authed_user: user ID who do the request
        :return: {
            "result": "True or False",
            "message": "optional message (optional)"
        }
        :internal_api: delete_object_data
        """
        driver.PolicyManager.delete_object_data(moon_user_id=authed_user, policy_id=uuid,
                                                category_id=category_id, data_id=data_id)

        slaves = slave_class.Slaves.get().get("slaves")

        invalidate_data_in_slaves(slaves=slaves, policy_id=None, category_id=None,
                                  data_id=data_id, type="object")

        return {"result": True}


class ActionData(object):
    """
    Endpoint for action data requests
    """

    @staticmethod
    @hug.local()
    @hug.get("/policies/{uuid}/action_data", requires=api_key_authentication)
    @hug.get("/policies/{uuid}/action_data/{category_id}", requires=api_key_authentication)
    @hug.get("/policies/{uuid}/action_data/{category_id}/{data_id}",
             requires=api_key_authentication)
    def get(uuid: hug.types.text, category_id: hug.types.text = None,
            data_id: hug.types.text = None, authed_user: hug.directives.user = None):
        """Retrieve all action categories or a specific one if sid is given
        for a given policy

        :param uuid: uuid of the policy
        :param category_id: uuid of the action category
        :param data_id: uuid of the action data
        :param authed_user: user ID who do the request
        :return: [{
            "policy_id": "policy_id1",
            "category_id": "category_id1",
            "data": {
                "action_data_id": {
                    "name": "name of the data",
                    "description": "description of the data (optional)"
                }
            }
        }]
        :internal_api: get_action_data
        """
        data = driver.PolicyManager.get_action_data(moon_user_id=authed_user, policy_id=uuid,
                                                    category_id=category_id, data_id=data_id)

        return {"action_data": data}

    @staticmethod
    @hug.local()
    @hug.post("/policies/{uuid}/action_data/{category_id}", requires=api_key_authentication)
    def post(body: validate_input("name"), uuid: hug.types.text, category_id: hug.types.text,
             authed_user: hug.directives.user = None):
        """Create or update a action.

        :param body: body of the request
        :param uuid: uuid of the policy
        :param category_id: uuid of the action category
        :param authed_user: user ID who do the request
        :request body: {
            "name": "name of the data (mandatory)",
            "description": "description of the data (optional)"
        }
        :return: {
            "policy_id": "policy_id1",
            "category_id": "category_id1",
            "data": {
                "action_data_id": {
                    "name": "name of the data",
                    "description": "description of the data (optional)"
                }
            }
        }
        :internal_api: add_action_data
        """
        data = driver.PolicyManager.add_action_data(moon_user_id=authed_user, policy_id=uuid,
                                                    category_id=category_id, value=body)

        slaves = slave_class.Slaves.get().get("slaves")
        invalidate_data_in_slaves(slaves=slaves, policy_id=uuid, category_id=category_id,
                                  data_id=None, type="action")

        return {"action_data": data}

    @staticmethod
    @hug.local()
    @hug.delete("/policies/{uuid}/action_data/{category_id}/{data_id}",
                requires=api_key_authentication)
    def delete(uuid: hug.types.text, data_id: hug.types.text,
               category_id: hug.types.text = None, authed_user: hug.directives.user = None):
        """Delete a action for a given policy

        :param uuid: uuid of the policy
        :param category_id: uuid of the action category
        :param data_id: uuid of the action data
        :param authed_user: user ID who do the request
        :return: {
            "result": "True or False",
            "message": "optional message (optional)"
        }
        :internal_api: delete_action_data
        """
        driver.PolicyManager.delete_action_data(moon_user_id=authed_user, policy_id=uuid,
                                                category_id=category_id, data_id=data_id)

        slaves = slave_class.Slaves.get().get("slaves")

        invalidate_data_in_slaves(slaves=slaves, policy_id=None, category_id=None,
                                  data_id=data_id, type="action")
        return {"result": True}


def human_display_entity_data(entity_data_json):
    """
    Common static method for entity (subject, object)
    :param entity_data_json: subject_data_json or object_data_json
    :return:
    """
    human_result = "Data\n"
    human_result += "\tpolicy_id : " + entity_data_json.get("policy_id") + "\n"
    human_result += "\tcategory_id : " + entity_data_json.get("category_id") + "\n"
    human_result += "\tdata : \n"
    for data in entity_data_json.get("data"):
        human_result += "\t\t" + data + "\n"
        human_result += human_display_data(entity_data_json.get("data").get(data))
    return human_result

def human_display_data(data_json, tabulations: int = 3):
    """
    :param data_json:
    :param tabulations: nombre de caractères de tabulations
    :return:
    """
    tab = ""
    for i in range(tabulations):
        tab += "\t"
    human_result = tab + "id:" + data_json.get("id") + "\n"
    human_result += tab + "name:" + data_json.get("name") + "\n"
    human_result += tab + "description:" + data_json.get("description") + "\n"
    human_result += tab + "category_id:" + data_json.get("category_id") + "\n"
    human_result += tab + "policy_id:" + data_json.get("policy_id") + "\n"

    return human_result


SubjectDataAPI = hug.API(name='subject_data', doc=SubjectData.__doc__)


@hug.object(name='subject_data', version='1.0.0', api=SubjectDataAPI)
class SubjectDataCLI(object):
    """An example of command like calls via an Object"""

    @staticmethod
    @hug.object.cli
    def list(policy_name_or_id, name_or_id="", human: bool = False):
        """Retrieve all subject categories or a specific one if data_id is give for a given policy"""
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        policy_id = list(policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
        _data_req = requests.get(
            "{}/policies/{}/subject_data".format(db_conf.get("url"), policy_id),
            headers={"x-api-key": manager_api_key}
            )
        _data_to_return = None
        _data_list = _data_req.json().get("subject_data")
        if _data_req.status_code == 200:
            if name_or_id:
                _data = None
                for _data_item in _data_list:
                    if policy_id != _data_item["policy_id"]:
                        continue
                    for _data_key, _data_value in _data_item.get("data").items():
                        if _data_value.get("name") == name_or_id:
                            _data_to_return = _data_value
                            break
                        elif _data_key == name_or_id:
                            _data_to_return = _data_value
                            break
                if not _data_to_return:
                    raise Exception("Cannot find Subject Data with name or ID {}".format(
                        name_or_id))
                if human:
                    result = _data_to_return
                else:
                    result = {"subject_data": _data_to_return}
            else:
                result = _data_req.json()

            if human:
                if name_or_id:
                    return human_display_data(result, 1)
                else:
                    return SubjectDataCLI.human_display(result)
            else:
                return result
        LOGGER.error('Cannot list Subject Data {}'.format(_data_req.status_code))

    @staticmethod
    @hug.object.cli
    def add(name, category_name_or_id, policy_name_or_id, description="", human: bool=False):
        """
        Add an subject data in the database
        :return: JSON status output
        """
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        category_id = list(meta_data.SubjectCategoriesCLI.list(category_name_or_id)
                           .get("subject_categories").keys())[0]
        policy_id = list(policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
        _url = "{}/policies/{}/subject_data/{}".format(
            db_conf.get("url"), policy_id, category_id)
        _data_req = requests.post(
            _url,
            json={
                "name": name,
                "description": description,
            },
            headers={
                "x-api-key": manager_api_key,
                "Content-Type": "application/json"
            }
        )
        if _data_req.status_code == 200:
            LOGGER.warning('Create {}'.format(_data_req.content))
            if human:
                return human_display_entity_data(_data_req.json().get("subject_data"))
            else:
                return _data_req.json()
        LOGGER.error('Cannot create {}'.format(name, _data_req.content[:40]))

    @staticmethod
    @hug.object.cli
    def delete(name_or_id, category_name_or_id, policy_name_or_id):
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        _subject_data = SubjectDataCLI.list(policy_name_or_id)
        category_id = list(meta_data.SubjectCategoriesCLI.list(category_name_or_id)
                           .get("subject_categories").keys())[0]
        for element in _subject_data.get("subject_data"):
            for _data_id, _data_value in element.get("data").items():
                if _data_value.get("name") == name_or_id:
                    policy_id = list(
                        policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
                    _url = "{}/policies/{}/subject_data/{}/{}".format(
                        db_conf.get("url"), policy_id, category_id, _data_id)
                    req = requests.delete(
                        _url,
                        headers={"x-api-key": manager_api_key}
                    )
                    if req.status_code == 200:
                        LOGGER.warning('Deleted {}'.format(name_or_id))
                        continue
                    LOGGER.error("Cannot delete Subject Data with name {}".format(name_or_id))

    @staticmethod
    def human_display(subject_data_json):
        human_result = "Subjects Data\n"
        for subject_data in subject_data_json.get('subject_data'):
            human_result += human_display_entity_data(subject_data)
        return human_result

ObjectDataAPI = hug.API(name='object_data', doc=ObjectData.__doc__)


@hug.object(name='object_data', version='1.0.0', api=ObjectDataAPI)
class ObjectDataCLI(object):
    """An example of command like calls via an Object"""

    @staticmethod
    @hug.object.cli
    def list(policy_name_or_id, name_or_id="", human: bool = False):
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        policy_id = list(policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
        _data_req = requests.get(
            "{}/policies/{}/object_data".format(db_conf.get("url"), policy_id),
            headers={"x-api-key": manager_api_key}
            )
        _data_to_return = None
        _data_list = _data_req.json().get("object_data")
        if _data_req.status_code == 200:
            if name_or_id:
                _data = None
                for _data_item in _data_list:
                    if policy_id != _data_item["policy_id"]:
                        continue
                    for _data_key, _data_value in _data_item.get("data").items():
                        if _data_value.get("name") == name_or_id:
                            _data_to_return = _data_value
                            break
                        elif _data_key == name_or_id:
                            _data_to_return = _data_value
                            break
                if not _data_to_return:
                    raise Exception("Cannot find Object Data with name or ID {}".format(
                        name_or_id))
                if human:
                    result = _data_to_return
                else:
                    result = {"object_data": _data_to_return}
            else:
                result = _data_req.json()

            if human:
                if name_or_id:
                    return human_display_data(result, 1)
                else:
                    return ObjectDataCLI.human_display(result)
            else:
                return result
        LOGGER.error('Cannot list Object Data {}'.format(_data_req.status_code))

    @staticmethod
    @hug.object.cli
    def add(name, category_name_or_id, policy_name_or_id, description="", human: bool = False):
        """
        Add
        :param name:
        :param category_name_or_id:
        :param policy_name_or_id:
        :param description:
        :param human:
        :return:
        """
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        category_id = list(meta_data.ObjectCategoriesCLI.list(category_name_or_id)
                           .get("object_categories").keys())[0]
        policy_id = list(policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
        _url = "{}/policies/{}/object_data/{}".format(
            db_conf.get("url"), policy_id, category_id)
        _data_req = requests.post(
            _url,
            json={
                "name": name,
                "description": description,
            },
            headers={
                "x-api-key": manager_api_key,
                "Content-Type": "application/json"
            }
        )
        if _data_req.status_code == 200:
            LOGGER.warning('Create {}'.format(_data_req.content))
            if human:
                return human_display_entity_data(_data_req.json().get("object_data"))
            else:
                return _data_req.json()
        LOGGER.error('Cannot create {}'.format(name, _data_req.content[:40]))

    @staticmethod
    @hug.object.cli
    def delete(name_or_id, category_name_or_id, policy_name_or_id):
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        _object_data = ObjectDataCLI.list(policy_name_or_id)
        category_id = list(meta_data.ObjectCategoriesCLI.list(category_name_or_id)
                           .get("object_categories").keys())[0]
        for element in _object_data.get("object_data"):
            for _data_id, _data_value in element.get("data").items():
                if _data_value.get("name") == name_or_id:
                    policy_id = list(
                        policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
                    _url = "{}/policies/{}/object_data/{}/{}".format(
                        db_conf.get("url"), policy_id, category_id, _data_id)
                    req = requests.delete(
                        _url,
                        headers={"x-api-key": manager_api_key}
                    )
                    if req.status_code == 200:
                        LOGGER.warning('Deleted {}'.format(name_or_id))
                        continue
                    LOGGER.error("Cannot delete Object Data with name {}".format(name_or_id))

    @staticmethod
    def human_display(object_data_json):
        human_result = "Objects Data\n"
        for object_data in object_data_json.get('object_data'):
            human_result += human_display_entity_data(object_data)
        return human_result

ActionDataAPI = hug.API(name='action_data', doc=ActionData.__doc__)


@hug.object(name='action_data', version='1.0.0', api=ActionDataAPI)
class ActionDataCLI(object):
    """An example of command like calls via an Action"""

    @staticmethod
    @hug.object.cli
    def list(policy_name_or_id, name_or_id="", human: bool = False):
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        policy_id = list(policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
        _data_req = requests.get(
            "{}/policies/{}/action_data".format(db_conf.get("url"), policy_id),
            headers={"x-api-key": manager_api_key}
            )
        _data_to_return = None
        _data_list = _data_req.json().get("action_data")
        if _data_req.status_code == 200:
            if name_or_id:
                _data = None
                for _data_item in _data_list:
                    if policy_id != _data_item["policy_id"]:
                        continue
                    for _data_key, _data_value in _data_item.get("data").items():
                        if _data_value.get("name") == name_or_id:
                            _data_to_return = _data_value
                            break
                        elif _data_key == name_or_id:
                            _data_to_return = _data_value
                            break
                if not _data_to_return:
                    raise Exception("Cannot find Action Data with name or ID {}".format(
                        name_or_id))
                if human:
                    result = _data_to_return
                else:
                    result = {"action_data": _data_to_return}
            else:
                result = _data_req.json()

            if human:
                if name_or_id:
                    return human_display_data(result, 1)
                else:
                    return ActionDataCLI.human_display(result)
            else:
                return result
        LOGGER.error('Cannot list Action Data {}'.format(_data_req.status_code))

    @staticmethod
    @hug.object.cli
    def add(name, category_name_or_id, policy_name_or_id, description="", human:bool = False):
        """
        Add an action data in the database
        :return: JSON status output
        """
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        category_id = list(meta_data.ActionCategoriesCLI.list(category_name_or_id)
                           .get("action_categories").keys())[0]
        policy_id = list(policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
        _url = "{}/policies/{}/action_data/{}".format(
            db_conf.get("url"), policy_id, category_id)
        _data_req = requests.post(
            _url,
            json={
                "name": name,
                "description": description,
            },
            headers={
                "x-api-key": manager_api_key,
                "Content-Type": "application/json"
            }
        )
        if _data_req.status_code == 200:
            LOGGER.warning('Create {}'.format(_data_req.content))
            if human:
                return human_display_entity_data(_data_req.json().get("action_data"))
            else:
                return _data_req.json()
        LOGGER.error('Cannot create {}'.format(name, _data_req.content[:40]))


    @staticmethod
    def human_display(action_data_json):
        human_result = "Actions Data\n"
        for action_data in action_data_json.get('action_data'):
            human_result += human_display_entity_data(action_data)
        return human_result

    @staticmethod
    @hug.object.cli
    def delete(name_or_id, category_name_or_id, policy_name_or_id):
        db_conf = configuration.get_configuration(key='management')
        manager_api_key = connect_from_env()
        _action_data = ActionDataCLI.list(policy_name_or_id)
        category_id = list(meta_data.ActionCategoriesCLI.list(category_name_or_id)
                           .get("action_categories").keys())[0]
        for element in _action_data.get("action_data"):
            for _data_id, _data_value in element.get("data").items():
                if _data_value.get("name") == name_or_id:
                    policy_id = list(
                        policy.PoliciesCLI.list(policy_name_or_id).get("policies").keys())[0]
                    _url = "{}/policies/{}/action_data/{}/{}".format(
                        db_conf.get("url"), policy_id, category_id, _data_id)
                    req = requests.delete(
                        _url,
                        headers={"x-api-key": manager_api_key}
                    )
                    if req.status_code == 200:
                        LOGGER.warning('Deleted {}'.format(name_or_id))
                        continue
                    LOGGER.error("Cannot delete Action Data with name {}".format(name_or_id))