File: responses.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (95 lines) | stat: -rw-r--r-- 3,168 bytes parent folder | download
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
"""Handles incoming shield requests, invokes methods, returns responses."""

import json

from moto.core.responses import BaseResponse

from .models import ShieldBackend, shield_backends


class ShieldResponse(BaseResponse):
    """Handler for Shield requests and responses."""

    def __init__(self) -> None:
        super().__init__(service_name="shield")

    @property
    def shield_backend(self) -> ShieldBackend:
        """Return backend instance specific for this region."""
        return shield_backends[self.current_account][self.region]

    def create_protection(self) -> str:
        params = json.loads(self.body)
        name = params.get("Name")
        resource_arn = params.get("ResourceArn")
        tags = params.get("Tags")
        protection_id = self.shield_backend.create_protection(
            name=name,
            resource_arn=resource_arn,
            tags=tags,
        )
        return json.dumps({"ProtectionId": protection_id})

    def describe_protection(self) -> str:
        params = json.loads(self.body)
        protection_id = params.get("ProtectionId")
        resource_arn = params.get("ResourceArn")
        protection = self.shield_backend.describe_protection(
            protection_id=protection_id,
            resource_arn=resource_arn,
        )
        return json.dumps({"Protection": protection.to_dict()})

    def list_protections(self) -> str:
        params = json.loads(self.body)
        inclusion_filters = params.get("InclusionFilters")
        protections = self.shield_backend.list_protections(
            inclusion_filters=inclusion_filters,
        )
        return json.dumps(
            {"Protections": [protection.to_dict() for protection in protections]}
        )

    def delete_protection(self) -> str:
        params = json.loads(self.body)
        protection_id = params.get("ProtectionId")
        self.shield_backend.delete_protection(
            protection_id=protection_id,
        )
        return "{}"

    def list_tags_for_resource(self) -> str:
        params = json.loads(self.body)
        resource_arn = params.get("ResourceARN")
        tags = self.shield_backend.list_tags_for_resource(
            resource_arn=resource_arn,
        )
        return json.dumps({"Tags": tags})

    def tag_resource(self) -> str:
        params = json.loads(self.body)
        resource_arn = params.get("ResourceARN")
        tags = params.get("Tags")
        self.shield_backend.tag_resource(
            resource_arn=resource_arn,
            tags=tags,
        )
        return "{}"

    def untag_resource(self) -> str:
        params = json.loads(self.body)
        resource_arn = params.get("ResourceARN")
        tag_keys = params.get("TagKeys")
        self.shield_backend.untag_resource(
            resource_arn=resource_arn,
            tag_keys=tag_keys,
        )
        return "{}"

    def create_subscription(self) -> str:
        self.shield_backend.create_subscription()
        return "{}"

    def describe_subscription(self) -> str:
        subscription = self.shield_backend.describe_subscription()
        return json.dumps({"Subscription": subscription.to_dict()})