seiso-connector 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +2 -0
- data/.travis.yml +3 -0
- data/README.md +4 -0
- data/lib/seiso/connector/connector_v1.rb +62 -0
- data/lib/seiso/connector/connector_v2.rb +23 -0
- data/lib/seiso/connector/request_failed_error.rb +6 -0
- data/lib/seiso/connector.rb +71 -81
- data/seiso-connector.gemspec +1 -1
- data/test/test_connector.rb +4 -4
- data/test/test_connector_v1.rb +26 -0
- data/test/test_connector_v2.rb +26 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGQwOTc5MGEyMzk1ODZiODQ3MGU1MGJhMjNkMmQyNTBjMjExZGY1Mg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NDE5OWZiNjQ1OWMzY2RmZjYxNDg4NGMzZjA4ZWY3YmU1MDJjOTFhOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjNjMjRkOGFkNGQ2ZGFhNzVmMTlhZWQyMWNjN2RmOTE5ZjVmZmNjNWY0NjYz
|
10
|
+
Nzk3MmJiYjBhNGVlOWRkYjgzMzZhOGI4ODNkNjIyODJjY2JjY2ZjM2UyN2I4
|
11
|
+
NzhhZTcxNWUzY2Q0YTgzZDQ4ZjRmZTQyNjJjMzBiNzE4ZmU4Y2M=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjY0NDgwOWU3NmY0YTMyNzg1N2M0ZTk0YzY5NjIyMzEwMGNkZjE2NTIyZmEx
|
14
|
+
MGZkNzc2ZjlhM2Q5OTFkYjc4ZTk2MjY5ZDE1YzBkYmZhOTJkYjIxNzU1NGE0
|
15
|
+
MzQyOWQyMWJlZmU0NGYxNzE0NTk1MGZmYmJhNDhmY2E1ZDQ1YmE=
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Seiso::Connector
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/seiso-connector.svg)](http://badge.fury.io/rb/seiso-connector)
|
4
|
+
[![Build Status](https://travis-ci.org/ExpediaDotCom/seiso-connector-ruby.svg)](https://travis-ci.org/ExpediaDotCom/seiso-connector-ruby)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/ExpediaDotCom/seiso-connector-ruby.svg?branch=master)](http://inch-ci.org/github/ExpediaDotCom/seiso-connector-ruby)
|
6
|
+
|
3
7
|
Seiso connector for Ruby clients.
|
4
8
|
|
5
9
|
## Installation
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require_relative 'request_failed_error'
|
3
|
+
|
4
|
+
module Seiso
|
5
|
+
class Connector
|
6
|
+
class ConnectorV1
|
7
|
+
attr_reader :http
|
8
|
+
attr_reader :username
|
9
|
+
|
10
|
+
def initialize(http, username, password)
|
11
|
+
@http = http
|
12
|
+
@username = username
|
13
|
+
@password = password
|
14
|
+
@request_headers = {
|
15
|
+
"Accept" => "application/json",
|
16
|
+
"Content-Type" => "application/json",
|
17
|
+
"X-User-Agent" => username
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def do_get(path)
|
22
|
+
request = Net::HTTP::Get.new(path, initheader = @request_headers)
|
23
|
+
request.basic_auth(@username, @password)
|
24
|
+
response = @http.request(request)
|
25
|
+
raise(Seiso::Connector::RequestFailedError, response.body) if !response.kind_of?(Net::HTTPSuccess)
|
26
|
+
response.body
|
27
|
+
end
|
28
|
+
|
29
|
+
def do_post(path, data)
|
30
|
+
puts "POST #{path}"
|
31
|
+
request = Net::HTTP::Post.new(path, initheader = @request_headers)
|
32
|
+
request.basic_auth(@username, @password)
|
33
|
+
request.body = data.to_json
|
34
|
+
response = @http.request(request)
|
35
|
+
display_response(response)
|
36
|
+
end
|
37
|
+
|
38
|
+
def do_put(path, data)
|
39
|
+
puts "PUT #{path}"
|
40
|
+
request = Net::HTTP::Put.new(path, initheader = @request_headers)
|
41
|
+
request.basic_auth(@username, @password)
|
42
|
+
request.body = data.to_json
|
43
|
+
response = @http.request(request)
|
44
|
+
display_response(response)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# Displays an HTTP response on the command line.
|
50
|
+
# - response: response to display
|
51
|
+
def display_response(response)
|
52
|
+
case response.code
|
53
|
+
when "500"
|
54
|
+
puts "Server error: #{response.code} #{response.message}"
|
55
|
+
else
|
56
|
+
puts "#{response.code} #{response.message}"
|
57
|
+
puts "#{response.body}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require_relative 'request_failed_error'
|
3
|
+
|
4
|
+
module Seiso
|
5
|
+
class Connector
|
6
|
+
class ConnectorV2
|
7
|
+
attr_reader :http
|
8
|
+
attr_reader :username
|
9
|
+
|
10
|
+
def initialize(http, username, password)
|
11
|
+
@http = http
|
12
|
+
@username = username
|
13
|
+
@password = password
|
14
|
+
@request_headers = {
|
15
|
+
"Accept" => "application/hal+json",
|
16
|
+
"Content-Type" => "application/hal+json"
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
# TODO
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/seiso/connector.rb
CHANGED
@@ -1,60 +1,64 @@
|
|
1
1
|
require "net/http"
|
2
2
|
require "uri"
|
3
|
+
require_relative "connector/connector_v1"
|
4
|
+
require_relative "connector/connector_v2"
|
3
5
|
|
4
|
-
# Seiso
|
5
|
-
#
|
6
|
-
# TODO Update to use _self links when pushing data to Seiso.
|
7
|
-
#
|
8
|
-
# Author:: Willie Wheeler (mailto:wwheeler@expedia.com)
|
9
|
-
# Copyright:: Copyright (c) 2014-2015 Expedia, Inc.
|
10
|
-
# License:: Apache 2.0
|
11
|
-
|
6
|
+
# Seiso namespace module
|
12
7
|
module Seiso
|
13
8
|
|
9
|
+
# Seiso connector.
|
10
|
+
#
|
11
|
+
# Author:: Willie Wheeler
|
12
|
+
# Copyright:: Copyright (c) 2014-2015 Expedia, Inc.
|
13
|
+
# License:: Apache 2.0
|
14
14
|
class Connector
|
15
|
-
attr_reader :base_uri
|
16
|
-
attr_reader :username
|
17
|
-
|
18
|
-
# FIXME X-User-Agent is deprecated.
|
19
|
-
REQUEST_HEADERS = {
|
20
|
-
"Accept" => "application/json",
|
21
|
-
"Content-Type" => "application/json",
|
22
|
-
"X-User-Agent" => "dummy"
|
23
|
-
}
|
24
15
|
|
25
|
-
# settings
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
16
|
+
# - settings: a hash containing the following entries:
|
17
|
+
# - host
|
18
|
+
# - port
|
19
|
+
# - use_ssl (default false)
|
20
|
+
# - ignore_cert (default false)
|
21
|
+
# - username
|
22
|
+
# - password
|
32
23
|
def initialize(settings)
|
33
24
|
host = settings['host']
|
34
25
|
port = settings['port']
|
35
26
|
use_ssl = settings['use_ssl'] | false
|
36
27
|
ignore_cert = settings['ignore_cert'] | false
|
37
|
-
|
28
|
+
username = settings['username']
|
29
|
+
password = settings['password']
|
38
30
|
|
39
|
-
|
40
|
-
if use_ssl
|
41
|
-
@base_uri = "https://#{host}"
|
42
|
-
if port != 443
|
43
|
-
@base_uri << ":#{port}"
|
44
|
-
end
|
45
|
-
else
|
46
|
-
@base_uri = "http://#{host}"
|
47
|
-
if port != 80
|
48
|
-
@base_uri << ":#{port}"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
puts "Using base_uri=#{@base_uri}"
|
31
|
+
http = create_http(host, port, use_ssl, ignore_cert)
|
52
32
|
|
53
|
-
@
|
54
|
-
@
|
33
|
+
@connector_v1 = Seiso::Connector::ConnectorV1.new(http, username, password)
|
34
|
+
@connector_v2 = Seiso::Connector::ConnectorV2.new(http, username, password)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Tests can override this
|
38
|
+
def connector_v1
|
39
|
+
@connector_v1
|
40
|
+
end
|
41
|
+
|
42
|
+
# Tests can override this
|
43
|
+
def connector_v2
|
44
|
+
@connector_v2
|
45
|
+
end
|
46
|
+
|
47
|
+
# GET item list
|
48
|
+
def get_items(type)
|
49
|
+
connector_v1.do_get("/v1/#{type}")
|
55
50
|
end
|
56
51
|
|
57
|
-
|
52
|
+
# GET key list. We currently support only type=people.
|
53
|
+
# - type: item type
|
54
|
+
# def get_keys(type, source, page)
|
55
|
+
# raise(NotImplementedError, "get_keys not implemented for type #{type}") if type != "people"
|
56
|
+
# path = "/v2/#{type}?view=keys&source=#{source}&page=#{page}"
|
57
|
+
# do_get path
|
58
|
+
# end
|
59
|
+
|
60
|
+
# POST items to Seiso
|
61
|
+
def post_items(type, items)
|
58
62
|
if type == 'ip-address-roles'
|
59
63
|
do_put_ip_address_roles items
|
60
64
|
elsif type == 'node-ip-addresses'
|
@@ -62,12 +66,25 @@ module Seiso
|
|
62
66
|
elsif type == 'service-instance-ports'
|
63
67
|
do_put_service_instance_ports items
|
64
68
|
else
|
65
|
-
|
69
|
+
connector_v1.do_post("/v1/#{type}?mode=batch", items)
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
73
|
+
# PUT a single item to Seiso.
|
74
|
+
# - type: Item type
|
75
|
+
# - item: Item
|
76
|
+
# - key: Item key
|
77
|
+
def put_item(type, item, key)
|
78
|
+
connector_v1.do_put("/v1/#{type}/#{key}", item)
|
79
|
+
end
|
80
|
+
|
69
81
|
private
|
70
|
-
|
82
|
+
|
83
|
+
# Create HTTP connector
|
84
|
+
# - host: Seiso host
|
85
|
+
# - port: Seiso port
|
86
|
+
# - use_ssl: Use HTTPS?
|
87
|
+
# - ignore_cert: Ignore bad certs?
|
71
88
|
def create_http(host, port, use_ssl, ignore_cert)
|
72
89
|
http = Net::HTTP.new(host, port)
|
73
90
|
if use_ssl
|
@@ -81,63 +98,36 @@ module Seiso
|
|
81
98
|
http
|
82
99
|
end
|
83
100
|
|
84
|
-
#
|
85
|
-
|
86
|
-
do_post("/v1/#{type}?mode=batch", items)
|
87
|
-
end
|
88
|
-
|
89
|
-
def do_post(path, item)
|
90
|
-
puts "POST #{path}"
|
91
|
-
request = Net::HTTP::Post.new(path, initheader = REQUEST_HEADERS)
|
92
|
-
request.basic_auth(@username, @password)
|
93
|
-
request.body = item.to_json
|
94
|
-
response = @http.request request
|
95
|
-
if response.code == "500"
|
96
|
-
puts "Error: #{response.code} #{response.message}"
|
97
|
-
else
|
98
|
-
puts "#{response.code} #{response.message}"
|
99
|
-
puts "#{response.body}"
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
101
|
+
# PUTs a list of IP address roles
|
102
|
+
# - roles: IP address roles
|
103
103
|
def do_put_ip_address_roles(roles)
|
104
104
|
# TODO Implement batch endpoint
|
105
105
|
roles.each do |r|
|
106
106
|
si_key = r['serviceInstance']['key']
|
107
107
|
name = r['name']
|
108
|
-
do_put("/v1/service-instances/#{si_key}/ip-address-roles/#{name}", r)
|
108
|
+
connector_v1.do_put("/v1/service-instances/#{si_key}/ip-address-roles/#{name}", r)
|
109
109
|
end
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
|
+
# PUTs a list of node IP addresses
|
113
|
+
# - nips: Node IP addresses
|
112
114
|
def do_put_node_ip_addresses(nips)
|
113
115
|
# TODO Implement batch endpoint
|
114
116
|
nips.each do |nip|
|
115
117
|
node_name = nip['node']['name']
|
116
118
|
ip_address = nip['ipAddress']
|
117
|
-
do_put("/v1/nodes/#{node_name}/ip-addresses/#{ip_address}", nip)
|
119
|
+
connector_v1.do_put("/v1/nodes/#{node_name}/ip-addresses/#{ip_address}", nip)
|
118
120
|
end
|
119
121
|
end
|
120
|
-
|
122
|
+
|
123
|
+
# PUTs a list of service instance ports
|
124
|
+
# - ports: Service instance ports
|
121
125
|
def do_put_service_instance_ports(ports)
|
122
126
|
# TODO Implement batch endpoint
|
123
127
|
ports.each do |p|
|
124
128
|
si_key = p['serviceInstance']['key']
|
125
129
|
number = p['number']
|
126
|
-
do_put("/v1/service-instances/#{si_key}/ports/#{number}", p)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def do_put(path, item)
|
131
|
-
puts "PUT #{path}"
|
132
|
-
request = Net::HTTP::Put.new(path, initheader = REQUEST_HEADERS)
|
133
|
-
request.basic_auth(@username, @password)
|
134
|
-
request.body = item.to_json
|
135
|
-
response = @http.request request
|
136
|
-
if response.code == "500"
|
137
|
-
puts "Error: #{response.code} #{response.message}"
|
138
|
-
else
|
139
|
-
puts "#{response.code} #{response.message}"
|
140
|
-
puts "#{response.body}"
|
130
|
+
connector_v1.do_put("/v1/service-instances/#{si_key}/ports/#{number}", p)
|
141
131
|
end
|
142
132
|
end
|
143
133
|
end
|
data/seiso-connector.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "seiso-connector"
|
7
|
-
spec.version = "0.0
|
7
|
+
spec.version = "0.1.0"
|
8
8
|
spec.authors = ["Willie Wheeler"]
|
9
9
|
spec.email = ["wwheeler@expedia.com"]
|
10
10
|
spec.summary = "Connector to make it easier for Ruby clients to talk to the Seiso REST API."
|
data/test/test_connector.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'seiso/connector'
|
2
2
|
require 'minitest/autorun'
|
3
3
|
|
4
|
-
# Author:: Willie Wheeler
|
4
|
+
# Author:: Willie Wheeler
|
5
5
|
# Copyright:: Copyright (c) 2014-2015 Expedia, Inc.
|
6
6
|
# License:: Apache 2.0
|
7
|
-
|
8
7
|
class TestConnector < MiniTest::Unit::TestCase
|
9
8
|
|
10
9
|
def setup
|
@@ -19,10 +18,11 @@ class TestConnector < MiniTest::Unit::TestCase
|
|
19
18
|
|
20
19
|
# This creates the HTTP connector internally.
|
21
20
|
# Probably want to inject a mock?
|
22
|
-
@connector = Seiso::Connector.new
|
21
|
+
@connector = Seiso::Connector.new(@settings)
|
23
22
|
end
|
24
23
|
|
25
24
|
def test_init
|
26
|
-
|
25
|
+
refute_nil(@connector.connector_v1)
|
26
|
+
refute_nil(@connector.connector_v2)
|
27
27
|
end
|
28
28
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'seiso/connector/connector_v1'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
# Author:: Willie Wheeler
|
5
|
+
# Copyright:: Copyright (c) 2015 Expedia, Inc.
|
6
|
+
# License:: Apache 2.0
|
7
|
+
class TestConnectorV1 < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
|
11
|
+
# Test data
|
12
|
+
@username = "willie"
|
13
|
+
@password = "rocket88"
|
14
|
+
|
15
|
+
# Dependencies
|
16
|
+
@http = {}
|
17
|
+
|
18
|
+
# Class under test
|
19
|
+
@connector = Seiso::Connector::ConnectorV1.new(@http, @username, @password)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_init
|
23
|
+
assert_equal(@http, @connector.http)
|
24
|
+
assert_equal(@username, @connector.username)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'seiso/connector/connector_v2'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
# Author:: Willie Wheeler
|
5
|
+
# Copyright:: Copyright (c) 2015 Expedia, Inc.
|
6
|
+
# License:: Apache 2.0
|
7
|
+
class TestConnectorV2 < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
|
11
|
+
# Test data
|
12
|
+
@username = "willie"
|
13
|
+
@password = "rocket88"
|
14
|
+
|
15
|
+
# Dependencies
|
16
|
+
@http = {}
|
17
|
+
|
18
|
+
# Class under test
|
19
|
+
@connector = Seiso::Connector::ConnectorV2.new(@http, @username, @password)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_init
|
23
|
+
assert_equal(@http, @connector.http)
|
24
|
+
assert_equal(@username, @connector.username)
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seiso-connector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Willie Wheeler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,13 +61,19 @@ extensions: []
|
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
63
|
- .gitignore
|
64
|
+
- .travis.yml
|
64
65
|
- Gemfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
67
68
|
- Rakefile
|
68
69
|
- lib/seiso/connector.rb
|
70
|
+
- lib/seiso/connector/connector_v1.rb
|
71
|
+
- lib/seiso/connector/connector_v2.rb
|
72
|
+
- lib/seiso/connector/request_failed_error.rb
|
69
73
|
- seiso-connector.gemspec
|
70
74
|
- test/test_connector.rb
|
75
|
+
- test/test_connector_v1.rb
|
76
|
+
- test/test_connector_v2.rb
|
71
77
|
homepage: http://seiso.io
|
72
78
|
licenses:
|
73
79
|
- Apache-2.0
|
@@ -94,3 +100,5 @@ specification_version: 4
|
|
94
100
|
summary: Connector to make it easier for Ruby clients to talk to the Seiso REST API.
|
95
101
|
test_files:
|
96
102
|
- test/test_connector.rb
|
103
|
+
- test/test_connector_v1.rb
|
104
|
+
- test/test_connector_v2.rb
|