Class: CFnDK::KeyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/cfndk/key_pair.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data, option, global_config, credentials) ⇒ KeyPair

Returns a new instance of KeyPair.



4
5
6
7
8
9
10
11
12
# File 'lib/cfndk/key_pair.rb', line 4

def initialize(name, data, option, global_config, credentials)
  @global_config = global_config
  @name = name
  data = {} unless data
  @key_file = data['key_file'] || nil
  @region = data['region'] || @global_config.region
  @option = option
  @client = Aws::EC2::Client.new(credentials: credentials, region: @region)
end

Instance Attribute Details

#key_fileObject (readonly)

Returns the value of attribute key_file.



3
4
5
# File 'lib/cfndk/key_pair.rb', line 3

def key_file
  @key_file
end

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
# File 'lib/cfndk/key_pair.rb', line 14

def create
  CFnDK.logger.info(('creating keypair: ' + name).color(:green))
  key_pair = @client.create_key_pair(
    key_name: name
  )
  CFnDK.logger.info(('created keypair: ' + name).color(:green))

  create_key_file(key_pair)
end

#destroyObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cfndk/key_pair.rb', line 24

def destroy
  if exists?
    CFnDK.logger.info(('deleting keypair: ' + name).color(:green))
    @client.delete_key_pair(
      key_name: name
    )
    CFnDK.logger.info(('deleted keypair: ' + name).color(:green))
  else
    CFnDK.logger.info(('do not delete keypair: ' + name).color(:red))
  end
end

#exists?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/cfndk/key_pair.rb', line 36

def exists?
  !@client.describe_key_pairs(
    key_names: [
      name,
    ]
  ).key_pairs.empty?
rescue Aws::EC2::Errors::InvalidKeyPairNotFound
  false
end

#nameObject



46
47
48
# File 'lib/cfndk/key_pair.rb', line 46

def name
  [@name, @option[:uuid]].compact.join('-')
end

#original_nameObject



50
51
52
# File 'lib/cfndk/key_pair.rb', line 50

def original_name
  @name
end