ec2

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SubnetCIDRWithinVPC

func SubnetCIDRWithinVPC(vpcCIDR, subnetCIDR string) bool

SubnetCIDRWithinVPC returns true if the subnet CIDR block is fully contained within the VPC CIDR block.

func ValidateCIDR

func ValidateCIDR(cidr string) bool

ValidateCIDR checks whether a CIDR string is syntactically valid.

Types

type EC2Service

type EC2Service struct {
	// contains filtered or unexported fields
}

EC2Service is the cloudmock implementation of the AWS EC2 API.

func New

func New(accountID, region string) *EC2Service

New returns a new EC2Service for the given AWS account ID and region. It automatically creates the default VPC (172.31.0.0/16) with 3 subnets, a default route table, security group, NACL, and an attached internet gateway.

func (*EC2Service) Actions

func (s *EC2Service) Actions() []service.Action

Actions returns the list of EC2 API actions supported by this service.

func (*EC2Service) HandleRequest

func (s *EC2Service) HandleRequest(ctx *service.RequestContext) (*service.Response, error)

HandleRequest routes an incoming EC2 request to the appropriate handler. EC2 uses form-encoded POST bodies; the Action may appear in the query string (already parsed into ctx.Params) or in the form-encoded body.

func (*EC2Service) HealthCheck

func (s *EC2Service) HealthCheck() error

HealthCheck always returns nil (no external dependencies).

func (*EC2Service) Name

func (s *EC2Service) Name() string

Name returns the AWS service name used for routing.

func (*EC2Service) ResourceSchemas

func (s *EC2Service) ResourceSchemas() []schema.ResourceSchema

ResourceSchemas returns schemas for EC2 VPC, Subnet, SecurityGroup, and Instance resources.

type ElasticIP

type ElasticIP struct {
	AllocationId       string
	PublicIp           string
	AssociationId      string
	InstanceId         string
	NetworkInterfaceId string
	Tags               map[string]string
}

ElasticIP represents an allocated Elastic IP address.

type IGWAttachment

type IGWAttachment struct {
	VpcId string
	State string // attached
}

IGWAttachment records a VPC attached to an internet gateway.

type Instance

type Instance struct {
	InstanceId       string
	ImageId          string
	InstanceType     string
	SubnetId         string
	VpcId            string
	SecurityGroupIds []string
	KeyName          string
	State            string // pending, running, stopping, stopped, shutting-down, terminated
	LaunchTime       time.Time
	PrivateIpAddress string
	Tags             map[string]string
}

Instance represents an EC2 instance.

type InternetGateway

type InternetGateway struct {
	IgwId       string
	Attachments []IGWAttachment
	Tags        map[string]string
}

InternetGateway represents an EC2 internet gateway.

type NACLEntry

type NACLEntry struct {
	RuleNumber int
	Protocol   string
	RuleAction string // allow, deny
	Egress     bool
	CidrBlock  string
}

NACLEntry represents a single rule in a Network ACL.

type NatGateway

type NatGateway struct {
	NatGatewayId string
	SubnetId     string
	VpcId        string
	AllocationId string
	State        string // available, deleted
	Tags         map[string]string
}

NatGateway represents an EC2 NAT gateway.

type NetworkACL

type NetworkACL struct {
	NetworkAclId string
	VpcId        string
	Entries      []NACLEntry
	IsDefault    bool
	Tags         map[string]string
}

NetworkACL represents a VPC network ACL (auto-created with VPC).

type NetworkInterface

type NetworkInterface struct {
	NetworkInterfaceId string
	SubnetId           string
	VpcId              string
	PrivateIpAddress   string
	SecurityGroupIds   []string
	Status             string // available, in-use
	Tags               map[string]string
}

NetworkInterface represents an EC2 elastic network interface.

type Route

