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

rename aws.instance model to server

This commit is contained in:
Wesley Beary 2010-01-08 11:29:07 -08:00
parent acdb9086fd
commit 466e077c02
14 changed files with 316 additions and 316 deletions

View file

@ -41,8 +41,8 @@ YML
@@s3.buckets
end
def instances
@@ec2.instances
def servers
@@ec2.servers
end
def key_pairs

View file

@ -22,12 +22,12 @@ module Fog
def self.reload
load "fog/aws/models/ec2/address.rb"
load "fog/aws/models/ec2/addresses.rb"
load "fog/aws/models/ec2/instance.rb"
load "fog/aws/models/ec2/instances.rb"
load "fog/aws/models/ec2/key_pair.rb"
load "fog/aws/models/ec2/key_pairs.rb"
load "fog/aws/models/ec2/security_group.rb"
load "fog/aws/models/ec2/security_groups.rb"
load "fog/aws/models/ec2/server.rb"
load "fog/aws/models/ec2/servers.rb"
load "fog/aws/models/ec2/snapshot.rb"
load "fog/aws/models/ec2/snapshots.rb"
load "fog/aws/models/ec2/volume.rb"

View file

@ -6,7 +6,7 @@ module Fog
identity :public_ip, 'publicIp'
attribute :instance_id, 'instanceId'
attribute :server_id, 'instanceId'
def destroy
requires :public_ip
@ -15,9 +15,9 @@ module Fog
true
end
def instance=(new_instance)
if new_instance
associate(new_instance)
def server=(new_server)
if new_server
associate(new_server)
else
disassociate
end
@ -26,27 +26,27 @@ module Fog
def save
data = connection.allocate_address
@public_ip = data.body['publicIp']
if @instance
self.instance = @instance
if @server
self.server = @server
end
true
end
private
def associate(new_instance)
def associate(new_server)
if new_record?
@instance = new_instance
@server = new_server
else
@instance = nil
@instance_id = new_instance.id
connection.associate_address(@instance_id, @public_ip)
@server = nil
@server_id = new_server.id
connection.associate_address(@server_id, @public_ip)
end
end
def disassociate
@instance = nil
@instance_id = nil
@server = nil
@server_id = nil
unless new_record?
connection.disassociate_address(@public_ip)
end

View file

@ -11,7 +11,7 @@ module Fog
class Addresses < Fog::Collection
attribute :public_ip
attribute :instance
attribute :server
model Fog::AWS::EC2::Address
@ -31,8 +31,8 @@ module Fog
data['addressesSet'].each do |address|
addresses << new(address.reject {|key, value| value.nil? || value.empty? })
end
if instance
addresses = addresses.select {|address| address.instance_id == instance.id}
if server
addresses = addresses.select {|address| address.instance_id == server.id}
end
self.replace(addresses)
end
@ -46,8 +46,8 @@ module Fog
end
def new(attributes = {})
if instance
super({ :instance => instance }.merge!(attributes))
if server
super({ :server => server }.merge!(attributes))
else
super(attributes)
end

View file

@ -2,7 +2,7 @@ module Fog
module AWS
class EC2
class Instance < Fog::Model
class Server < Fog::Model
identity :id, 'instanceId'
@ -26,7 +26,7 @@ module Fog
def addresses
requires :id
connection.addresses(:instance => self)
connection.addresses(:server => self)
end
def destroy
@ -113,7 +113,7 @@ module Fog
def volumes
requires :id
connection.volumes(:instance => self)
connection.volumes(:server => self)
end
private

View file

@ -2,28 +2,28 @@ module Fog
module AWS
class EC2
def instances
Fog::AWS::EC2::Instances.new(:connection => self)
def servers
Fog::AWS::EC2::Servers.new(:connection => self)
end
class Instances < Fog::Collection
class Servers < Fog::Collection
attribute :instance_id
attribute :server_id
model Fog::AWS::EC2::Instance
model Fog::AWS::EC2::Server
def initialize(attributes)
@instance_id ||= []
@server_id ||= []
super
end
def all(instance_id = @instance_id)
@instance_id = instance_id
def all(server_id = @server_id)
@server_id = server_id
if @loaded
clear
end
@loaded = true
data = connection.describe_instances(instance_id).body
data = connection.describe_instances(server_id).body
data['reservationSet'].each do |reservation|
reservation['instancesSet'].each do |instance|
self << new(instance)
@ -32,9 +32,9 @@ module Fog
self
end
def get(instance_id)
if instance_id
all(instance_id).first
def get(server_id)
if server_id
all(server_id).first
end
rescue Excon::Errors::BadRequest
nil

View file

@ -10,7 +10,7 @@ module Fog
attribute :availability_zone, 'availabilityZone'
attribute :create_time, 'createTime'
attribute :device
attribute :instance_id, 'instanceId'
attribute :server_id, 'instanceId'
attribute :size
attribute :snapshot_id, 'snapshotId'
attribute :status
@ -29,9 +29,9 @@ module Fog
true
end
def instance=(new_instance)
if new_instance
attach(new_instance)
def server=(new_server)
if new_server
attach(new_server)
else
detach
end
@ -43,8 +43,8 @@ module Fog
data = connection.create_volume(@availability_zone, @size, @snapshot_id).body
new_attributes = data.reject {|key,value| key == 'requestId'}
merge_attributes(new_attributes)
if @instance
self.instance = @instance
if @server
self.server = @server
end
true
end
@ -57,20 +57,20 @@ module Fog
private
def attach(new_instance)
def attach(new_server)
if new_record?
@instance = new_instance
@availability_zone = new_instance.availability_zone
elsif new_instance
@instance = nil
@instance_id = new_instance.id
connection.attach_volume(@instance_id, @id, @device)
@server = new_server
@availability_zone = new_server.availability_zone
elsif new_server
@server = nil
@server_id = new_server.id
connection.attach_volume(@server_id, @id, @device)
end
end
def detach
@instance = nil
@instance_id = nil
@server = nil
@server_id = nil
unless new_record?
connection.detach_volume(@id)
end

View file

@ -11,7 +11,7 @@ module Fog
class Volumes < Fog::Collection
attribute :volume_id
attribute :instance
attribute :server
model Fog::AWS::EC2::Volume
@ -31,8 +31,8 @@ module Fog
data['volumeSet'].each do |volume|
volumes << new(volume)
end
if instance
volumes = volumes.select {|volume| volume.instance_id == instance.id}
if server
volumes = volumes.select {|volume| volume.instance_id == server.id}
end
self.replace(volumes)
end
@ -46,8 +46,8 @@ module Fog
end
def new(attributes = {})
if instance
super({ :instance => instance }.merge!(attributes))
if server
super({ :server => server }.merge!(attributes))
else
super
end

View file

@ -9,7 +9,7 @@ describe 'Fog::AWS::EC2::Address' do
'instanceId' => 'i-00000000',
'publicIp' => '0.0.0.0'
)
address.instance_id.should == 'i-00000000'
address.server_id.should == 'i-00000000'
address.public_ip.should == '0.0.0.0'
end
@ -37,24 +37,24 @@ describe 'Fog::AWS::EC2::Address' do
end
describe "#instance=" do
describe "#server=" do
before(:each) do
@address = ec2.addresses.new
@instance = ec2.instances.create(:image_id => GENTOO_AMI)
@server = ec2.servers.create(:image_id => GENTOO_AMI)
end
after(:each) do
if @address.public_ip
@address.destroy
end
@instance.destroy
@server.destroy
end
it "should associate with instance to an already saved address" do
it "should associate with server to an already saved address" do
@address.save.should be_true
@instance.wait_for { state == 'running' }
@address.instance = @instance
@address.instance_id.should == @instance.id
@server.wait_for { state == 'running' }
@address.server = @server
@address.server_id.should == @server.id
end
end

View file

