Skip to content

Commit

Permalink
Adding runtime config tags on update stemcell
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisAhausSAP committed Feb 7, 2025
1 parent 5b188b4 commit a42fcfd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
11 changes: 10 additions & 1 deletion src/bosh-director/lib/bosh/director/jobs/update_stemcell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ def perform
upload_suffix = ' (already exists, skipped)' unless needs_upload
track_and_log("Uploading stemcell #{@name}/#{@version} to the cloud#{cpi_suffix}#{upload_suffix}") do
if needs_upload
stemcell.cid = cloud.create_stemcell(@stemcell_image, @cloud_properties, {})
tags = {}
runtime_configs = Bosh::Director::Api::RuntimeConfigManager.new.list(1, "default")
if !runtime_configs.nil? && !runtime_configs[0].nil?
raw = runtime_configs[0]
if !raw.nil?
hash = raw.to_hash
tags.merge!(hash["tags"])
end
end
stemcell.cid = cloud.create_stemcell(@stemcell_image, @cloud_properties, {"tags" => tags})
logger.info("Cloud created stemcell#{cpi_suffix}: #{stemcell.cid}")
else
logger.info("Skipping stemcell upload, already exists#{cpi_suffix}")
Expand Down
45 changes: 30 additions & 15 deletions src/bosh-director/spec/unit/jobs/update_stemcell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,22 @@
end
end


let(:runtime_config_manager) { instance_double(Bosh::Director::Api::RuntimeConfigManager) }
let(:config) { instance_double(Bosh::Director::Models::Config) }
let(:runtime_config_list) do
[config]

end

before do

allow(Bosh::Director::Api::RuntimeConfigManager).to receive(:new).and_return(runtime_config_manager)
allow(Bosh::Director::Models::Config).to receive(:new).and_return(config)
allow(config).to receive(:to_hash).and_return({"tags"=> {"any"=> "value"}})
allow(runtime_config_manager).to receive(:list).with(1, 'default').and_return(runtime_config_list)


allow(Bosh::Director::Config).to receive(:event_log).and_return(event_log)
allow(Bosh::Director::Config).to receive(:uuid).and_return('meow-uuid')
allow(Bosh::Director::Config).to receive(:cloud_options).and_return({'provider' => {'path' => '/path/to/default/cpi'}})
Expand All @@ -78,7 +93,7 @@
context 'uploading a local stemcell' do
it 'should upload a local stemcell' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eq(stemcell_image_content)
'stemcell-cid'
Expand Down Expand Up @@ -115,7 +130,7 @@

it 'should upload a local stemcell' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eq(stemcell_image_content)
'stemcell-cid'
Expand Down Expand Up @@ -143,7 +158,7 @@

it 'should upload a remote stemcell' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand Down Expand Up @@ -199,7 +214,7 @@

it 'should upload a remote stemcell' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand Down Expand Up @@ -227,7 +242,7 @@

it 'should cleanup the stemcell file' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand Down Expand Up @@ -311,8 +326,8 @@
end

it 'creates multiple stemcell records with different cpi attributes' do
expect(cloud1).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }).and_return('stemcell-cid1')
expect(cloud3).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }).and_return('stemcell-cid3')
expect(cloud1).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}).and_return('stemcell-cid1')
expect(cloud3).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}).and_return('stemcell-cid3')

expect(cloud1).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud2).to receive(:info).and_return('stemcell_formats' => ['dummy1'])
Expand Down Expand Up @@ -367,7 +382,7 @@
end

it 'skips creating a stemcell match when a CPI fails' do
expect(cloud1).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }).and_raise('I am flaky')
expect(cloud1).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}).and_raise('I am flaky')
expect(cloud1).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud_factory).to receive(:get).with('cloud1').and_return(cloud1)
expect(cloud_factory).to receive(:all_names).exactly(3).times.and_return(['cloud1'])
Expand All @@ -393,7 +408,7 @@
expect(cloud2).to receive(:info).and_return('stemcell_formats' => ['dummy1'])
expect(cloud3).to receive(:info).and_return('stemcell_formats' => ['dummy'])

expect(cloud3).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }).and_return('stemcell-cid3')
expect(cloud3).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}).and_return('stemcell-cid3')

expect(Bosh::Director::Models::Stemcell.all.count).to eq(1)
expect(Bosh::Director::Models::StemcellUpload.all.count).to eq(1)
Expand All @@ -419,7 +434,7 @@

it 'still works with the default cpi' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }).and_return('stemcell-cid')
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}).and_return('stemcell-cid')

expect(cloud_factory).to receive(:get_cpi_aliases).with('').and_return([''])
expect(cloud_factory).to receive(:all_names).exactly(3).times.and_return([''])
Expand Down Expand Up @@ -460,7 +475,7 @@
context 'when stemcell does not have stemcell formats' do
it 'should not fail' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand All @@ -482,7 +497,7 @@

it 'should not fail' do
expect(cloud).to receive(:info).and_return({})
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand All @@ -504,7 +519,7 @@

it 'should not fail' do
expect(cloud).to receive(:info).and_raise(Bosh::Clouds::NotImplemented)
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand All @@ -527,7 +542,7 @@

it 'should not fail' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eql(stemcell_image_content)
'stemcell-cid'
Expand All @@ -553,7 +568,7 @@

it 'should update api_version' do
expect(cloud).to receive(:info).and_return('stemcell_formats' => ['dummy'])
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }) do |image, _|
expect(cloud).to receive(:create_stemcell).with(anything, { 'ram' => '2gb' }, {"tags"=>{"any"=>"value"}}) do |image, _|
contents = File.open(image, &:read)
expect(contents).to eq(stemcell_image_content)
'stemcell-cid'
Expand Down

0 comments on commit a42fcfd

Please sign in to comment.