type Route struct {
	DestinationCidrBlock string
	GatewayId            string // igw-*, local, or vpce-*
	NatGatewayId         string
	VpcEndpointId        string
	State                string // active
	Origin               string // CreateRoute or CreateRouteTable
}

Route represents a single entry in a route table.

type RouteTable

type RouteTable struct {
	RouteTableId string
	VpcId        string
	IsMain       bool
	Routes       []Route
	Tags         map[string]string
}

RouteTable represents a VPC route table (auto-created with VPC).

type RouteTableAssociation

type RouteTableAssociation struct {
	AssociationId string
	RouteTableId  string
	SubnetId      string
	Main          bool
}

RouteTableAssociation links a subnet to a route table.

type SGRule

type SGRule struct {
	IpProtocol string // tcp, udp, icmp, -1 (all)
	FromPort   int
	ToPort     int
	CidrBlocks []string // CIDR ranges (e.g. "0.0.0.0/0")
	GroupIds   []string // referenced security group IDs
}

SGRule represents a single inbound or outbound security group rule.

type SecurityGroup

type SecurityGroup struct {
	GroupId      string
	GroupName    string
	Description  string
	VpcId        string
	IngressRules []SGRule
	EgressRules  []SGRule
	Tags         map[string]string
}

SecurityGroup represents a VPC security group (auto-created with VPC).

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store manages all EC2 resources.

func NewStore

func NewStore(accountID, region string) *Store

NewStore returns a new Store for the given account and region.

func (*Store) AcceptVPCPeeringConnection

func (s *Store) AcceptVPCPeeringConnection(pcxId string) (*VPCPeeringConnection, string)

AcceptVPCPeeringConnection accepts a pending peering connection. Returns the updated connection and an error code string.

func (*Store) AllocateAddress

func (s *Store) AllocateAddress() *ElasticIP

AllocateAddress allocates a new Elastic IP in the vpc domain.

func (*Store) AssociateAddress

func (s *Store) AssociateAddress(allocationId, instanceId, networkInterfaceId string) (string, string)

AssociateAddress associates an EIP with an instance or ENI. Returns the association ID and an error code string.

func (*Store) AssociateRouteTable

func (s *Store) AssociateRouteTable(rtbId, subnetId string) (string, string)

AssociateRouteTable links a subnet to a route table. Returns the association ID and an error code string.

func (*Store) AttachInternetGateway

func (s *Store) AttachInternetGateway(igwId, vpcId string) string

AttachInternetGateway attaches an IGW to a VPC. Returns an error code string. Empty string means success.

func (*Store) AuthorizeSecurityGroupEgress

func (s *Store) AuthorizeSecurityGroupEgress(groupId string, rules []SGRule) string

AuthorizeSecurityGroupEgress adds egress rules to a security group.

func (*Store) AuthorizeSecurityGroupIngress

func (s *Store) AuthorizeSecurityGroupIngress(groupId string, rules []SGRule) string

AuthorizeSecurityGroupIngress adds ingress rules to a security group.

func (*Store) CreateInternetGateway

func (s *Store) CreateInternetGateway() *InternetGateway

CreateInternetGateway creates a new detached internet gateway.

func (*Store) CreateNatGateway

func (s *Store) CreateNatGateway(subnetId, allocationId string) (*NatGateway, string)

CreateNatGateway creates a new NAT gateway in the given subnet with the given EIP allocation ID. Returns the gateway and an error code string.

func (*Store) CreateNetworkACL

func (s *Store) CreateNetworkACL(vpcId string) (*NetworkACL, string)

CreateNetworkACL creates a new (non-default) network ACL in the given VPC.

func (*Store) CreateNetworkACLEntry

func (s *Store) CreateNetworkACLEntry(aclId string, entry NACLEntry) string

CreateNetworkACLEntry adds a rule to a NACL. Returns an error code string.

func (*Store) CreateNetworkInterface

func (s *Store) CreateNetworkInterface(subnetId string, sgIds []string) (*NetworkInterface, string)

