Skip to content

Commit

Permalink
Make platform loading from YAML files more robust
Browse files Browse the repository at this point in the history
This ensures users upgrading from 0.1.0 -> 0.2.0 can run rake db:seed
after a db:migrate and pick up the latest platforms
  • Loading branch information
bbrowning committed Dec 20, 2010
1 parent 0917467 commit 31af52c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
7 changes: 4 additions & 3 deletions app/models/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ def needs_storage_volume?
!storage_volume_capacity.blank?
end

# See Platform.create_from_yaml_file
# See Platform.load_from_yaml_file
def self.new_from_yaml(yaml)
services = yaml.delete('services')
cloud_images = yaml.delete('cloud_images')
image = Image.find_or_create_by_uid(yaml)
image.update_attributes!(yaml)
image_services = image.services
image.services.clear
services && services.each do |service_name|
service = Service.find_or_create_by_name(service_name)
image.services << service unless image_services.include?(service)
image.services << service
end
image.cloud_images.clear
cloud_images && cloud_images.each do |cloud_image_yaml|
image.cloud_images.find_or_create_by_cloud_id(cloud_image_yaml)
end
Expand Down
7 changes: 5 additions & 2 deletions app/models/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ def self.load_from_yaml_file(file_path)
platform_versions = platform_yaml.delete('platform_versions') || []
platform = Platform.find_or_create_by_name(platform_yaml)
platform_versions.each do |version_yaml|
if !platform.platform_versions.exists?(:version_number => version_yaml['version_number'].to_s)
platform.platform_versions << PlatformVersion.new_from_yaml(version_yaml)
version = platform.platform_versions.first(:conditions => {:version_number => version_yaml['version_number'].to_s})
unless version
version = PlatformVersion.new
end
version.update_from_yaml(version_yaml)
platform.platform_versions << version
end
platform.save!
end
Expand Down
14 changes: 7 additions & 7 deletions app/models/platform_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ class PlatformVersion < ActiveRecord::Base
accepts_nested_attributes_for :images, :allow_destroy => true

validates_presence_of :version_number

def to_s
version = version_number.blank? ? '' : " v#{version_number}"
version = version_number.blank? ? '' : " v#{version_number}"
"#{platform}#{version}"
end

# See Platform.create_from_yaml_file
def self.new_from_yaml(yaml)
# See Platform.load_from_yaml_file
def update_from_yaml(yaml)
images = yaml.delete('images') || []
version = PlatformVersion.new(yaml)
update_attributes(yaml)
self.images.clear
images.each do |image_yaml|
version.images << Image.new_from_yaml(image_yaml)
self.images << Image.new_from_yaml(image_yaml)
end
version
end
end
15 changes: 15 additions & 0 deletions spec/fixtures/platforms/cloud_images_support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
platforms:
- name: Test Platform
platform_versions:
- version_number: 123
images:
- name: Test Image 123
uid: image_123
cloud_images:
- cloud: ec2
region: us-west-1
architecture: i386
cloud_id: ami-12345
- name: Test Image 234
uid: image_234
3 changes: 2 additions & 1 deletion spec/fixtures/platforms/images_support.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ platforms:
uid: image_234
- version_number: 234
images:
- uid: image_123
- name: Test Image 123
uid: image_123
24 changes: 24 additions & 0 deletions spec/models/platform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,29 @@
version.images.should_not be_empty
Image.find_all_by_uid('image_123').count.should be(1)
end

it "should support cloud images" do
Platform.load_from_yaml_file(File.join(@yaml_path, 'cloud_images_support.yml'))
cloud_image = Image.find_by_uid('image_123').cloud_images.first
cloud_image.should_not be_nil
cloud_image.cloud.should == 'ec2'
cloud_image.region.should == 'us-west-1'
cloud_image.architecture.should == 'i386'
cloud_image.cloud_id.should == 'ami-12345'
end

it "should update cloud images" do
yaml_path = File.join(@yaml_path, 'cloud_images_support.yml')
Platform.load_from_yaml_file(yaml_path)
cloud_image = Image.find_by_uid('image_123').cloud_images.first
cloud_image.cloud_id.should == 'ami-12345'

yaml = YAML::load_file(yaml_path)
yaml['platforms'].first['platform_versions'].first['images'].first['cloud_images'].first['cloud_id'] = 'ami-23456'
YAML.should_receive(:load_file).and_return(yaml)
Platform.load_from_yaml_file(yaml_path)
cloud_image = Image.find_by_uid('image_123').cloud_images.first
cloud_image.cloud_id.should == 'ami-23456'
end
end
end

0 comments on commit 31af52c

Please sign in to comment.