Class: OpenStack::Network::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack/network/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
# File 'lib/openstack/network/connection.rb', line 8

def initialize(connection)
  @connection = connection
  OpenStack::Authentication.init(@connection)
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/openstack/network/connection.rb', line 6

def connection
  @connection
end

Instance Method Details

#add_router_interface(id, subnet_id) ⇒ Object



134
135
136
137
# File 'lib/openstack/network/connection.rb', line 134

def add_router_interface(id, subnet_id)
  req_body = JSON.generate({'subnet_id' => subnet_id})
  @connection.req('PUT', "/routers/#{id}/add_router_interface", {:data => req_body})
end

#add_router_interface_by_name(name, interface) ⇒ Object



130
131
132
# File 'lib/openstack/network/connection.rb', line 130

def add_router_interface_by_name(name, interface)
  @connection.req('PUT', "/routers/#{get_router_id(name)}/#{interface}")
end

#authok?Boolean

Returns true if the authentication was successful and returns false otherwise.

cs.authok?
=> true

Returns:

  • (Boolean)


17
18
19
# File 'lib/openstack/network/connection.rb', line 17

def authok?
  @connection.authok
end

#create_network(name, parameter = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/openstack/network/connection.rb', line 35

def create_network(name, parameter={})
  body_hash = {"network" => {"name"=>name}}
  body_hash['network'].merge! parameter
  req_body = JSON.generate(body_hash)
  response = @connection.req("POST", "/networks", {:data=>req_body})
  OpenStack::Network::Network.new(JSON.parse(response.body)["network"])
end

#create_port(network_id, opts = {}) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/openstack/network/connection.rb', line 87

def create_port(network_id, opts={})
  body_hash = {"port"=>{"network_id"=> network_id}}
  body_hash["port"].merge!(opts) #fixme - validation?
  req_body = JSON.generate(body_hash)
  response = @connection.req("POST", "/ports", {:data=>req_body})
  OpenStack::Network::Port.new(JSON.parse(response.body)["port"])
end

#create_qos_policy(options) ⇒ Object



162
163
164
165
166
167
# File 'lib/openstack/network/connection.rb', line 162

def create_qos_policy(options)
  data = JSON.generate(:policy => options)
  response = @connection.req("POST", "/qos/policies", {:data => data})
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  OpenStack::Network::QoSPolicy.new(@connection, JSON.parse(response.body)["policy"])
end

#create_router(name, admin_state_up, opts = {}) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/openstack/network/connection.rb', line 114

def create_router(name, admin_state_up, opts={})
  body_hash = {'router'=> {'name' => name, 'admin_state_up' => admin_state_up}}
  body_hash['router'].merge! opts
  req_body = JSON.generate body_hash
  response = @connection.req('POST', '/routers', {:data => req_body })
  OpenStack::Network::Router.new(JSON.parse(response.body)['router'])
end

#create_subnet(network_id, cidr, ip_version = "4", opts = {}) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/openstack/network/connection.rb', line 61

def create_subnet(network_id, cidr, ip_version="4", opts={})
  body_hash = {"subnet"=>{"network_id"=> network_id, "cidr"=>cidr, "ip_version"=>ip_version}}
  body_hash["subnet"].merge!(opts) #fixme - validation?
  req_body = JSON.generate(body_hash)
  response = @connection.req("POST", "/subnets", {:data=>req_body})
  OpenStack::Network::Subnet.new(JSON.parse(response.body)["subnet"])
end

#delete_network(id) ⇒ Object



43
44
45
46
# File 'lib/openstack/network/connection.rb', line 43

def delete_network(id)
  @connection.req("DELETE", "/networks/#{id}")
  true
end

#delete_port(id) ⇒ Object



102
103
104
105
# File 'lib/openstack/network/connection.rb', line 102

def delete_port(id)
  @connection.req("DELETE", "/ports/#{id}")
  true
end

#delete_router(id) ⇒ Object



126
127
128
# File 'lib/openstack/network/connection.rb', line 126

def delete_router(id)
  @connection.req('DELETE', "/routers/#{id}")
end

#delete_router_by_name(name) ⇒ Object



122
123
124
# File 'lib/openstack/network/connection.rb', line 122

def delete_router_by_name(name)
  @connection.req('DELETE', "/routers/#{get_router_id(name)}")
end

#delete_subnet(id) ⇒ Object



69
70
71
72
# File 'lib/openstack/network/connection.rb', line 69

def delete_subnet(id)
  @connection.req("DELETE", "/subnets/#{id}")
  true
end

#get_ip_availabilitiesObject



183
184
185
186
187
# File 'lib/openstack/network/connection.rb', line 183

def get_ip_availabilities
  response = @connection.req("GET", "/network-ip-availabilities")
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  JSON.parse(response.body)["network_ip_availabilities"]
end

#get_ip_availability(network_id) ⇒ Object



189
190
191
192
193
# File 'lib/openstack/network/connection.rb', line 189

def get_ip_availability(network_id)
  response = @connection.req("GET", "/network-ip-availabilities/#{network_id}")
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  JSON.parse(response.body)["network_ip_availability"]
end

