Skip to content

Commit

Permalink
chore: Static analysis fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Nov 30, 2024
1 parent 99079a4 commit 29f06fc
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
4 changes: 3 additions & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1305.v04f5ec1f3743</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -167,8 +168,9 @@
<!-- Test Dependencies -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import jenkins.plugins.openstack.compute.internal.Openstack;
import jenkins.plugins.openstack.compute.slaveopts.LauncherFactory;
import jenkins.util.Timer;
import org.acegisecurity.Authentication;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.cloudstats.CloudStatistics;
import org.jenkinsci.plugins.cloudstats.ProvisioningActivity;
Expand All @@ -44,6 +43,7 @@
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.openstack4j.api.exceptions.AuthenticationException;
import org.openstack4j.model.compute.Server;
import org.springframework.security.core.Authentication;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -293,7 +293,8 @@ private void injectReferenceIntoTemplates() {
}

@Override
public Collection<NodeProvisioner.PlannedNode> provision(Label label, int excessWorkload) {
public Collection<NodeProvisioner.PlannedNode> provision(CloudState cs, int excessWorkload) {
Label label = cs.getLabel();
excessWorkload = Math.min(excessWorkload, MaxProvisioningExcessWorkLoadCap);
Queue<JCloudsSlaveTemplate> templateProvider = getAvailableTemplateProvider(label, excessWorkload);

Expand Down Expand Up @@ -345,9 +346,9 @@ public Node call() {
}

@Override
public boolean canProvision(final Label label) {
public boolean canProvision(final CloudState cs) {
for (JCloudsSlaveTemplate t : templates)
if (t.canProvision(label))
if (t.canProvision(cs.getLabel()))
return true;
return false;
}
Expand Down Expand Up @@ -424,10 +425,10 @@ public void doProvision(StaplerRequest req, StaplerResponse rsp, @QueryParameter
}

private void provisionAsynchronouslyNotToBlockTheRequestThread(JCloudsSlaveTemplate t) throws Throwable {
Authentication auth = Jenkins.getAuthentication();
Authentication auth = Jenkins.getAuthentication2();
Callable<Void> performProvisioning = () -> {
// Impersonate current identity inside the worker thread not to lose the owner info
try (ACLContext ignored = ACL.as(auth)) {
try (ACLContext ignored = ACL.as2(auth)) {
try {
provisionSlaveExplicitly(t);
return null;
Expand Down Expand Up @@ -595,7 +596,7 @@ public ListBoxModel doFillCredentialsIdItems() {
jenkins.checkPermission(Jenkins.ADMINISTER);

return new StandardListBoxModel()
.includeMatchingAs(ACL.SYSTEM, jenkins, StandardCredentials.class,
.includeMatchingAs(ACL.SYSTEM2, jenkins, StandardCredentials.class,
Collections.<DomainRequirement>emptyList(),
CredentialsMatchers.instanceOf(OpenstackCredential.class))
.includeEmptyValue();
Expand All @@ -617,7 +618,7 @@ public ProvisioningFailedException(String msg) {
}
}

/*package*/ static final class LoginFailure extends RuntimeException {
public static final class LoginFailure extends RuntimeException {

private static final long serialVersionUID = 4085466675398031930L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ public Descriptor<JCloudsSlaveTemplate> getDescriptor() {

@Extension
public static final class DescriptorImpl extends Descriptor<JCloudsSlaveTemplate> {
@Nonnull
@Override
public String getDisplayName() {
return clazz.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class OpenstackCredentials {
public static @CheckForNull OpenstackCredential getCredential(@CheckForNull String credentialsId) {
if (credentialsId == null) return null;

List<OpenstackCredential> credentials = CredentialsProvider.lookupCredentials(
OpenstackCredential.class, Jenkins.get(), ACL.SYSTEM,
List<OpenstackCredential> credentials = CredentialsProvider.lookupCredentialsInItemGroup(
OpenstackCredential.class, Jenkins.get(), ACL.SYSTEM2,
Collections.emptyList()
);
return CredentialsMatchers.firstOrNull(credentials, CredentialsMatchers.withId(credentialsId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ public ComputerLauncher createLauncher(@Nonnull JCloudsSlave slave) {
String publicAddress;
try {
publicAddress = slave.getPublicAddress();
if (publicAddress == null) {
throw new JCloudsCloud.ProvisioningFailedException("No accessible address provided for agent " + slave.getNodeName());
}
} catch (NoSuchElementException ex) {
throw new JCloudsCloud.ProvisioningFailedException(ex.getMessage(), ex);
}
Expand Down Expand Up @@ -220,13 +223,13 @@ public ComputerLauncher createLauncher(@Nonnull JCloudsSlave slave) {
public static final class Desc extends Descriptor<LauncherFactory> {
@Restricted(DoNotUse.class)
@RequirePOST
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup context) {
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> context) {
if (!(context instanceof AccessControlled ? (AccessControlled) context : Jenkins.get()).hasPermission(Computer.CONFIGURE)) {
return new ListBoxModel();
}

return new StandardUsernameListBoxModel()
.includeMatchingAs(ACL.SYSTEM, context, StandardUsernameCredentials.class,
.includeMatchingAs(ACL.SYSTEM2, context, StandardUsernameCredentials.class,
Collections.singletonList(SSHLauncher.SSH_SCHEME), SSHAuthenticator.matcher(Connection.class))
.includeEmptyValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public JCloudsCloud configureSlaveProvisioningWithFloatingIP(final JCloudsCloud
}

public JCloudsCloud configureSlaveProvisioning(JCloudsCloud cloud, Collection<NetworkAddress> networks) {
if (cloud.getTemplates().size() == 0) throw new Error("Unable to provision - no templates provided");
if (cloud.getTemplates().isEmpty()) throw new Error("Unable to provision - no templates provided");

final List<Server> running = new ArrayList<>();
Openstack os = cloud.getOpenstack();
Expand Down Expand Up @@ -425,7 +425,7 @@ public JCloudsCloud configureSlaveProvisioning(JCloudsCloud cloud, Collection<Ne
}

public JCloudsSlave provision(JCloudsCloud cloud, String label) throws ExecutionException, InterruptedException, IOException {
Collection<PlannedNode> slaves = cloud.provision(Label.get(label), 1);
Collection<PlannedNode> slaves = cloud.provision(new Cloud.CloudState(Label.get(label), 1), 1);
if (slaves.size() != 1) throw new AssertionError("One slave expected to be provisioned, was " + slaves.size());

PlannedNode plannedNode = slaves.iterator().next();
Expand Down Expand Up @@ -670,7 +670,7 @@ public static final class Descriptor extends hudson.model.Descriptor<Cloud> {
}

public TypeSafeMatcher<FormValidation> validateAs(final FormValidation.Kind kind, final String msg) {
return new TypeSafeMatcher<FormValidation>() {
return new TypeSafeMatcher<>() {
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText(kind.toString() + ": " + msg);
Expand All @@ -689,7 +689,7 @@ protected boolean matchesSafely(FormValidation item) {
}

public TypeSafeMatcher<FormValidation> validateAs(final FormValidation expected) {
return new TypeSafeMatcher<FormValidation>() {
return new TypeSafeMatcher<>() {
@Override
public void describeTo(org.hamcrest.Description description) {
description.appendText(expected.kind.toString() + ": " + expected.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jenkins.plugins.openstack.compute;

import hudson.slaves.Cloud;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebRequest;
import org.htmlunit.xml.XmlPage;
Expand Down Expand Up @@ -182,7 +183,7 @@ public void doProvision() throws Exception {
// Exceed template quota
XmlPage provision = invokeProvisioning(cloud, wc, "/provision?name=" + constrained.getName());
assertThat(provision.getWebResponse().getStatusCode(), equalTo(200));
while (Jenkins.get().getNodes().size() == 0) {
while (Jenkins.get().getNodes().isEmpty()) {
Thread.sleep(500);
}
String slaveName = j.jenkins.getNodes().get(0).getNodeName();
Expand Down Expand Up @@ -309,7 +310,7 @@ public void timeoutProvisioning() throws Exception {
Server server = mock(Server.class);
when(os.getServersByName(anyString())).thenReturn(Collections.singletonList(server));

for (NodeProvisioner.PlannedNode pn : c.provision(Label.get("label"), 1)) {
for (NodeProvisioner.PlannedNode pn : c.provision(new Cloud.CloudState(Label.get("label"), 0), 1)) {
try {
pn.future.get();
fail();
Expand All @@ -330,7 +331,7 @@ public void timeoutProvisioning() throws Exception {
public void timeoutLaunchingJnlp() throws Exception {
final SlaveOptions opts = j.defaultSlaveOptions().getBuilder().startTimeout(7000).build();
final JCloudsCloud cloud = j.configureSlaveProvisioningWithFloatingIP(j.dummyCloud(opts, j.dummySlaveTemplate("asdf")));
final Iterable<NodeProvisioner.PlannedNode> pns = cloud.provision(Label.get("asdf"), 1);
final Iterable<NodeProvisioner.PlannedNode> pns = cloud.provision(new Cloud.CloudState(Label.get("asdf"), 0), 1);
assertThat(pns, iterableWithSize(1));
final PlannedNode pn = pns.iterator().next();
final Future<Node> pnf = pn.future;
Expand Down Expand Up @@ -455,7 +456,7 @@ public void preferFixedIpv4() throws Exception {

@Test
public void findFixedIpv4WhenNoExplicitTypeIsGiven() throws Exception {
verifyPreferredAddressUsed("43.43.43.", Arrays.asList(
verifyPreferredAddressUsed("43.43.43.", List.of(
NetworkAddress.FIXED_4_NO_EXPLICIT_TYPE
));
}
Expand Down

0 comments on commit 29f06fc

Please sign in to comment.