CreateNetworkInterface creates a new ENI in the given subnet. Assigns a private IP from the subnet's CIDR (10.0.x.x range).

func (*Store) CreateRoute

func (s *Store) CreateRoute(rtbId, destCidr, gatewayId, natGatewayId, vpcEndpointId string) string

CreateRoute adds a new route to a route table. Returns an error code string. Validates that the gateway/NAT GW target exists.

func (*Store) CreateRouteTable

func (s *Store) CreateRouteTable(vpcId string) (*RouteTable, string)

CreateRouteTable creates a new route table in the given VPC with a local route.

func (*Store) CreateSecurityGroup

func (s *Store) CreateSecurityGroup(groupName, description, vpcId string) (*SecurityGroup, string)

CreateSecurityGroup creates a new security group in the given VPC. Returns the new group and an error code string (empty on success).

func (*Store) CreateSubnet

func (s *Store) CreateSubnet(vpcId, cidrBlock, availabilityZone string) (*Subnet, string)

CreateSubnet creates a new subnet within a VPC. Validates that the subnet CIDR falls within the VPC CIDR.

func (*Store) CreateTags

func (s *Store) CreateTags(resourceIds []string, tags map[string]string)

CreateTags applies tags to one or more resources.

func (*Store) CreateVPC

func (s *Store) CreateVPC(cidrBlock string, enableDnsSupport, enableDnsHostnames bool) (*VPC, error)

CreateVPC creates a new VPC with the given CIDR block and also creates the default route table, security group, and network ACL for the VPC.

func (*Store) CreateVPCEndpoint

func (s *Store) CreateVPCEndpoint(vpcId, serviceName, epType string) (*VPCEndpoint, string)

CreateVPCEndpoint creates a new VPC endpoint. Returns the endpoint and an error code string.

func (*Store) CreateVPCPeeringConnection

func (s *Store) CreateVPCPeeringConnection(requesterVpcId, accepterVpcId string) (*VPCPeeringConnection, string)

CreateVPCPeeringConnection creates a new peering connection between two VPCs. Returns the connection and an error code string.

func (*Store) DeleteInternetGateway

func (s *Store) DeleteInternetGateway(igwId string) string

DeleteInternetGateway deletes an IGW. Fails if still attached to a VPC.

func (*Store) DeleteNatGateway

func (s *Store) DeleteNatGateway(natGatewayId string) string

DeleteNatGateway sets a NAT gateway's state to deleted. Returns an error code.

func (*Store) DeleteNetworkACL

func (s *Store) DeleteNetworkACL(aclId string) string

DeleteNetworkACL removes a non-default NACL. Returns an error code string.

func (*Store) DeleteNetworkACLEntry

func (s *Store) DeleteNetworkACLEntry(aclId string, ruleNumber int, egress bool) string

DeleteNetworkACLEntry removes a rule from a NACL. Returns an error code string.

func (*Store) DeleteNetworkInterface

func (s *Store) DeleteNetworkInterface(eniId string) string

DeleteNetworkInterface removes an ENI. Returns an error code string.

func (*Store) DeleteRoute

func (s *Store) DeleteRoute(rtbId, destCidr string) string

DeleteRoute removes a route by destination CIDR from a route table.

func (*Store) DeleteRouteTable

func (s *Store) DeleteRouteTable(rtbId string) string

DeleteRouteTable deletes a route table. Fails if it has subnet associations.

func (*Store) DeleteSecurityGroup

func (s *Store) DeleteSecurityGroup(groupId string) string

DeleteSecurityGroup removes a security group. Returns an error code string or empty on success. Fails if any other security group references this one.

func (*Store) DeleteSubnet

func (s *Store) DeleteSubnet(subnetId string) bool

DeleteSubnet removes a subnet. Returns false if not found.

func (*Store) DeleteTags

func (s *Store) DeleteTags(resourceIds []string, keys []string)

