File: virtual_node.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 (566 lines) | stat: -rw-r--r-- 15,705 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
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
from dataclasses import asdict, dataclass, field
from typing import Any, Optional

from moto.appmesh.dataclasses.shared import (
    Duration,
    Metadata,
    MissingField,
    Status,
    Timeout,
)
from moto.appmesh.utils.common import clean_dict


@dataclass
class CertificateFile:
    certificate_chain: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"certificateChain": self.certificate_chain}


@dataclass
class CertificateFileWithPrivateKey(CertificateFile):
    private_key: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {
            "certificateChain": self.certificate_chain,
            "privateKey": self.private_key,
        }


@dataclass
class SDS:
    secret_name: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"secretName": self.secret_name}


@dataclass
class Certificate:
    file: Optional[CertificateFileWithPrivateKey]
    sds: Optional[SDS]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "file": (self.file or MissingField()).to_dict(),
                "sds": (self.sds or MissingField()).to_dict(),
            }
        )


@dataclass
class ListenerCertificateACM:
    certificate_arn: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"certificateArn": self.certificate_arn}


@dataclass
class TLSListenerCertificate(Certificate):
    acm: Optional[ListenerCertificateACM]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "acm": (self.acm or MissingField()).to_dict(),
                "file": (self.file or MissingField()).to_dict(),
                "sds": (self.sds or MissingField()).to_dict(),
            }
        )


@dataclass
class Match:
    exact: list[str]

    to_dict = asdict


@dataclass
class SubjectAlternativeNames:
    match: Match

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"match": self.match.to_dict()}


@dataclass
class ACM:
    certificate_authority_arns: list[str]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"certificateAuthorityArns": self.certificate_authority_arns}


@dataclass
class Trust:
    file: Optional[CertificateFile]
    sds: Optional[SDS]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "file": (self.file or MissingField()).to_dict(),
                "sds": (self.sds or MissingField()).to_dict(),
            }
        )


@dataclass
class BackendTrust(Trust):
    acm: Optional[ACM]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "acm": (self.acm or MissingField()).to_dict(),
                "file": (self.file or MissingField()).to_dict(),
                "sds": (self.sds or MissingField()).to_dict(),
            }
        )


@dataclass
class Validation:
    subject_alternative_names: Optional[SubjectAlternativeNames]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "subjectAlternativeNames": (
                    self.subject_alternative_names or MissingField()
                ).to_dict()
            }
        )


@dataclass
class TLSBackendValidation(Validation):
    trust: BackendTrust

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "subjectAlternativeNames": (
                    self.subject_alternative_names or MissingField()
                ).to_dict(),
                "trust": self.trust.to_dict(),
            }
        )


@dataclass
class TLSListenerValidation(Validation):
    trust: Trust

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "subjectAlternativeNames": (
                    self.subject_alternative_names or MissingField()
                ).to_dict(),
                "trust": self.trust.to_dict(),
            }
        )


@dataclass
class TLSClientPolicy:
    certificate: Optional[Certificate]
    enforce: Optional[bool]
    ports: Optional[list[int]]
    validation: TLSBackendValidation

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "certificate": (self.certificate or MissingField()).to_dict(),
                "enforce": self.enforce,
                "ports": self.ports,
                "validation": self.validation.to_dict(),
            }
        )


@dataclass
class ClientPolicy:
    tls: Optional[TLSClientPolicy]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict({"tls": (self.tls or MissingField()).to_dict()})


@dataclass
class BackendDefaults:
    client_policy: Optional[ClientPolicy]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {"clientPolicy": (self.client_policy or MissingField()).to_dict()}
        )


@dataclass
class VirtualService:
    client_policy: Optional[ClientPolicy]
    virtual_service_name: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "clientPolicy": (self.client_policy or MissingField()).to_dict(),
                "virtualServiceName": self.virtual_service_name,
            }
        )


@dataclass
class Backend:
    virtual_service: Optional[VirtualService]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {"virtualService": (self.virtual_service or MissingField()).to_dict()}
        )


