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

Force unique endpoint hostname only for same type #12912

Merged
merged 1 commit into from
Jan 4, 2017

Conversation

moolitayer
Copy link

@moolitayer moolitayer commented Nov 30, 2016

In the use case of a provider of alerts/metrics for other providers added in #12205, our prominent use case in the beginning (cm-ops) would include two providers from the same host - a container provider and a datawarehouse provider running inside it.

@moolitayer moolitayer changed the title Ease unique hostname constraint Force unique hostname for endpoints only for the same provider type Dec 1, 2016
@moolitayer moolitayer changed the title Force unique hostname for endpoints only for the same provider type Force unique endpoint hostname only for same type Dec 1, 2016
@moolitayer moolitayer force-pushed the remove_constraint branch 2 times, most recently from 371e032 to a2f890e Compare December 1, 2016 14:35
existing_hostnames = Endpoint.where.not(:resource_id => id).pluck(:hostname).compact.map(&:downcase)
# check uniqueness per provider type
ids = ExtManagementSystem.where(:type => type).pluck(:id).compact - [id]
existing_hostnames = Endpoint.where(:resource_id => [ids]).pluck(:hostname).compact.map(&:downcase)
Copy link
Member

Choose a reason for hiding this comment

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

I think this code was flawed before, because it was not taking :resource_type into account which can lead to a collision of :ids - which admittedly is rare, but...

TBH I also think the usage of Endpoint here is wrong, because later on hostname is delegated to default_endpoint, which is a special endpoint etc.

Why not just

existing_hostnames = (self.class.all - [self]).map(&:hostname).compact.map(&:downcase)

cc @juliancheal

@moolitayer
Copy link
Author

@durandom @juliancheal PTAL

@moolitayer moolitayer closed this Dec 6, 2016
@moolitayer moolitayer reopened this Dec 6, 2016
@durandom
Copy link
Member

durandom commented Dec 6, 2016

LGTM
@juliancheal can you have a look at my original comment? Maybe I oversaw something with the Endpoints

@moolitayer
Copy link
Author

@blomquisg PTAL

@moolitayer
Copy link
Author

@blomquisg ping

@blomquisg
Copy link
Member

@moolitayer I think the error message should be more specific now that the conditions have changed, but I have no idea how to phrase the error message.

Any ideas ... if we can't figure it out, we could merge this and deal with the error message as a translation issue.

@moolitayer
Copy link
Author

@blomquisg I'm not sure I have anything better. How about:

  • [hostname] has already been taken for this type
  • there is already a [provider type] with this hostname


existing_hostnames = Endpoint.where.not(:resource_id => id).pluck(:hostname).compact.map(&:downcase)

existing_hostnames = (self.class.all - [self]).map(&:hostname).compact.map(&:downcase)
errors.add(:hostname, "has already been taken") if existing_hostnames.include?(hostname.downcase)
Copy link
Member

Choose a reason for hiding this comment

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

How about

errors.add(:hostname, "has to be unique per provider type")

Also you have to wrap the message in _() to be translated. See https://github.com/durandom/manageiq/blob/master/app/models/dialog_tab.rb#L28

Copy link
Member

Choose a reason for hiding this comment

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

@durandom I think I saw that the error was already being I18N'd. I'll have dig up where I saw that. Basically, the UI is taking the error message from the model and doing an I18N lookup from what I saw.

Copy link
Member

Choose a reason for hiding this comment

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

@mzazrivec ☝️ I guess @blomquisg is right, but then dialog_tab.rb#L28 is wrong I guess. Time for @martinpovolny i18n rubocop

Copy link
Contributor

Choose a reason for hiding this comment

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

I think dialog_tab.rb is correct. In general, if a message (a validation message in this case) will be propagated into the UI, it needs to be inside _(). In some cases, it needs to be in N_() and
the actual translation is done by _() somewhere in the UI (controller or view), but that's a different
discussion really.

Copy link
Author

Choose a reason for hiding this comment

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

@mzazrivec So I understand in this case we need N_ (insert to catalog but don't translate) since it's being translated somewhere else. Please correct if wrong...

Copy link
Contributor

Choose a reason for hiding this comment

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

@moolitayer I don't know if in this case the message would be translated somewhere else (I'd say it's not). All I'm saying is that there are cases, where you cannot use plain _() in models, like
for example when the validation message is first put into a log file and then rendered in UI,
we need to use N_() in the model (because we want english only messages in the logs) and then
find the place where that same message is being rendered for UI and apply the _() there.

Anyway, I think it's okay to use _() here.

Copy link
Author

Choose a reason for hiding this comment

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

This is like the example you provided, the error is being put in the log so I'm keeping it N_() unless someone says otherwise...

@miq-bot
Copy link
Member

miq-bot commented Dec 14, 2016

Checked commit moolitayer@0941c98 with ruby 2.2.5, rubocop 0.37.2, and haml-lint 0.16.1
2 files checked, 0 offenses detected
Everything looks good. 🏆

@moolitayer
Copy link
Author

@blomquisg please review

@moolitayer
Copy link
Author

@blomquisg PTAL

@blomquisg blomquisg merged commit 7bfb5f3 into ManageIQ:master Jan 4, 2017
@blomquisg blomquisg added this to the Sprint 52 Ending Jan 16, 2017 milestone Jan 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants