Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[aws|rds] Rds security group mocking. shindo security group. #679

Merged
merged 3 commits into from Dec 28, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/fog/aws/rds.rb
Expand Up @@ -5,6 +5,8 @@ module AWS
class RDS < Fog::Service

class IdentifierTaken < Fog::Errors::Error; end

class AuthorizationAlreadyExists < Fog::Errors::Error; end

requires :aws_access_key_id, :aws_secret_access_key
recognizes :region, :host, :path, :port, :scheme, :persistent
Expand Down Expand Up @@ -62,7 +64,8 @@ def self.data
owner_id = Fog::AWS::Mock.owner_id
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:servers => {}
:servers => {},
:security_groups => {}
}
end
end
Expand Down Expand Up @@ -187,6 +190,8 @@ def request(params)
raise Fog::AWS::RDS::NotFound.slurp(error, match[2])
when 'DBParameterGroupAlreadyExists'
raise Fog::AWS::RDS::IdentifierTaken.slurp(error, match[2])
when 'AuthorizationAlreadyExists'
raise Fog::AWS::RDS::AuthorizationAlreadyExists.slurp(error, match[2])
else
raise
end
Expand Down
31 changes: 30 additions & 1 deletion lib/fog/aws/requests/rds/authorize_db_security_group_ingress.rb
Expand Up @@ -33,7 +33,36 @@ def authorize_db_security_group_ingress(name, opts={})
class Mock

def authorize_db_security_group_ingress(name, opts = {})
Fog::Mock.not_implemented
unless opts.key?('CIDRIP') || (opts.key?('EC2SecurityGroupName') && opts.key?('EC2SecurityGroupOwnerId'))
raise ArgumentError, 'Must specify CIDRIP, or both EC2SecurityGroupName and EC2SecurityGroupOwnerId'
end

response = Excon::Response.new

if sec_group = self.data[:security_groups][name]
if opts.key?('CIDRIP')
if sec_group['IPRanges'].detect{|h| h['CIDRIP'] == opts['CIDRIP']}
raise Fog::AWS::RDS::AuthorizationAlreadyExists.new("AuthorizationAlreadyExists => #{opts['CIDRIP']} is alreay defined")
end
sec_group['IPRanges'] << opts.merge({"Status" => 'authorizing'})
else
if sec_group['EC2SecurityGroups'].detect{|h| h['EC2SecurityGroupName'] == opts['EC2SecurityGroupName']}
raise Fog::AWS::RDS::AuthorizationAlreadyExists.new("AuthorizationAlreadyExists => #{opts['EC2SecurityGroupName']} is alreay defined")
end
sec_group['EC2SecurityGroups'] << opts.merge({"Status" => 'authorizing'})
end
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
'AuthorizeDBSecurityGroupIngressResult' => {
'DBSecurityGroup' => sec_group
}
}
response
else
raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
end

end

end
Expand Down
13 changes: 8 additions & 5 deletions lib/fog/aws/requests/rds/create_db_instance.rb
Expand Up @@ -77,10 +77,10 @@ def create_db_instance(db_name, options={})
{
"DBInstanceIdentifier"=> db_name,
"DBName" => options["DBName"],
"created_at" => nil,
"InstanceCreateTime" => nil,
"AutoMinorVersionUpgrade"=>true,
"Endpoint"=>{},
"ReadReplicaDBInstanceIdentifiers"=>[],
"ReadReplicaDBInstanceIdentifiers"=>['bla'],
"PreferredMaintenanceWindow"=>"mon:04:30-mon:05:00",
"Engine"=> options["Engine"],
"EngineVersion"=> options["EngineVersion"] || "5.1.57",
Expand All @@ -91,14 +91,17 @@ def create_db_instance(db_name, options={})
"DBInstanceStatus"=>"creating",
"BackupRetentionPeriod"=> options["BackupRetentionPeriod"] || 1,
"AllocatedStorage"=> options["AllocatedStorage"],
"DBParameterGroups"=> # I think groups shoul be in the self.data method
"DBParameterGroups"=> # I think groups should be in the self.data method
[{"DBParameterGroupName"=>"default.mysql5.1",
"ParameterApplyStatus"=>"in-sync"}],
"DBSecurityGroups"=>
[{"Status"=>"active",
"DBSecurityGroupName"=>"default"}],
"LicenseModel"=>"general-public-license",
"PreferredBackupWindow"=>"08:00-08:30"
"PreferredBackupWindow"=>"08:00-08:30",
# "ReadReplicaSourceDBInstanceIdentifier" => nil,
# "LatestRestorableTime" => nil,
"AvailabilityZone" => options["AvailabilityZone"]
}