#get_network(network_id) ⇒ Object Also known as: network



29
30
31
32
# File 'lib/openstack/network/connection.rb', line 29

def get_network(network_id)
  response = @connection.req("GET", "/networks/#{network_id}")
  OpenStack::Network::Network.new(JSON.parse(response.body)["network"])
end

#get_port(port_id) ⇒ Object Also known as: port



81
82
83
84
# File 'lib/openstack/network/connection.rb', line 81

def get_port(port_id)
  response = @connection.req("GET", "/ports/#{port_id}")
  OpenStack::Network::Port.new(JSON.parse(response.body)["port"])
end

#get_qos_policy(policy_id) ⇒ Object



177
178
179
180
181
# File 'lib/openstack/network/connection.rb', line 177

def get_qos_policy(policy_id)
  response = @connection.req("GET", "/qos/policies/#{policy_id}")
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  OpenStack::Network::QoSPolicy.new(@connection, JSON.parse(response.body)["policy"])
end

#get_router_id(name) ⇒ Object



156
157
158
159
160
# File 'lib/openstack/network/connection.rb', line 156

def get_router_id(name)
  routers.detect do |value|
    return value.id if value.name == name
  end
end

#get_subnet(subnet_id) ⇒ Object Also known as: subnet



55
56
57
58
# File 'lib/openstack/network/connection.rb', line 55

def get_subnet(subnet_id)
  response = @connection.req("GET", "/subnets/#{subnet_id}")
  OpenStack::Network::Subnet.new(JSON.parse(response.body)["subnet"])
end

#list_networks(options = {}) ⇒ Object Also known as: networks



21
22
23
24
25
26
# File 'lib/openstack/network/connection.rb', line 21

def list_networks(options = {})
  path = options.empty? ? "/networks" : "/networks?#{options.to_query}"
  response = @connection.req("GET", path)
  nets_hash = JSON.parse(response.body)["networks"]
  nets_hash.inject([]){|res, current| res << OpenStack::Network::Network.new(current); res}
end

#list_portsObject Also known as: ports



74
75
76
77
78
# File 'lib/openstack/network/connection.rb', line 74

def list_ports
  response = @connection.req("GET", "/ports")
  ports_hash = JSON.parse(response.body)["ports"]
  ports_hash.inject([]){|res, current| res << OpenStack::Network::Port.new(current); res}
end

#list_qos_policies(options = {}) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/openstack/network/connection.rb', line 169

def list_qos_policies(options = {})
  path = options.empty? ? "/qos/policies" : "/qos/policies?#{options.to_query}"
  response = @connection.req("GET", path)
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  qos_hash = JSON.parse(response.body)["policies"]
  qos_hash.inject([]){|res, current| res << OpenStack::Network::QoSPolicy.new(@connection, current); res}
end

#list_routersObject Also known as: routers



107
108
109
110
111
# File 'lib/openstack/network/connection.rb', line 107

def list_routers
  response = @connection.req('GET', '/routers')
  nets_hash = JSON.parse(response.body)['routers']
  nets_hash.inject([]){|res, current| res << OpenStack::Network::Router.new(current); res}
end

#list_subnetsObject Also known as: subnets



48
49
50
51
52
# File 'lib/openstack/network/connection.rb', line 48

def list_subnets
  response = @connection.req("GET", "/subnets")
  nets_hash = JSON.parse(response.body)["subnets"]
  nets_hash.inject([]){|res, current| res << OpenStack::Network::Subnet.new(current); res}
end

#remove_router_interface(id, subnet_id) ⇒ Object



139
140
141
142
# File 'lib/openstack/network/connection.rb', line 139

def remove_router_interface(id, subnet_id)
  req_body = JSON.generate({'subnet_id' => subnet_id})
  @connection.req('PUT', "/routers/#{id}/remove_router_interface", {:data => req_body})
end

#update_port(id, options) ⇒ Object



95
96
97
98
99
100
# File 'lib/openstack/network/connection.rb', line 95

def update_port(id, options)
  data = JSON.generate(:port => options)
  response = @connection.req("PUT", "/ports/#{id}", {:data => data})
  OpenStack::Exception.raise_exception(response) unless response.code.match(/^20.$/)
  OpenStack::Network::Port.new(JSON.parse(response.body)["port"])
end

#update_router(id, opts = {}) ⇒ Object



150
151
152
153
154
# File 'lib/openstack/network/connection.rb', line 150

def update_router(id,opts={})
  req_body = JSON.generate({'router' => opts})
  response = @connection.req('PUT',"/routers/#{id}",{:data => req_body})
  OpenStack::Network::Router.new(JSON.parse(response.body)['router'])
end

#update_router_by_name(name, opts = {}) ⇒ Object



144
145
146
147
148
# File 'lib/openstack/network/connection.rb', line 144

def update_router_by_name(name,opts={})
  req_body = JSON.generate opts
  response = @connection.req('PUT',"/routers/#{get_router_id(name)}",{:data => req_body})
  OpenStack::Network::Router.new(JSON.parse(response.body)['router'])
end