vnet_api_client 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c9692d1d0c36a83f2f96cae9f4c04cb87d829a2
4
+ data.tar.gz: 615a548f72bab017dad951798ddeb543ca419773
5
+ SHA512:
6
+ metadata.gz: 79379cf32e11fdaf7941ba3ab44201c3ec83cae9aab7dc474fcea5512d847ee033a2bd1f8035f23192ea4e32df4f972442908130197aabdfc6a88cc6b35a5cb8
7
+ data.tar.gz: 2191645275d2879000c6b185df515de69526ef1a8a4572e0d33d970132ab99d7b1c1dde7f08bc61e921814fa917c3c3c92400f786fd3cc0feeb64247a5654ec0
@@ -0,0 +1,47 @@
1
+ A Ruby library for accessing the OpenVNet WebAPI. All this library does is call Ruby's built in `Net::HTTP`. No further external libraries required.
2
+
3
+ The JSON responses from the API are returned as Ruby hashes.
4
+
5
+ Installation:
6
+
7
+ ```bash
8
+ gem install vnet_api_client
9
+ ```
10
+
11
+ Usage:
12
+
13
+ ```ruby
14
+ require 'rubygems'
15
+ require 'vnet_api_client'
16
+
17
+ # This is the default value. If your OpenVNet WebAPI is located at localhost
18
+ # port 9090, you don't have to include this line.
19
+ VNetAPIClient.uri = 'http://localhost:9090'
20
+
21
+ # Creates a new network
22
+ VNetAPIClient::Network.create(display_name: 'my_network',
23
+ ipv4_network: '192.168.3.0',
24
+ ipv4_prefix: 24)
25
+
26
+ # Enables routing and changes display name for an interface
27
+ VNetAPIClient::Interface.update('i-abcdefg', enable_routing: true,
28
+ display_name: 'my new name')
29
+
30
+ # Retrieves all datapaths
31
+ VNetAPIClient::Datapath.index
32
+
33
+ # Retrieves one interface
34
+ VNetAPIClient::Interface.show('i-abcdefg')
35
+
36
+ # Deletes one ip lease
37
+ VNetAPIClient::IpLease.delete('il-begone')
38
+
39
+ # Adds an interface to a security group
40
+ VNetAPIClient::SecurityGroup.add_interface('sg-enter', 'i-getin')
41
+
42
+ # Shows all networks in a datapath
43
+ VNetAPIClient::Datapath.show_networks('dp-mypath')
44
+
45
+ # Deletes a static address from a translation
46
+ VNetAPIClient::Translation.remove_static_address('tr-xxxxx')
47
+ ```
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ require_relative 'vnet_api_client/api_resource'
7
+ require_relative 'vnet_api_client/api_resources'
8
+ require_relative 'vnet_api_client/response_format'
9
+
10
+ module VNetAPIClient
11
+ def self.uri=(u)
12
+ ApiResource.api_uri = u
13
+ end
14
+
15
+ def self.version=(v)
16
+ ApiResource.api_version = v
17
+ end
18
+
19
+ def self.format=(f)
20
+ ApiResource.api_format = f
21
+ end
22
+ end
23
+
24
+ # Set default values
25
+ VNetAPIClient.uri = 'http://localhost:9090'
26
+ VNetAPIClient.format = :json
@@ -0,0 +1,112 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module VNetAPIClient
4
+
5
+ class ApiResource
6
+ class << self
7
+ attr_accessor :api_uri
8
+ attr_accessor :api_format
9
+
10
+ def api_full_uri(suffix)
11
+ u = ApiResource.api_uri
12
+ f = ApiResource.api_format
13
+
14
+ uri = "#{u}/api/1.0"
15
+ uri += "/#{suffix}" if suffix
16
+ uri += ".#{f}"
17
+
18
+ URI(uri)
19
+ end
20
+
21
+ def send_request(verb, suffix, params = nil)
22
+ uri = api_full_uri(suffix)
23
+ uri.query = URI.encode_www_form(params) if params
24
+
25
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
26
+ request = verb.new(uri.request_uri)
27
+ http.request(request)
28
+ end
29
+
30
+ response_format = ApiResource.api_format.to_sym
31
+
32
+ ResponseFormats[response_format].parse(response)
33
+ end
34
+
35
+ #
36
+ # Metaprogramming to define common methods
37
+ #
38
+ def api_suffix(suffix)
39
+ @api_suffix = suffix
40
+ end
41
+
42
+ def metaclass
43
+ class << self
44
+ self
45
+ end
46
+ end
47
+
48
+ def define_standard_crud_methods
49
+ metaclass.instance_eval do
50
+ define_method(:create) do |params = nil|
51
+ send_request(Net::HTTP::Post, @api_suffix, params)
52
+ end
53
+
54
+ define_method(:update) do |uuid, params = nil|
55
+ send_request(Net::HTTP::Put, "#{@api_suffix}/#{uuid}", params)
56
+ end
57
+
58
+ define_method(:delete) do |uuid|
59
+ send_request(Net::HTTP::Delete, "#{@api_suffix}/#{uuid}")
60
+ end
61
+
62
+ define_method(:show) do |uuid|
63
+ send_request(Net::HTTP::Get, "#{@api_suffix}/#{uuid}")
64
+ end
65
+
66
+ define_method(:index) do
67
+ send_request(Net::HTTP::Get, @api_suffix)
68
+ end
69
+ end
70
+ end
71
+
72
+ def define_relation_methods(relation_name)
73
+ define_add_relation(relation_name)
74
+ define_show_relation(relation_name)
75
+ define_remove_relation(relation_name)
76
+ end
77
+
78
+ def define_add_relation(relation_name)
79
+ metaclass.instance_eval do
80
+ singular_name = relation_name.to_s.chomp('s')
81
+
82
+ define_method("add_#{singular_name}") do |uuid, relation_uuid, params = nil|
83
+ suffix = "#{@api_suffix}/#{uuid}/#{relation_name}/#{relation_uuid}"
84
+ send_request(Net::HTTP::Post, suffix, params)
85
+ end
86
+ end
87
+ end
88
+
89
+ def define_show_relation(relation_name)
90
+ metaclass.instance_eval do
91
+ define_method("show_#{relation_name}") do |uuid|
92
+ send_request(Net::HTTP::Get, "#{@api_suffix}/#{uuid}/#{relation_name}")
93
+ end
94
+ end
95
+ end
96
+
97
+ def define_remove_relation(relation_name)
98
+ metaclass.instance_eval do
99
+ singular_name = relation_name.to_s.chomp('s')
100
+
101
+ define_method("remove_#{singular_name}") do |uuid, relation_uuid|
102
+ suffix = "#{@api_suffix}/#{uuid}/#{relation_name}/#{relation_uuid}"
103
+ send_request(Net::HTTP::Delete, suffix)
104
+ end
105
+ end
106
+ end
107
+
108
+ end
109
+ end
110
+
111
+ end
112
+
@@ -0,0 +1,164 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module VNetAPIClient
4
+
5
+ class Datapath < ApiResource
6
+ api_suffix :datapaths
7
+
8
+ define_standard_crud_methods
9
+ define_relation_methods(:networks)
10
+ define_relation_methods(:route_links)
11
+ end
12
+
13
+ class DnsService < ApiResource
14
+ api_suffix :dns_services
15
+
16
+ define_standard_crud_methods
17
+
18
+ define_show_relation(:dns_records)
19
+ define_remove_relation(:dns_records)
20
+
21
+ def self.add_dns_record(dns_service_uuid, params = nil)
22
+ send_request(Net::HTTP::Post,
23
+ "#{@api_suffix}/#{dns_service_uuid}/dns_records",
24
+ params)
25
+ end
26
+ end
27
+
28
+ class Interface < ApiResource
29
+ api_suffix :interfaces
30
+
31
+ define_standard_crud_methods
32
+ define_relation_methods(:security_groups)
33
+
34
+ define_show_relation(:ports)
35
+
36
+ def self.rename(interface_uuid, params = nil)
37
+ send_request(Net::HTTP::Put, "#{@api_suffix}/#{interface_uuid}/rename", params)
38
+ end
39
+
40
+ def self.add_port(interface_uuid, params = nil)
41
+ send_request(Net::HTTP::Post, "#{@api_suffix}/#{interface_uuid}/ports", params)
42
+ end
43
+
44
+ def self.remove_port(interface_uuid, params = nil)
45
+ send_request(Net::HTTP::Delete, "#{@api_suffix}/#{interface_uuid}/ports", params)
46
+ end
47
+ end
48
+
49
+ class IpLease < ApiResource
50
+ api_suffix :ip_leases
51
+
52
+ define_standard_crud_methods
53
+ end
54
+
55
+ class IpRangeGroup < ApiResource
56
+ api_suffix :ip_range_groups
57
+
58
+ define_standard_crud_methods
59
+
60
+ define_show_relation(:ip_ranges)
61
+ define_remove_relation(:ip_ranges)
62
+
63
+ def self.add_range(ip_range_group_uuid, params = nil)
64
+ send_request(Net::HTTP::Post,
65
+ "#{@api_suffix}/#{ip_range_group_uuid}/ip_ranges",
66
+ params)
67
+ end
68
+ end
69
+
70
+ class IpLeaseContainer < ApiResource
71
+ api_suffix :ip_lease_containers
72
+
73
+ define_standard_crud_methods
74
+
75
+ define_show_relation(:ip_leases)
76
+ end
77
+
78
+ class IpRetentionContainer < ApiResource
79
+ api_suffix :ip_retention_containers
80
+
81
+ define_standard_crud_methods
82
+
83
+ define_show_relation(:ip_retentions)
84
+ end
85
+
86
+ class LeasePolicy < ApiResource
87
+ api_suffix :lease_policies
88
+
89
+ define_standard_crud_methods
90
+ define_relation_methods(:ip_lease_containers)
91
+ define_relation_methods(:ip_retention_containers)
92
+ define_relation_methods(:networks)
93
+ define_relation_methods(:interfaces)
94
+
95
+ def self.add_lease(lease_policy_uuid, params = nil)
96
+ send_request(Net::HTTP::Post,
97
+ "#{@api_suffix}/#{lease_policy_uuid}/ip_leases",
98
+ params)
99
+ end
100
+ end
101
+
102
+ class MacLease < ApiResource
103
+ api_suffix :mac_leases
104
+
105
+ define_standard_crud_methods
106
+ end
107
+
108
+ class Network < ApiResource
109
+ api_suffix :networks
110
+
111
+ define_standard_crud_methods
112
+ end
113
+
114
+ class NetworkService < ApiResource
115
+ api_suffix :network_services
116
+
117
+ define_standard_crud_methods
118
+ end
119
+
120
+ class Route < ApiResource
121
+ api_suffix :routes
122
+
123
+ define_standard_crud_methods
124
+ end
125
+
126
+ class RouteLink < ApiResource
127
+ api_suffix :route_links
128
+
129
+ define_standard_crud_methods
130
+ end
131
+
132
+ class SecurityGroup < ApiResource
133
+ api_suffix :security_groups
134
+
135
+ define_standard_crud_methods
136
+ define_relation_methods(:interfaces)
137
+ end
138
+
139
+ class Translation < ApiResource
140
+ api_suffix :translations
141
+
142
+ define_standard_crud_methods
143
+
144
+ def self.add_static_address(translation_uuid, params = nil)
145
+ send_request(Net::HTTP::Post,
146
+ "#{@api_suffix}/#{translation_uuid}/static_address",
147
+ params)
148
+ end
149
+
150
+ def self.remove_static_address(translation_uuid, params = nil)
151
+ send_request(Net::HTTP::Delete,
152
+ "#{@api_suffix}/#{translation_uuid}/static_address",
153
+ params)
154
+ end
155
+ end
156
+
157
+ class VlanTranslation < ApiResource
158
+ api_suffix :vlan_translations
159
+
160
+ define_standard_crud_methods
161
+ end
162
+
163
+ end
164
+
@@ -0,0 +1,28 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module VNetAPIClient
4
+ module ResponseFormats
5
+
6
+ def self.[](format)
7
+ case format
8
+ when :json
9
+ Json.new
10
+ else
11
+ raise "Unknown response format: #{format}"
12
+ end
13
+ end
14
+
15
+ class Format
16
+ def parse(response)
17
+ raise NotImplementedError
18
+ end
19
+ end
20
+
21
+ class Json < Format
22
+ def parse(response)
23
+ JSON.parse(response.body) if response.body
24
+ end
25
+ end
26
+
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vnet_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.7'
5
+ platform: ruby
6
+ authors:
7
+ - Axsh Co. LTD
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby wrapper for OpenVNet's RESTful API
14
+ email: dev@axsh.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/vnet_api_client.rb
20
+ - lib/vnet_api_client/api_resources.rb
21
+ - lib/vnet_api_client/response_format.rb
22
+ - lib/vnet_api_client/api_resource.rb
23
+ - README.md
24
+ homepage: http://openvnet.org
25
+ licenses:
26
+ - LGPLv3
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 2.1.1
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.0.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Ruby wrapper for OpenVNet's RESTful API
48
+ test_files: []
49
+ has_rdoc: