Class: Fog::Compute::OpenStack::KeyPair

Inherits:
OpenStack::Model show all
Defined in:
lib/fog/compute/openstack/models/key_pair.rb

Instance Attribute Summary collapse

Attributes inherited from OpenStack::Model

#project

Instance Method Summary collapse

Methods inherited from OpenStack::Model

#create, #initialize, #update

Constructor Details

This class inherits a constructor from Fog::OpenStack::Model

Instance Attribute Details

#public_keyObject

Returns the value of attribute public_key


15
16
17
# File 'lib/fog/compute/openstack/models/key_pair.rb', line 15

def public_key
  @public_key
end

Instance Method Details

#destroyObject


17
18
19
20
21
22
# File 'lib/fog/compute/openstack/models/key_pair.rb', line 17

def destroy
  requires :name

  service.delete_key_pair(name)
  true
end

#saveObject


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/compute/openstack/models/key_pair.rb', line 24

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


50
51
52
# File 'lib/fog/compute/openstack/models/key_pair.rb', line 50

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

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


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/compute/openstack/models/key_pair.rb', line 37

def write(path = "#{ENV['HOME']}/.ssh/fog_#{Fog.credential}_#{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