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
|
import random
import uuid
from datetime import datetime, timezone
from typing import Any, Optional
from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.utilities.paginator import paginate
from moto.utilities.tagging_service import TaggingService
from moto.vpclattice.exceptions import (
ResourceNotFoundException,
ValidationException,
)
class VPCLatticeService(BaseModel):
def __init__(
self,
region: str,
account_id: str,
auth_type: str,
certificate_arn: Optional[str],
client_token: str,
custom_domain_name: Optional[str],
name: str,
tags: Optional[dict[str, str]],
) -> None:
self.id: str = f"svc-{str(uuid.uuid4())[:17]}"
self.auth_type: str = auth_type
self.certificate_arn: str = certificate_arn or ""
self.client_token: str = client_token
self.custom_domain_name: str = custom_domain_name or ""
self.dns_entry: VPCLatticeDNSEntry = VPCLatticeDNSEntry(
region, self.id, self.custom_domain_name
)
self.name: str = name
self.arn: str = f"arn:aws:vpc-lattice:{region}:{account_id}:service/{self.id}"
self.status: str = "ACTIVE"
self.tags: dict[str, str] = tags or {}
def to_dict(self) -> dict[str, Any]:
return {
"arn": self.arn,
"authType": self.auth_type,
"certificateArn": self.certificate_arn,
"customDomainName": self.custom_domain_name,
"dnsEntry": self.dns_entry.to_dict(),
"id": self.id,
"name": self.name,
"status": self.status,
}
class VPCLatticeServiceNetwork(BaseModel):
def __init__(
self,
region: str,
account_id: str,
auth_type: str,
client_token: str,
name: str,
sharing_config: Optional[dict[str, Any]],
tags: Optional[dict[str, str]],
) -> None:
self.auth_type: str = auth_type
self.client_token: str = client_token
self.id: str = f"sn-{str(uuid.uuid4())[:17]}"
self.name: str = name
self.arn: str = (
f"arn:aws:vpc-lattice:{region}:{account_id}:servicenetwork/{self.id}"
)
self.sharing_config: dict[str, Any] = sharing_config or {}
self.tags: dict[str, str] = tags or {}
def to_dict(self) -> dict[str, Any]:
return {
"arn": self.arn,
"authType": self.auth_type,
"id": self.id,
"name": self.name,
"sharingConfig": self.sharing_config,
}
class VPCLatticeServiceNetworkVpcAssociation(BaseModel):
def __init__(
self,
region: str,
account_id: str,
client_token: str,
security_group_ids: Optional[list[str]],
service_network_identifier: str,
tags: Optional[dict[str, str]],
vpc_identifier: str,
) -> None:
self.id: str = f"snva-{service_network_identifier[:4]}-{vpc_identifier[:4]}"
self.arn: str = f"arn:aws:vpc-lattice:{region}:{account_id}:servicenetworkvpcassociation/{self.id}"
self.created_by: str = "user"
self.security_group_ids: list[str] = security_group_ids or []
self.status: str = "ACTIVE"
self.tags: dict[str, str] = tags or {}
def to_dict(self) -> dict[str, Any]:
return {
"arn": self.arn,
"createdBy": self.created_by,
"id": self.id,
"securityGroupIds": self.security_group_ids,
"status": self.status,
"tags": self.tags,
}
class VPCLatticeRule(BaseModel):
def __init__(
self,
region: str,
account_id: str,
action: dict[str, Any],
client_token: str,
listener_identifier: str,
match: dict[str, Any],
name: str,
priority: int,
service_identifier: str,
tags: dict[str, str],
) -> None:
self.action: dict[str, Any] = action or {}
self.id: str = f"rule-[0-9a-z]{17}"
self.arn: str = (
f"arn:aws:vpc-lattice:{region}:{account_id}:service/{service_identifier}"
f"/listener/listener-{listener_identifier}/rule/{self.id}"
)
self.client_token: str = client_token
self.listener_identifier: str = listener_identifier
self.match: dict[str, Any] = match or {}
self.name: str = name
self.priority: int = priority
self.service_identifier: str = service_identifier
self.tags: dict[str, str] = tags or {}
def to_dict(self) -> dict[str, Any]:
return {
"arn": self.arn,
"id": self.id,
"name": self.name,
"priority": self.priority,
"action": self.action,
"match": self.match,
"serviceIdentifier": self.service_identifier,
"listenerIdentifier": self.listener_identifier,
"tags": self.tags,
}
class VPCLatticeDNSEntry:
def __init__(
self,
region_name: str,
service_id: str,
custom_domain_name: Optional[str] = None,
) -> None:
self.domain_name: str = (
custom_domain_name or f"{service_id}.{region_name}.vpclattice.amazonaws.com"
)
self.hosted_zone_id: str = f"Z{random.randint(100000, 999999)}XYZ"
def to_dict(self) -> dict[str, str]:
return {"domainName": self.domain_name, "hostedZoneId": self.hosted_zone_id}
class VPCLatticeAccessLogSubscription(BaseModel):
def __init__(
self,
region: str,
account_id: str,
destinationArn: str,
resourceArn: str,
resourceId: str, # resourceIdentifier
serviceNetworkLogType: Optional[str],
tags: Optional[dict[str, str]],
) -> None:
self.id: str = f"als-{str(uuid.uuid4())[:17]}"
self.arn: str = (
f"arn:aws:vpc-lattice:{region}:{account_id}:accesslogsubscription/{self.id}"
)
self.created_at = datetime.now(timezone.utc).isoformat()
self.destinationArn = destinationArn
self.last_updated_at = datetime.now(timezone.utc).isoformat()
self.resourceArn = resourceArn
self.resourceId = resourceId
self.serviceNetworkLogType = serviceNetworkLogType or "SERVICE"
self.tags = tags or {}
def to_dict(self) -> dict[str, Any]:
return {
"arn": self.arn,
"createdAt": self.created_at,
"destinationArn": self.destinationArn,
"id": self.id,
"lastUpdatedAt": self.last_updated_at,
"resourceArn": self.resourceArn,
"resourceId": self.resourceId,
"serviceNetworkLogType": self.serviceNetworkLogType,
"tags": self.tags,
}
class VPCLatticeBackend(BaseBackend):
PAGINATION_MODEL = {
"list_services": {
"input_token": "next_token",
"limit_key": "max_results",
"limit_default": 50,
"unique_attribute": "id",
},
"list_service_networks": {
"input_token": "next_token",
"limit_key": "max_results",
"limit_default": 50,
"unique_attribute": "id",
},
}
def __init__(self, region_name: str, account_id: str) -> None:
super().__init__(region_name, account_id)
self.services: dict[str, VPCLatticeService] = {}
self.service_networks: dict[str, VPCLatticeServiceNetwork] = {}
self.service_network_vpc_associations: dict[
str, VPCLatticeServiceNetworkVpcAssociation
] = {}
self.rules: dict[str, VPCLatticeRule] = {}
self.tagger: TaggingService = TaggingService()
self.access_log_subscriptions: dict[str, VPCLatticeAccessLogSubscription] = {}
def create_service(
self,
auth_type: str,
certificate_arn: Optional[str],
client_token: str,
custom_domain_name: Optional[str],
name: str,
tags: Optional[dict[str, str]],
) -> VPCLatticeService:
service = VPCLatticeService(
self.region_name,
self.account_id,
auth_type,
certificate_arn,
client_token,
custom_domain_name,
name,
tags,
)
self.services[service.id] = service
self.tag_resource(service.arn, tags or {})
return service
def get_service(self, service_identifier: str) -> VPCLatticeService:
service = self.services.get(service_identifier)
if not service:
raise ResourceNotFoundException(service_identifier)
return service
@paginate(pagination_model=PAGINATION_MODEL)
def list_services(self) -> list[VPCLatticeService]:
return list(self.services.values())
def create_service_network(
self,
auth_type: str,
client_token: str,
name: str,
sharing_config: Optional[dict[str, Any]],
tags: Optional[dict[str, str]],
) -> VPCLatticeServiceNetwork:
"""
WARNING: This method currently does NOT fail if there is a disassociation in progress.
"""
sn = VPCLatticeServiceNetwork(
self.region_name,
self.account_id,
auth_type,
client_token,
name,
sharing_config,
tags,
)
self.service_networks[sn.id] = sn
self.tag_resource(sn.arn, tags or {})
return sn
def get_service_network(
self, service_network_identifier: str
) -> VPCLatticeServiceNetwork:
service_network = self.service_networks.get(service_network_identifier)
if not service_network:
raise ResourceNotFoundException(service_network_identifier)
return service_network
@paginate(pagination_model=PAGINATION_MODEL)
def list_service_networks(self) -> list[VPCLatticeServiceNetwork]:
return list(self.service_networks.values())
def create_service_network_vpc_association(
self,
client_token: str,
security_group_ids: Optional[list[str]],
service_network_identifier: str,
tags: Optional[dict[str, str]],
vpc_identifier: str,
) -> VPCLatticeServiceNetworkVpcAssociation:
assoc = VPCLatticeServiceNetworkVpcAssociation(
self.region_name,
self.account_id,
client_token,
security_group_ids,
service_network_identifier,
tags,
vpc_identifier,
)
self.service_network_vpc_associations[assoc.id] = assoc
self.tag_resource(assoc.arn, tags or {})
return assoc
def create_rule(
self,
action: dict[str, Any],
client_token: str,
listener_identifier: str,
match: dict[str, Any],
name: str,
priority: int,
service_identifier: str,
tags: dict[str, str],
) -> VPCLatticeRule:
rule = VPCLatticeRule(
self.region_name,
self.account_id,
action,
client_token,
listener_identifier,
match,
name,
priority,
service_identifier,
tags,
)
self.rules[rule.id] = rule
self.tag_resource(rule.arn, tags or {})
return rule
def tag_resource(self, resource_arn: str, tags: dict[str, str]) -> None:
tags_input = self.tagger.convert_dict_to_tags_input(tags or {})
self.tagger.tag_resource(resource_arn, tags_input)
def list_tags_for_resource(self, resource_arn: str) -> dict[str, str]:
return self.tagger.get_tag_dict_for_resource(resource_arn)
def untag_resource(self, resource_arn: str, tag_keys: list[str]) -> None:
if not isinstance(tag_keys, list):
tag_keys = [tag_keys]
self.tagger.untag_resource_using_names(resource_arn, tag_keys)
def create_access_log_subscription(
self,
resourceIdentifier: str,
destinationArn: str,
client_token: Optional[str],
serviceNetworkLogType: Optional[str],
tags: Optional[dict[str, str]],
) -> VPCLatticeAccessLogSubscription:
resource: Any = None
if resourceIdentifier.startswith("sn-"):
resource = self.service_networks.get(resourceIdentifier)
elif resourceIdentifier.startswith("svc-"):
resource = self.services.get(resourceIdentifier)
else:
raise ValidationException(
"Invalid parameter resourceIdentifier, must start with 'sn-' or 'svc-'"
)
if not resource:
raise ResourceNotFoundException(f"Resource {resourceIdentifier} not found")
sub = VPCLatticeAccessLogSubscription(
self.region_name,
self.account_id,
destinationArn,
resource.arn,
resource.id,
serviceNetworkLogType,
tags,
)
self.access_log_subscriptions[sub.id] = sub
return sub
def get_access_log_subscription(
self, accessLogSubscriptionIdentifier: str
) -> VPCLatticeAccessLogSubscription:
sub = self.access_log_subscriptions.get(accessLogSubscriptionIdentifier)
if not sub:
raise ResourceNotFoundException(
f"Access Log Subscription {accessLogSubscriptionIdentifier} not found"
)
return sub
def list_access_log_subscriptions(
self,
resourceIdentifier: str,
maxResults: Optional[int] = None,
nextToken: Optional[str] = None,
) -> list[VPCLatticeAccessLogSubscription]:
return [
sub
for sub in self.access_log_subscriptions.values()
if sub.resourceId == resourceIdentifier
][:maxResults]
def update_access_log_subscription(
self,
accessLogSubscriptionIdentifier: str,
destinationArn: str,
) -> VPCLatticeAccessLogSubscription:
sub = self.access_log_subscriptions.get(accessLogSubscriptionIdentifier)
if not sub:
raise ResourceNotFoundException(
f"Access Log Subscription {accessLogSubscriptionIdentifier} not found"
)
sub.destinationArn = destinationArn
sub.last_updated_at = datetime.now(timezone.utc).isoformat()
return sub
def delete_access_log_subscription(
self, accessLogSubscriptionIdentifier: str
) -> None:
sub = self.access_log_subscriptions.get(accessLogSubscriptionIdentifier)
if not sub:
raise ResourceNotFoundException(
f"Access Log Subscription {accessLogSubscriptionIdentifier} not found"
)
del self.access_log_subscriptions[accessLogSubscriptionIdentifier]
vpclattice_backends: BackendDict[VPCLatticeBackend] = BackendDict(
VPCLatticeBackend, "vpc-lattice"
)
|