@dataclass
class HTTPConnection:
    max_connections: int
    max_pending_requests: Optional[int]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "maxConnections": self.max_connections,
                "maxPendingRequests": self.max_pending_requests,
            }
        )


@dataclass
class GRPCOrHTTP2Connection:
    max_requests: int

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"maxRequests": self.max_requests}


@dataclass
class TCPConnection:
    max_connections: int

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"maxConnections": self.max_connections}


@dataclass
class ConnectionPool:
    grpc: Optional[GRPCOrHTTP2Connection]
    http: Optional[HTTPConnection]
    http2: Optional[GRPCOrHTTP2Connection]
    tcp: Optional[TCPConnection]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "grpc": (self.grpc or MissingField()).to_dict(),
                "http": (self.http or MissingField()).to_dict(),
                "http2": (self.http2 or MissingField()).to_dict(),
                "tcp": (self.tcp or MissingField()).to_dict(),
            }
        )


@dataclass
class HealthCheck:
    healthy_threshold: int
    interval_millis: int
    path: Optional[str]
    port: Optional[int]
    protocol: str
    timeout_millis: int
    unhealthy_threshold: int

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "healthyThreshold": self.healthy_threshold,
                "intervalMillis": self.interval_millis,
                "path": self.path,
                "port": self.port,
                "protocol": self.protocol,
                "timeoutMillis": self.timeout_millis,
                "unhealthyThreshold": self.unhealthy_threshold,
            }
        )


@dataclass
class OutlierDetection:
    base_ejection_duration: Duration
    interval: Duration
    max_ejection_percent: int
    max_server_errors: int

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {
            "baseEjectionDuration": self.base_ejection_duration.to_dict(),
            "interval": self.interval.to_dict(),
            "maxEjectionPercent": self.max_ejection_percent,
            "maxServerErrors": self.max_server_errors,
        }


@dataclass
class PortMapping:
    port: int
    protocol: str
    to_dict = asdict


@dataclass
class TCPTimeout:
    idle: Duration

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return {"idle": self.idle.to_dict()}


@dataclass
class ProtocolTimeouts:
    grpc: Optional[Timeout]
    http: Optional[Timeout]
    http2: Optional[Timeout]
    tcp: Optional[TCPTimeout]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "grpc": (self.grpc or MissingField()).to_dict(),
                "http": (self.http or MissingField()).to_dict(),
                "http2": (self.http2 or MissingField()).to_dict(),
                "tcp": (self.tcp or MissingField()).to_dict(),
            }
        )


@dataclass
class ListenerTLS:
    certificate: TLSListenerCertificate
    mode: str
    validation: Optional[TLSListenerValidation]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "certificate": self.certificate.to_dict(),
                "mode": self.mode,
                "validation": (self.validation or MissingField()).to_dict(),
            }
        )


@dataclass
class Listener:
    connection_pool: Optional[ConnectionPool]
    health_check: Optional[HealthCheck]
    outlier_detection: Optional[OutlierDetection]
    port_mapping: PortMapping
    timeout: Optional[ProtocolTimeouts]
    tls: Optional[ListenerTLS]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "connectionPool": (self.connection_pool or MissingField()).to_dict(),
                "healthCheck": (self.health_check or MissingField()).to_dict(),
                "outlierDetection": (
                    self.outlier_detection or MissingField()
                ).to_dict(),
                "portMapping": self.port_mapping.to_dict(),
                "timeout": (self.timeout or MissingField()).to_dict(),
                "tls": (self.tls or MissingField()).to_dict(),
            }
        )


@dataclass
class KeyValue:
    key: str
    value: str
    to_dict = asdict


@dataclass
class LoggingFormat:
    json: Optional[list[KeyValue]]
    text: Optional[str]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {"json": [pair.to_dict() for pair in self.json or []], "text": self.text}
        )


@dataclass
class AccessLogFile:
    format: Optional[LoggingFormat]
    path: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {"format": (self.format or MissingField()).to_dict(), "path": self.path}
        )


