Module: Xolo::Admin::Connection
- Defined in:
- lib/xolo/admin/connection.rb
Overview
connection to the xolo server from xadm
Constant Summary collapse
- TIMEOUT =
Constants
300- UPLOAD_TIMEOUT =
1800- OPEN_TIMEOUT =
10- PING_ROUTE =
'/ping'- PING_RESPONSE =
'pong'- LOGIN_ROUTE =
'/auth/login'
Class Method Summary collapse
-
.included(includer) ⇒ Object
when this module is included.
Instance Method Summary collapse
-
#login(test: false) ⇒ Object
Authenticate to the Xolo server and get a session token (maintained by Faraday via the Xolo::Admin::CookieJar middleware).
-
#server_cnx(host: nil) ⇒ Faraday::Connection
A connection for requests without any file uploads.
-
#server_url(host: nil) ⇒ URI
The server base URL.
-
#ssl_opts ⇒ Hash
The SSL options for Faraday connections.
-
#streaming_cnx(host: nil) ⇒ Faraday::Connection
A connection for responses that stream the progress of a long server process.
-
#upload_cnx(host: nil) ⇒ Faraday::Connection
A connection for POST requests with file uploads.
Class Method Details
.included(includer) ⇒ Object
when this module is included
36 37 38 |
# File 'lib/xolo/admin/connection.rb', line 36 def self.included(includer) Xolo.verbose_include includer, self end |
Instance Method Details
#login(test: false) ⇒ Object
Authenticate to the Xolo server and get a session token (maintained by Faraday via the Xolo::Admin::CookieJar middleware)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/xolo/admin/connection.rb', line 48 def login(test: false) return if !test && cmd_details[:no_login] hostname = config.hostname admin = config.admin pw = fetch_pw xadm = Xolo::Admin::EXECUTABLE_FILENAME raise Xolo::MissingDataError, "No xolo server hostname. Please run '#{xadm} config'" unless hostname raise Xolo::MissingDataError, "No xolo admin username. Please run '#{xadm} config'" unless admin payload = { admin: admin, password: pw } payload[:proxy_admin] = global_opts[:proxy_admin] if global_opts[:proxy_admin] # provide the hostname to make a persistent Faraday connection object # so in the future we just call server_cnx with no hostname to get the same # connection object resp = server_cnx.post Xolo::Admin::Connection::LOGIN_ROUTE, payload if resp.success? @logged_in = true return end case resp.status when 401 raise Xolo::AuthenticationError, resp.body[:error] else raise Xolo::ServerError, "#{resp.status}: #{resp.body}" end rescue Faraday::UnauthorizedError msg = "Invalid username or password. If you recently changed your Jamf Pro password, please update it using 'xadm config'." raise Xolo::AuthenticationError, msg end |
#server_cnx(host: nil) ⇒ Faraday::Connection
A connection for requests without any file uploads
None of our GET routes expected any request body, so it doesn’t matter if its set to be JSON.
For our POST routes that dont upload files (e.g. setloglevel), the request body, if any, will be JSON.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/xolo/admin/connection.rb', line 119 def server_cnx(host: nil) @server_cnx = nil if host return @server_cnx if @server_cnx @server_cnx = Faraday.new(server_url(host: host), ssl: ssl_opts) do |cnx| cnx.[:timeout] = TIMEOUT cnx.[:open_timeout] = OPEN_TIMEOUT cnx.request :json cnx.use Xolo::Admin::CookieJar cnx.response :json, parser_options: { symbolize_names: true } cnx.response :raise_error cnx.adapter :net_http end # @server_cnx.headers['X-Proxy-Admin'] = global_opts[:proxy_admin] if global_opts[:proxy_admin] end |
#server_url(host: nil) ⇒ URI
Returns The server base URL.
99 100 101 102 103 104 |
# File 'lib/xolo/admin/connection.rb', line 99 def server_url(host: nil) @server_url = nil if host return @server_url if @server_url @server_url = URI.parse "https://#{host || config.hostname}" end |
#ssl_opts ⇒ Hash
Returns the SSL options for Faraday connections.
86 87 88 89 90 91 92 93 |
# File 'lib/xolo/admin/connection.rb', line 86 def ssl_opts # true if nil or true, false if false verify = config.ssl_verify.nil? || config.ssl_verify { verify: verify } end |
#streaming_cnx(host: nil) ⇒ Faraday::Connection
A connection for responses that stream the progress of a long server process.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/xolo/admin/connection.rb', line 148 def streaming_cnx(host: nil) @streaming_cnx = nil if host return @streaming_cnx if @streaming_cnx # this proc every time we get a chunk, just print it to stdout # and make note if any of them conain an error streaming_proc = proc do |chunk, _size, _env| puts chunk @streaming_error ||= chunk.include? STREAMING_OUTPUT_ERROR end req_opts = { on_data: streaming_proc } @streaming_cnx = Faraday.new(server_url(host: host), request: req_opts, ssl: ssl_opts) do |cnx| cnx.[:timeout] = TIMEOUT cnx.[:open_timeout] = OPEN_TIMEOUT cnx.use Xolo::Admin::CookieJar cnx.response :raise_error cnx.adapter :net_http end end |
#upload_cnx(host: nil) ⇒ Faraday::Connection
A connection for POST requests with file uploads
The request body will be multipart/url-encoded and authentication is required
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/xolo/admin/connection.rb', line 180 def upload_cnx(host: nil) @upload_cnx = nil if host return @upload_cnx if @upload_cnx @upload_cnx = Faraday.new(server_url(host: host), ssl: ssl_opts) do |cnx| cnx.[:timeout] = UPLOAD_TIMEOUT cnx.[:open_timeout] = OPEN_TIMEOUT cnx.request :multipart cnx.request :url_encoded cnx.use Xolo::Admin::CookieJar cnx.response :json, parser_options: { symbolize_names: true } cnx.response :raise_error cnx.adapter :net_http end end |