go_api_client 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/go_api_client.rb +2 -0
- data/lib/go_api_client/api/agent.rb +65 -0
- data/lib/go_api_client/domain/agent.rb +12 -0
- data/lib/go_api_client/http_fetcher.rb +20 -41
- data/lib/go_api_client/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9292590704bef4c1e044e3f8ea526f4c8894fa9
|
4
|
+
data.tar.gz: ec1650ae8801c4a8d3fd323eb4d3b3d409eb27e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfa5833ca5651d494ff944c6c3b3f3befbb4fd45087b21b1868b875e95ca7a75ae4b0bc6f4e42ae0a0c3bc4e22e5963a8de925d5c27972fc4b21afd2fec3f516
|
7
|
+
data.tar.gz: 1983b4014f5882a462fcdfc34c4e5a5ddada31511696bc45df79ffb2bc3f471129c396169d9e8eefce2b01ff5d9563a402bcf00322d7931c9a3272b5fd33401b
|
data/lib/go_api_client.rb
CHANGED
@@ -9,6 +9,7 @@ module GoApiClient
|
|
9
9
|
|
10
10
|
module Api
|
11
11
|
autoload :AbstractApi, 'go_api_client/api/abstract_api'
|
12
|
+
autoload :Agent, 'go_api_client/api/agent'
|
12
13
|
autoload :Cctray, 'go_api_client/api/cctray'
|
13
14
|
autoload :Config, 'go_api_client/api/config'
|
14
15
|
autoload :Pipeline, 'go_api_client/api/pipeline'
|
@@ -19,6 +20,7 @@ module GoApiClient
|
|
19
20
|
end
|
20
21
|
|
21
22
|
module Domain
|
23
|
+
autoload :Agent, 'go_api_client/domain/agent'
|
22
24
|
autoload :Author, 'go_api_client/domain/author'
|
23
25
|
autoload :Artifact, 'go_api_client/domain/artifact'
|
24
26
|
autoload :Changeset, 'go_api_client/domain/changeset'
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module GoApiClient
|
4
|
+
module Api
|
5
|
+
class Agent < GoApiClient::Api::AbstractApi
|
6
|
+
|
7
|
+
def initialize(attributes = {})
|
8
|
+
super(attributes)
|
9
|
+
@agent_uri = "#{@base_uri}/api/agents"
|
10
|
+
@agent_headers = {
|
11
|
+
'Content-Type' => 'application/json',
|
12
|
+
'Accept' => 'application/vnd.go.cd.v1+json'
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def agents(options={})
|
17
|
+
options = ({:uuid => nil}).merge(options)
|
18
|
+
uri = options[:uuid] ? "#{@agent_uri}/#{options[:uuid]}" : @agent_uri
|
19
|
+
|
20
|
+
response = JSON.parse(@http_fetcher.get!(uri, {:headers=>@agent_headers}))
|
21
|
+
if response['_embedded']
|
22
|
+
response['_embedded']['agents'].map do |agent_hash|
|
23
|
+
agent_hash.delete('_links')
|
24
|
+
GoApiClient::Domain::Agent.new(agent_hash)
|
25
|
+
end
|
26
|
+
else
|
27
|
+
response.delete('_links')
|
28
|
+
GoApiClient::Domain::Agent.new(response)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# API available since v15.2.0
|
34
|
+
#
|
35
|
+
# Possible options:
|
36
|
+
# * hostname String The new hostname.
|
37
|
+
# * resources String | Array A comma separated strings of resources, or an array of string resources.
|
38
|
+
# * enabled Boolean Whether an agent should be enabled. In case of agents awaiting approval, setting this will approve the agents.
|
39
|
+
#
|
40
|
+
def update(uuid, options={})
|
41
|
+
uri = "#{@agent_uri}/#{uuid}"
|
42
|
+
response = JSON.parse(@http_fetcher.patch!(uri, {:headers=>@agent_headers, :params=>options}))
|
43
|
+
response.delete('_links')
|
44
|
+
GoApiClient::Domain::Agent.new(response)
|
45
|
+
end
|
46
|
+
|
47
|
+
def enable(uuid)
|
48
|
+
update(uuid, {:enabled => true})
|
49
|
+
end
|
50
|
+
|
51
|
+
def disable(uuid)
|
52
|
+
update(uuid, {:enabled => false})
|
53
|
+
end
|
54
|
+
|
55
|
+
def delete(uuid)
|
56
|
+
uri = "#{@agent_uri}/#{uuid}"
|
57
|
+
@http_fetcher.delete!(uri, {:headers=>@agent_headers})
|
58
|
+
true
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
attr_reader :agents_uri
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module GoApiClient
|
2
|
+
module Domain
|
3
|
+
class Agent < GoApiClient::AttributeHelper
|
4
|
+
|
5
|
+
attr_accessor :uuid, :hostname, :ip_address, :enabled, :sandbox, :status, :operating_system, :free_space, :resources, :environments
|
6
|
+
|
7
|
+
def initialize(attributes)
|
8
|
+
super(attributes)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'net/https'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
module GoApiClient
|
5
6
|
class HttpFetcher
|
@@ -44,9 +45,10 @@ module GoApiClient
|
|
44
45
|
@username = options[:username]
|
45
46
|
@password = options[:password]
|
46
47
|
@ssl_verify_mode = options[:ssl_verify_mode]
|
48
|
+
@read_timeout = options[:read_timeout]
|
47
49
|
end
|
48
50
|
|
49
|
-
%w(get post).each do |meth|
|
51
|
+
%w(get post delete patch).each do |meth|
|
50
52
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
51
53
|
def #{meth}!(url, options={})
|
52
54
|
response_body = #{meth}(url, options).body
|
@@ -62,6 +64,10 @@ module GoApiClient
|
|
62
64
|
GoApiClient.logger.error(e.backtrace.collect {|l| " \#{l}"}.join("\n"))
|
63
65
|
raise ConnectionError.new(e)
|
64
66
|
end
|
67
|
+
|
68
|
+
def #{meth}(url, options={}, limit=10)
|
69
|
+
call('#{meth}', url, options, limit)
|
70
|
+
end
|
65
71
|
RUBY_EVAL
|
66
72
|
end
|
67
73
|
|
@@ -78,70 +84,43 @@ module GoApiClient
|
|
78
84
|
end
|
79
85
|
|
80
86
|
private
|
81
|
-
|
82
|
-
|
83
|
-
def get(url, options={}, limit = 10)
|
84
|
-
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
|
85
|
-
uri = URI.parse(url)
|
86
|
-
|
87
|
-
password = options[:password] || uri.password || @password
|
88
|
-
username = options[:username] || uri.user || @username
|
89
|
-
ssl_verify_mode = options[:ssl_verify_mode] || @ssl_verify_mode
|
90
|
-
params = options[:params] || {}
|
91
|
-
|
92
|
-
uri.query = URI.encode_www_form(params) if params.any?
|
93
|
-
|
94
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
95
|
-
http.use_ssl = uri.scheme == 'https'
|
96
|
-
http.verify_mode = ssl_verify_mode == 0 ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
97
|
-
|
98
|
-
|
99
|
-
response = http.start do |http|
|
100
|
-
req = Net::HTTP::Get.new(uri.request_uri)
|
101
|
-
req.basic_auth(username, password) if username || password
|
102
|
-
http.request(req)
|
103
|
-
end
|
104
|
-
|
105
|
-
case response
|
106
|
-
when Net::HTTPRedirection then
|
107
|
-
@response = get(response['location'], options, limit - 1)
|
108
|
-
else
|
109
|
-
@response = response
|
110
|
-
end
|
111
|
-
|
112
|
-
@response
|
113
|
-
end
|
114
|
-
|
115
|
-
def post(url, options={}, limit = 10)
|
87
|
+
def call(method_name, url, options, limit)
|
116
88
|
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
|
117
89
|
uri = URI.parse(url)
|
118
90
|
|
119
91
|
password = options[:password] || uri.password || @password
|
120
92
|
username = options[:username] || uri.user || @username
|
121
93
|
ssl_verify_mode = options[:ssl_verify_mode] || @ssl_verify_mode
|
94
|
+
read_timeout = options[:read_timeout] || @read_timeout || 60
|
122
95
|
params = options[:params] || {}
|
123
96
|
headers = options[:headers] || {}
|
124
97
|
|
125
98
|
http = Net::HTTP.new(uri.host, uri.port)
|
126
99
|
http.use_ssl = uri.scheme == 'https'
|
127
100
|
http.verify_mode = ssl_verify_mode == 0 ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
|
101
|
+
http.read_timeout = read_timeout
|
128
102
|
|
129
|
-
|
103
|
+
class_name = method_name.slice(0, 1).capitalize + method_name.slice(1..-1)
|
104
|
+
method_class = "Net::HTTP::#{class_name}".split('::').inject(Object) { |n, c| n.const_get c }
|
105
|
+
req = method_class.new(uri.request_uri)
|
130
106
|
|
131
107
|
headers.each do |header_name, value|
|
132
108
|
req[header_name] = value
|
133
109
|
end
|
134
110
|
|
135
111
|
req.basic_auth(username, password) if username || password
|
112
|
+
if headers['Content-Type'] && headers['Content-Type'] == 'application/json'
|
113
|
+
req.body = params.to_json
|
114
|
+
else
|
115
|
+
req.set_form_data(params)
|
116
|
+
end
|
136
117
|
|
137
|
-
req.set_form_data(params)
|
138
118
|
response = http.request(req)
|
139
|
-
|
140
119
|
case response
|
141
120
|
when Net::HTTPRedirection then
|
142
|
-
@response =
|
121
|
+
@response = self.send(method_name.to_sym, response['location'], options, limit - 1)
|
143
122
|
else
|
144
|
-
|
123
|
+
@response = response
|
145
124
|
end
|
146
125
|
|
147
126
|
@response
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: go_api_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nikhil Mungel
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: webmock
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- go_api_client.gemspec
|
133
133
|
- lib/go_api_client.rb
|
134
134
|
- lib/go_api_client/api/abstract_api.rb
|
135
|
+
- lib/go_api_client/api/agent.rb
|
135
136
|
- lib/go_api_client/api/cctray.rb
|
136
137
|
- lib/go_api_client/api/config.rb
|
137
138
|
- lib/go_api_client/api/feed.rb
|
@@ -141,6 +142,7 @@ files:
|
|
141
142
|
- lib/go_api_client/api/stage.rb
|
142
143
|
- lib/go_api_client/attribute_helper.rb
|
143
144
|
- lib/go_api_client/client.rb
|
145
|
+
- lib/go_api_client/domain/agent.rb
|
144
146
|
- lib/go_api_client/domain/artifact.rb
|
145
147
|
- lib/go_api_client/domain/author.rb
|
146
148
|
- lib/go_api_client/domain/changeset.rb
|