Class: Rudy::AWS::EC2::KeyPairs

Inherits:
Object
  • Object
show all
Includes:
Base, ObjectBase
Defined in:
lib/rudy/aws/ec2/keypair.rb

Instance Attribute Summary

Attributes included from Base

#ec2

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#initialize

Methods included from Huxtable

change_environment, change_position, change_region, change_role, change_zone, #check_keys, #config_dirname, create_domain, #current_group_name, #current_machine_address, #current_machine_count, #current_machine_group, #current_machine_hostname, #current_machine_image, #current_machine_name, #current_machine_size, #current_user, #current_user_keypairpath, debug?, #debug?, domain, domain_exists?, #group_metadata, #has_keypair?, #has_keys?, #has_pem_keys?, #has_root_keypair?, keypair_path_to_name, #known_machine_group?, #root_keypairname, #root_keypairpath, #switch_user, update_config, update_global, update_logger, #user_keypairname, #user_keypairpath

Class Method Details

.from_hash(h) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rudy/aws/ec2/keypair.rb', line 63

def self.from_hash(h)
  # keyName: test-c5g4v3pe
  # keyFingerprint: 65:d0:ce:e7:6a:b0:88:4a:9c:c7:2d:b8:33:0c:fd:3b:c8:0f:0a:3c
  # keyMaterial: |-
  #   -----BEGIN RSA PRIVATE KEY-----
  # 
  keypair = Rudy::AWS::EC2::KeyPair.new
  keypair.fingerprint = h['keyFingerprint']
  keypair.name = h['keyName']
  keypair.private_key = h['keyMaterial']
  keypair
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/rudy/aws/ec2/keypair.rb', line 76

def any?
  keypairs = list || []
  !keypairs.empty?
end

#create(name) ⇒ Object



32
33
34
35
36
# File 'lib/rudy/aws/ec2/keypair.rb', line 32

def create(name)
  raise "No name provided" unless name
  ret = @ec2.create_keypair(:key_name => name)
  self.class.from_hash(ret)
end

#destroy(name) ⇒ Object



38
39
40
41
42
43
# File 'lib/rudy/aws/ec2/keypair.rb', line 38

def destroy(name)
  name = name.name if name.is_a?(Rudy::AWS::EC2::KeyPair)
  raise "No name provided" unless name.is_a?(String)
  ret = @ec2.delete_keypair(:key_name => name)
  (ret && ret['return'] == 'true') # BUG? Always returns true
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/rudy/aws/ec2/keypair.rb', line 87

def exists?(name)
  return false unless name
  kp = get(name) rescue nil
  !kp.nil?
end

#get(name) ⇒ Object



81
82
83
84
85
# File 'lib/rudy/aws/ec2/keypair.rb', line 81

def get(name)
  keypairs = list(name) || []
  return if keypairs.empty?
  keypairs.first
end

#list(*names) ⇒ Object



45
46
47
48
49
# File 'lib/rudy/aws/ec2/keypair.rb', line 45

def list(*names)
  keypairs = list_as_hash(names)
  keypairs &&= keypairs.values
  keypairs
end

#list_as_hash(*names) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rudy/aws/ec2/keypair.rb', line 51

def list_as_hash(*names)
  names = names.flatten
  klist = @ec2.describe_keypairs(:key_name => names)
  return unless klist['keySet'].is_a?(Hash)
  keypairs = {}
  klist['keySet']['item'].each do |oldkp| 
    kp = self.class.from_hash(oldkp)
    keypairs[kp.name] = kp
  end
  keypairs
end