Class: TpmApi

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/tpm_api.rb

Class Method Summary collapse

Class Method Details

.delete(url) ⇒ Object

delete method api url ex: “/projects/ID.json” => delete project with id=ID



53
54
55
# 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 = {}) ⇒ Object

get method api url ex: “/passwords.json” => get list passwords options is hash data query.



33
34
35
# 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

.initObject

load default setting



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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 = {}) ⇒ Object

post method api url ex: “/projects.json” => create passwords options is hash data query. ex: TpmApi.post(“/projects”, ‘abc’, tags: ‘test,tool’)



40
41
42
# 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 = {}) ⇒ Object

put method api url ex: “/passwords/ID.json” => update password with id=ID options is hash data query. ex: TpmApi.put(“/passwords/1.json”, ‘change name password’)



47
48
49
# 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) ⇒ Object

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]



60
61
62
63
64
# 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