@ -1,161 +0,0 @@
require File.dirname(__FILE__) + '/../../../spec_helper'
describe 'Fog::AWS::EC2::Instance' do
describe "#initialize" do
it "should remap attributes from parser" do
instance = ec2.instances.new({
'amiLaunchIndex' => 'ami_launch_index',
'dnsName' => 'dns_name',
'groupId' => 'group_id',
'imageId' => 'image_id',
'instanceId' => 'instance_id',
'instanceType' => 'instance_type',
'kernelId' => 'kernel_id',
'keyName' => 'key_name',
'launchTime' => 'launch_time',
'productCodes' => 'product_codes',
'privateDnsName' => 'private_dns_name',
'ramdiskId' => 'ramdisk_id'
})
instance.ami_launch_index.should == 'ami_launch_index'
instance.dns_name.should == 'dns_name'
instance.group_id.should == 'group_id'
instance.image_id.should == 'image_id'
instance.id.should == 'instance_id'
instance.type.should == 'instance_type'
instance.kernel_id.should == 'kernel_id'
instance.key_name.should == 'key_name'
instance.launch_time.should == 'launch_time'
instance.product_codes.should == 'product_codes'
instance.private_dns_name.should == 'private_dns_name'
instance.ramdisk_id.should == 'ramdisk_id'
end
end
describe "#addresses" do
it "should return a Fog::AWS::EC2::Addresses" do
instance = ec2.instances.create(:image_id => GENTOO_AMI)
instance.addresses.should be_a(Fog::AWS::EC2::Addresses)
instance.destroy
end
end
describe "#destroy" do
it "should return true if the instance is deleted" do
instance = ec2.instances.create(:image_id => GENTOO_AMI)
instance.destroy.should be_true
end
end
describe "#state" do
it "should remap values out of hash" do
instance = Fog::AWS::EC2::Instance.new({
'instanceState' => { 'name' => 'instance_state' },
})
instance.state.should == 'instance_state'
end
end
describe "#collection" do
it "should return a Fog::AWS::EC2::Instances" do
ec2.instances.new.collection.should be_a(Fog::AWS::EC2::Instances)
end
it "should be the instances the instance is related to" do
instances = ec2.instances
instances.new.collection.should == instances
end
end
describe "#key_pair" do
it "should have tests"
end
describe "#key_pair=" do
it "should have tests"
end
describe "#monitoring=" do
it "should remap values out of hash" do
instance = Fog::AWS::EC2::Instance.new({
'monitoring' => { 'state' => true }
})
instance.monitoring.should == true
end
end
describe "#placement=" do
it "should remap values into availability_zone" do
instance = Fog::AWS::EC2::Instance.new({
'placement' => { 'availabilityZone' => 'availability_zone' }
})
instance.availability_zone.should == 'availability_zone'
end
end
describe "#reload" do
before(:each) do
@instance = ec2.instances.create(:image_id => GENTOO_AMI)
@reloaded = @instance.reload
end
after(:each) do
@instance.destroy
end
it "should return a Fog::AWS::EC2::Instance" do
@reloaded.should be_a(Fog::AWS::EC2::Instance)
end
it "should reset attributes to remote state" do
@instance.attributes.should == @reloaded.attributes
end
end
describe "#save" do
before(:each) do
@instance = ec2.instances.new(:image_id => GENTOO_AMI)
end
it "should return true when it succeeds" do
@instance.save.should be_true
@instance.destroy
end
it "should not exist in instances before save" do
ec2.instances.get(@instance.id).should be_nil
end
it "should exist in buckets after save" do
@instance.save
ec2.instances.get(@instance.id).should_not be_nil
@instance.destroy
end
end
describe "#volumes" do
it "should return a Fog::AWS::EC2::Volumes" do
instance = ec2.instances.create(:image_id => GENTOO_AMI)
instance.volumes.should be_a(Fog::AWS::EC2::Volumes)
instance.destroy
end
end
end

View file

@ -1,70 +0,0 @@
require File.dirname(__FILE__) + '/../../../spec_helper'
describe 'Fog::AWS::EC2::Instances' do
describe "#all" do
it "should return a Fog::AWS::EC2::Instances" do
ec2.instances.all.should be_a(Fog::AWS::EC2::Instances)
end
it "should include persisted instances" do
instance = ec2.instances.create(:image_id => GENTOO_AMI)
ec2.instances.get(instance.id).should_not be_nil
instance.destroy
end
end
describe "#create" do
before(:each) do
@instance = ec2.instances.create(:image_id => GENTOO_AMI)
end
after(:each) do
@instance.destroy
end
it "should return a Fog::AWS::EC2::Instance" do
@instance.should be_a(Fog::AWS::EC2::Instance)
end
it "should exist on ec2" do
ec2.instances.get(@instance.id).should_not be_nil
end
end
describe "#get" do
it "should return a Fog::AWS::EC2::Instance if a matching instance exists" do
instance = ec2.instances.create(:image_id => GENTOO_AMI)
get = ec2.instances.get(instance.id)
instance.attributes.should == get.attributes
instance.destroy
end
it "should return nil if no matching instance exists" do
ec2.instances.get('i-00000000').should be_nil
end
end
describe "#new" do
it "should return a Fog::AWS::EC2::Instance" do
ec2.instances.new(:image_id => GENTOO_AMI).should be_a(Fog::AWS::EC2::Instance)
end
end
describe "#reload" do
it "should return a Fog::AWS::EC2::Instances" do
ec2.instances.all.reload.should be_a(Fog::AWS::EC2::Instances)
end
end
end

