class TpmApi
Public Class Methods
delete(url)
click to toggle source
delete method api url ex: “/projects/ID.json” => delete project with id=ID
# File lib/tpm_api.rb, line 53 def self.delete(url) HTTParty.delete("#{self.url}#{url}", :body => options.to_json, :headers => {'Content-Type' =>'application/json; charset=utf-8'}, :basic_auth => {:username => self.username, :password => self.pwd}) end
get(url, options = {})
click to toggle source
get method api url ex: “/passwords.json” => get list passwords options is hash data query.
# File lib/tpm_api.rb, line 33 def self.get(url, options = {}) HTTParty.get("#{self.url}#{url}", :headers => {'Content-Type' =>'application/json; charset=utf-8'}, :basic_auth => {:username => self.username, :password => self.pwd} ) end
init()
click to toggle source
load default setting
# File lib/tpm_api.rb, line 10 def self.init cnf = YAML.load(ERB.new(File.read("#{Rails.root}/config/tpm_api.yml")).result)[Rails.env] self.url = cnf["url"] self.username = cnf["username"] self.pwd = cnf["pwd"] self.label_private_key = cnf["label_private_key"] || "Private Key" # find or create default project by name project_name = cnf["project_name"] || "Tapptic's internal certificate renewal tool" project_tags = cnf["project_tags"] || "project" project = TpmApi.get("/projects/search/#{CGI.escape(project_name)}.json").try(:first) if project.present? puts "/projects/search/#{URI::encode(project_name)}.json" self.project_id = project["id"] else self.project_id = TpmApi.post("/projects.json", {name: project_name, tags: project_tags})["id"] end end
post(url, options = {})
click to toggle source
post method api url ex: “/projects.json” => create passwords options is hash data query. ex: TpmApi.post
(“/projects”, {name: 'abc', tags: 'test,tool'})
# File lib/tpm_api.rb, line 40 def self.post(url, options = {}) HTTParty.post("#{self.url}#{url}", :body => options.to_json, :headers => {'Content-Type' =>'application/json; charset=utf-8'}, :basic_auth => {:username => self.username, :password => self.pwd}) end
put(url, options = {})
click to toggle source
put method api url ex: “/passwords/ID.json” => update password with id=ID options is hash data query. ex: TpmApi.put
(“/passwords/1.json”, {name: 'change name password'})
# File lib/tpm_api.rb, line 47 def self.put(url, options = {}) HTTParty.put("#{self.url}#{url}", :body => options.to_json, :headers => {'Content-Type' =>'application/json; charset=utf-8'}, :basic_auth => {:username => self.username, :password => self.pwd}) end
upload_private_key(name, passphrase="", content)
click to toggle source
upload private key will call 2 api: 1- create password with pwd=passphrase, name=name, custom_field=content 2- update label_custom_field to math with config in file tpm_api.yml [label_private_key]
# File lib/tpm_api.rb, line 60 def self.upload_private_key(name, passphrase="", content) new_pwd = TpmApi.post("/passwords.json", {name: name, password: passphrase, project_id: self.project_id, :custom_data1 => content}) TpmApi.put("/passwords/#{new_pwd['id']}/custom_fields.json", {:custom_label1=>self.label_private_key, :custom_type1 =>"text"}) new_pwd end