Expand All @@ -109,7 +112,7 @@ def create_db_instance(db_name, options={})
}
response.status = 200
# This values aren't showed at creating time but at available time
self.data[:servers][db_name]["created_at"] = Time.now
self.data[:servers][db_name]["InstanceCreateTime"] = Time.now
response
end

Expand Down
20 changes: 19 additions & 1 deletion lib/fog/aws/requests/rds/create_db_security_group.rb
Expand Up @@ -27,7 +27,25 @@ def create_db_security_group(name, description = name)
class Mock

def create_db_security_group(name, description = name)
Fog::Mock.not_implemented
response = Excon::Response.new
if self.data[:security_groups] and self.data[:security_groups][name]
raise Fog::AWS::RDS::IdentifierTaken.new("DBInstanceAlreadyExists => The security group '#{name}' already exists")
end

data = {
'DBSecurityGroupName' => name,
'DBSecurityGroupDescription' => description,
'EC2SecurityGroups' => [],
'IPRanges' => [],
'OwnerId' => '0123456789'
}
self.data[:security_groups][name] = data
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
'CreateDBSecurityGroupResult' => { 'DBSecurityGroup' => data }
}
response

end

end
Expand Down
12 changes: 11 additions & 1 deletion lib/fog/aws/requests/rds/delete_db_security_group.rb
Expand Up @@ -25,7 +25,17 @@ def delete_db_security_group(name)
class Mock

def delete_db_security_group(name, description = name)
Fog::Mock.not_implemented
response = Excon::Response.new

if self.data[:security_groups].delete(name)
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
}
response
else
raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
end
end

end
Expand Down
22 changes: 19 additions & 3 deletions lib/fog/aws/requests/rds/describe_db_instances.rb
Expand Up @@ -48,21 +48,37 @@ def describe_db_instances(identifier=nil, opts={})
server_set.each do |server|
case server["DBInstanceStatus"]
when "creating"
if Time.now - server['created_at'] >= Fog::Mock.delay * 2
if Time.now - server['InstanceCreateTime'] >= Fog::Mock.delay * 2
region = "us-east-1"
server["DBInstanceStatus"] = "available"
server["availability_zone"] = region + 'a'
server["AvailabilityZone"] = region + 'a'
server["Endpoint"] = {"Port"=>3306,
"Address"=> Fog::AWS::Mock.rds_address(server["DBInstanceIdentifier"],region) }
server["PendingModifiedValues"] = {}
end
when "rebooting"
when "rebooting" # I don't know how to show rebooting just once before it changes to available
# it applies pending modified values
if server["PendingModifiedValues"]
server.merge!(server["PendingModifiedValues"])
server["PendingModifiedValues"] = {}
self.data[:tmp] ||= Time.now + Fog::Mock.delay * 2
if self.data[:tmp] <= Time.now
server["DBInstanceStatus"] = 'available'
self.data.delete(:tmp)
end
end
when "modifying"
# TODO there are some fields that only applied after rebooting
if server["PendingModifiedValues"]
server.merge!(server["PendingModifiedValues"])
server["PendingModifiedValues"] = {}
server["DBInstanceStatus"] = 'available'
end
when "available" # I'm not sure if amazon does this
if server["PendingModifiedValues"]
server["DBInstanceStatus"] = 'modifying'
end

end
end

Expand Down
46 changes: 44 additions & 2 deletions lib/fog/aws/requests/rds/describe_db_security_groups.rb
Expand Up @@ -28,8 +28,50 @@ def describe_db_security_groups(opts={})

class Mock

def describe_db_security_group(opts={})
Fog::Mock.not_implemented
def describe_db_security_groups(opts={})
response = Excon::Response.new
sec_group_set = []
if opts.is_a?(String)
sec_group_name = opts
if sec_group = self.data[:security_groups][sec_group_name]
sec_group_set << sec_group
else
raise Fog::AWS::RDS::NotFound.new("Security Group #{sec_group_name} not found")
end
else
sec_group_set = self.data[:security_groups].values
end

sec_group_set.each do |sec_group|
sec_group["IPRanges"].each do |iprange|
if iprange["Status"] == "authorizing" || iprange["Status"] == "revoking"
iprange[:tmp] ||= Time.now + Fog::Mock.delay * 2
if iprange[:tmp] <= Time.now
iprange["Status"] = "authorized" if iprange["Status"] == "authorizing"
iprange.delete(:tmp)
sec_group["IPRanges"].delete(iprange) if iprange["Status"] == "revoking"
end
end
end

sec_group["EC2SecurityGroups"].each do |ec2_secg|
if ec2_secg["Status"] == "authorizing" || iprange["Status"] == "revoking"
ec2_secg[:tmp] ||= Time.now + Fog::Mock.delay * 2
if ec2_secg[:tmp] <= Time.now
ec2_secg["Status"] = "authorized" if ec2_secg["Status"] == "authorizing"
ec2_secg.delete(:tmp)
sec_group["EC2SecurityGroups"].delete(ec2_secg) if ec2_secg["Status"] == "revoking"
end
end
end
end

response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"DescribeDBSecurityGroupsResult" => { "DBSecurityGroups" => sec_group_set }
}
response
end

end
Expand Down
18 changes: 10 additions & 8 deletions lib/fog/aws/requests/rds/modify_db_instance.rb
Expand Up @@ -46,22 +46,24 @@ class Mock

def modify_db_instance(db_name, apply_immediately, options={})
response = Excon::Response.new
if server = self.data[:servers][db_name]
if server["DBInstanceStatus"] != "available"
if self.data[:servers][db_name]
if self.data[:servers][db_name]["DBInstanceStatus"] != "available"
raise Fog::AWS::RDS::NotFound.new("DBInstance #{db_name} not available for modification")
else
# TODO verify the params options
# if apply_immediately is false, all the options go to pending_modified_values and then apply and clear after either
# a reboot or the maintainance window
if apply_immediately
modified_server = server.merge(options)
else
modified_server = server["PendingModifiedValues"].merge!(options) # it appends
end
#if apply_immediately
# modified_server = server.merge(options)
#else
# modified_server = server["PendingModifiedValues"].merge!(options) # it appends
#end
self.data[:servers][db_name]["PendingModifiedValues"].merge!(options) # it appends
#self.data[:servers][db_name]["DBInstanceStatus"] = "modifying"
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"ModifyDBInstanceResult" => { "DBInstance" => modified_server }
"ModifyDBInstanceResult" => { "DBInstance" => self.data[:servers][db_name] }
}
response

Expand Down
12 changes: 6 additions & 6 deletions lib/fog/aws/requests/rds/reboot_db_instance.rb
Expand Up @@ -26,21 +26,21 @@ class Mock

def reboot_db_instance(instance_identifier)
response = Excon::Response.new
if server = self.data[:servers][instance_identifier]
if server["DBInstanceStatus"] != "available"
raise Fog::AWS::RDS::NotFound.new("DBInstance #{db_name} not available for rebooting")
if self.data[:servers][instance_identifier]
if self.data[:servers][instance_identifier]["DBInstanceStatus"] != "available"
raise Fog::AWS::RDS::NotFound.new("DBInstance #{instance_identifier} not available for rebooting")
else
server["DBInstanceStatus"] = 'rebooting'
self.data[:servers][instance_identifier]["DBInstanceStatus"] = 'rebooting'
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"RebootDBInstanceResult" => { "DBInstance" => server }
"RebootDBInstanceResult" => { "DBInstance" => self.data[:servers][instance_identifier] }
}
response

end
else
raise Fog::AWS::RDS::NotFound.new("DBInstance #{db_name} not found")
raise Fog::AWS::RDS::NotFound.new("DBInstance #{instance_identifier} not found")
end
end

Expand Down
28 changes: 27 additions & 1 deletion lib/fog/aws/requests/rds/revoke_db_security_group_ingress.rb
Expand Up @@ -33,7 +33,33 @@ def revoke_db_security_group_ingress(name, opts={})
class Mock

def revoke_db_security_group_ingress(name, opts = {})
Fog::Mock.not_implemented
unless opts.key?('CIDRIP') || (opts.key?('EC2SecurityGroupName') && opts.key?('EC2SecurityGroupOwnerId'))
raise ArgumentError, 'Must specify CIDRIP, or both EC2SecurityGroupName and EC2SecurityGroupOwnerId'
end

response = Excon::Response.new

if sec_group = self.data[:security_groups][name]
if opts.key?('CIDRIP')
sec_group['IPRanges'].each do |iprange|
iprange['Status']= 'revoking' if iprange['CIDRIP'] == opts['CIDRIP']
end
else
sec_group['EC2SecurityGroups'].each do |ec2_secg|
ec2_secg['Status']= 'revoking' if ec2_secg['EC2SecurityGroupName'] == opts['EC2SecurityGroupName']
end
end
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
'RevokeDBSecurityGroupIngressResult' => {
'DBSecurityGroup' => sec_group
}
}
response
else
raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
end
end

end
Expand Down