View file

@ -0,0 +1,161 @@
require File.dirname(__FILE__) + '/../../../spec_helper'
describe 'Fog::AWS::EC2::Server' do
describe "#initialize" do
it "should remap attributes from parser" do
server = ec2.servers.new({
'amiLaunchIndex' => 'ami_launch_index',
'dnsName' => 'dns_name',
'groupId' => 'group_id',
'imageId' => 'image_id',
'instanceId' => 'instance_id',
'instanceType' => 'instance_type',
'kernelId' => 'kernel_id',
'keyName' => 'key_name',
'launchTime' => 'launch_time',
'productCodes' => 'product_codes',
'privateDnsName' => 'private_dns_name',
'ramdiskId' => 'ramdisk_id'
})
server.ami_launch_index.should == 'ami_launch_index'
server.dns_name.should == 'dns_name'
server.group_id.should == 'group_id'
server.image_id.should == 'image_id'
server.id.should == 'instance_id'
server.type.should == 'instance_type'
server.kernel_id.should == 'kernel_id'
server.key_name.should == 'key_name'
server.launch_time.should == 'launch_time'
server.product_codes.should == 'product_codes'
server.private_dns_name.should == 'private_dns_name'
server.ramdisk_id.should == 'ramdisk_id'
end
end
describe "#addresses" do
it "should return a Fog::AWS::EC2::Addresses" do
server = ec2.servers.create(:image_id => GENTOO_AMI)
server.addresses.should be_a(Fog::AWS::EC2::Addresses)
server.destroy
end
end
describe "#destroy" do
it "should return true if the server is deleted" do
server = ec2.servers.create(:image_id => GENTOO_AMI)
server.destroy.should be_true
end
end
describe "#state" do
it "should remap values out of hash" do
server = Fog::AWS::EC2::Server.new({
'instanceState' => { 'name' => 'instance_state' },
})
server.state.should == 'instance_state'
end
end
describe "#collection" do
it "should return a Fog::AWS::EC2::Servers" do
ec2.servers.new.collection.should be_a(Fog::AWS::EC2::Servers)
end
it "should be the servers the server is related to" do
servers = ec2.servers
servers.new.collection.should == servers
end
end
describe "#key_pair" do
it "should have tests"
end
describe "#key_pair=" do
it "should have tests"
end
describe "#monitoring=" do
it "should remap values out of hash" do
server = Fog::AWS::EC2::Server.new({
'monitoring' => { 'state' => true }
})
server.monitoring.should == true
end
end
describe "#placement=" do
it "should remap values into availability_zone" do
server = Fog::AWS::EC2::Server.new({
'placement' => { 'availabilityZone' => 'availability_zone' }
})
server.availability_zone.should == 'availability_zone'
end
end
describe "#reload" do
before(:each) do
@server = ec2.servers.create(:image_id => GENTOO_AMI)
@reloaded = @server.reload
end
after(:each) do
@server.destroy
end
it "should return a Fog::AWS::EC2::Server" do
@reloaded.should be_a(Fog::AWS::EC2::Server)
end
it "should reset attributes to remote state" do
@server.attributes.should == @reloaded.attributes
end
end
describe "#save" do
before(:each) do
@server = ec2.servers.new(:image_id => GENTOO_AMI)
end
it "should return true when it succeeds" do
@server.save.should be_true
@server.destroy
end
it "should not exist in servers before save" do
ec2.servers.get(@server.id).should be_nil
end
it "should exist in buckets after save" do
@server.save
ec2.servers.get(@server.id).should_not be_nil
@server.destroy
end
end
describe "#volumes" do
it "should return a Fog::AWS::EC2::Volumes" do
server = ec2.servers.create(:image_id => GENTOO_AMI)
server.volumes.should be_a(Fog::AWS::EC2::Volumes)
server.destroy
end
end
end

