From c24a7acc8873910de1a3480728e73d9ff8edbf39 Mon Sep 17 00:00:00 2001 From: Sandra Parsick Date: Fri, 7 Jan 2022 15:13:00 +0100 Subject: [PATCH] Use legacy instance id instead of Instance Identity (#95) --- .../builders/AciDeploymentTemplateBuilder.java | 12 ++++++------ .../util/CustomJenkinsFacade.java | 15 +++++++++++++++ .../util/InstanceIdentityFacade.java | 17 ----------------- .../AciDeploymentTemplateBuilderTest.java | 8 ++++---- 4 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/microsoft/jenkins/containeragents/util/CustomJenkinsFacade.java delete mode 100644 src/main/java/com/microsoft/jenkins/containeragents/util/InstanceIdentityFacade.java diff --git a/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java b/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java index 1ed144b..a785d30 100644 --- a/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java +++ b/src/main/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilder.java @@ -18,7 +18,7 @@ import com.microsoft.jenkins.containeragents.util.AzureContainerUtils; import com.microsoft.jenkins.containeragents.util.Constants; import com.microsoft.jenkins.containeragents.util.DockerRegistryUtils; -import com.microsoft.jenkins.containeragents.util.InstanceIdentityFacade; +import com.microsoft.jenkins.containeragents.util.CustomJenkinsFacade; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.EnvVars; import hudson.model.ItemGroup; @@ -45,15 +45,15 @@ public final class AciDeploymentTemplateBuilder { = "/com/microsoft/jenkins/containeragents/aci/networkProfileSnippet.json"; private final JenkinsFacade jenkins; - private final InstanceIdentityFacade instanceIdentityFacade; + private final CustomJenkinsFacade customJenkinsFacade; public AciDeploymentTemplateBuilder() { - this(new JenkinsFacade(), new InstanceIdentityFacade()); + this(new JenkinsFacade(), new CustomJenkinsFacade()); } - AciDeploymentTemplateBuilder(JenkinsFacade jenkins, InstanceIdentityFacade instanceIdentityFacade) { + AciDeploymentTemplateBuilder(JenkinsFacade jenkins, CustomJenkinsFacade customJenkinsFacade) { this.jenkins = jenkins; - this.instanceIdentityFacade = instanceIdentityFacade; + this.customJenkinsFacade = customJenkinsFacade; } @NonNull @@ -78,7 +78,7 @@ public AciDeploymentTemplate buildDeploymentTemplate(AciCloud cloud, AciContaine variables.put("cpu", template.getCpu()); variables.put("memory", template.getMemory()); variables.put("jenkinsInstance", - instanceIdentityFacade.getInstanceId()); + customJenkinsFacade.getLegacyInstanceId()); addLogAnalytics(tmp, parameters, mapper, cloud); addCommandNode(tmp, template.getCommand(), agent); diff --git a/src/main/java/com/microsoft/jenkins/containeragents/util/CustomJenkinsFacade.java b/src/main/java/com/microsoft/jenkins/containeragents/util/CustomJenkinsFacade.java new file mode 100644 index 0000000..c5b2685 --- /dev/null +++ b/src/main/java/com/microsoft/jenkins/containeragents/util/CustomJenkinsFacade.java @@ -0,0 +1,15 @@ +package com.microsoft.jenkins.containeragents.util; + +import jenkins.model.Jenkins; + +/** + * Facade to Jenkins instance . Encapsulates all calls to Jenkins instance so that tests can replace this facade + * with a stub. It is inspired by Jenkins Facade of plugin-util-api-plugin. + * + */ +public class CustomJenkinsFacade { + + public String getLegacyInstanceId() { + return Jenkins.get().getLegacyInstanceId(); + } +} diff --git a/src/main/java/com/microsoft/jenkins/containeragents/util/InstanceIdentityFacade.java b/src/main/java/com/microsoft/jenkins/containeragents/util/InstanceIdentityFacade.java deleted file mode 100644 index ec2d48f..0000000 --- a/src/main/java/com/microsoft/jenkins/containeragents/util/InstanceIdentityFacade.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.microsoft.jenkins.containeragents.util; - -import org.jenkinsci.main.modules.instance_identity.InstanceIdentity; - -import java.nio.charset.StandardCharsets; - -/** - * Facade to InstanceIdentity. Encapsulates all calls to tInstanceIdentity so that tests can replace this facade - * with a stub. It is inspired by Jenkins Facade of plugin-util-api-plugin. - * - */ -public class InstanceIdentityFacade { - - public String getInstanceId() { - return new String(InstanceIdentity.get().getPublic().getEncoded(), StandardCharsets.UTF_8); - } -} diff --git a/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java b/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java index aa62b46..cffd7cc 100644 --- a/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java +++ b/src/test/java/com/microsoft/jenkins/containeragents/builders/AciDeploymentTemplateBuilderTest.java @@ -5,7 +5,7 @@ import com.microsoft.jenkins.containeragents.aci.AciCloud; import com.microsoft.jenkins.containeragents.aci.AciContainerTemplate; import com.microsoft.jenkins.containeragents.aci.AciPrivateIpAddress; -import com.microsoft.jenkins.containeragents.util.InstanceIdentityFacade; +import com.microsoft.jenkins.containeragents.util.CustomJenkinsFacade; import hudson.slaves.RetentionStrategy; import hudson.slaves.SlaveComputer; import io.jenkins.plugins.util.JenkinsFacade; @@ -31,10 +31,10 @@ public void setup(){ SlaveComputer slaveMock = mock(SlaveComputer.class); when(agentMock.getComputer()).thenReturn(slaveMock); - InstanceIdentityFacade instanceIdentityFacadeMock = mock(InstanceIdentityFacade.class); - when(instanceIdentityFacadeMock.getInstanceId()).thenReturn("instanceId"); + CustomJenkinsFacade customJenkinsFacadeMock = mock(CustomJenkinsFacade.class); + when(customJenkinsFacadeMock.getLegacyInstanceId()).thenReturn("instanceId"); - builderUnderTest = new AciDeploymentTemplateBuilder(mock(JenkinsFacade.class), instanceIdentityFacadeMock); + builderUnderTest = new AciDeploymentTemplateBuilder(mock(JenkinsFacade.class), customJenkinsFacadeMock); } @Test