Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS-9587 unique offer urls #1273

Merged
merged 4 commits into from
Jul 18, 2024
Merged

Conversation

kian99
Copy link
Contributor

@kian99 kian99 commented Jul 16, 2024

Description

This PR builds on #1272. Review the second commit from this PR.

This change ensures uniqueness of offer URLs. Offer URLs are used by Juju clients when performing cross-model relations. It's important that offer URLs maintain uniqueness as we use the URL to query our DB for offer details. Currently it is possible for an offer URL to be duplicated if a user creates an offer for 2 models where the models have the same name and the offer name is the same.

Two models with the same name are possible when they are owned by separate users. The offer URL resembles username/model-name.offer-name. To resolve this issue, add a unique constrain to the DB and simply check if the offer URL already exists before attempting to create it. If it does exist, return an error and request the user to use a different alias for the offer-name. By default when no offer-name alias is provided, the name will match the application being offered but the user can supply an alternative name.

Fixes CSS-9587

Engineering checklist

Check only items that apply

  • Documentation updated
  • Covered by unit tests
  • Covered by integration tests

@kian99 kian99 requested a review from a team as a code owner July 16, 2024 14:30
internal/jimm/applicationoffer.go Show resolved Hide resolved
Comment on lines +75 to +76
err = j.Database.GetApplicationOffer(ctx, &offerCheck)
if err == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Since err is just used in the if-else statement, you can limit the scope by doing if err := ...; err == nil {,

if err == nil {
return errors.E(fmt.Sprintf("offer %s already exists, use a different name", offerURL.String()), errors.CodeAlreadyExists)
} else {
if errors.ErrorCode(err) != errors.CodeNotFound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a leftover after removing something? -> else if
feel free to leave it as is, only a semantic difference

@kian99 kian99 force-pushed the CSS-9587-unique-offer-urls branch from 071858b to bc94895 Compare July 17, 2024 15:38
@@ -0,0 +1,4 @@
-- 1_11.sql is a migration that enforces uniqueness on URLs in application offers.
ALTER TABLE application_offers ADD UNIQUE (url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess again can we have the gorm tag too plz

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add this in a follow-up

offerCheck.URL = offerURL.String()
err = j.Database.GetApplicationOffer(ctx, &offerCheck)
if err == nil {
return errors.E(fmt.Sprintf("offer %s already exists, use a different name", offerURL.String()), errors.CodeAlreadyExists)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a different offer name would be better language

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add this in a follow-up

Copy link
Contributor

@ale8k ale8k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@kian99 kian99 merged commit 352a731 into canonical:v3 Jul 18, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants