mgmt_console 0.4.1

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.
@@ -0,0 +1,257 @@
1
+ require 'sawyer'
2
+ require 'mgmt_console/api_error'
3
+ require 'mgmt_console/authentication'
4
+ require 'mgmt_console/configurable'
5
+ require 'mgmt_console/client/server_environments'
6
+ require 'mgmt_console/client/engage_environments'
7
+ require 'mgmt_console/client/engage_communities'
8
+ require 'mgmt_console/client/engage_servers'
9
+ require 'mgmt_console/client/engage_instances'
10
+ require 'mgmt_console/client/spigit_configs'
11
+
12
+ module MgmtConsole
13
+
14
+ # Client for the Mgmt Console API
15
+ #
16
+ # @see https://console.mindjet.com
17
+ class Client
18
+
19
+ include MgmtConsole::Authentication
20
+ include MgmtConsole::Configurable
21
+ include MgmtConsole::Client::ServerEnvironments
22
+ include MgmtConsole::Client::EngageEnvironments
23
+ include MgmtConsole::Client::EngageCommunities
24
+ include MgmtConsole::Client::EngageServers
25
+ include MgmtConsole::Client::EngageInstances
26
+ include MgmtConsole::Client::SpigitConfigs
27
+
28
+ # Header keys that can be passed in options hash to {#get},{#head}
29
+ CONVENIENCE_HEADERS = Set.new([:accept, :content_type])
30
+
31
+ def initialize(options = {})
32
+ # Use options passed in, but fall back to module defaults
33
+ MgmtConsole::Configurable.keys.each do |key|
34
+ instance_variable_set(:"@#{key}", resolve_opt(options, key))
35
+ end
36
+ end
37
+
38
+ # Compares client options to a Hash of requested options
39
+ #
40
+ # @param opts [Hash] Options to compare with current client options
41
+ # @return [Boolean]
42
+ def same_options?(opts)
43
+ opts.hash == options.hash
44
+ end
45
+
46
+ # Text representation of the client, masking tokens and passwords
47
+ #
48
+ # @return [String]
49
+ def inspect
50
+ inspected = super
51
+
52
+ # Only show last 4 of token, secret
53
+ if @access_token
54
+ inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
55
+ end
56
+
57
+ inspected
58
+ end
59
+
60
+ # Make a HTTP GET request
61
+ #
62
+ # @param url [String] The path, relative to {#api_endpoint}
63
+ # @param options [Hash] Query and header params for request
64
+ # @return [Sawyer::Resource]
65
+ def get(url, options = {})
66
+ request :get, url, parse_query_and_convenience_headers(options)
67
+ end
68
+
69
+ # Make a HTTP POST request
70
+ #
71
+ # @param url [String] The path, relative to {#api_endpoint}
72
+ # @param options [Hash] Body and header params for request
73
+ # @return [Sawyer::Resource]
74
+ def post(url, options = {})
75
+ request :post, url, options
76
+ end
77
+
78
+ # Make a HTTP PUT request
79
+ #
80
+ # @param url [String] The path, relative to {#api_endpoint}
81
+ # @param options [Hash] Body and header params for request
82
+ # @return [Sawyer::Resource]
83
+ def put(url, options = {})
84
+ request :put, url, options
85
+ end
86
+
87
+ # Make a HTTP PATCH request
88
+ #
89
+ # @param url [String] The path, relative to {#api_endpoint}
90
+ # @param options [Hash] Body and header params for request
91
+ # @return [Sawyer::Resource]
92
+ def patch(url, options = {})
93
+ request :patch, url, options
94
+ end
95
+
96
+ # Make a HTTP DELETE request
97
+ #
98
+ # @param url [String] The path, relative to {#api_endpoint}
99
+ # @param options [Hash] Query and header params for request
100
+ # @return [Sawyer::Resource]
101
+ def delete(url, options = {})
102
+ request :delete, url, options
103
+ end
104
+
105
+ # Make a HTTP HEAD request
106
+ #
107
+ # @param url [String] The path, relative to {#api_endpoint}
108
+ # @param options [Hash] Query and header params for request
109
+ # @return [Sawyer::Resource]
110
+ def head(url, options = {})
111
+ request :head, url, parse_query_and_convenience_headers(options)
112
+ end
113
+
114
+ # Make one or more HTTP GET requests, optionally fetching
115
+ # the next page of results from URL in Link response header based
116
+ # on value in {#auto_paginate}.
117
+ #
118
+ # @param url [String] The path, relative to {#api_endpoint}
119
+ # @param options [Hash] Query and header params for request
120
+ # @param block [Block] Block to perform the data concatination of the
121
+ # multiple requests. The block is called with two parameters, the first
122
+ # contains the contents of the requests so far and the second parameter
123
+ # contains the latest response.
124
+ # @return [Sawyer::Resource]
125
+ def paginate(url, options = {}, &block)
126
+ opts = parse_query_and_convenience_headers(options.dup)
127
+ if @auto_paginate || @per_page
128
+ opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil)
129
+ end
130
+
131
+ data = request(:get, url, opts)
132
+
133
+ if @auto_paginate
134
+ while @last_response.rels[:next]# && rate_limit.remaining > 0
135
+ @last_response = @last_response.rels[:next].get
136
+ if block_given?
137
+ yield(data, @last_response)
138
+ else
139
+ data.concat(@last_response.data) if @last_response.data.is_a?(Array)
140
+ end
141
+ end
142
+
143
+ end
144
+
145
+ data
146
+ end
147
+
148
+ # Hypermedia agent for the GitHub API
149
+ #
150
+ # @return [Sawyer::Agent]
151
+ def agent
152
+ @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
153
+ http.headers[:accept] = default_media_type
154
+ http.headers[:content_type] = "application/json"
155
+ http.headers[:user_agent] = user_agent
156
+ http.ssl[:verify] = ssl_verify
157
+
158
+ if token_authenticated?
159
+ http.headers['X-User-Token'] = @access_token
160
+ end
161
+ end
162
+ end
163
+
164
+ # Fetch the root resource for the API
165
+ #
166
+ # @return [Sawyer::Resource]
167
+ def root
168
+ get "/"
169
+ end
170
+
171
+ # Response for last HTTP request
172
+ #
173
+ # @return [Sawyer::Response]
174
+ def last_response
175
+ @last_response if defined? @last_response
176
+ end
177
+
178
+ # Set OAuth access token for authentication
179
+ #
180
+ # @param value [String] 40 character GitHub OAuth access token
181
+ def access_token=(value)
182
+ reset_agent
183
+ @access_token = value
184
+ end
185
+
186
+ # Wrapper around Kernel#warn to print warnings unless
187
+ # OCTOKIT_SILENT is set to true.
188
+ #
189
+ # @return [nil]
190
+ def octokit_warn(*message)
191
+ unless ENV['OCTOKIT_SILENT']
192
+ warn message
193
+ end
194
+ end
195
+
196
+ private
197
+
198
+ def resolve_opt(options, key)
199
+ !options[key].nil? ? options[key] : MgmtConsole.instance_variable_get(:"@#{key}")
200
+ end
201
+
202
+ def reset_agent
203
+ @agent = nil
204
+ end
205
+
206
+ def request(method, path, data, options = {})
207
+ if data.is_a?(Hash)
208
+ options[:query] = data.delete(:query) || {}
209
+ options[:headers] = data.delete(:headers) || {}
210
+ if accept = data.delete(:accept)
211
+ options[:headers][:accept] = accept
212
+ end
213
+ end
214
+
215
+ @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
216
+ response.data
217
+ end
218
+
219
+ # Executes the request, checking if it was successful
220
+ #
221
+ # @return [Boolean] True on success, false otherwise
222
+ def boolean_from_response(method, path, options = {})
223
+ request(method, path, options)
224
+ @last_response.status == 204
225
+ rescue MgmtConsole::NotFound
226
+ false
227
+ end
228
+
229
+
230
+ def sawyer_options
231
+ opts = {
232
+ :links_parser => Sawyer::LinkParsers::Simple.new
233
+ }
234
+ conn_opts = @connection_options
235
+ conn_opts[:builder] = @middleware if @middleware
236
+ conn_opts[:proxy] = @proxy if @proxy
237
+ opts[:faraday] = Faraday.new(conn_opts)
238
+
239
+ opts
240
+ end
241
+
242
+ def parse_query_and_convenience_headers(options)
243
+ headers = options.fetch(:headers, {})
244
+ CONVENIENCE_HEADERS.each do |h|
245
+ if header = options.delete(h)
246
+ headers[h] = header
247
+ end
248
+ end
249
+ query = options.delete(:query)
250
+ opts = {:query => options}
251
+ opts[:query].merge!(query) if query && query.is_a?(Hash)
252
+ opts[:headers] = headers unless headers.empty?
253
+
254
+ opts
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,110 @@
1
+ module MgmtConsole
2
+
3
+ # Configuration options for {Client}, defaulting to values
4
+ # in {Default}
5
+ module Configurable
6
+ # @!attribute [w] access_token
7
+ # @see https://developer.github.com/v3/oauth/
8
+ # @return [String] OAuth2 access token for authentication
9
+ # @!attribute api_endpoint
10
+ # @return [String] Base URL for API requests. default: https://api.github.com/
11
+ # @!attribute auto_paginate
12
+ # @return [Boolean] Auto fetch next page of results until rate limit reached
13
+ # @!attribute client_id
14
+ # @see https://developer.github.com/v3/oauth/
15
+ # @return [String] Configure OAuth app key
16
+ # @!attribute [w] client_secret
17
+ # @see https://developer.github.com/v3/oauth/
18
+ # @return [String] Configure OAuth app secret
19
+ # @!attribute default_media_type
20
+ # @see https://developer.github.com/v3/media/
21
+ # @return [String] Configure preferred media type (for API versioning, for example)
22
+ # @!attribute connection_options
23
+ # @see https://github.com/lostisland/faraday
24
+ # @return [Hash] Configure connection options for Faraday
25
+ # @!attribute login
26
+ # @return [String] GitHub username for Basic Authentication
27
+ # @!attribute middleware
28
+ # @see https://github.com/lostisland/faraday
29
+ # @return [Faraday::Builder or Faraday::RackBuilder] Configure middleware for Faraday
30
+ # @!attribute ssl_verify
31
+ # @return [Boolean] Configure SSL verification for Faraday
32
+ # @!attribute netrc
33
+ # @return [Boolean] Instruct MgmtConsole to get credentials from .netrc file
34
+ # @!attribute netrc_file
35
+ # @return [String] Path to .netrc file. default: ~/.netrc
36
+ # @!attribute [w] password
37
+ # @return [String] GitHub password for Basic Authentication
38
+ # @!attribute per_page
39
+ # @return [String] Configure page size for paginated results. API default: 30
40
+ # @!attribute proxy
41
+ # @see https://github.com/lostisland/faraday
42
+ # @return [String] URI for proxy server
43
+ # @!attribute user_agent
44
+ # @return [String] Configure User-Agent header for requests.
45
+ # @!attribute web_endpoint
46
+ # @return [String] Base URL for web URLs. default: https://github.com/
47
+
48
+ attr_accessor :access_token, :auto_paginate,
49
+ :default_media_type, :connection_options,
50
+ :middleware, :ssl_verify,
51
+ :per_page, :proxy, :user_agent
52
+ attr_writer :web_endpoint, :api_endpoint
53
+
54
+ class << self
55
+
56
+ # List of configurable keys for {MgmtConsole::Client}
57
+ # @return [Array] of option keys
58
+ def keys
59
+ @keys ||= [
60
+ :access_token,
61
+ :api_endpoint,
62
+ :auto_paginate,
63
+ :connection_options,
64
+ :default_media_type,
65
+ :middleware,
66
+ :per_page,
67
+ :proxy,
68
+ :ssl_verify,
69
+ :user_agent,
70
+ :web_endpoint
71
+ ]
72
+ end
73
+ end
74
+
75
+ # Set configuration options using a block
76
+ def configure
77
+ yield self
78
+ end
79
+
80
+ # Reset configuration options to default values
81
+ def reset!
82
+ MgmtConsole::Configurable.keys.each do |key|
83
+ instance_variable_set(:"@#{key}", MgmtConsole::Default.options[key])
84
+ end
85
+ self
86
+ end
87
+ alias setup reset!
88
+
89
+ def api_endpoint
90
+ File.join(@api_endpoint, "")
91
+ end
92
+
93
+ def ssl_verify
94
+ @ssl_verify
95
+ end
96
+
97
+ # Base URL for generated web URLs
98
+ #
99
+ # @return [String] Default: https://github.com/
100
+ def web_endpoint
101
+ File.join(@web_endpoint, "")
102
+ end
103
+
104
+ private
105
+
106
+ def options
107
+ Hash[MgmtConsole::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,125 @@
1
+ require 'mgmt_console/response/raise_error'
2
+ require 'mgmt_console/response/feed_parser'
3
+ require 'mgmt_console/version'
4
+
5
+ module MgmtConsole
6
+
7
+ # Default configuration options for {Client}
8
+ module Default
9
+
10
+ # Default API endpoint
11
+ API_ENDPOINT = "https://console.mindjet.com/api/v1".freeze
12
+
13
+ # Default User Agent header string
14
+ USER_AGENT = "Mgmt Console Ruby Gem #{MgmtConsole::VERSION}".freeze
15
+
16
+ # Default SSL verify
17
+ SSL_VERIFY = true.freeze
18
+
19
+ # Default media type
20
+ MEDIA_TYPE = "application/json".freeze
21
+
22
+ # Default WEB endpoint
23
+ WEB_ENDPOINT = "https://console.mindjet.com".freeze
24
+
25
+ # In Faraday 0.9, Faraday::Builder was renamed to Faraday::RackBuilder
26
+ RACK_BUILDER_CLASS = defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
27
+
28
+ # Default Faraday middleware stack
29
+ MIDDLEWARE = RACK_BUILDER_CLASS.new do |builder|
30
+ builder.use MgmtConsole::Response::RaiseError
31
+ builder.use MgmtConsole::Response::FeedParser
32
+ builder.adapter Faraday.default_adapter
33
+ end
34
+
35
+ class << self
36
+
37
+ # Configuration options
38
+ # @return [Hash]
39
+ def options
40
+ Hash[MgmtConsole::Configurable.keys.map{|key| [key, send(key)]}]
41
+ end
42
+
43
+ # Default access token from ENV
44
+ # @return [String]
45
+ def access_token
46
+ ENV['MGMT_CONSOLE_ACCESS_TOKEN']
47
+ end
48
+
49
+ # Default API endpoint from ENV or {API_ENDPOINT}
50
+ # @return [String]
51
+ def api_endpoint
52
+ ENV['MGMT_CONSOLE_API_ENDPOINT'] || API_ENDPOINT
53
+ end
54
+
55
+ # Default pagination preference from ENV
56
+ # @return [String]
57
+ def auto_paginate
58
+ ENV['MGMT_CONSOLE_AUTO_PAGINATE']
59
+ end
60
+
61
+ # Default options for Faraday::Connection
62
+ # @return [Hash]
63
+ def connection_options
64
+ {
65
+ :headers => {
66
+ :accept => default_media_type,
67
+ :user_agent => user_agent
68
+ }
69
+ }
70
+ end
71
+
72
+ # Default ssl verify from ENV or {SSL_VERIFY}
73
+ # @return [Boolean]
74
+ def ssl_verify
75
+ ENV['MGMT_CONSOLE_SSL_VERIFY'] || SSL_VERIFY
76
+ end
77
+
78
+ # Default media type from ENV or {MEDIA_TYPE}
79
+ # @return [String]
80
+ def default_media_type
81
+ ENV['MGMT_CONSOLE_DEFAULT_MEDIA_TYPE'] || MEDIA_TYPE
82
+ end
83
+
84
+ # Default middleware stack for Faraday::Connection
85
+ # from {MIDDLEWARE}
86
+ # @return [String]
87
+ def middleware
88
+ MIDDLEWARE
89
+ end
90
+
91
+ # Default GitHub password for Basic Auth from ENV
92
+ # @return [String]
93
+ def password
94
+ ENV['MGMT_CONSOLE_PASSWORD']
95
+ end
96
+
97
+ # Default pagination page size from ENV
98
+ # @return [Fixnum] Page size
99
+ def per_page
100
+ page_size = ENV['MGMT_CONSOLE_PER_PAGE']
101
+
102
+ page_size.to_i if page_size
103
+ end
104
+
105
+ # Default proxy server URI for Faraday connection from ENV
106
+ # @return [String]
107
+ def proxy
108
+ ENV['MGMT_CONSOLE_PROXY']
109
+ end
110
+
111
+ # Default User-Agent header string from ENV or {USER_AGENT}
112
+ # @return [String]
113
+ def user_agent
114
+ ENV['MGMT_CONSOLE_USER_AGENT'] || USER_AGENT
115
+ end
116
+
117
+ # Default web endpoint from ENV or {WEB_ENDPOINT}
118
+ # @return [String]
119
+ def web_endpoint
120
+ ENV['MGMT_CONSOLE_WEB_ENDPOINT'] || WEB_ENDPOINT
121
+ end
122
+
123
+ end
124
+ end
125
+ end