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

Add validations and dynamic default for cloud name (for UI users only) #392

Merged
merged 5 commits into from
Aug 9, 2023

Conversation

pdk27
Copy link
Collaborator

@pdk27 pdk27 commented Aug 3, 2023

Issue: #387
Follow-up of #390

Changes:

  • adding cloud name validation to maintain uniqueness
    Before, duplicates were allowed. Now, disallowing duplicates for default name suggestions and user provided names as cloud name is used for tracking agents to (volatile) cloud objects and it's name should really be unique. See this for context.

  • add dynamic default for cloud name
    Replacing a static default FleetCloud with a dynamic default to avoid duplicates. A number suffix will be added like FleetCloud-1 if FleetCloud exists.

Testing:

Pasted Graphic 5
Pasted Graphic 4

@pdk27 pdk27 marked this pull request as ready for review August 3, 2023 19:55
@pdk27 pdk27 changed the title Uniq cloud name Add validations and dynamic default for cloud name (for UI users only) Aug 3, 2023
Comment on lines 6 to 21
public class CloudUtil {

public static Boolean isCloudNameUnique(final String name) {
return !Jenkins.get().clouds.stream().anyMatch(c -> c.name.equals(name));
}

public static String getUniqDefaultCloudName(final List<String> existingCloudNames, final String defaultCloudName) {
String uniqName = defaultCloudName;
int suffix = 1;
while (existingCloudNames.contains(uniqName)) {
uniqName = defaultCloudName + "-" + suffix++;
}

return uniqName;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. It is generally discouraged for class names to include 'Util'. What about CloudNames (since all the methods deal with the names of Cloud objects)? Then the methods become isUnique and generateUnique.
  2. In getUniqDefaultCloudName, since isCloudNameUnique pulls names from the Jenkins instance it seems that should be an option too. Could refactor into two methods generateUnique(baseName) and generateUniqueAmongst(cloudNames, baseName). The generateUnique method can then consolidate the logic that is currently in the EC2CloudFleet.getUnqiueCloudName method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

  1. I was going for a helper class for some future refactoring needs. But, CloudNames is cleaner. Made the change.
  2. refactored generateUnique too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Was this file added by accident? It doesn't appear to be referenced anywhere.

value = "EC2FleetCloud/name-required-configuration-as-code.yml",
expected = ConfiguratorException.class,
message = "error configuring 'jenkins' with class io.jenkins.plugins.casc.core.JenkinsConfigurator configurator")
public void configurationWithNullName_shouldFail() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

name-required-configuration-as-code.yml is used here.

@cjerad cjerad self-requested a review August 9, 2023 15:47
@pdk27 pdk27 merged commit 7743d70 into jenkinsci:master Aug 9, 2023
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.

2 participants