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

#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) ⇒ Object



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

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

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



84
85
86
87
88
89
90
# File 'lib/openstack/network/connection.rb', line 84

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_subnet(network_id, cidr, ip_version = "4", opts = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/openstack/network/connection.rb', line 58

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



40
41
42
43
# File 'lib/openstack/network/connection.rb', line 40

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

#delete_port(id) ⇒ Object



92
93
94
95
# File 'lib/openstack/network/connection.rb', line 92

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

#delete_subnet(id) ⇒ Object



66
67
68
69
# File 'lib/openstack/network/connection.rb', line 66

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

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



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

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



78
79
80
81
# File 'lib/openstack/network/connection.rb', line 78

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

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



52
53
54
55
# File 'lib/openstack/network/connection.rb', line 52

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

#list_networksObject Also known as: networks



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

def list_networks
  response = @connection.req("GET", "/networks")
  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



71
72
73
74
75
# File 'lib/openstack/network/connection.rb', line 71

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_subnetsObject Also known as: subnets



45
46
47
48
49
# File 'lib/openstack/network/connection.rb', line 45

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