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 (369 lines) | stat: -rw-r--r-- 15,484 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
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
"""Handles incoming workspacesweb requests, invokes methods, returns responses."""

import json
from typing import Any
from urllib.parse import unquote

from moto.core.responses import TYPE_RESPONSE, BaseResponse

from .models import WorkSpacesWebBackend, workspacesweb_backends


class WorkSpacesWebResponse(BaseResponse):
    """Handler for WorkSpacesWeb requests and responses."""

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

    @property
    def workspacesweb_backend(self) -> WorkSpacesWebBackend:
        """Return backend instance specific for this region."""
        return workspacesweb_backends[self.current_account][self.region]

    @staticmethod
    def network_settings(request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE:  # type: ignore[misc]
        handler = WorkSpacesWebResponse()
        handler.setup_class(request, full_url, headers)
        if request.method == "GET":
            return handler.get_network_settings()
        else:
            return handler.delete_network_settings()

    @staticmethod
    def browser_settings(request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE:  # type: ignore[misc]
        handler = WorkSpacesWebResponse()
        handler.setup_class(request, full_url, headers)
        if request.method == "GET":
            return handler.get_browser_settings()
        else:
            return handler.delete_browser_settings()

    @staticmethod
    def user_settings(request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE:  # type: ignore[misc]
        handler = WorkSpacesWebResponse()
        handler.setup_class(request, full_url, headers)
        if request.method == "GET":
            return handler.get_user_settings()
        else:
            return handler.delete_user_settings()

    @staticmethod
    def user_access_logging_settings(  # type: ignore[misc]
        request: Any, full_url: str, headers: Any
    ) -> TYPE_RESPONSE:
        handler = WorkSpacesWebResponse()
        handler.setup_class(request, full_url, headers)
        if request.method == "GET":
            return handler.get_user_access_logging_settings()
        else:
            return handler.delete_user_access_logging_settings()

    @staticmethod
    def portal(request: Any, full_url: str, headers: Any) -> TYPE_RESPONSE:  # type: ignore[misc]
        handler = WorkSpacesWebResponse()
        handler.setup_class(request, full_url, headers)
        if request.method == "GET":
            return handler.get_portal()
        else:
            return handler.delete_portal()

    def create_browser_settings(self) -> str:
        additional_encryption_context = self._get_param("additionalEncryptionContext")
        browser_policy = self._get_param("browserPolicy")
        client_token = self._get_param("clientToken")
        customer_managed_key = self._get_param("customerManagedKey")
        tags = self._get_param("tags")
        browser_settings_arn = self.workspacesweb_backend.create_browser_settings(
            additional_encryption_context=additional_encryption_context,
            browser_policy=browser_policy,
            client_token=client_token,
            customer_managed_key=customer_managed_key,
            tags=tags,
        )
        return json.dumps({"browserSettingsArn": browser_settings_arn})

    def create_network_settings(self) -> str:
        security_group_ids = self._get_param("securityGroupIds")
        subnet_ids = self._get_param("subnetIds")
        tags = self._get_param("tags")
        vpc_id = self._get_param("vpcId")
        network_settings_arn = self.workspacesweb_backend.create_network_settings(
            security_group_ids=security_group_ids,
            subnet_ids=subnet_ids,
            tags=tags,
            vpc_id=vpc_id,
        )
        return json.dumps({"networkSettingsArn": network_settings_arn})

    def get_network_settings(self) -> TYPE_RESPONSE:
        network_settings_arn = unquote(
            self.parsed_url.path.split("/networkSettings/")[-1]
        )
        network_settings = self.workspacesweb_backend.get_network_settings(
            network_settings_arn=network_settings_arn,
        )
        return 200, {}, json.dumps({"networkSettings": network_settings})

    def create_portal(self) -> str:
        additional_encryption_context = self._get_param("additionalEncryptionContext")
        authentication_type = self._get_param("authenticationType")
        client_token = self._get_param("clientToken")
        customer_managed_key = self._get_param("customerManagedKey")
        display_name = self._get_param("displayName")
        instance_type = self._get_param("instanceType")
        max_concurrent_sessions = self._get_param("maxConcurrentSessions")
        tags = self._get_param("tags")
        portal_arn, portal_endpoint = self.workspacesweb_backend.create_portal(
            additional_encryption_context=additional_encryption_context,
            authentication_type=authentication_type,
            client_token=client_token,
            customer_managed_key=customer_managed_key,
            display_name=display_name,
            instance_type=instance_type,
            max_concurrent_sessions=max_concurrent_sessions,
            tags=tags,
        )
        return json.dumps({"portalArn": portal_arn, "portalEndpoint": portal_endpoint})

    def list_browser_settings(self) -> str:
        browser_settings = self.workspacesweb_backend.list_browser_settings()
        return json.dumps({"browserSettings": browser_settings})

    def list_network_settings(self) -> str:
        network_settings = self.workspacesweb_backend.list_network_settings()
        return json.dumps({"networkSettings": network_settings})

    def list_portals(self) -> str:
        portals = self.workspacesweb_backend.list_portals()
        return json.dumps({"portals": portals})

    def get_browser_settings(self) -> TYPE_RESPONSE:
        browser_settings_arn = unquote(
            self.parsed_url.path.split("/browserSettings/")[-1]
        )
        browser_settings = self.workspacesweb_backend.get_browser_settings(
            browser_settings_arn=browser_settings_arn,
        )
        return 200, {}, json.dumps({"browserSettings": browser_settings})

    def delete_browser_settings(self) -> TYPE_RESPONSE:
        browser_settings_arn = unquote(
            self.parsed_url.path.split("/browserSettings/")[-1]
        )
        self.workspacesweb_backend.delete_browser_settings(
            browser_settings_arn=browser_settings_arn
        )
        return 200, {}, "{}"

    def delete_network_settings(self) -> TYPE_RESPONSE:
        network_settings_arn = unquote(
            self.parsed_url.path.split("/networkSettings/")[-1]
        )
        self.workspacesweb_backend.delete_network_settings(
            network_settings_arn=network_settings_arn,
        )
        return 200, {}, "{}"

    def get_portal(self) -> TYPE_RESPONSE:
        portal_arn = unquote(self.parsed_url.path.split("/portals/")[-1])
        portal = self.workspacesweb_backend.get_portal(portal_arn=portal_arn)
        return 200, {}, json.dumps({"portal": portal})

    def delete_portal(self) -> TYPE_RESPONSE:
        portal_arn = unquote(self.parsed_url.path.split("/portals/")[-1])
        self.workspacesweb_backend.delete_portal(portal_arn=portal_arn)
        return 200, {}, "{}"

    def associate_browser_settings(self) -> str:
        browser_settings_arn = unquote(self._get_param("browserSettingsArn"))
        portal_arn = unquote(
            self.parsed_url.path.split("/portals/")[-1].split("/browserSettings")[0]
        )
        browser_settings_arn, portal_arn = (
            self.workspacesweb_backend.associate_browser_settings(
                browser_settings_arn=browser_settings_arn,
                portal_arn=portal_arn,
            )
        )
        return json.dumps(
            {"browserSettingsArn": browser_settings_arn, "portalArn": portal_arn}
        )

    def associate_network_settings(self) -> str:
        network_settings_arn = unquote(self._get_param("networkSettingsArn"))
        portal_arn = unquote(
            self.parsed_url.path.split("/portals/")[-1].split("/networkSettings")[0]
        )
        network_settings_arn, portal_arn = (
            self.workspacesweb_backend.associate_network_settings(
                network_settings_arn=network_settings_arn,
                portal_arn=portal_arn,
            )
        )
        return json.dumps(
            {"networkSettingsArn": network_settings_arn, "portalArn": portal_arn}
        )

    def create_user_settings(self) -> str:
        additional_encryption_context = self._get_param("additionalEncryptionContext")
        client_token = self._get_param("clientToken")
        cookie_synchronization_configuration = self._get_param(
            "cookieSynchronizationConfiguration"
        )
        copy_allowed = self._get_param("copyAllowed")
        customer_managed_key = self._get_param("customerManagedKey")
        deep_link_allowed = self._get_param("deepLinkAllowed")
        disconnect_timeout_in_minutes = self._get_param("disconnectTimeoutInMinutes")
        download_allowed = self._get_param("downloadAllowed")
        idle_disconnect_timeout_in_minutes = self._get_param(
            "idleDisconnectTimeoutInMinutes"
        )
        paste_allowed = self._get_param("pasteAllowed")
        print_allowed = self._get_param("printAllowed")
        tags = self._get_param("tags")
        upload_allowed = self._get_param("uploadAllowed")
        user_settings_arn = self.workspacesweb_backend.create_user_settings(
            additional_encryption_context=additional_encryption_context,
            client_token=client_token,
            cookie_synchronization_configuration=cookie_synchronization_configuration,
            copy_allowed=copy_allowed,
            customer_managed_key=customer_managed_key,
            deep_link_allowed=deep_link_allowed,
            disconnect_timeout_in_minutes=disconnect_timeout_in_minutes,
            download_allowed=download_allowed,
            idle_disconnect_timeout_in_minutes=idle_disconnect_timeout_in_minutes,
            paste_allowed=paste_allowed,
            print_allowed=print_allowed,
            tags=tags,
            upload_allowed=upload_allowed,
        )
        return json.dumps({"userSettingsArn": user_settings_arn})

    def get_user_settings(self) -> TYPE_RESPONSE:
        user_settings_arn = unquote(self.parsed_url.path.split("/userSettings/")[-1])
        user_settings = self.workspacesweb_backend.get_user_settings(
            user_settings_arn=user_settings_arn,
        )
        return 200, {}, json.dumps({"userSettings": user_settings})

    def delete_user_settings(self) -> TYPE_RESPONSE:
        user_settings_arn = unquote(self.parsed_url.path.split("/userSettings/")[-1])
        self.workspacesweb_backend.delete_user_settings(
            user_settings_arn=user_settings_arn,
        )
        return 200, {}, "{}"

    def create_user_access_logging_settings(self) -> str:
        params = self._get_params()
        params = json.loads(list(params.keys())[0])
        client_token = params.get("clientToken")
        kinesis_stream_arn = params.get("kinesisStreamArn")
        tags = params.get("tags")
        user_access_logging_settings_arn = (
            self.workspacesweb_backend.create_user_access_logging_settings(
                client_token=client_token,
                kinesis_stream_arn=kinesis_stream_arn,
                tags=tags,
            )
        )
        return json.dumps(
            {"userAccessLoggingSettingsArn": user_access_logging_settings_arn}
        )

    def get_user_access_logging_settings(self) -> TYPE_RESPONSE:
        user_access_logging_settings_arn = unquote(
            self.parsed_url.path.split("/userAccessLoggingSettings/")[-1]
        )
        user_access_logging_settings = (
            self.workspacesweb_backend.get_user_access_logging_settings(
                user_access_logging_settings_arn=user_access_logging_settings_arn,
            )
        )
        return (
            200,
            {},
            json.dumps({"userAccessLoggingSettings": user_access_logging_settings}),
        )

    def delete_user_access_logging_settings(self) -> TYPE_RESPONSE:
        user_access_logging_settings_arn = unquote(
            self.parsed_url.path.split("/userAccessLoggingSettings/")[-1]
        )
        self.workspacesweb_backend.delete_user_access_logging_settings(
            user_access_logging_settings_arn=user_access_logging_settings_arn,
        )
        return 200, {}, "{}"

    def associate_user_settings(self) -> str:
        user_settings_arn = unquote(self._get_param("userSettingsArn"))
        portal_arn = unquote(
            self.parsed_url.path.split("/portals/")[-1].split("/userSettings")[0]
        )
        user_settings_arn, portal_arn = (
            self.workspacesweb_backend.associate_user_settings(
                user_settings_arn=user_settings_arn,
                portal_arn=portal_arn,
            )
        )
        return json.dumps(
            {"userSettingsArn": user_settings_arn, "portalArn": portal_arn}
        )

    def associate_user_access_logging_settings(self) -> str:
        user_access_logging_settings_arn = unquote(
            self._get_param("userAccessLoggingSettingsArn")
        )
        portal_arn = unquote(
            self.parsed_url.path.split("/portals/")[-1].split(
                "/userAccessLoggingSettings"
            )[0]
        )
        user_access_logging_settings_arn, portal_arn = (
            self.workspacesweb_backend.associate_user_access_logging_settings(
                user_access_logging_settings_arn=user_access_logging_settings_arn,
                portal_arn=portal_arn,
            )
        )
        return json.dumps(
            {
                "userAccessLoggingSettingsArn": user_access_logging_settings_arn,
                "portalArn": portal_arn,
            }
        )

    def list_user_settings(self) -> str:
        user_settings = self.workspacesweb_backend.list_user_settings()
        return json.dumps({"userSettings": user_settings})

    def list_user_access_logging_settings(self) -> str:
        user_access_logging_settings = (
            self.workspacesweb_backend.list_user_access_logging_settings()
        )
        return json.dumps({"userAccessLoggingSettings": user_access_logging_settings})

    def tag_resource(self) -> str:
        client_token = self._get_param("clientToken")
        resource_arn = unquote(self._get_param("resourceArn"))
        tags = self._get_param("tags")
        self.workspacesweb_backend.tag_resource(
            client_token=client_token,
            resource_arn=resource_arn,
            tags=tags,
        )
        return json.dumps({})

    def untag_resource(self) -> str:
        tagKeys = self.__dict__["data"]["tagKeys"]
        resource_arn = unquote(self._get_param("resourceArn"))
        self.workspacesweb_backend.untag_resource(
            resource_arn=resource_arn,
            tag_keys=tagKeys,
        )
        return json.dumps({})

    def list_tags_for_resource(self) -> str:
        resource_arn = unquote(self.parsed_url.path.split("/tags/")[-1])
        tags = self.workspacesweb_backend.list_tags_for_resource(
            resource_arn=resource_arn,
        )
        return json.dumps({"tags": tags})