File: models.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 (197 lines) | stat: -rw-r--r-- 7,392 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
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
from collections import OrderedDict
from typing import Any

from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.utilities.utils import get_partition

from .exceptions import ClientError


class Channel(BaseModel):
    def __init__(self, **kwargs: Any):
        self.arn = kwargs.get("arn")
        self.channel_id = kwargs.get("channel_id")
        self.description = kwargs.get("description")
        self.tags = kwargs.get("tags")

    def to_dict(self) -> dict[str, Any]:
        return {
            "arn": self.arn,
            "id": self.channel_id,
            "description": self.description,
            "tags": self.tags,
        }


class OriginEndpoint(BaseModel):
    def __init__(self, **kwargs: Any):
        self.arn = kwargs.get("arn")
        self.authorization = kwargs.get("authorization")
        self.channel_id = kwargs.get("channel_id")
        self.cmaf_package = kwargs.get("cmaf_package")
        self.dash_package = kwargs.get("dash_package")
        self.description = kwargs.get("description")
        self.hls_package = kwargs.get("hls_package")
        self.id = kwargs.get("endpoint_id")
        self.manifest_name = kwargs.get("manifest_name")
        self.mss_package = kwargs.get("mss_package")
        self.origination = kwargs.get("origination")
        self.startover_window_seconds = kwargs.get("startover_window_seconds")
        self.tags = kwargs.get("tags")
        self.time_delay_seconds = kwargs.get("time_delay_seconds")
        self.url = kwargs.get("url")
        self.whitelist = kwargs.get("whitelist")

    def to_dict(self) -> dict[str, Any]:
        return {
            "arn": self.arn,
            "authorization": self.authorization,
            "channelId": self.channel_id,
            "cmafPackage": self.cmaf_package,
            "dashPackage": self.dash_package,
            "description": self.description,
            "hlsPackage": self.hls_package,
            "id": self.id,
            "manifestName": self.manifest_name,
            "mssPackage": self.mss_package,
            "origination": self.origination,
            "startoverWindowSeconds": self.startover_window_seconds,
            "tags": self.tags,
            "timeDelaySeconds": self.time_delay_seconds,
            "url": self.url,
            "whitelist": self.whitelist,
        }


class MediaPackageBackend(BaseBackend):
    def __init__(self, region_name: str, account_id: str):
        super().__init__(region_name, account_id)
        self._channels: dict[str, Channel] = OrderedDict()
        self._origin_endpoints: dict[str, OriginEndpoint] = OrderedDict()

    def create_channel(
        self, description: str, channel_id: str, tags: dict[str, str]
    ) -> Channel:
        arn = f"arn:{get_partition(self.region_name)}:mediapackage:channel:{channel_id}"
        channel = Channel(
            arn=arn,
            description=description,
            channel_id=channel_id,
            tags=tags,
        )
        self._channels[channel_id] = channel
        return channel

    def list_channels(self) -> list[dict[str, Any]]:
        return [c.to_dict() for c in self._channels.values()]

    def describe_channel(self, channel_id: str) -> Channel:
        try:
            return self._channels[channel_id]
        except KeyError:
            raise ClientError(
                "NotFoundException", f"channel with id={channel_id} not found"
            )

    def delete_channel(self, channel_id: str) -> Channel:
        if channel_id in self._channels:
            return self._channels.pop(channel_id)
        raise ClientError(
            "NotFoundException", f"channel with id={channel_id} not found"
        )

    def create_origin_endpoint(
        self,
        authorization: dict[str, str],
        channel_id: str,
        cmaf_package: dict[str, Any],
        dash_package: dict[str, Any],
        description: str,
        hls_package: dict[str, Any],
        endpoint_id: str,
        manifest_name: str,
        mss_package: dict[str, Any],
        origination: str,
        startover_window_seconds: int,
        tags: dict[str, str],
        time_delay_seconds: int,
        whitelist: list[str],
    ) -> OriginEndpoint:
        arn = f"arn:{get_partition(self.region_name)}:mediapackage:origin_endpoint:{endpoint_id}"
        url = f"https://origin-endpoint.mediapackage.{self.region_name}.amazonaws.com/{endpoint_id}"
        origin_endpoint = OriginEndpoint(
            arn=arn,
            authorization=authorization,
            channel_id=channel_id,
            cmaf_package=cmaf_package,
            dash_package=dash_package,
            description=description,
            hls_package=hls_package,
            endpoint_id=endpoint_id,
            manifest_name=manifest_name,
            mss_package=mss_package,
            origination=origination,
            startover_window_seconds=startover_window_seconds,
            tags=tags,
            time_delay_seconds=time_delay_seconds,
            url=url,
            whitelist=whitelist,
        )
        self._origin_endpoints[endpoint_id] = origin_endpoint
        return origin_endpoint

    def describe_origin_endpoint(self, endpoint_id: str) -> OriginEndpoint:
        try:
            return self._origin_endpoints[endpoint_id]
        except KeyError:
            raise ClientError(
                "NotFoundException", f"origin endpoint with id={endpoint_id} not found"
            )

    def list_origin_endpoints(self) -> list[dict[str, Any]]:
        return [o.to_dict() for o in self._origin_endpoints.values()]

    def delete_origin_endpoint(self, endpoint_id: str) -> OriginEndpoint:
        if endpoint_id in self._origin_endpoints:
            return self._origin_endpoints.pop(endpoint_id)
        raise ClientError(
            "NotFoundException", f"origin endpoint with id={endpoint_id} not found"
        )

    def update_origin_endpoint(
        self,
        authorization: dict[str, str],
        cmaf_package: dict[str, Any],
        dash_package: dict[str, Any],
        description: str,
        hls_package: dict[str, Any],
        endpoint_id: str,
        manifest_name: str,
        mss_package: dict[str, Any],
        origination: str,
        startover_window_seconds: int,
        time_delay_seconds: int,
        whitelist: list[str],
    ) -> OriginEndpoint:
        try:
            origin_endpoint = self._origin_endpoints[endpoint_id]
            origin_endpoint.authorization = authorization
            origin_endpoint.cmaf_package = cmaf_package
            origin_endpoint.dash_package = dash_package
            origin_endpoint.description = description
            origin_endpoint.hls_package = hls_package
            origin_endpoint.manifest_name = manifest_name
            origin_endpoint.mss_package = mss_package
            origin_endpoint.origination = origination
            origin_endpoint.startover_window_seconds = startover_window_seconds
            origin_endpoint.time_delay_seconds = time_delay_seconds
            origin_endpoint.whitelist = whitelist
            return origin_endpoint
        except KeyError:
            raise ClientError(
                "NotFoundException", f"origin endpoint with id={endpoint_id} not found"
            )


mediapackage_backends = BackendDict(MediaPackageBackend, "mediapackage")