@dataclass
class AccessLog:
    file: Optional[AccessLogFile]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict({"file": (self.file or MissingField()).to_dict()})


@dataclass
class Logging:
    access_log: Optional[AccessLog]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict({"accessLog": (self.access_log or MissingField()).to_dict()})


@dataclass
class AWSCloudMap:
    attributes: Optional[list[KeyValue]]
    ip_preference: Optional[str]
    namespace_name: str
    service_name: str

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "attributes": [
                    attribute.to_dict() for attribute in self.attributes or []
                ],
                "ipPreference": self.ip_preference,
                "namespaceName": self.namespace_name,
                "serviceName": self.service_name,
            }
        )


@dataclass
class DNS:
    hostname: str
    ip_preference: Optional[str]
    response_type: Optional[str]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "hostname": self.hostname,
                "ipPreference": self.ip_preference,
                "responseType": self.response_type,
            }
        )


@dataclass
class ServiceDiscovery:
    aws_cloud_map: Optional[AWSCloudMap]
    dns: Optional[DNS]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "awsCloudMap": (self.aws_cloud_map or MissingField()).to_dict(),
                "dns": (self.dns or MissingField()).to_dict(),
            }
        )


@dataclass
class VirtualNodeSpec:
    backend_defaults: Optional[BackendDefaults]
    backends: Optional[list[Backend]]
    listeners: Optional[list[Listener]]
    logging: Optional[Logging]
    service_discovery: Optional[ServiceDiscovery]

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "backendDefaults": (self.backend_defaults or MissingField()).to_dict(),
                "backends": [backend.to_dict() for backend in self.backends or []],
                "listeners": [listener.to_dict() for listener in self.listeners or []],
                "logging": (self.logging or MissingField()).to_dict(),
                "serviceDiscovery": (
                    self.service_discovery or MissingField()
                ).to_dict(),
            }
        )


@dataclass
class VirtualNodeMetadata(Metadata):
    mesh_name: str = field(default="")
    virtual_node_name: str = field(default="")

    def __post_init__(self) -> None:
        if self.mesh_name == "":
            raise TypeError("__init__ missing 1 required argument: 'mesh_name'")
        if self.mesh_owner == "":
            raise TypeError("__init__ missing 1 required argument: 'route_name'")
        if self.virtual_node_name == "":
            raise TypeError("__init__ missing 1 required argument: 'virtual_node_name'")

    def formatted_for_list_api(self) -> dict[str, Any]:  # type: ignore
        return {
            "arn": self.arn,
            "createdAt": self.created_at.strftime("%d/%m/%Y, %H:%M:%S"),
            "lastUpdatedAt": self.last_updated_at.strftime("%d/%m/%Y, %H:%M:%S"),
            "meshName": self.mesh_name,
            "meshOwner": self.mesh_owner,
            "resourceOwner": self.resource_owner,
            "version": self.version,
            "virtualNodeName": self.virtual_node_name,
        }

    def formatted_for_crud_apis(self) -> dict[str, Any]:  # type: ignore
        return {
            "arn": self.arn,
            "createdAt": self.created_at.strftime("%d/%m/%Y, %H:%M:%S"),
            "lastUpdatedAt": self.last_updated_at.strftime("%d/%m/%Y, %H:%M:%S"),
            "meshOwner": self.mesh_owner,
            "resourceOwner": self.resource_owner,
            "uid": self.uid,
            "version": self.version,
        }


@dataclass
class VirtualNode:
    mesh_name: str
    mesh_owner: str
    metadata: VirtualNodeMetadata
    spec: VirtualNodeSpec
    virtual_node_name: str
    status: Status = field(default_factory=lambda: {"status": "ACTIVE"})
    tags: list[dict[str, str]] = field(default_factory=list)

    def to_dict(self) -> dict[str, Any]:  # type: ignore[misc]
        return clean_dict(
            {
                "meshName": self.mesh_name,
                "metadata": self.metadata.formatted_for_crud_apis(),
                "spec": self.spec.to_dict(),
                "status": self.status,
                "virtualNodeName": self.virtual_node_name,
            }
        )