Sha256: 3390ba0e86284d749a4342c2098c1edcc7aed6186083dabcf4c16e206b539761

Contents?: true

Size: 1.29 KB

Versions: 39

Compression:

Stored size: 1.29 KB

Contents

module Fog
  module Compute
    class AWS
      class KeyPair < Fog::Model
        identity  :name,        :aliases => 'keyName'

        attribute :fingerprint, :aliases => 'keyFingerprint'
        attribute :private_key, :aliases => 'keyMaterial'

        attr_accessor :public_key

        def destroy
          requires :name

          service.delete_key_pair(name)
          true
        end

        def save
          requires :name

          data = if public_key
            service.import_key_pair(name, public_key).body
          else
            service.create_key_pair(name).body
          end
          new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)}
          merge_attributes(new_attributes)
          true
        end

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

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

Version data entries

39 entries across 37 versions & 3 rubygems

Version Path
fog-aws-3.1.0 lib/fog/aws/models/compute/key_pair.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/fog-aws-2.0.1/lib/fog/aws/models/compute/key_pair.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/fog-aws-2.0.1/lib/fog/aws/models/compute/key_pair.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/fog-aws-2.0.1/lib/fog/aws/models/compute/key_pair.rb
fog-aws-3.0.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-2.0.1 lib/fog/aws/models/compute/key_pair.rb
fog-aws-2.0.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.4.1 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.4.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.3.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.2.1 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.2.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.1.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-1.0.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-0.13.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-0.12.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-0.11.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-0.10.0 lib/fog/aws/models/compute/key_pair.rb
fog-aws-0.9.4 lib/fog/aws/models/compute/key_pair.rb
fog-aws-0.9.3 lib/fog/aws/models/compute/key_pair.rb