January 8, 2016

Classes
Tags fog sshkey

Managing SSH Key Pairs with fog-softlayer

Using the fog-softlayer gem to create, destroy, and get information about SSH keys

Create a connection to SoftLayer Compute Service

require 'fog/softlayer'
@sl = Fog::Compute[:softlayer]

Key Pair Basics

Create

kp1 = @sl.key_pairs.create(:label => 'my-new-key', :key => 'ssh-rsa AAAAxbU2lx...')
# => <Fog::Compute::Softlayer::KeyPair>
kp2 = @sl.key_pairs.new
kp2.label = 'my-new-new-key'
kp2.key = 'ssh-rsa AAAAxbU2lx...'
kp2.save
# => <Fog::Compute::Softlayer::KeyPair>

Get

# By id:
kp = @sl.key_pairs.get(123456)
# => <Fog::Compute::Softlayer::KeyPair>

# By label:
kp = @sl.key_pairs.by_label('my-new-key')
# => <Fog::Compute::Softlayer::KeyPair>

Destroy

kp = @sl.key_pairs.by_label('my-new-key')
# => <Fog::Compute::Softlayer::KeyPair>
kp.destroy

Key Pairs with Servers

Create a server with one or more key pairs

the_first_key = @sl.key_pairs.by_label('my-new-key')
# => <Fog::Compute::Softlayer::KeyPair>
the_second_key = @sl.key_pairs.by_label('my-other-new-key')
# => <Fog::Compute::Softlayer::KeyPair>

opts = {
		:flavor_id => 'm1.small',
		:os_code => 'UBUNTU_LATEST',
		:datacenter => 'hkg02',
		:name => 'cphrmky',
		:key_pairs => [ the_first_key, the_second_key ]
}
@sl.servers.create(opts)
# => <Fog::Compute::Softlayer::Server>

Look at the key pairs on a server.

server = @sl.servers.get(12345)
server.key_pairs
# => [ <Fog::Compute::Softlayer::Server>,
# <Fog::Compute::Softlayer::Server>]

Feedback?

If this article contains any error, or leaves any of your questions unanswered, please help us out by opening up a github issue.
Open an issue