Skip to content

Commit 4497cc7

Browse files
c0d1ngm0nk3ypbusko
authored andcommitted
fix buildpack uniqness check and manifest message
Co-authored-by: Pavel Busko <[email protected]>
1 parent 133546b commit 4497cc7

File tree

9 files changed

+18
-14
lines changed

9 files changed

+18
-14
lines changed

app/actions/buildpack_create.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def create(message)
2929

3030
def validation_error!(error, create_message)
3131
error!(%(Stack '#{create_message.stack}' does not exist)) if error.errors.on(:stack)&.include?(:buildpack_stack_does_not_exist)
32-
error!(%(Buildpack with name '#{error.model.name}' and stack '#{error.model.stack}' already exists)) if error.errors.on(%i[name stack])&.include?(:unique)
32+
if error.errors.on(%i[name stack lifecycle])&.include?(:unique)
33+
error!(%(Buildpack with name '#{error.model.name}', stack '#{error.model.stack}' and lifecycle '#{error.model.lifecycle}' already exists))
34+
end
3335
error!(%(Buildpack with name '#{error.model.name}' and an unassigned stack already exists)) if error.errors.on(:stack)&.include?(:unique)
3436

3537
error!(error.message)

app/controllers/runtime/buildpacks_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def self.dependencies
1616
query_parameters :name, :stack, :lifecycle
1717

1818
def self.translate_validation_exception(e, attributes)
19-
buildpack_errors = e.errors.on(%i[name stack])
19+
buildpack_errors = e.errors.on(%i[name stack lifecycle])
2020
if buildpack_errors && buildpack_errors.include?(:unique)
21-
CloudController::Errors::ApiError.new_from_details('BuildpackNameStackTaken', attributes['name'], attributes['stack'])
21+
CloudController::Errors::ApiError.new_from_details('BuildpackNameStackLifecycleTaken', attributes['name'], attributes['stack'], attributes['lifecycle'])
2222
else
2323
CloudController::Errors::ApiError.new_from_details('BuildpackInvalid', e.errors.full_messages)
2424
end

app/messages/app_manifest_message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def app_lifecycle_hash
127127
cnb_lifecycle_data
128128
elsif requested?(:docker)
129129
docker_lifecycle_data
130-
else
130+
elsif requested?(:buildpacks) || requested?(:buildpack) || requested?(:stack)
131131
buildpacks_lifecycle_data
132132
end
133133

app/models/runtime/buildpack.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def state
8484
def validate_multiple_nil_stacks
8585
return unless stack.nil?
8686

87-
errors.add(:stack, :unique) if Buildpack.exclude(guid:).where(name: name, stack: nil).present?
87+
errors.add(:stack, :unique) if Buildpack.exclude(guid:).where(name: name, stack: nil, lifecycle: lifecycle).present?
8888
end
8989

9090
def validate_stack_change

errors/v2.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,9 @@
964964
message: "Encountered an error while attempting to sync cloud controller with the service broker's catalog: %s"
965965

966966
290000:
967-
name: BuildpackNameStackTaken
967+
name: BuildpackNameStackLifecycleTaken
968968
http_code: 422
969-
message: "The buildpack name %s is already in use for the stack %s"
969+
message: "The buildpack name %s is already in use for the stack %s and the lifecycle %s"
970970

971971
290001:
972972
name: BuildpackNameTaken

lib/cloud_controller/upload_buildpack.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ def upload_buildpack(buildpack, bits_file_path, new_filename)
5656
private
5757

5858
def raise_translated_api_error(buildpack)
59-
raise CloudController::Errors::ApiError.new_from_details('BuildpackNameStackTaken', buildpack.name, buildpack.stack) if buildpack.errors.on(%i[name stack]).try(:include?,
60-
:unique)
59+
if buildpack.errors.on(%i[name stack lifecycle]).try(:include?, :unique)
60+
raise CloudController::Errors::ApiError.new_from_details('BuildpackNameStackLifecycleTaken', buildpack.name, buildpack.stack, buildpack.lifecycle)
61+
end
62+
6163
raise CloudController::Errors::ApiError.new_from_details('BuildpackStacksDontMatch', buildpack.stack, buildpack.initial_value(:stack)) if buildpack.errors.on(:stack).try(
6264
:include?, :buildpack_cant_change_stacks
6365
)

spec/unit/actions/buildpack_create_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ module VCAP::CloudController
178178
message = BuildpackCreateMessage.new(name: name, stack: 'the-stack')
179179
expect do
180180
BuildpackCreate.new.create(message)
181-
end.to raise_error(BuildpackCreate::Error, "Buildpack with name 'the-name' and stack 'the-stack' already exists")
181+
end.to raise_error(BuildpackCreate::Error, "Buildpack with name 'the-name', stack 'the-stack' and lifecycle 'buildpack' already exists")
182182
end
183183
end
184184
end

spec/unit/lib/cloud_controller/upload_buildpack_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ module VCAP::CloudController
131131
VCAP::CloudController::Buildpack.create(name: buildpack.name, stack: valid_zip_manifest_stack)
132132
expect do
133133
upload_buildpack.upload_buildpack(buildpack, valid_zip, filename)
134-
end.to raise_error(CloudController::Errors::ApiError, /The buildpack name #{buildpack.name} is already in use for the stack #{valid_zip_manifest_stack}/)
134+
end.to raise_error(CloudController::Errors::ApiError,
135+
/The buildpack name #{buildpack.name} is already in use for the stack #{valid_zip_manifest_stack} and the lifecycle #{buildpack.lifecycle}/)
135136
end
136137
end
137138
end

spec/unit/messages/app_manifest_message_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,11 +1993,10 @@ module VCAP::CloudController
19931993
context 'when no lifecycle data is requested in the manifest' do
19941994
let(:parsed_yaml) { {} }
19951995

1996-
it 'defaults to the buildpack lifecycle' do
1996+
it 'does not forward missing attributes to the AppUpdateMessage' do
19971997
message = AppManifestMessage.create_from_yml(parsed_yaml)
19981998

1999-
expect(message.app_update_message.requested?(:lifecycle)).to be true
2000-
expect(message.app_update_message.lifecycle_type).to eq('buildpack')
1999+
expect(message.app_update_message.requested?(:lifecycle)).to be false
20012000
end
20022001
end
20032002

0 commit comments

Comments
 (0)