DeleteTags removes specific tag keys from one or more resources.

func (*Store) DeleteVPC

func (s *Store) DeleteVPC(vpcId string) (string, bool)

DeleteVPC removes a VPC and its associated default resources. Returns an error string if the VPC has subnets (DependencyViolation) or is not found.

func (*Store) DeleteVPCEndpoint

func (s *Store) DeleteVPCEndpoint(epId string) string

DeleteVPCEndpoint removes a VPC endpoint. Returns an error code string.

func (*Store) DeleteVPCPeeringConnection

func (s *Store) DeleteVPCPeeringConnection(pcxId string) string

DeleteVPCPeeringConnection deletes a peering connection. Returns an error code.

func (*Store) DetachInternetGateway

func (s *Store) DetachInternetGateway(igwId, vpcId string) string

DetachInternetGateway detaches an IGW from a VPC. Returns an error code string.

func (*Store) DisassociateAddress

func (s *Store) DisassociateAddress(assocId string) string

DisassociateAddress removes an EIP association. Returns an error code string.

func (*Store) DisassociateRouteTable

func (s *Store) DisassociateRouteTable(assocId string) string

DisassociateRouteTable removes a subnet-to-route-table association.

func (*Store) GetSecurityGroup

func (s *Store) GetSecurityGroup(groupId string) (*SecurityGroup, bool)

GetSecurityGroup returns a security group by ID.

func (*Store) GetSubnet

func (s *Store) GetSubnet(subnetId string) (*Subnet, bool)

GetSubnet returns a subnet by ID.

func (*Store) GetVPC

func (s *Store) GetVPC(vpcId string) (*VPC, bool)

GetVPC returns a VPC by ID.

func (*Store) ListAddresses

func (s *Store) ListAddresses(ids []string) []*ElasticIP

ListAddresses returns EIPs, optionally filtered by allocation IDs.

func (*Store) ListInstances

func (s *Store) ListInstances(ids []string, filters map[string][]string) []*Instance

ListInstances returns instances filtered by IDs and/or filters. filters: map of filter-name -> values (any match).

func (*Store) ListInternetGateways

func (s *Store) ListInternetGateways(ids []string, filterVpcId string) []*InternetGateway

ListInternetGateways returns IGWs filtered by IDs and/or attached VPC ID.

func (*Store) ListNatGateways

func (s *Store) ListNatGateways(ids []string, filterSubnetId, filterVpcId, filterState string) []*NatGateway

ListNatGateways returns NAT gateways filtered by IDs, subnet ID, VPC ID, and/or state. Any empty filter is ignored.

func (*Store) ListNetworkACLs

func (s *Store) ListNetworkACLs(ids []string, filterVpcId string) []*NetworkACL

ListNetworkACLs returns NACLs filtered by IDs and/or VPC ID.

func (*Store) ListNetworkInterfaces

func (s *Store) ListNetworkInterfaces(ids []string, filterSubnetId string) []*NetworkInterface

ListNetworkInterfaces returns ENIs filtered by IDs and/or subnet ID.

func (*Store) ListRouteTableAssociations

func (s *Store) ListRouteTableAssociations(rtbId string) []*RouteTableAssociation

ListRouteTableAssociations returns all associations for a given route table.

func (*Store) ListRouteTables

func (s *Store) ListRouteTables(ids []string, filterVpcId string) []*RouteTable

ListRouteTables returns route tables filtered by IDs and/or VPC ID.

func (*Store) ListSecurityGroups

func (s *Store) ListSecurityGroups(ids []string, vpcId, groupName string) []*SecurityGroup

ListSecurityGroups returns security groups filtered by IDs, VPC ID, and/or group name. Any filter that is empty / nil is ignored.

func (*Store) ListSubnets

func (s *Store) ListSubnets(ids []string, vpcId string) []*Subnet

ListSubnets returns subnets, optionally filtered by subnet IDs and/or VPC ID.

