Skip to content

Commit a8c26a9

Browse files
authored
Merge pull request #30 from lenovo/connection_url
Connection url
2 parents f2ae2ac + b7f7608 commit a8c26a9

3 files changed

Lines changed: 39 additions & 36 deletions

File tree

lib/xclarity_client/xclarity_base.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
require 'faraday'
22
require 'json'
33
require 'uri'
4+
require 'uri/https'
45

56
module XClarityClient
67
class XClarityBase
78

89
token_auth = '/session'.freeze
9-
1010
attr_reader :conn
1111

1212
def initialize(conf, uri)
@@ -15,8 +15,16 @@ def initialize(conf, uri)
1515

1616
def connection_builder(conf, uri)
1717
$lxca_log.info "XClarityClient::XClarityBase connection_builder", "Creating connection to #{conf.host + uri}"
18-
#Building configuration
19-
@conn = Faraday.new(url: conf.host + uri) do |faraday|
18+
# Building configuration, convert to https if protocol not specified
19+
hostname = URI.parse(conf.host)
20+
unless hostname.scheme
21+
hostname = URI::HTTPS.build({ :host => hostname.host,
22+
:port => hostname.port,
23+
:path => hostname.path,
24+
:query => hostname.query,
25+
:fragment => hostname.fragment })
26+
end
27+
@conn = Faraday.new(:url => "#{hostname}#{uri}") do |faraday|
2028
faraday.request :url_encoded # form-encode POST params
2129
faraday.response :logger # log requests to STDOUT -- This line, should be uncommented if you wanna inspect the URL Request
2230
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP

spec/spec_helper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
require 'apib/mock_server'
44
require 'webmock/rspec'
55

6-
7-
base_url = "http://example.com"
6+
base_url = 'https://example.com'
87
# These environment variables must be defined
98
ENV['LXCA_USERNAME'] ||= ''
109
ENV['LXCA_PASSWORD'] ||= ''
1110
ENV['LXCA_HOST'] ||= base_url
1211
ENV['LXCA_AUTH_TYPE'] ||= ''
1312
ENV['LXCA_VERIFY_SSL'] ||= 'NONE'
1413

15-
blueprints = ""
14+
blueprints = ''
1615
Dir.glob('docs/apib/*.apib') do |blueprint|
1716
blueprints << File.open(blueprint).read
1817
end

spec/xclarity_client_node_spec.rb

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
describe XClarityClient do
44
before :all do
5-
WebMock.allow_net_connect! # -- This line should be uncommented if you're using external mock test
5+
# -- The next line should be uncommented
6+
# if you're using external mock test
7+
WebMock.allow_net_connect!
68

79
conf = XClarityClient::Configuration.new(
810
username: ENV['LXCA_USERNAME'],
@@ -13,33 +15,32 @@
1315
)
1416

1517
@client = XClarityClient::Client.new(conf)
18+
@host = ENV['LXCA_HOST']
1619

17-
@includeAttributes = %w(accessState activationKeys)
18-
@excludeAttributes = %w(accessState activationKeys)
20+
@include_attributes = %w(access_state activation_keys)
21+
@exclude_attributes = %w(access_state activation_keys)
1922
end
2023

2124
before :each do
22-
@uuidArray = @client.discover_nodes.map { |node| node.uuid }
25+
@uuid_array = @client.discover_nodes.map(&:uuid)
2326
end
2427

2528
it 'has a version number' do
2629
expect(XClarityClient::VERSION).not_to be nil
2730
end
2831

2932
describe 'GET /nodes' do
30-
3133
it 'should respond with an array' do
3234
expect(@client.discover_nodes.class).to eq(Array)
3335
end
34-
3536
end
3637

3738
describe 'GET /nodes/UUID' do
3839
context 'with include attributes' do
3940
it 'required attributes should not be nil' do
40-
response = @client.fetch_nodes(@uuidArray, @includeAttributes,nil)
41+
response = @client.fetch_nodes(@uuid_array, @include_attributes, nil)
4142
response.map do |node|
42-
@includeAttributes.map do |attribute|
43+
@include_attributes.map do |attribute|
4344
expect(node.send(attribute)).not_to be_nil
4445
end
4546
end
@@ -48,9 +49,9 @@
4849

4950
context 'with excludeAttributes' do
5051
it 'excluded attributes should to be nil' do
51-
response = @client.fetch_nodes(@uuidArray, nil, @excludeAttributes)
52+
response = @client.fetch_nodes(@uuid_array, nil, @exclude_attributes)
5253
response.map do |node|
53-
@excludeAttributes.map do |attribute|
54+
@exclude_attributes.map do |attribute|
5455
expect(node.send(attribute)).to be_nil
5556
end
5657
end
@@ -59,12 +60,11 @@
5960
end
6061

