class MU::Cloud::AWS::VPC::Subnet

Subnets are almost a first-class resource. So let's kinda sorta treat them like one. This should only be invoked on objects that already exists in the cloud layer.

Attributes

az[R]
cloud_desc[R]
cloud_id[R]
config[R]
ip_block[R]
mu_name[R]
name[R]

Public Class Methods

new(parent, config) click to toggle source

@param parent [MU::Cloud::AWS::VPC]: The parent VPC of this subnet. @param config [Hash<String>]:

# File modules/mu/providers/aws/vpc_subnet.rb, line 37
def initialize(parent, config)
  @config = MU::Config.manxify(config)
  MU::Cloud::AWS.resourceInitHook(self, @deploy)
  @parent = parent
  @cloud_id = config['cloud_id']
  @credentials ||= config['credentials']
  @mu_name = config['mu_name']
  @name = config['name']
  @deploydata = config # This is a dummy for the sake of describe()
  resp = MU::Cloud::AWS.ec2(region: @region, credentials: @credentials).describe_subnets(subnet_ids: [@cloud_id]).subnets.first
  @az = resp.availability_zone
  @ip_block = resp.cidr_block
  @cloud_desc = resp # XXX this really isn't the cloud implementation's business

end

Public Instance Methods

defaultRoute() click to toggle source

Return the cloud identifier for the default route of this subnet. @return [String,nil]

# File modules/mu/providers/aws/vpc_subnet.rb, line 55
def defaultRoute
  resp = MU::Cloud::AWS.ec2(region: @region, credentials: @credentials).describe_route_tables(
      filters: [{name: "association.subnet-id", values: [@cloud_id]}]
  )
  if resp.route_tables.size == 0 # use default route table for the VPC
    resp = MU::Cloud::AWS.ec2(region: @region, credentials: @credentials).describe_route_tables(
       filters: [{name: "vpc-id", values: [@parent.cloud_id]}]
    )
  end
  resp.route_tables.each { |route_table|
    route_table.routes.each { |route|
      if route.destination_cidr_block =="0.0.0.0/0" and route.state != "blackhole"
        return route.instance_id if !route.instance_id.nil?
        return route.gateway_id if !route.gateway_id.nil?
        return route.vpc_peering_connection_id if !route.vpc_peering_connection_id.nil?
        return route.network_interface_id if !route.network_interface_id.nil?
      end
    }
  }
  return nil
end
private?() click to toggle source

Is this subnet privately-routable only, or public? @return [Boolean]

# File modules/mu/providers/aws/vpc_subnet.rb, line 79
def private?
  return false if @cloud_id.nil?
  resp = MU::Cloud::AWS.ec2(region: @region, credentials: @credentials).describe_route_tables(
      filters: [{name: "association.subnet-id", values: [@cloud_id]}]
  )
  if resp.route_tables.size == 0 # use default route table for the VPC
    resp = MU::Cloud::AWS.ec2(region: @region, credentials: @credentials).describe_route_tables(
       filters: [{name: "vpc-id", values: [@parent.cloud_id]}]
    )
  end
  resp.route_tables.each { |route_table|
    route_table.routes.each { |route|
      return false if !route.gateway_id.nil? and route.gateway_id != "local" # you can have an IgW and route it to a subset of IPs instead of 0.0.0.0/0
      if route.destination_cidr_block == "0.0.0.0/0"
        return true if !route.instance_id.nil?
        return true if route.nat_gateway_id
      end
    }
  }
  return true
end