Puppet Function: define_network_resources

Defined in:
lib/puppet/functions/define_network_resources.rb
Function type:
Ruby 4.x API

Overview

define_network_resources(Any $status, Any $vpc_data, Any $zones, Any $security_group_rules)Any

Parameters:

  • status (Any)
  • vpc_data (Any)
  • zones (Any)
  • security_group_rules (Any)

Returns:

  • (Any)


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
# File 'lib/puppet/functions/define_network_resources.rb', line 4

Puppet::Functions.create_function('define_network_resources') do
  def define_network_resources(status, vpc_data, zones, security_group_rules)
    # First, lets calculate the facts we need to have:

    facts = {
        :internet_gateway => zones.any?{|x| x['public_ip']},
        :nat_gateway => zones.any?{|x| !x['nat'].nil?},
        :availabilty => vpc_data['availability'],
        :public_zone_label => zones.reduce(nil){|label, x| (PuppetX::IntechWIFI::Logical.logical_true(x["public_ip"]) and label.nil?) ? x["label"] : label}
    }

    vpc_security_group = status == 'present' ? {
        vpc_data['name'] => {
            :ensure => status,
            :region => vpc_data['region'],
            :vpc   => vpc_data['name'],
            :tags => vpc_data['tags'],

        }
    } : { }

    vpc_security_group_rules = status == 'present' ? {
        vpc_data['name'] => {
            :ensure => 'present',
            :region => vpc_data['region'],
            :in => security_group_rules[:ingress],
            :out => security_group_rules[:egress],
        }
    } : { }

    nat_gateways = facts[:nat_gateway] ?
        zones.reduce({}){ | natgw, zone |
            availability = zone['availability'].nil? ? facts[:availabilty] : zone['availability']

            if !zone['nat'].nil?
                nats = zone['nat'].kind_of?(Array) ? zone['nat'] : [ zone['nat'] ]

                availability[0..(nats.length-1)].each_with_index { |az, index|
                    nat_gateway_name = sprintf(facts[:public_zone_label], {
                        :vpc => vpc_data['name'],
                        :az  => az,
                        :index => index
                    })
                    natgw[nat_gateway_name] = {
                        'ensure' => status,
                        'region' => vpc_data['region'],
                        'elastic_ip' => nats[index],
                        'internet_gateway' => vpc_data['name'],

                    }
                }
            end

            natgw
        } : { }

    internet_gateway = {
        vpc_data['name'] => {
            :ensure => (facts[:internet_gateway] and status == 'present') ? 'present' : 'absent',
            :region => vpc_data['region'],
            :vpc   => vpc_data['name'],
            :nat_gateways => nat_gateways.keys,
        }
    }

    #
    # This is the Data structure we are returning...
    #

    [
        {
            'resource_type' => "vpc",
            'resources' => {
                vpc_data['name'] => {
                    :ensure => status,
                    :region => vpc_data['region'],
                    :cidr   => vpc_data['cidr'],
                    :tags => vpc_data['tags'],
                    :dns_hostnames => vpc_data['dns_hostnames'],
                    :dns_resolution => vpc_data['dns_resolution'],
                }
            }
        },
        {
            #  Public subnets use the vpc default route table.
            #  We only need to create route tables for nat gateways.
            'resource_type' => "route_table",
            'resources' => zones.reduce({}) { |route_tables, zone|
                availability = zone['availability'].nil? ? facts[:availabilty] : zone['availability']

                if !zone['nat'].nil?
                    nats = zone['nat'].kind_of?(Array) ? zone['nat'] : [ zone['nat'] ]

                    availability[0..(nats.length-1)].each.with_index { |az, index|
                        route_table_name = sprintf(zone['label'], {
                            :vpc => vpc_data['name'],
                            :az  => az,
                            :index => index
                        })
                        route_tables[route_table_name] = {
                            'ensure' => status,
                            'region' => vpc_data['region'],
                            'vpc' => vpc_data['name'],
                            'tags' => vpc_data['tags'],
                        }
                    }
                end
                route_tables
            }
        },
        {
            'resource_type' => "subnet",
            'resources' => zones.reduce({}) { |subnets, zone|
                availability = zone['availability'].nil? ? facts[:availabilty] : zone['availability']

                nats = zone['nat'].nil? ? [] : zone['nat'].kind_of?(Array) ? zone['nat'] : [ zone['nat'] ]

                subnets_without_nat = availability[nats.length..-1].map.with_index { |az, index|
                  sprintf(zone['label'], {
                      :vpc => vpc_data['name'],
                      :az  => az,
                      :index => index
                  })
                }

                default_route_table = nats.length > 0 ? sprintf(zone['label'], {
                    :vpc => vpc_data['name'],
                    :az  => availability[0],
                    :index => 0
                }) : vpc_data['name']


                availability.each.with_index { |az, index|
                    subnet_name = sprintf(zone['label'], {
                        :vpc => vpc_data['name'],
                        :az  => az,
                        :index => index
                    })

                    subnets[subnet_name] = {
                        'ensure' => status,
                        'region' => vpc_data['region'],
                        'vpc'    => vpc_data['name'],
                        'availability_zone' => az,
                        'cidr'   => PuppetX::IntechWIFI::Network_Rules.MakeCidr(zone['cidr'], index, availability.length),
                        'tags' => vpc_data['tags'],
                        'route_table' => subnets_without_nat.include?(subnet_name) ? default_route_table : subnet_name,
                        'public_ip' => zone['public_ip'].nil? ? false : zone['public_ip']
                    }

                }
                subnets
            }
        },
        {
            'resource_type' => "security_group",
            'resources' => vpc_security_group
        },
        {
            'resource_type' => "security_group_rules",
            'resources' => vpc_security_group_rules
        },
        {
            'resource_type' => "internet_gateway",
            'resources' => internet_gateway
        },
        {
            'resource_type' => "nat_gateway",
            'resources' => nat_gateways
        },
        {
            'resource_type' => "route_table_routes",
            'resources' => zones.reduce({}) { |route_table_routes, zone|
                availability = zone['availability'].nil? ? facts[:availabilty] : zone['availability']

                if !zone['nat'].nil?
                    nats = zone['nat'].kind_of?(Array) ? zone['nat'] : [ zone['nat'] ]

                    extra_subnets = availability[nats.length..-1].map.with_index { |az, index|
                      sprintf(zone['label'], {
                          :vpc => vpc_data['name'],
                          :az  => az,
                          :index => index
                      })
                    }

                    availability[1..(nats.length-1)].each.with_index { |az, index|
                        route_table_routes_name = sprintf(zone['label'], {
                            :vpc => vpc_data['name'],
                            :az  => az,
                            :index => index
                        })
                        nat_gateway_name = sprintf(facts[:public_zone_label], {
                            :vpc => vpc_data['name'],
                            :az  => az,
                            :index => index
                        })
                        route_table_routes[route_table_routes_name] = {
                            'ensure' => status,
                            'region' => vpc_data['region'],
                            'routes' => [
                                "0.0.0.0/0|nat|#{nat_gateway_name}",
                                vpc_data['routes']
                            ].flatten.select{|x| !x.nil?}
                        }
                    }

                    route_table_routes_name = zone['label'] % {
                        :vpc => vpc_data['name'],
                        :az  => availability[0],
                        :index => 0,
                    }
                    nat_gateway_name = sprintf(facts[:public_zone_label], {
                        :vpc => vpc_data['name'],
                        :az  => availability[0],
                        :index => 0,
                    })

                    route_table_routes[route_table_routes_name] = {
                        'ensure' => status,
                        'region' => vpc_data['region'],
                        'routes' => [
                            "0.0.0.0/0|nat|#{nat_gateway_name}",
                            vpc_data['routes']
                        ].flatten.select{|x| !x.nil?}
                    }
                end
                route_table_routes
            }
        }
    ]

  end
end