6162
describe 'GET /nodes/UUID,UUID,...,UUID' do
62-
6363
context 'with includeAttributes' do
6464
it 'required attributes shoud not be nil ' do
65-
response = @client.fetch_nodes(@uuidArray, @includeAttributes,nil)
65+
response = @client.fetch_nodes(@uuid_array, @include_attributes, nil)
6666
response.map do |node|
67-
@includeAttributes.map do |attribute|
67+
@include_attributes.map do |attribute|
6868
expect(node.send(attribute)).not_to be_nil
6969
end
7070
end
@@ -73,9 +73,9 @@
7373

7474
context 'with excludeAttributes' do
7575
it 'excluded attributes shoud to be nil' do
76-
response = @client.fetch_nodes(@uuidArray, nil, @excludeAttributes)
76+
response = @client.fetch_nodes(@uuid_array, nil, @exclude_attributes)
7777
response.map do |node|
78-
@excludeAttributes.map do |attribute|
78+
@exclude_attributes.map do |attribute|
7979
expect(node.send(attribute)).to be_nil
8080
end
8181
end
@@ -84,12 +84,11 @@
8484
end
8585

8686
describe 'GET /nodes' do
87-
8887
context 'with includeAttributes' do
8988
it 'required attributes should not be nil' do
90-
response = @client.fetch_nodes(nil,@includeAttributes,nil)
89+
response = @client.fetch_nodes(nil, @include_attributes, nil)
9190
response.first do |node|
92-
@includeAttributes.map do |attribute|
91+
@include_attributes.map do |attribute|
9392
expect(node.send(attribute)).not_to be_nil
9493
end
9594
end
@@ -98,9 +97,9 @@
9897

9998
context 'with excludeAttributes' do
10099
it 'excluded attributes should be nil' do
101-
response = @client.fetch_nodes(nil,nil,@excludeAttributes)
100+
response = @client.fetch_nodes(nil, nil, @exclude_attributes)
102101
response.map do |node|
103-
@excludeAttributes.map do |attribute|
102+
@exclude_attributes.map do |attribute|
104103
expect(node.send(attribute)).to be_nil
105104
end
106105
end
@@ -110,38 +109,35 @@
110109

111110
describe 'Get /node' do
112111
it 'should power down system' do
113-
response = @client.power_off_node(@uuidArray[0])
112+
response = @client.power_off_node(@uuid_array[0])
114113
expect(response.status).to eq(200)
115-
116114
end
117115
end
118116

119117
describe 'PUT /nodes/UUID' do
120118
context 'with a leds object' do
121119
context 'with state == "On" and name == "Identify"' do
122120
it 'turns on the location led' do
123-
@client.turn_on_loc_led(@uuidArray[0])
124-
uri = "http://example.com/nodes/#{@uuidArray[0]}"
121+
@client.turn_on_loc_led(@uuid_array[0])
122+
uri = "#{@host}/nodes/#{@uuid_array[0]}"
125123
request_body = { 'body' => { 'leds' => [{ 'name' => 'Identify',
126124
'state' => 'On' }] } }
127125
expect(a_request(:put, uri).with(request_body)).to have_been_made
128126
end
129127
end
130-
131128
context 'with state == "Off" and name == "Identify"' do
132129
it 'turns off the location led' do
133-
@client.turn_off_loc_led(@uuidArray[0])
134-
uri = "http://example.com/nodes/#{@uuidArray[0]}"
130+
@client.turn_off_loc_led(@uuid_array[0])
131+
uri = "#{@host}/nodes/#{@uuid_array[0]}"
135132
request_body = { 'body' => { 'leds' => [{ 'name' => 'Identify',
136133
'state' => 'Off' }] } }
137134
expect(a_request(:put, uri).with(request_body)).to have_been_made
138135
end
139136
end
140-
141137
context 'with state == "Blinking" and name == "Identify"' do
142138
it 'turns on the blinking location led' do
143-
@client.blink_loc_led(@uuidArray[0])
144-
uri = "http://example.com/nodes/#{@uuidArray[0]}"
139+
@client.blink_loc_led(@uuid_array[0])
140+
uri = "#{@host}/nodes/#{@uuid_array[0]}"
145141
request_body = { 'body' => { 'leds' => [{ 'name' => 'Identify',
146142
'state' => 'Blinking' }] } }
147143
expect(a_request(:put, uri).with(request_body)).to have_been_made

0 commit comments

Comments
 (0)