-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added lib/installation/scc_client.rb
for connecting to the SCC server
- Loading branch information
Ladislav Slezak
committed
Dec 17, 2013
1 parent
19cd17d
commit d7b52be
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# encoding: utf-8 | ||
|
||
require "yast" | ||
|
||
require "json" | ||
|
||
# HTTP libs | ||
require "uri" | ||
require "net/http" | ||
|
||
module Yast | ||
|
||
class SccClient | ||
|
||
attr_accessor :url, :email, :reg_code | ||
|
||
# FIXME: internal testing SCC instance, change to the public production server later | ||
DEFAULT_SCC_URL = "http://10.122.166.25:3000" | ||
|
||
MAX_REDIRECTS = 10 | ||
|
||
def initialize | ||
|
||
end | ||
|
||
# initial registration via API | ||
# TODO: proxy support? (http://apidock.com/ruby/Net/HTTP) | ||
def announce(location = nil, redirects = MAX_REDIRECTS) | ||
raise RuntimeError, "Reached maximum number of HTTP redirects, aborting" if redirects == 0 | ||
|
||
scc_url = URI(location || DEFAULT_SCC_URL + "/connect/announce") | ||
|
||
http = Net::HTTP.new(scc_url.host, scc_url.port) | ||
|
||
if scc_url.scheme.downcase == "https" | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_PEER | ||
end | ||
|
||
request = Net::HTTP::Post.new(scc_url.request_uri) | ||
# see https://github.com/SUSE/happy-customer/wiki/Connect-API#wiki-sys_create | ||
request["Authorization"] = "Token token=\"#{token}\"" | ||
request["Content-Type"] = "application/json" | ||
request["Accept"] = "application/json" | ||
|
||
# FIXME | ||
request.body = { | ||
"email" => email, | ||
"hostname" => "Foo", | ||
"hwinfo": { | ||
"sockets": 2, | ||
"graphics": "nvidia" | ||
} | ||
}.to_json | ||
|
||
response = http.request(request) | ||
|
||
case response | ||
when Net::HTTPSuccess then | ||
if response["content-type"] == "application/json" | ||
result = JSON.parse(response.body) | ||
else | ||
raise RuntimeError, "Unexpected content-type: #{response['content-type']}" | ||
end | ||
when Net::HTTPRedirection then | ||
location = response['location'] | ||
Builtins.y2milestone("Redirected to #{location}") | ||
announce(location, redirects - 1) | ||
else | ||
# TODO error handling | ||
response.value | ||
end | ||
|
||
end | ||
end | ||
end |