-
Notifications
You must be signed in to change notification settings - Fork 83
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
Conversation
jenkinsci#382 Co-authored-by: naraharip2017
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; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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 becomeisUnique
andgenerateUnique
. - In
getUniqDefaultCloudName
, sinceisCloudNameUnique
pulls names from theJenkins
instance it seems that should be an option too. Could refactor into two methodsgenerateUnique(baseName)
andgenerateUniqueAmongst(cloudNames, baseName)
. ThegenerateUnique
method can then consolidate the logic that is currently in theEC2CloudFleet.getUnqiueCloudName
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
- I was going for a
helper
class for some future refactoring needs. But,CloudNames
is cleaner. Made the change. - refactored
generateUnique
too.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
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 likeFleetCloud-1
ifFleetCloud
exists.Testing: