Module: PuppetX::IntechWIFI::Declare_Environment_Resources::SubnetHelpers

Defined in:
lib/puppet_x/intechwifi/declare_environment_resources.rb

Class Method Summary collapse

Class Method Details

.CalculateSubnetData(name, network, zones) ⇒ Object



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/puppet_x/intechwifi/declare_environment_resources.rb', line 863

def self.CalculateSubnetData(name, network, zones)
  vpc_cidr_size = network['cidr'].split('/')[1].to_i
  total_weight = zones.keys.map{|x| ZoneHelpers.ZoneValue(zones[x],'ipaddr_weighting')}.reduce{|t, v| t = t + v}
  azs = network['availability'].length

  base_cidr = CidrMaths.CidrToLong(network['cidr'])

  cidr_data = zones.keys.map { |x|
    #  Calculate the cidr size for each zone
    [x, CidrMaths.CalculateBlockSize(vpc_cidr_size, ZoneHelpers.ZoneValue(zones[x],'ipaddr_weighting'), total_weight, azs) ]
  }.each{ |x|
    # validate CIDR is viable.
    raise CidrMaths::CidrSizeTooSmallForSubnet if x[1] > 28
  }.sort{ |a, b|
    #  Sort the zones into decending cidr size order.
    a[1] <=> b[1]
  }.map { |x|
    network['availability'].map.with_index{ |az, i|
      cidr = base_cidr
      base_cidr += CidrMaths.IpAddrsInCidrBlock(x[1])
      { :zone => x[0], :az => az, :cidr => CidrMaths.LongToCidr(cidr, x[1]), :index => i, :name => SubnetHelpers.GenerateSubnetName(name, zones, x[0], az, i) }
    }
  }.flatten
end

.GenerateSubnetName(name, zones, zone, az, index) ⇒ Object



888
889
890
891
892
893
894
895
# File 'lib/puppet_x/intechwifi/declare_environment_resources.rb', line 888

def self.GenerateSubnetName(name, zones, zone, az, index)
  sprintf(ZoneHelpers.ZoneValue(zones[zone], 'format'), {
      :vpc => name,
      :az  => az,
      :index => index,
      :zone => zone,
  })
end

.GenerateSubnetResources(name, status, region, network, zones, scratch, tags) ⇒ Object



897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/puppet_x/intechwifi/declare_environment_resources.rb', line 897

def self.GenerateSubnetResources(name, status, region, network, zones, scratch, tags)
  {
      'resource_type' => "subnet",
      'resources' => scratch[:subnet_data].reduce({}) do |subnets, sn_data|

        subnet_name = GenerateSubnetName(name, zones, sn_data[:zone], sn_data[:az], sn_data[:index])

        subnets.merge(
            {
                subnet_name => {
                    :ensure => status,
                    :region => region,
                    :vpc => name,
                    :availability_zone => sn_data[:az],
                    :cidr => sn_data[:cidr],
                    :tags => tags,
                    :route_table => scratch[:route_table_data].select{|rt_data|
                      # Is this route table for this subnet?
                      rt_data[:zone] == sn_data[:zone]
                    }.reduce(nil){|memo, rt_data|
                      result = nil

                      # First choice, if memo matches the AZ, we ain't changing our choice
                      result = memo if !memo.nil? and memo[:az] == sn_data[:az]

                      # Second choice, if the route applies to all AZ's then use it
                      result = rt_data if result.nil? and rt_data[:az].nil?

                      # Third choice, we have matching AZ's
                      result = rt_data if result.nil? and rt_data[:az] == sn_data[:az]

                      # Last choice if rt_data matches the first AZ then we can use this.
                      result = rt_data if result.nil? and rt_data[:az] == network['availability'][0]

                      # Still here? Then we keep our current choice
                      result = memo if result.nil?

                      result

                    }[:name],
                    :public_ip => sn_data[:zone] == "public"
                }
            }
        )
      end
  }
end