1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

first pass at key pair mocks and fix describe errors for addresses/volumes

This commit is contained in:
Wesley Beary 2009-08-16 22:19:35 -07:00
parent 068cccf394
commit 64742c52d0
14 changed files with 216 additions and 65 deletions

View file

@ -33,6 +33,14 @@ module Fog
hex(32)
end
def self.key_fingerprint
fingerprint = []
20.times do
fingerprint << hex(2)
end
fingerprint.join(':')
end
def self.instance_id
end
@ -44,6 +52,16 @@ module Fog
ip.join('.')
end
def self.key_material
key_material = ['-----BEGIN RSA PRIVATE KEY-----']
20.times do
key_material << base64(76)
end
key_material << base64(67) + '='
key_material << '-----END RSA PRIVATE KEY-----'
key_material.join("\n")
end
def self.request_id
request_id = []
request_id << hex(8)
@ -94,6 +112,13 @@ module Fog
)
end
def self.base64(length)
random_selection(
"ABCDEFGHIJKLMNOP QRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
length
)
end
end
end

View file

@ -175,7 +175,7 @@ module Fog
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
if Fog.mocking?
@data = { :deleted_at => {}, :addresses => {}, :volumes => {} }
@data = { :deleted_at => {}, :addresses => {}, :key_pairs => {}, :volumes => {} }
end
end

View file

@ -1,26 +1,60 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Create a new key pair
#
# ==== Parameters
# * key_name<~String> - Unique name for key pair.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'keyFingerprint'<~String> - SHA-1 digest of DER encoded private key
# * 'keyMaterial'<~String> - Unencrypted encoded PEM private key
# * 'keyName'<~String> - Name of key
# * 'requestId'<~String> - Id of request
def create_key_pair(key_name)
request({
'Action' => 'CreateKeyPair',
'KeyName' => key_name
}, Fog::Parsers::AWS::EC2::CreateKeyPair.new)
end
# Create a new key pair
#
# ==== Parameters
# * key_name<~String> - Unique name for key pair.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'keyName'<~String> - Name of key
# * 'keyFingerprint'<~String> - SHA-1 digest of DER encoded private key
# * 'keyMaterial'<~String> - Unencrypted encoded PEM private key
# * 'requestId'<~String> - Id of request
def create_key_pair(key_name)
request({
'Action' => 'CreateKeyPair',
'KeyName' => key_name
}, Fog::Parsers::AWS::EC2::CreateKeyPair.new)
end
end
end
else
module Fog
module AWS
class EC2
def create_key_pair(key_name)
response = Fog::Response.new
unless @data[:key_pairs][key_name]
response.status = 200
data = {
'keyFingerprint' => Fog::AWS::Mock.key_fingerprint,
'keyMaterial' => Fog::AWS::Mock.key_material,
'keyName' => key_name
}
@data[:key_pairs][key_name] = data
response.body = {
'requestId' => Fog::AWS::Mock.request_id
}.merge!(data)
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end
end
end
end
end

View file

@ -1,24 +1,49 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Delete a key pair that you own
#
# ==== Parameters
# * key_name<~String> - Name of the key pair.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> id of request
# * 'return'<~Boolean> - success?
def delete_key_pair(key_name)
request({
'Action' => 'DeleteKeyPair',
'KeyName' => key_name
}, Fog::Parsers::AWS::EC2::Basic.new)
end
# Delete a key pair that you own
#
# ==== Parameters
# * key_name<~String> - Name of the key pair.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> id of request
# * 'return'<~Boolean> - success?
def delete_key_pair(key_name)
request({
'Action' => 'DeleteKeyPair',
'KeyName' => key_name
}, Fog::Parsers::AWS::EC2::Basic.new)
end
end
end
else
module Fog
module AWS
class EC2
def delete_key_pair(key_name)
response = Fog::Response.new
@data[:key_pairs].delete(key_name)
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
response
end
end
end
end
end

View file

@ -49,6 +49,7 @@ else
}
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end

View file

