class TarkinClient
Constants
- API
- SETTINGS_FILES
Attributes
Public Class Methods
Constructor. Needs to know the Tarkin Server parameters.
- They can be passed with three differen ways
-
the options hash. Notice that in this case the token will not be stored in the ~/.tarkin file >> tc =
TarkinClient.new
email: 'user@example.com', password: 'password0', tarkin_url: 'tarkin.tg.pl' #TarkinClient
<server: tarkin.tg.pl, authorized: true> >> tc =TarkinClient.new
tarkin_url: 'tarkin.tg.pl', token: 'Ahoishwqbn32ldhw.…..' #TarkinClient
<server: tarkin.tg.pl, authorized: true> -
by reading the stored parameters in settings file (like ~/.tarkin)
>> tc =
TarkinClient.new
#TarkinClient
<server: localhost:3000, authorized: true> -
by asking user via command line (when file not found) >> tc =
TarkinClient.new
# Your Tarkin server URL: |tarkin.tg.pl| localhost:3000 # Your Tarkin account email: |user@example.com| grychu@gmail.com # Password for grychu@gmail.com: ******** #TarkinClient
<server: localhost:3000, authorized: true>
# File lib/tarkin.rb, line 34 def initialize(**options) @authorized = false @settings = {} if options[:email] && options[:password] && options[:tarkin_url] @settings = options.select { |k,v| [:email, :tarkin_url].include? k } @settings[:token] = get_token(@settings[:email], options[:password]) elsif options[:token] && options[:tarkin_url] @settings[:tarkin_url] = options[:tarkin_url] @settings[:token] = options[:token] else get_settings save_settings end @api_client = ARest.new(api_url, token: @settings[:token]) end
Public Instance Methods
Search for a username or directory Input: a String
to search, may contain wildcard (*) Output: array of hashes: [ { :label, :redirect_to }]
> tc.find 'sys' #> [{:label=>"/db/prod/oracle/C84PROD/sys", :redirect_to=>"/db/prod/oracle/C84PROD#4"}, {:label=>"/db/prod/oracle/C84PROD/sysadm", :redirect_to=>"/db/prod/oracle/C84PROD#5"}]
# File lib/tarkin.rb, line 119 def find(term) response = @api_client.get("_find.json", form_data: {term: term}) if response.ok? response.deserialize else raise TarkinClientException, "Can't search for '#{term}', server returns #{response.code}: #{response.message}" end end
# File lib/tarkin.rb, line 141 def inspect "TarkinClient <server: #{@settings[:tarkin_url]}, authorized: #{authorized?}>" end
Returns the content of given directory Output is a hash containing:
-
directories => [ {:name, :id, :created_at, :updated_at, :description} ]
-
items => [ {:username, :id, :created_at, :updated_at, :description} ]
> tc.ls '/db/prod/oracle' #> {:directories=>
[{:name=>"C84PROD", :id=>14, :created_at=>"2015-06-07T10:36:56.463Z", :updated_at=>"2015-06-07T10:36:56.463Z", :description=>"Production"}], :items=> [{:id=>6, :username=>"scott", :created_at=>"2015-06-07T10:38:27.981Z", :updated_at=>"2015-06-07T10:38:27.981Z", :description=>"The same user in all production databases"}]}
# File lib/tarkin.rb, line 97 def ls(path = '/') u = path.strip.chomp.sub(/(\/)+$/,'') # remove trailing slashes u = if u == '' then '_dir.json' else "_dir/#{u}.json" end response = @api_client.get(u) if response.ok? response.deserialize else if response.code == "404" raise TarkinClientException, "No such file or directory" else raise TarkinClientException, "Can't list directory, server returns #{response.code}: #{response.message}" end end end
Returns Hash containing :id, :username and :password Gets Item.id or full path to the Item as a parameter
>> tc.password(107) # { # "id" => 110, # "username" => "sysdba", # "password" => "secret_top" # } >> tc.password('/db/oracle/C84PCPY/sysdba') # { # "id" => 110, # "username" => "sysdba", # "password" => "secret_top" # }
# File lib/tarkin.rb, line 66 def password(path_or_id) case path_or_id when String # full path given u = "#{path_or_id}.json" when Fixnum # Item ID given u = "_password/#{path_or_id}.json" end response = @api_client.get(u) if response.ok? response.deserialize else raise TarkinClientException, "Can't get password, server returns #{response.code}: #{response.message}" end end
Returns string with valid token
> tc.token #> "zvaY5...sds="
# File lib/tarkin.rb, line 131 def token @settings[:token] end
Private Instance Methods
# File lib/tarkin.rb, line 146 def api_url "#{@settings[:tarkin_url]}/#{API}" end
# File lib/tarkin.rb, line 196 def check_connectivity # @authorized = api_uri['_ping'].get.ok? unless @authorized # @authorized = "#{api_url}/_ping".to_uri.get("", "Authorization" => "Token token=#{@settings[:token]}").ok? unless @authorized @api_client.get("_ping").ok? unless @authorized end
# File lib/tarkin.rb, line 150 def get_settings settings_file = SETTINGS_FILES.find {|file| File.exists? file} if settings_file @settings = YAML::load_file settings_file else new_settings end end
# File lib/tarkin.rb, line 179 def get_token(email, password) begin client = ARest.new(api_url, username: email, password: password) response = client.get('_authorize.json') rescue SocketError say "<%= color('Cannot connect to server.', BOLD) %> Please retry." return nil end unless response.ok? say "<%= color('#{response.message}', BOLD) %>. Please retry." nil else @authorized = true response.deserialize[:token] end end
# File lib/tarkin.rb, line 159 def new_settings @settings = Hash.new until @settings[:token] @settings[:tarkin_url] = ask("Your <%= color('Tarkin', BOLD) %> server URL: ") do |q| q.default = @settings[:tarkin_url] || "http://tarkin.tg.pl" q.validate = /^(http|https):\/\/.*/ix end @settings[:email] = ask("Your <%= color('Tarkin', BOLD) %> account email: ") do |q| q.default = @settings[:email] || "user@example.com" q.validate = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i end password = ask("Password for #{@settings[:email]}: ") { |q| q.echo = "*" } @settings[:token] = get_token(@settings[:email], password) end end
# File lib/tarkin.rb, line 175 def save_settings File.open(SETTINGS_FILES.first, 'w') { |f| f.puts @settings.to_yaml } end