-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_token
executable file
·51 lines (44 loc) · 1.44 KB
/
get_token
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
require "rubygems"
require "yaml"
require "httparty"
class GetToken
include HTTParty
format :json
def self.get_token(setting)
api_endpoint, client_id, client_secret = get_workable_config(setting)
token_endpoint = [api_endpoint, "token"].join("/")
content = %Q{\{"client_id":"#{client_id}", "grant_type":"none"\}}
options = {
:body => content,
:headers => {"Content-Type" => "application/json"},
:basic_auth=>{:username => client_id, :password => client_secret}
}
response = post(token_endpoint, options)
if response["access_token"]
return response["access_token"]
else
puts "#{token_endpoint}, #{client_id}, #{client_secret}"
raise response.inspect
end
end
def self.get_workable_config(setting)
fog_config_path = ENV['FOG_RC'] || File.expand_path("~/.fog")
fog_config_file = File.open(fog_config_path)
fog_config = YAML.load(fog_config_file)
if fog_config.keys.include?(setting)
api_endpoint = fog_config[setting][:brightbox_api_url] || "https://api.gb1.brightbox.com"
client_id = fog_config[setting][:brightbox_client_id]
client_secret = fog_config[setting][:brightbox_secret]
return api_endpoint, client_id, client_secret
else
raise "#{setting} is not a configured environment in the .fog file"
end
end
end
if ARGV[0]
setting = ARGV[0].to_sym
else
setting = :default
end
puts GetToken.get_token(setting)