Skip to content

Commit

Permalink
Support picture create via Picture object or options hash
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Apr 29, 2019
1 parent 58b5dbd commit 75d8e0b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
19 changes: 10 additions & 9 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,7 @@ def create_resource_actions(ae_endpoints)
end

def self.create_from_options(options)
create(options.except(:config_info, :picture).merge(:options => { :config_info => options[:config_info] })).tap do |tmp|
if options[:picture]
tmp.update_attributes!(:picture => Picture.create(:content => options[:picture][:content], :extension => options[:picture][:extension]))
end
end
create!(options.except(:config_info).merge(:options => { :config_info => options[:config_info] }))
end
private_class_method :create_from_options

Expand All @@ -403,6 +399,14 @@ def provision_request(user, options = nil, request_options = {})
result[:request]
end

def picture=(value)
if value
super unless value.kind_of?(Hash)

super(create_picture(value))
end
end

def queue_order(user_id, options, request_options)
MiqQueue.submit_job(
:class_name => self.class.name,
Expand Down Expand Up @@ -520,10 +524,7 @@ def resource_action_list

def update_from_options(params)
options[:config_info] = params[:config_info]
if params[:picture]
update_attributes!(:picture => Picture.create(:content => params[:picture][:content], :extension => params[:picture][:extension]))
end
update_attributes!(params.except(:config_info, :picture))
update_attributes!(params.except(:config_info))
end

def construct_config_info
Expand Down
49 changes: 35 additions & 14 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@

expect(service_template.name).to eq('Atomic Service Template')
expect(service_template.service_resources.count).to eq(1)
expect(Base64.strict_encode64(service_template.picture.content)).to start_with('aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQUFFQUFBQUJDQVlBQ')
expect(service_template.service_resources.first.resource_type).to eq('MiqRequest')
expect(service_template.dialogs.first).to eq(service_dialog)
expect(service_template.resource_actions.pluck(:action)).to include('Provision', 'Retirement')
Expand Down Expand Up @@ -1038,20 +1039,40 @@
@catalog_item.update_attributes!(:options => @catalog_item.options.merge(:foo => 'bar'))
end

it 'updates the catalog item' do
updated = @catalog_item.update_catalog_item(updated_catalog_item_options, user)

# Removes Retirement / Adds Reconfigure
expect(updated.resource_actions.pluck(:action)).to match_array(%w(Provision Reconfigure))
expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision
expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1')
expect(updated.resource_actions.last.dialog).to eq(service_dialog)
expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1')
expect(updated.name).to eq('Updated Template Name')
expect(Base64.strict_encode64(updated.picture.content)).to eq('aVZCT1J3MEtHZ29BQUFBTg==')
expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update
expect(updated.config_info).to eq(updated_catalog_item_options[:config_info])
expect(updated.options.key?(:foo)).to be_truthy # Test that the options were merged
context "without config info" do
it 'updates the catalog item' do
updated = @catalog_item.update_catalog_item(updated_catalog_item_options, user)

# Removes Retirement / Adds Reconfigure
expect(updated.resource_actions.pluck(:action)).to match_array(%w[Provision Reconfigure])
expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision
expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1')
expect(updated.resource_actions.last.dialog).to eq(service_dialog)
expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1')
expect(updated.name).to eq('Updated Template Name')
expect(Base64.strict_encode64(updated.picture.content)).to eq('aVZCT1J3MEtHZ29BQUFBTg==')
expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update
expect(updated.config_info).to eq(updated_catalog_item_options[:config_info])
expect(updated.options.key?(:foo)).to be_truthy # Test that the options were merged
end
end

context "with config info" do
it 'updates the catalog item' do
@catalog_item.update_attributes!(:options => @catalog_item.options.merge(:config_info => 'bar'))
updated = @catalog_item.update_catalog_item(updated_catalog_item_options, user)

expect(updated.resource_actions.pluck(:action)).to match_array(%w[Provision Reconfigure])
expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision
expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1')
expect(updated.resource_actions.last.dialog).to eq(service_dialog)
expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1')
expect(updated.name).to eq('Updated Template Name')
expect(Base64.strict_encode64(updated.picture.content)).to eq('aVZCT1J3MEtHZ29BQUFBTg==')
expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update
expect(updated.config_info).to eq(updated_catalog_item_options[:config_info])
expect(updated.options.key?(:foo)).to be_truthy # Test that the options were merged
end
end

it 'does not allow service_type to be changed' do
Expand Down

0 comments on commit 75d8e0b

Please sign in to comment.