@ -1,26 +1,63 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Describe all or specified key pairs
#
# ==== Parameters
# * key_name<~Array>:: List of key names to describe, defaults to all
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'keySet'<~Array>:
# * 'keyName'<~String> - Name of key
# * 'keyFingerprint'<~String> - Fingerprint of key
def describe_key_pairs(key_name = [])
params = indexed_params('KeyName', key_name)
request({
'Action' => 'DescribeKeyPairs'
}.merge!(params), Fog::Parsers::AWS::EC2::DescribeKeyPairs.new)
end
# Describe all or specified key pairs
#
# ==== Parameters
# * key_name<~Array>:: List of key names to describe, defaults to all
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'keySet'<~Array>:
# * 'keyName'<~String> - Name of key
# * 'keyFingerprint'<~String> - Fingerprint of key
def describe_key_pairs(key_name = [])
params = indexed_params('KeyName', key_name)
request({
'Action' => 'DescribeKeyPairs'
}.merge!(params), Fog::Parsers::AWS::EC2::DescribeKeyPairs.new)
end
end
end
else
module Fog
module AWS
class EC2
def describe_key_pairs(key_name = [])
key_name = [*key_name]
response = Fog::Response.new
if key_name != []
key_set = @data[:key_pairs].reject {|key, value| !key_name.include?(key)}.values
else
key_set = @data[:key_pairs].values
end
if key_name.length == 0 || key_name.length == key_set.length
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'keySet' => key_set.map do |key|
key.reject {|key,value| !['keyFingerprint', 'keyName'].include?(key)}
end
}
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end
end
end
end
end

View file

@ -73,6 +73,7 @@ else
}
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end

View file

@ -34,7 +34,7 @@ else
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
'return' => true
}
else
response.status = 400

View file

@ -18,4 +18,10 @@ describe 'EC2.create_key_pair' do
actual.body['requestId'].should be_a(String)
end
it "should raise a BadRequest when the key pair already exists" do
lambda {
@ec2.create_key_pair('fog_key_pair')
}.should raise_error(Fog::Errors::BadRequest)
end
end

View file

@ -13,4 +13,8 @@ describe 'EC2.delete_key_pair' do
[false, true].should include(actual.body['return'])
end
it "should not raise an error if the key pair does not exist" do
@ec2.delete_key_pair('not_a_key_pair')
end
end

View file

@ -25,4 +25,10 @@ describe 'EC2.describe_addresses' do
item.should_not be_nil
end
it "should raise a BadRequest error if ip does not exist" do
lambda {
@ec2.describe_addresses('127.0.0.1')
}.should raise_error(Fog::Errors::BadRequest)
end
end

View file

@ -21,7 +21,7 @@ describe 'EC2.describe_key_pairs' do
end
it "should return proper attributes with params" do
actual = @ec2.describe_key_pairs(['key_name'])
actual = @ec2.describe_key_pairs('key_name')
actual.body['keySet'].should be_an(Array)
actual.body['requestId'].should be_a(String)
key_pair = actual.body['keySet'].select {|key| key['keyName'] == 'key_name' }.first
@ -29,4 +29,10 @@ describe 'EC2.describe_key_pairs' do
key_pair['keyName'].should be_a(String)
end
it "should raise a BadRequest error if the key does not exist" do
lambda {
@ec2.describe_key_pairs('not_a_key_name')
}.should raise_error(Fog::Errors::BadRequest)
end
end

View file

@ -37,4 +37,10 @@ describe 'EC2.describe_volumes' do
volume['attachmentSet'].should == []
end
it "should raise a BadRequest error if volume does not exist" do
lambda {
@ec2.describe_volumes('vol-00000000')
}.should raise_error(Fog::Errors::BadRequest)
end
end

View file

@ -15,7 +15,7 @@ describe 'EC2.release_address' do
it "should raise a BadRequest error if address does not exist" do
lambda {
@ec2.release_address('0.0.0.0')
@ec2.release_address('127.0.0.1')
}.should raise_error(Fog::Errors::BadRequest)
end