Class: Fog::Compute::QingCloud::KeyPair

Inherits:
QingCloud::Model show all
Defined in:
lib/fog/qingcloud/models/compute/key_pair.rb

Instance Method Summary collapse

Methods inherited from QingCloud::Model

#changing?, inherited, #wait_for

Instance Method Details

#attach(server_id) ⇒ Object



62
63
64
65
66
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 62

def attach(server_id)
  requires :id
  service.attach_key_pairs(id, server_id)
  true
end

#destroyObject



19
20
21
22
23
24
25
26
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 19

def destroy
  requires :id
  service.delete_key_pairs(id)
  true
rescue Fog::QingCloud::Errors::PermissionDenied => e
  raise e unless e.message =~ /has already been deleted/i
  true
end

#detach(server_id) ⇒ Object



68
69
70
71
72
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 68

def detach(server_id)
  requires :id
  service.detach_key_pairs(id, server_id)
  true
end

#saveObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 28

def save
  if persisted?
    modify_attributes(name, description)
  else
    data = if public_key
      service.create_key_pair(name, 'user', 'ignore', public_key).body
    else
      encrypt_method ||= 'ssh-rsa'
      service.create_key_pair(name, 'system', encrypt_method, 'ignore').body
    end
    merge_attributes('keypair_id' => data['keypair_id'], 'private_key' => data['private_key'])
    merge_attributes('public_key' => data['pub_key']) unless public_key
  end
  true
end

#serversObject



74
75
76
77
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 74

def servers
  return nil unless server_ids && server_ids.any?
  service.servers.all('instance-id' => server_ids)
end

#writable?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 58

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

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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/qingcloud/models/compute/key_pair.rb', line 44

def write(path="#{ENV['HOME']}/.ssh/fog_#{Fog.credential.to_s}_#{id}.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 0400
    end
    "Key file built: #{path}"
  else
    "Invalid private key"
  end
end