|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import List, Literal, Optional |
| 4 | + |
| 5 | +from samtranslator.internal.schema_source.common import ( |
| 6 | + BaseModel, |
| 7 | + DictStrAny, |
| 8 | + PassThroughProp, |
| 9 | + ResourceAttributes, |
| 10 | + SamIntrinsicable, |
| 11 | + get_prop, |
| 12 | +) |
| 13 | + |
| 14 | +PROPERTIES_STEM = "sam-resource-capacityprovider" |
| 15 | +VPC_CONFIG_STEM = "sam-property-capacityprovider-vpcconfig" |
| 16 | +INSTANCE_REQUIREMENTS_STEM = "sam-property-capacityprovider-instancerequirements" |
| 17 | +SCALING_CONFIG_STEM = "sam-property-capacityprovider-scalingconfig" |
| 18 | + |
| 19 | +properties = get_prop(PROPERTIES_STEM) |
| 20 | +vpcconfig = get_prop(VPC_CONFIG_STEM) |
| 21 | +instancerequirements = get_prop(INSTANCE_REQUIREMENTS_STEM) |
| 22 | +scalingconfig = get_prop(SCALING_CONFIG_STEM) |
| 23 | + |
| 24 | + |
| 25 | +class VpcConfig(BaseModel): |
| 26 | + # Optional list of security group IDs - supports intrinsic functions for dynamic references |
| 27 | + SecurityGroupIds: Optional[List[SamIntrinsicable[str]]] = vpcconfig("SecurityGroupIds") |
| 28 | + # Required list of subnet IDs - supports intrinsic functions for dynamic VPC configuration |
| 29 | + SubnetIds: List[SamIntrinsicable[str]] = vpcconfig("SubnetIds") |
| 30 | + |
| 31 | + |
| 32 | +class InstanceRequirements(BaseModel): |
| 33 | + # Optional list of CPU architectures - maps to CFN InstanceRequirements.Architecture |
| 34 | + # Uses List[SamIntrinsicable[str]] to support intrinsic functions like !Ref for dynamic architecture values |
| 35 | + Architectures: Optional[List[SamIntrinsicable[str]]] = instancerequirements("Architectures") |
| 36 | + # Optional list of allowed EC2 instance types - maps to CFN InstanceRequirements.AllowedInstanceTypes |
| 37 | + # Uses List[SamIntrinsicable[str]] to support intrinsic functions like !Ref for dynamic instance types |
| 38 | + AllowedTypes: Optional[List[SamIntrinsicable[str]]] = instancerequirements("AllowedTypes") |
| 39 | + # Optional list of excluded EC2 instance types - maps to CFN InstanceRequirements.ExcludedInstanceTypes |
| 40 | + # Uses List[SamIntrinsicable[str]] to support intrinsic functions like !Ref for dynamic instance types |
| 41 | + ExcludedTypes: Optional[List[SamIntrinsicable[str]]] = instancerequirements("ExcludedTypes") |
| 42 | + |
| 43 | + |
| 44 | +class ScalingConfig(BaseModel): |
| 45 | + # Optional maximum instance count - maps to CFN CapacityProviderScalingConfig.MaxVCpuCount |
| 46 | + # Uses SamIntrinsicable[int] to support dynamic scaling limits via parameters/conditions |
| 47 | + MaxVCpuCount: Optional[SamIntrinsicable[int]] = scalingconfig("MaxVCpuCount") |
| 48 | + # Average CPU utilization target (0-100) - maps to CFN ScalingPolicies with CPU metric type |
| 49 | + # When specified, automatically sets ScalingMode to "Manual" |
| 50 | + # Uses SamIntrinsicable[float] to support dynamic scaling targets via parameters/conditions |
| 51 | + AverageCPUUtilization: Optional[SamIntrinsicable[float]] = scalingconfig("AverageCPUUtilization") |
| 52 | + |
| 53 | + |
| 54 | +class Properties(BaseModel): |
| 55 | + # TODO: Change back to passthrough_prop after CloudFormation schema is updated with AWS::Lambda::CapacityProvider |
| 56 | + # Optional capacity provider name - passes through directly to CFN AWS::Lambda::CapacityProvider |
| 57 | + # Uses PassThroughProp because it's a direct 1:1 mapping with no SAM transformation |
| 58 | + # CapacityProviderName: Optional[PassThroughProp] = passthrough_prop( |
| 59 | + # PROPERTIES_STEM, |
| 60 | + # "CapacityProviderName", |
| 61 | + # ["AWS::Lambda::CapacityProvider", "Properties", "CapacityProviderName"], |
| 62 | + # ) |
| 63 | + CapacityProviderName: Optional[PassThroughProp] # TODO: add documentation |
| 64 | + |
| 65 | + # Required VPC configuration - preserves CFN structure, required for EC2 instance networking |
| 66 | + # Uses custom VpcConfig class to validate required SubnetIds while maintaining passthrough behavior |
| 67 | + VpcConfig: VpcConfig = properties("VpcConfig") |
| 68 | + |
| 69 | + # Optional operator role ARN - if not provided, SAM auto-generates one with EC2 management permissions |
| 70 | + OperatorRole: Optional[PassThroughProp] = properties("OperatorRole") |
| 71 | + |
| 72 | + # Optional tags - SAM transforms key-value pairs to CFN Tag objects before passing to CFN |
| 73 | + # Uses DictStrAny to support flexible tag structure with string keys and any values |
| 74 | + Tags: Optional[DictStrAny] = properties("Tags") |
| 75 | + |
| 76 | + # Optional flag to propagate tags to resources created by this capacity provider |
| 77 | + # When true, all tags defined on the capacity provider will be propagated to generated resources |
| 78 | + PropagateTags: Optional[bool] = properties("PropagateTags") |
| 79 | + |
| 80 | + # Optional instance requirements - maps to CFN InstanceRequirements with property name shortening |
| 81 | + # Uses custom InstanceRequirements class because SAM shortens names |
| 82 | + InstanceRequirements: Optional[InstanceRequirements] = properties("InstanceRequirements") |
| 83 | + |
| 84 | + # Optional scaling configuration - maps to CFN CapacityProviderScalingConfig |
| 85 | + # Uses custom ScalingConfig class because SAM renames construct (CapacityProviderScalingConfig→ScalingConfig) |
| 86 | + ScalingConfig: Optional[ScalingConfig] = properties("ScalingConfig") |
| 87 | + |
| 88 | + # TODO: Change back to passthrough_prop after CloudFormation schema is updated with AWS::Lambda::CapacityProvider |
| 89 | + # Optional KMS key ARN - passes through directly to CFN for encryption configuration |
| 90 | + # Uses PassThroughProp because it's a direct 1:1 mapping with no SAM transformation |
| 91 | + # KMSKeyArn: Optional[PassThroughProp] = passthrough_prop( |
| 92 | + # PROPERTIES_STEM, |
| 93 | + # "KMSKeyArn", |
| 94 | + # ["AWS::Lambda::CapacityProvider", "Properties", "KMSKeyArn"], |
| 95 | + # ) |
| 96 | + KMSKeyArn: Optional[PassThroughProp] # TODO: add documentation |
| 97 | + |
| 98 | + |
| 99 | +class Globals(BaseModel): |
| 100 | + # Global VPC configuration - can be inherited by capacity providers if not overridden |
| 101 | + # Uses custom VpcConfig class to validate required SubnetIds while maintaining passthrough behavior |
| 102 | + VpcConfig: Optional[VpcConfig] = properties("VpcConfig") |
| 103 | + |
| 104 | + # Global operator role ARN - can be inherited by capacity providers if not overridden |
| 105 | + OperatorRole: Optional[PassThroughProp] = properties("OperatorRole") |
| 106 | + |
| 107 | + # Global tags - can be inherited and merged with resource-specific tags |
| 108 | + # Uses DictStrAny to support flexible tag structure with string keys and any values |
| 109 | + Tags: Optional[DictStrAny] = properties("Tags") |
| 110 | + |
| 111 | + # Global flag to propagate tags to resources created by capacity providers |
| 112 | + # When true, all tags defined on capacity providers will be propagated to generated resources |
| 113 | + PropagateTags: Optional[bool] = properties("PropagateTags") |
| 114 | + |
| 115 | + # Global instance requirements - can be inherited by capacity providers if not overridden |
| 116 | + # Uses custom InstanceRequirements class because SAM shortens names |
| 117 | + InstanceRequirements: Optional[InstanceRequirements] = properties("InstanceRequirements") |
| 118 | + |
| 119 | + # Global scaling configuration - can be inherited by capacity providers if not overridden |
| 120 | + # Uses custom ScalingConfig class because SAM renames construct (CapacityProviderScalingConfig→ScalingConfig) |
| 121 | + ScalingConfig: Optional[ScalingConfig] = properties("ScalingConfig") |
| 122 | + |
| 123 | + KMSKeyArn: Optional[PassThroughProp] # TODO: add documentation |
| 124 | + |
| 125 | + |
| 126 | +class Resource(ResourceAttributes): |
| 127 | + # Literal type ensures only correct resource type is accepted |
| 128 | + Type: Literal["AWS::Serverless::CapacityProvider"] |
| 129 | + # Required properties using the Properties class for full validation |
| 130 | + Properties: Properties |
0 commit comments