Class: Fog::Compute::QingCloud::KeyPair
- Inherits:
-
QingCloud::Model
- Object
- Model
- QingCloud::Model
- Fog::Compute::QingCloud::KeyPair
- Defined in:
- lib/fog/qingcloud/models/compute/key_pair.rb
Instance Method Summary collapse
- #attach(server_id) ⇒ Object
- #destroy ⇒ Object
- #detach(server_id) ⇒ Object
- #save ⇒ Object
- #servers ⇒ Object
- #writable? ⇒ Boolean
- #write(path = "#{ENV['HOME']}/.ssh/fog_#{Fog.credential.to_s}_#{id}.pem") ⇒ Object
Methods inherited from QingCloud::Model
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 |
#destroy ⇒ Object
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. =~ /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 |
#save ⇒ Object
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 |
#servers ⇒ Object
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
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 |