Class: HP::Cloud::SubnetHelper

Inherits:
BaseHelper show all
Defined in:
lib/hpcloud/subnet_helper.rb

Instance Attribute Summary collapse

Attributes inherited from BaseHelper

#connection, #cstatus, #fog

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseHelper

#is_valid?, #set_error, #to_hash

Constructor Details

#initialize(connection, foggy = nil) ⇒ SubnetHelper

Returns a new instance of SubnetHelper.



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
# File 'lib/hpcloud/subnet_helper.rb', line 34

def initialize(connection, foggy = nil)
  super(connection, foggy)
  if foggy.nil?
    return
  end
  @id = foggy.id
  @name = foggy.name
  @tenant_id = foggy.tenant_id
  @network_id = foggy.network_id
  @cidr = foggy.cidr
  @ip_version = foggy.ip_version
  @dns_nameservers = foggy.dns_nameservers
  @nameservers = ""
  if foggy.dns_nameservers.kind_of? Array
    @nameservers = foggy.dns_nameservers.join(",")
  end
  @allocation_pools = foggy.allocation_pools
  @allocation = ""
  if foggy.allocation_pools.kind_of? Array
    foggy.allocation_pools.each { |hsh|
      if hsh.kind_of? Hash
        @allocation += "," unless @allocation.empty?
        @allocation += "#{hsh['start']}-#{hsh['end']}"
      end
    }
  end
  @host_routes = foggy.host_routes
  @routes = ""
  if foggy.host_routes.kind_of? Array
    foggy.host_routes.each{ |route|
      if route.kind_of? Hash
        @routes += ';' unless @routes.empty?
        @routes += "#{route['destination']},#{route['nexthop']}"
      end
    }
  end
  @gateway = foggy.gateway_ip
  @dhcp = foggy.enable_dhcp
end

Instance Attribute Details

#allocationObject

Returns the value of attribute allocation.



28
29
30
# File 'lib/hpcloud/subnet_helper.rb', line 28

def allocation
  @allocation
end

#allocation_poolsObject

Returns the value of attribute allocation_pools.



27
28
29
# File 'lib/hpcloud/subnet_helper.rb', line 27

def allocation_pools
  @allocation_pools
end

#cidrObject

Returns the value of attribute cidr.



26
27
28
# File 'lib/hpcloud/subnet_helper.rb', line 26

def cidr
  @cidr
end

#dhcpObject

Returns the value of attribute dhcp.



27
28
29
# File 'lib/hpcloud/subnet_helper.rb', line 27

def dhcp
  @dhcp
end

#dns_nameserversObject

Returns the value of attribute dns_nameservers.



26
27
28
# File 'lib/hpcloud/subnet_helper.rb', line 26

def dns_nameservers
  @dns_nameservers
end

#gatewayObject

Returns the value of attribute gateway.



27
28
29
# File 'lib/hpcloud/subnet_helper.rb', line 27

def gateway
  @gateway
end

#host_routesObject

Returns the value of attribute host_routes.



27
28
29
# File 'lib/hpcloud/subnet_helper.rb', line 27

def host_routes
  @host_routes
end

#idObject

Returns the value of attribute id.



25
26
27
# File 'lib/hpcloud/subnet_helper.rb', line 25

def id
  @id
end

#ip_versionObject

Returns the value of attribute ip_version.



26
27
28
# File 'lib/hpcloud/subnet_helper.rb', line 26

def ip_version
  @ip_version
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/hpcloud/subnet_helper.rb', line 25

def name
  @name
end

#nameserversObject

Returns the value of attribute nameservers.



28
29
30
# File 'lib/hpcloud/subnet_helper.rb', line 28

def nameservers
  @nameservers
end

#network_idObject

Returns the value of attribute network_id.



26
27
28
# File 'lib/hpcloud/subnet_helper.rb', line 26

def network_id
  @network_id
end

#routesObject

Returns the value of attribute routes.



28
29
30
# File 'lib/hpcloud/subnet_helper.rb', line 28

def routes
  @routes
end

#tenant_idObject

Returns the value of attribute tenant_id.



25
26
27
# File 'lib/hpcloud/subnet_helper.rb', line 25

def tenant_id
  @tenant_id
end

Class Method Details

.get_keysObject



30
31
32
# File 'lib/hpcloud/subnet_helper.rb', line 30

def self.get_keys()
  return [ "id", "name", "network_id", "cidr", "nameservers", "routes", "gateway", "dhcp" ]
end

Instance Method Details

#destroyObject



165
166
167
# File 'lib/hpcloud/subnet_helper.rb', line 165

def destroy
  @connection.network.delete_subnet(@id)
end

#saveObject



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
# File 'lib/hpcloud/subnet_helper.rb', line 136

def save
  return false if is_valid? == false
  hsh = {
      :name => @name,
      :tenant_id => @tenant_id,
      :dns_nameservers => @dns_nameservers,
      :allocation_pools => @allocation_pools,
      :host_routes => @host_routes,
      :gateway_ip => @gateway,
      :enable_dhcp => @dhcp.to_s
    }
  if @fog.nil?
    response = @connection.network.create_subnet(@network_id, @cidr, @ip_version, hsh)
    if response.nil?
      set_error("Error creating subnet '#{@name}'")
      return false
    end
    @id = response.body["subnet"]["id"]
    @foggy = response.body["subnet"]
  else
    response = @connection.network.update_subnet(@id, hsh)
    if response.nil?
      set_error("Error updating subnet '#{@name}'")
      return false
    end
  end
  return true
end

#set_cidr(value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hpcloud/subnet_helper.rb', line 74

def set_cidr(value)
  require 'ipaddr'
  @cidr = value
  begin
    ipaddr = IPAddr.new(@cidr)
    @ip_version = 6 if ipaddr.ipv6?
    @ip_version = 4 if ipaddr.ipv4?
  rescue Exception => e
    set_error("Invalid CIDR value #{@cidr}", :incorrect_usage)
    return false
  end
  return true
end

#set_dns_nameservers(value) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hpcloud/subnet_helper.rb', line 103

def set_dns_nameservers(value)
  return true if value.nil?
  require 'ipaddr'
  @nameserver = value
  begin
    @dns_nameservers = value.split(',')
    @dns_nameservers.each{ |dns|
      IPAddr.new(dns)
    }
  rescue Exception => e
    set_error("Invalid DNS nameserver '#{value}' must be comma separated list of IPs", :incorrect_usage)
    return false
  end
  return true
end

#set_gateway(value) ⇒ Object



99
100
101
# File 'lib/hpcloud/subnet_helper.rb', line 99

def set_gateway(value)
  @gateway = value
end

#set_host_routes(value) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hpcloud/subnet_helper.rb', line 119

def set_host_routes(value)
  return true if value.nil?
  @routes = value
  begin
    @host_routes = []
    value.split(';').each{ |route|
      ray = route.split(',')
      raise Exception.new("not destination,nexthop") if ray.length != 2
      @host_routes << { "destination" => ray[0], "nexthop" => ray[1] }
    }
  rescue Exception => e
    set_error("Invalid host routes '#{value}' must be semicolon separated list of destination,nexthop", :incorrect_usage)
    return false
  end
  return true
end

#set_ip_version(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/hpcloud/subnet_helper.rb', line 88

def set_ip_version(value)
  return true if value.nil?
  value = value.to_s
  if value != "4" && value != "6"
    set_error("Invalid IP version '#{value}'", :incorrect_usage)
    return false
  end
  @ip_version = value
  return true
end