Class: ZAWS::Services::EC2::RouteTable

Inherits:
Object
  • Object
show all
Defined in:
lib/zaws/services/ec2/route_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(shellout, aws, undofile) ⇒ RouteTable

Returns a new instance of RouteTable.



10
11
12
13
14
15
# File 'lib/zaws/services/ec2/route_table.rb', line 10

def initialize(shellout, aws,undofile)
  @shellout=shellout
  @aws=aws
  @undofile=undofile
  @undofile ||= ZAWS::Helper::ZFile.new
end

Instance Method Details

#assoc_subnet(region, textout = nil, verbose = nil, vpcid, routetable, cidrblock, nagios, ufile) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/zaws/services/ec2/route_table.rb', line 145

def assoc_subnet(region, textout=nil, verbose=nil, vpcid, routetable, cidrblock, nagios, ufile)
  if ufile
    @undofile.prepend("zaws route_table delete_assoc_subnet #{routetable} #{cidrblock} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete route table association to subnet', ufile)
  end
  assoc_exists, subnetid, rtableid, rtassocid = subnet_assoc_exists(region, nil, verbose, vpcid, routetable, cidrblock)
  return ZAWS::Helper::Output.binary_nagios_check(assoc_exists, "OK: Route table association to subnet exists.", "CRITICAL: Route table association to subnet does not exist.", textout) if nagios
  if not assoc_exists
    comline="aws --region #{region} ec2 associate-route-table --subnet-id #{subnetid} --route-table-id #{rtableid}"
    assocreturn=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route table associated to subnet.") if assocreturn["AssociationId"]
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route table already associated to subnet. Skipping association.")
  end
  return 0
end

#declare(region, vpcid, externalid, nagios, textout = nil, verbose = nil, ufile = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zaws/services/ec2/route_table.rb', line 37

def declare(region, vpcid, externalid, nagios, textout=nil, verbose=nil, ufile=nil)
  if ufile
    @undofile.prepend("zaws route_table delete #{externalid} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete route table', ufile)
  end
  rtable_exists, rtable_id = exists(region, nil, verbose, vpcid, externalid)
  return ZAWS::Helper::Output.binary_nagios_check(rtable_exists, "OK: Route table exists.", "CRITICAL: Route table does not exist.", textout) if nagios
  if not rtable_exists
    comline="aws --region #{region} ec2 create-route-table --vpc-id #{vpcid}"
    rtable=JSON.parse(@shellout.cli(comline, verbose))
    rtableid=rtable["RouteTable"]["RouteTableId"]
    tagline="aws --region #{region} ec2 create-tags --resources #{rtableid} --tags \"Key=externalid,Value=#{externalid}\""
    tagresult=JSON.parse(@shellout.cli(tagline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route table created with external id: my_route_table.") if tagresult["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route table exists already. Skipping Creation.")
  end
  return 0
end

#declare_propagation_from_gateway(region, textout = nil, verbose = nil, vpcid, routetable, vgatewayid, nagios, ufile) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/zaws/services/ec2/route_table.rb', line 180

def declare_propagation_from_gateway(region, textout=nil, verbose=nil, vpcid, routetable, vgatewayid, nagios, ufile)
  if ufile
    @undofile.prepend("zaws route_table delete_propagation_from_gateway my_route_table #{vgatewayid} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete route propagation', ufile)
  end
  propagation_exists, rtableid = propagation_exists_from_gateway(region, nil, verbose, vpcid, routetable, vgatewayid)
  return ZAWS::Helper::Output.binary_nagios_check(propagation_exists, "OK: Route propagation from gateway enabled.", "CRITICAL: Route propagation from gateway not enabled.", textout) if nagios
  if not propagation_exists
    comline="aws --region #{region} ec2 enable-vgw-route-propagation --route-table-id #{rtableid} --gateway-id #{vgatewayid}"
    propreturn=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route propagation from gateway enabled.") if propreturn["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route propagation from gateway already enabled. Skipping propagation.")
  end
  return 0
end

#declare_route(region, textout = nil, verbose = nil, vpcid, routetable, cidrblock, externalid, nagios, ufile) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zaws/services/ec2/route_table.rb', line 78

def declare_route(region, textout=nil, verbose=nil, vpcid, routetable, cidrblock, externalid, nagios, ufile)
  if ufile
    @undofile.prepend("zaws route_table delete_route #{routetable} #{cidrblock} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete route', ufile)
  end
  # TODO: Route exists already of a different type?
  route_exists, instance_id, rtable_id = route_exists_by_instance(region, nil, verbose, vpcid, routetable, cidrblock, externalid)
  return ZAWS::Helper::Output.binary_nagios_check(route_exists, "OK: Route to instance exists.", "CRITICAL: Route to instance does not exist.", textout) if nagios
  if not route_exists
    comline="aws --region #{region} ec2 create-route --route-table-id #{rtable_id} --destination-cidr-block #{cidrblock} --instance-id #{instance_id}"
    routereturn=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route created to instance.") if routereturn["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route not created to instance. Skip creation.")
  end
  return 0
end

#declare_route_to_gateway(region, textout = nil, verbose = nil, vpcid, routetable, cidrblock, gatewayid, nagios, ufile) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/zaws/services/ec2/route_table.rb', line 118

def declare_route_to_gateway(region, textout=nil, verbose=nil, vpcid, routetable, cidrblock, gatewayid, nagios, ufile)
  if ufile
    @undofile.prepend("zaws route_table delete_route #{routetable} #{cidrblock} --region #{region} --vpcid #{vpcid} $XTRA_OPTS", '#Delete route', ufile)
  end
  # TODO: Route exists already of a different type?
  route_exists, rtable_id = route_exists_by_gatewayid(region, nil, verbose, vpcid, routetable, cidrblock, gatewayid)
  return ZAWS::Helper::Output.binary_nagios_check(route_exists, "OK: Route to gateway exists.", "CRITICAL: Route to gateway does not exist.", textout) if nagios
  if not route_exists
    comline="aws --region #{region} ec2 create-route --route-table-id #{rtable_id} --destination-cidr-block #{cidrblock} --gateway-id #{gatewayid}"
    routereturn=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route created to gateway.") if routereturn["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route to gateway exists. Skipping creation.")
  end
  return 0
end

#delete(region, textout = nil, verbose = nil, vpcid, externalid) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/zaws/services/ec2/route_table.rb', line 56

def delete(region, textout=nil, verbose=nil, vpcid, externalid)
  rtable_exists, rtable_id = exists(region, nil, verbose, vpcid, externalid)
  if rtable_exists
    comline="aws --region #{region} ec2 delete-route-table --route-table-id #{rtable_id}"
    deletion=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route table deleted.") if deletion["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route table does not exist. Skipping deletion.")
  end
end

#delete_assoc_subnet(region, textout = nil, verbose = nil, vpcid, rtable_externalid, cidrblock) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/zaws/services/ec2/route_table.rb', line 161

def delete_assoc_subnet(region, textout=nil, verbose=nil, vpcid, rtable_externalid, cidrblock)
  assoc_exists, subnetid, rtableid, rtassocid = subnet_assoc_exists(region, nil, verbose, vpcid, rtable_externalid, cidrblock)
  if assoc_exists
    comline="aws --region #{region} ec2 disassociate-route-table --association-id #{rtassocid}"
    assocreturn=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route table association to subnet deleted.") if assocreturn["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route table association to subnet not deleted because it does not exist.")
  end
end

#delete_propagation_from_gateway(region, textout = nil, verbose = nil, vpcid, rtable_externalid, vgatewayid) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/zaws/services/ec2/route_table.rb', line 196

def delete_propagation_from_gateway(region, textout=nil, verbose=nil, vpcid, rtable_externalid, vgatewayid)
  propagation_exists, rtableid = propagation_exists_from_gateway(region, nil, verbose, vpcid, rtable_externalid, vgatewayid)
  if propagation_exists
    comline="aws --region #{region} ec2 disable-vgw-route-propagation --route-table-id #{rtableid} --gateway-id #{vgatewayid}"
    assocreturn=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Deleted route propagation from gateway.") if assocreturn["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route propagation from gateway does not exist, skipping deletion.")
  end
end

#delete_route(region, textout = nil, verbose = nil, vpcid, routetable, cidrblock) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zaws/services/ec2/route_table.rb', line 95

def delete_route(region, textout=nil, verbose=nil, vpcid, routetable, cidrblock)
  rtable=JSON.parse(view(region, 'json', nil, verbose, vpcid, routetable))
  val = (rtable["RouteTables"].count == 1) && rtable["RouteTables"][0]["Routes"].any? { |x| x["DestinationCidrBlock"]=="#{cidrblock}" }
  rtable_id = (rtable["RouteTables"].count == 1) ? rtable["RouteTables"][0]["RouteTableId"] : nil
  if val
    comline="aws --region #{region} ec2 delete-route --route-table-id #{rtable_id} --destination-cidr-block #{cidrblock}"
    deletion=JSON.parse(@shellout.cli(comline, verbose))
    ZAWS::Helper::Output.out_change(textout, "Route deleted.") if deletion["return"] == "true"
  else
    ZAWS::Helper::Output.out_no_op(textout, "Route does not exist. Skipping deletion.")
  end
end

#exists(region, textout = nil, verbose = nil, vpcid, externalid) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/zaws/services/ec2/route_table.rb', line 29

def exists(region, textout=nil, verbose=nil, vpcid, externalid)
  rtable=JSON.parse(view(region, 'json', nil, verbose, vpcid, externalid))
  val = (rtable["RouteTables"].count == 1)
  rtable_id = val ? rtable["RouteTables"][0]["RouteTableId"] : nil
  textout.puts val.to_s if textout
  return val, rtable_id
end

#propagation_exists_from_gateway(region, textout = nil, verbose = nil, vpcid, rtable_externalid, vgatewayid) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/zaws/services/ec2/route_table.rb', line 172

def propagation_exists_from_gateway(region, textout=nil, verbose=nil, vpcid, rtable_externalid, vgatewayid)
  rtable=JSON.parse(view(region, 'json', nil, verbose, vpcid, rtable_externalid))
  val = ((rtable["RouteTables"].count == 1) and (rtable["RouteTables"][0]["PropagatingVgws"].any? { |x| x["GatewayId"]=="#{vgatewayid}" }))
  rtableid = (rtable["RouteTables"].count == 1) ? rtable["RouteTables"][0]["RouteTableId"] : nil
  textout.puts val.to_s if textout
  return val, rtableid
end

#route_exists_by_gatewayid(region, textout = nil, verbose = nil, vpcid, routetable, cidrblock, gatewayid) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/zaws/services/ec2/route_table.rb', line 108

def route_exists_by_gatewayid(region, textout=nil, verbose=nil, vpcid, routetable, cidrblock, gatewayid)
  # Returns the answer, route_table_id
  rtable=JSON.parse(view(region, 'json', nil, verbose, vpcid, routetable))
  val = (rtable["RouteTables"].count == 1) && rtable["RouteTables"][0]["Routes"].any? { |x| x["DestinationCidrBlock"]=="#{cidrblock}" && x["GatewayId"]=="#{gatewayid}" }
  rtable_id = (rtable["RouteTables"].count == 1) ? rtable["RouteTables"][0]["RouteTableId"] : nil
  textout.puts val.to_s if textout
  return val, rtable_id
end

#route_exists_by_instance(region, textout = nil, verbose = nil, vpcid, routetable, cidrblock, externalid) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/zaws/services/ec2/route_table.rb', line 67

def route_exists_by_instance(region, textout=nil, verbose=nil, vpcid, routetable, cidrblock, externalid)
  # Returns the answer, instance_id, route_table_id
  instance_id=@aws.ec2.compute.instance_id_by_external_id(region, externalid, vpcid, nil, verbose)
  return false, nil, nil if not instance_id
  rtable=JSON.parse(view(region, 'json', nil, verbose, vpcid, routetable))
  val = (rtable["RouteTables"].count == 1) && rtable["RouteTables"][0]["Routes"].any? { |x| x["DestinationCidrBlock"]=="#{cidrblock}" && x["InstanceId"]=="#{instance_id}" }
  rtable_id = (rtable["RouteTables"].count == 1) ? rtable["RouteTables"][0]["RouteTableId"] : nil
  textout.puts val.to_s if textout
  return val, instance_id, rtable_id
end

#subnet_assoc_exists(region, textout = nil, verbose = nil, vpcid, rtable_externalid, cidrblock) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/zaws/services/ec2/route_table.rb', line 135

def subnet_assoc_exists(region, textout=nil, verbose=nil, vpcid, rtable_externalid, cidrblock)
  rtable=JSON.parse(view(region, 'json', nil, verbose, vpcid, rtable_externalid))
  subnetid=@aws.ec2.subnet.id_by_cidrblock(region, verbose, vpcid, cidrblock)
  val = ((not subnetid.nil?) and (rtable["RouteTables"].count == 1) and (rtable["RouteTables"][0]["Associations"].any? { |x| x["SubnetId"]=="#{subnetid}" }))
  rtassocid= (val and rtable["RouteTables"].count == 1) ? (rtable["RouteTables"][0]["Associations"].select { |x| x["SubnetId"]=="#{subnetid}" })[0]["RouteTableAssociationId"] : nil
  rtableid = (rtable["RouteTables"].count == 1) ? rtable["RouteTables"][0]["RouteTableId"] : nil
  textout.puts val.to_s if textout
  return val, subnetid, rtableid, rtassocid
end

#view(region, view, textout = nil, verbose = nil, vpcid = nil, externalid = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/zaws/services/ec2/route_table.rb', line 17

def view(region, view, textout=nil, verbose=nil, vpcid=nil, externalid=nil)
  comline="aws --output #{view} --region #{region} ec2 describe-route-tables"
  if vpcid || externalid
    comline = comline + " --filter"
  end
  comline = comline + " \"Name=vpc-id,Values=#{vpcid}\"" if vpcid
  comline = comline + " \"Name=tag:externalid,Values=#{externalid}\"" if externalid
  rtables=@shellout.cli(comline, verbose)
  verbose.puts(rtables) if verbose
  return rtables
end