func (*Store) ListTags

func (s *Store) ListTags(filters map[string][]string) []TagEntry

ListTags returns all tags matching the provided filters. Supported filters: resource-id, resource-type, key, value.

func (*Store) ListVPCEndpoints

func (s *Store) ListVPCEndpoints(ids []string, filterVpcId string) []*VPCEndpoint

ListVPCEndpoints returns endpoints filtered by IDs and/or VPC ID.

func (*Store) ListVPCs

func (s *Store) ListVPCs(ids []string) []*VPC

ListVPCs returns all VPCs, optionally filtered by a list of IDs.

func (*Store) ModifyVPCAttribute

func (s *Store) ModifyVPCAttribute(vpcId string, dnsSupport, dnsHostnames *bool) bool

ModifyVPCAttribute updates DNS support or DNS hostnames on a VPC.

func (*Store) ReleaseAddress

func (s *Store) ReleaseAddress(allocationId string) string

ReleaseAddress releases an allocated Elastic IP. Returns an error code string.

func (*Store) ReplaceRoute

func (s *Store) ReplaceRoute(rtbId, destCidr, gatewayId, natGatewayId, vpcEndpointId string) string

ReplaceRoute updates the target for an existing route destination.

func (*Store) RevokeSecurityGroupEgress

func (s *Store) RevokeSecurityGroupEgress(groupId string, rules []SGRule) string

RevokeSecurityGroupEgress removes matching egress rules.

func (*Store) RevokeSecurityGroupIngress

func (s *Store) RevokeSecurityGroupIngress(groupId string, rules []SGRule) string

RevokeSecurityGroupIngress removes matching ingress rules.

func (*Store) RunInstances

func (s *Store) RunInstances(imageId, instanceType, subnetId, keyName string, sgIds []string, count int) ([]*Instance, string, string)

RunInstances creates count instances. Returns the created instances and a reservation ID, or an error code.

func (*Store) StartInstances

func (s *Store) StartInstances(ids []string) map[string][2]string

StartInstances sets state to running for the given instance IDs. Only instances in "stopped" state can be started.

func (*Store) StopInstances

func (s *Store) StopInstances(ids []string) map[string][2]string

StopInstances sets state to stopped for the given instance IDs. Only instances in "running" state can be stopped.

func (*Store) TerminateInstances

func (s *Store) TerminateInstances(ids []string) map[string][2]string

TerminateInstances sets state to terminated for the given instance IDs. Returns a map of instanceId -> {previousState, currentState}.

type Subnet

type Subnet struct {
	SubnetId                string
	VpcId                   string
	CidrBlock               string
	AvailabilityZone        string
	State                   string
	AvailableIpAddressCount int
	MapPublicIpOnLaunch     bool
	Tags                    map[string]string
}

Subnet represents an EC2 subnet within a VPC.

type TagEntry

type TagEntry struct {
	ResourceId   string
	ResourceType string
	Key          string
	Value        string
}

TagEntry represents a single tag associated with a resource.

type VPC

type VPC struct {
	VpcId              string
	CidrBlock          string
	State              string
	IsDefault          bool
	OwnerId            string
	EnableDnsSupport   bool
	EnableDnsHostnames bool
	DhcpOptionsId      string
	Tags               map[string]string
}

VPC represents an EC2 Virtual Private Cloud.

type VPCEndpoint

type VPCEndpoint struct {
	VpcEndpointId   string
	VpcId           string
	ServiceName     string
	VpcEndpointType string
	State           string
	Tags            map[string]string
}

VPCEndpoint represents a VPC endpoint.

type VPCPeeringConnection

type VPCPeeringConnection struct {
	PeeringConnectionId string
	RequesterVpcId      string
	AccepterVpcId       string
	Status              string // pending-acceptance, active, deleted
	Tags                map[string]string
}

VPCPeeringConnection represents a VPC peering connection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL