File: connection.rb

package info (click to toggle)
ruby-openstack 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 452 kB
  • sloc: ruby: 3,217; makefile: 6
file content (157 lines) | stat: -rw-r--r-- 5,203 bytes parent folder | download | duplicates (3)
1
2
3
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
module OpenStack
module Network

  class Connection

    attr_accessor   :connection

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

    # Returns true if the authentication was successful and returns false otherwise.
    #
    #   cs.authok?
    #   => true
    def authok?
      @connection.authok
    end

    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
    alias :networks :list_networks

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

    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

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

    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
    alias :subnets :list_subnets

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

    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

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

    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
    alias :ports :list_ports

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

    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

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

    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
    alias :routers :list_routers

    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

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

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

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

    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

    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

    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

    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

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

  end

end
end