View file

@ -0,0 +1,70 @@
require File.dirname(__FILE__) + '/../../../spec_helper'
describe 'Fog::AWS::EC2::Servers' do
describe "#all" do
it "should return a Fog::AWS::EC2::Servers" do
ec2.servers.all.should be_a(Fog::AWS::EC2::Servers)
end
it "should include persisted servers" do
server = ec2.servers.create(:image_id => GENTOO_AMI)
ec2.servers.get(server.id).should_not be_nil
server.destroy
end
end
describe "#create" do
before(:each) do
@server = ec2.servers.create(:image_id => GENTOO_AMI)
end
after(:each) do
@server.destroy
end
it "should return a Fog::AWS::EC2::Server" do
@server.should be_a(Fog::AWS::EC2::Server)
end
it "should exist on ec2" do
ec2.servers.get(@server.id).should_not be_nil
end
end
describe "#get" do
it "should return a Fog::AWS::EC2::Server if a matching server exists" do
server = ec2.servers.create(:image_id => GENTOO_AMI)
get = ec2.servers.get(server.id)
server.attributes.should == get.attributes
server.destroy
end
it "should return nil if no matching server exists" do
ec2.servers.get('i-00000000').should be_nil
end
end
describe "#new" do
it "should return a Fog::AWS::EC2::Server" do
ec2.servers.new(:image_id => GENTOO_AMI).should be_a(Fog::AWS::EC2::Server)
end
end
describe "#reload" do
it "should return a Fog::AWS::EC2::Servers" do
ec2.servers.all.reload.should be_a(Fog::AWS::EC2::Servers)
end
end
end

View file

@ -16,7 +16,7 @@ describe 'Fog::AWS::EC2::Volume' do
volume.attach_time.should == 'now'
volume.availability_zone.should == 'us-east-1a'
volume.create_time.should == 'recently'
volume.instance_id.should == 'i-00000000'
volume.server_id.should == 'i-00000000'
volume.snapshot_id.should == 'snap-00000000'
volume.id.should == 'vol-00000000'
end
@ -45,49 +45,49 @@ describe 'Fog::AWS::EC2::Volume' do
end
describe "#instance=" do
describe "#server=" do
before(:each) do
@instance = ec2.instances.create(:image_id => GENTOO_AMI)
@volume = ec2.volumes.new(:availability_zone => @instance.availability_zone, :size => 1, :device => '/dev/sdz1')
@instance.wait_for { state == 'running' }
@server = ec2.servers.create(:image_id => GENTOO_AMI)
@volume = ec2.volumes.new(:availability_zone => @server.availability_zone, :size => 1, :device => '/dev/sdz1')
@server.wait_for { state == 'running' }
end
after(:each) do
@instance.destroy
@server.destroy
if @volume.id
@volume.wait_for { status == 'attached' }
@volume.instance = nil
@volume.server = nil
@volume.wait_for { status == 'available' }
@volume.destroy
end
end
it "should not attach to instance if the volume has not been saved" do
@volume.instance = @instance
@volume.instance_id.should_not == @instance.id
it "should not attach to server if the volume has not been saved" do
@volume.server = @server
@volume.server_id.should_not == @server.id
end
it "should change the availability_zone if the volume has not been saved" do
@volume.instance = @instance
@volume.availability_zone.should == @instance.availability_zone
@volume.server = @server
@volume.availability_zone.should == @server.availability_zone
end
it "should attach to instance when the volume is saved" do
@volume.instance = @instance
it "should attach to server when the volume is saved" do
@volume.server = @server
@volume.save.should be_true
@volume.instance_id.should == @instance.id
@volume.server_id.should == @server.id
end
it "should attach to instance to an already saved volume" do
it "should attach to server to an already saved volume" do
@volume.save.should be_true
@volume.instance = @instance
@volume.instance_id.should == @instance.id
@volume.server = @server
@volume.server_id.should == @server.id
end
it "should not change the availability_zone if the volume has been saved" do
@volume.save.should be_true
@volume.instance = @instance
@volume.availability_zone.should == @instance.availability_zone
@volume.server = @server
@volume.availability_zone.should == @server.availability_zone
end
end