Class: Fog::Compute::HPV2::KeyPair

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/hp/models/compute_v2/key_pair.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject


14
15
16
17
18
# File 'lib/fog/hp/models/compute_v2/key_pair.rb', line 14

def destroy
  requires :name
  service.delete_key_pair(name)
  true
end

#saveObject


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/hp/models/compute_v2/key_pair.rb', line 20

def save
  requires :name
  data = if public_key
    service.create_key_pair(name, public_key).body['keypair']
  else
    service.create_key_pair(name).body['keypair']
  end
  new_attributes = data.reject {|key,value| !['fingerprint', 'public_key', 'name', 'private_key', 'user_id'].include?(key)}
  merge_attributes(new_attributes)
  true
end

#writable?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/fog/hp/models/compute_v2/key_pair.rb', line 45

def writable?
  !!(private_key && ENV.key?('HOME'))
end

#write(path = "#{ENV['HOME']}/.ssh/hp_#{Fog.credential.to_s}_#{name}.pem") ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fog/hp/models/compute_v2/key_pair.rb', line 32

def write(path="#{ENV['HOME']}/.ssh/hp_#{Fog.credential.to_s}_#{name}.pem")
  if writable?
    split_private_key = private_key.split(/\n/)
    File.open(path, "w") do |f|
      split_private_key.each {|line| f.puts line}
      f.chmod 0600
    end
    "Key file built: #{path}"
  else
    'Invalid private key'
  end
end