In part four of this series, we explored using fog-softlayer for managing your SoftLayer tags.
In this post, we’ll look at creating and assigning SSH key pairs for use with our compute instances.
These examples all assume you have ~/.fog
, which contains the following:
default:
softlayer_username: example-username
softlayer_api_key: 1a1a1a1a1a1a1a1a1a11a1a1a1a1a1a1a1a1a1
softlayer_default_domain: example.com
Create some new key pairs: kp1 = @sl.key_pairs.create(:label => ‘my-new-key’, :key => ‘ssh-rsa AAAAxbU2lx…’)
kp2 = @sl.key_pairs.new kp2.label = ‘my-new-new-key’ kp2.key = ‘ssh-rsa AAAAxbU2lx…’ kp2.save
Here’s how we get key pairs that have already been created:
kp = @sl.key_pairs.get(123456)
kp = @sl.key_pairs.by_label(‘my-new-key’)
Let’s destroy a key pair, just for fun.
kp = @sl.key_pairs.by_label('my-new-key') # =>\t
With basic CRUD out of the way on the key pairs, let’s see how we can assign them to servers.
Create a server with one or more key pairs using:
the_first_key = @sl.key_pairs.by_label('my-new-key') # =>If you already have the list of internal IDs that SoftLayer assigns to each of your key pairs, you can pass an array of hashes instead of loading an object for each key pair you assign to a compute instance.
key_pair_ids = [ \t{:id => 12345}, \t{:id => 12346}, \t{:id => 12347}, ]opts = { \t:flavor_id => ‘m1.small’, \t:os_code => ‘UBUNTU_LATEST’, \t:datacenter => ‘hkg02’, \t:name => ‘cphrmky’, \t:key_pairs => key_pair_ids } @sl.servers.create(opts)
Then, look at the key pairs on a server:
server = @sl.servers.get(12345) server.key_pairs # => [One of the excellent features included in fog-softlayer (and for all of the supported providers), is fantastic mocking.
Even if you don’t have a SoftLayer account, you can download fog-softlayer and start testing this code.
Just call Fog.mock!
immediately after requiring the gem, run all of the above code examples, and see how easy it is to work with. We’re working hard to get full coverage of Mock classes and methods. You may run across a MockNotImplemented exception here or there. If you do, the best course is probably to fork the repo, implement the mock, and open a pull request.
\- Matt
–
If you have questions about or problems with fog-softlayer open an issue or email me.