SlaveOptionsDescriptor extends OsAuthDescriptor<SlaveOptions> { @Restricted(DoNotUse.class) @RequirePOST public FormValidation doCheckAvailabilityZone( @QueryParameter String value, @RelativePath("../../slaveOptions") @QueryParameter("availabilityZone") String def, @RelativePath("..") @QueryParameter("endPointUrl") String endPointUrlCloud, @RelativePath("..") @QueryParameter("ignoreSsl") boolean ignoreSsl, @RelativePath("../..") @QueryParameter("endPointUrl") String endPointUrlTemplate, @RelativePath("..") @QueryParameter("credentialsId") String credentialsIdCloud, @RelativePath("../..") @QueryParameter("credentialsId") String credentialsIdTemplate, @RelativePath("..") @QueryParameter("zone") String zoneCloud, @RelativePath("../..") @QueryParameter("zone") String zoneTemplate ) { Jenkins.get().checkPermission(Jenkins.ADMINISTER); if (Util.fixEmpty(value) == null) { final String d = getDefault(def, opts().getAvailabilityZone()); if (d != null) return FormValidation.ok(def(d)); final String endPointUrl = getDefault(endPointUrlCloud,endPointUrlTemplate); final String credentialsId = getDefault(credentialsIdCloud,credentialsIdTemplate); final OpenstackCredential openstackCredential = OpenstackCredentials.getCredential(credentialsId); final String zone = getDefault(zoneCloud, zoneTemplate); if (haveAuthDetails(endPointUrl, openstackCredential, zone)) { try { final Openstack openstack = Openstack.Factory.get(endPointUrl, ignoreSsl, openstackCredential, zone); final int numberOfAZs = openstack.getAvailabilityZones().size(); if (numberOfAZs > 1) { return FormValidation.warning("Ambiguity warning: Multiple zones found."); } } catch (AuthenticationException | FormValidation | ConnectionException ex) { LOGGER.log(Level.FINEST, "Openstack call failed", ex); } catch (Exception ex) { LOGGER.log(Level.SEVERE, ex.getMessage(), ex); } } } return OK; } SlaveOptionsDescriptor(); @Override String getDisplayName(); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckInstanceCap(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("instanceCap") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckInstancesMin(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("instancesMin") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckStartTimeout(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("startTimeout") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckNumExecutors(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("numExecutors") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckRetentionTime(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("retentionTime") String def
    ); @Restricted(DoNotUse.class) @InjectOsAuth @RequirePOST ListBoxModel doFillFloatingIpPoolItems(
            @QueryParameter String floatingIpPool,
            @QueryParameter String endPointUrl, @QueryParameter boolean ignoreSsl,
            @QueryParameter String credentialsId, @QueryParameter String zone
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckFloatingIpPool(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("floatingIpPool") String def
    ); @Restricted(DoNotUse.class) @InjectOsAuth @RequirePOST ListBoxModel doFillHardwareIdItems(
            @QueryParameter String hardwareId, @QueryParameter String endPointUrl,
            @QueryParameter boolean ignoreSsl,
            @QueryParameter String credentialsId, @QueryParameter String zone
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckHardwareId(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("hardwareId") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckNetworkId(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("networkId") String def,
            // authentication fields can be in two places relative to us.
            @RelativePath("..") @QueryParameter("endPointUrl") String endPointUrlCloud,
            @RelativePath("..") @QueryParameter("ignoreSsl") boolean ignoreSsl,
            @RelativePath("../..") @QueryParameter("endPointUrl") String endPointUrlTemplate,
            @RelativePath("..") @QueryParameter("credentialsId") String credentialsIdCloud,
            @RelativePath("../..") @QueryParameter("credentialsId") String credentialsIdTemplate,
            @RelativePath("..") @QueryParameter("zone") String zoneCloud,
            @RelativePath("../..") @QueryParameter("zone") String zoneTemplate
    ); @Restricted(DoNotUse.class) @RequirePOST ListBoxModel doFillUserDataIdItems(); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckUserDataId(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("userDataId") String def
    ); @Restricted(DoNotUse.class) @RequirePOST ListBoxModel doFillConfigDriveItems(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("configDrive") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckSecurityGroups(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("securityGroups") String def
    ); @Restricted(DoNotUse.class) @InjectOsAuth @RequirePOST ComboBoxModel doFillAvailabilityZoneItems(
            @QueryParameter String availabilityZone, @QueryParameter String endPointUrl,
            @QueryParameter boolean ignoreSsl,
            @QueryParameter String credentialsId, @QueryParameter String zone
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckAvailabilityZone(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("availabilityZone") String def,
            // authentication fields can be in two places relative to us.
            @RelativePath("..") @QueryParameter("endPointUrl") String endPointUrlCloud,
            @RelativePath("..") @QueryParameter("ignoreSsl") boolean ignoreSsl,
            @RelativePath("../..") @QueryParameter("endPointUrl") String endPointUrlTemplate,
            @RelativePath("..") @QueryParameter("credentialsId") String credentialsIdCloud,
            @RelativePath("../..") @QueryParameter("credentialsId") String credentialsIdTemplate,
            @RelativePath("..") @QueryParameter("zone") String zoneCloud,
            @RelativePath("../..") @QueryParameter("zone") String zoneTemplate
    ); @Restricted(DoNotUse.class) @InjectOsAuth @RequirePOST ListBoxModel doFillKeyPairNameItems(
            @QueryParameter String keyPairName,
            @QueryParameter String endPointUrl,
            @QueryParameter boolean ignoreSsl,
            @QueryParameter String credentialsId, @QueryParameter String zone
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckKeyPairName(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("keyPairName") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckJvmOptions(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("jvmOptions") String def
    ); @Restricted(DoNotUse.class) @RequirePOST FormValidation doCheckFsRoot(
            @QueryParameter String value,
            @RelativePath("../../slaveOptions") @QueryParameter("fsRoot") String def
    ); @Override List<String> getAuthFieldsOffsets(); @Nonnull @Restricted(NoExternalUse.class) // used by Jelly EL only List<NodePropertyDescriptor> getNodePropertiesDescriptors(); @Override @Restricted(NoExternalUse.class) // Stapler SlaveOptions newInstance(@Nullable StaplerRequest req, @Nonnull JSONObject formData); static final FormValidation OK; static final FormValidation REQUIRED; }
@Test public void doCheckAvailabilityZoneGivenNoAZAndMultipleZoneToChooseFromThenReturnsWarning() { final AvailabilityZone az1 = mock(AvailabilityZone.class, "az1"); final AvailabilityZone az2 = mock(AvailabilityZone.class, "az2"); when(az1.getZoneName()).thenReturn("az1Name"); when(az2.getZoneName()).thenReturn("az2Name"); final List<AvailabilityZone> azs = Arrays.asList(az1, az2); final Openstack os = j.fakeOpenstackFactory(); doReturn(azs).when(os).getAvailabilityZones(); final String value = ""; final String def = ""; final String openstackAuth = j.dummyCredentials(); final FormValidation actual = d.doCheckAvailabilityZone(value, def, "OSurl", false, "OSurl",openstackAuth, openstackAuth, "OSzone", "OSzone"); assertThat(actual, j.validateAs(FormValidation.warning("Ambiguity warning: Multiple zones found."))); }
@Test public void doCheckAvailabilityZoneGivenAZThenReturnsOK() { final Openstack os = j.fakeOpenstackFactory(); final String openstackAuth = j.dummyCredentials(); final FormValidation actual = d.doCheckAvailabilityZone("chosenAZ", "", "OSurl", false,"OSurl", openstackAuth,openstackAuth,"OSzone", "OSzone"); assertThat(actual, j.validateAs(FormValidation.ok())); verifyNoMoreInteractions(os); }
@Test public void doCheckAvailabilityZoneGivenDefaultAZThenReturnsOKWithDefault() { final String value = ""; final String def = "defaultAZ"; final Openstack os = j.fakeOpenstackFactory(); final String openstackAuth = j.dummyCredentials(); final FormValidation actual = d.doCheckAvailabilityZone(value, def, "OSurl", false,"OSurl", openstackAuth,openstackAuth,"OSzone", "OSzone"); assertThat(actual, j.validateAs(OK, "Inherited value: " + def)); verifyNoMoreInteractions(os); }
@Test public void doCheckAvailabilityZoneGivenNoAZAndOnlyOneZoneToChooseFromThenReturnsOK() { final AvailabilityZone az1 = mock(AvailabilityZone.class, "az1"); when(az1.getZoneName()).thenReturn("az1Name"); final List<AvailabilityZone> azs = Collections.singletonList(az1); final Openstack os = j.fakeOpenstackFactory(); doReturn(azs).when(os).getAvailabilityZones(); final String openstackAuth = j.dummyCredentials(); final FormValidation actual = d.doCheckAvailabilityZone("", "", "OSurl", false,"OSurl", openstackAuth,openstackAuth, "OSzone", "OSzone"); assertThat(actual, j.validateAs(FormValidation.ok())); }
@Test public void doCheckAvailabilityZoneGivenNoAZAndNoSupportForAZsThenReturnsOK() { final Openstack os = j.fakeOpenstackFactory(); doThrow(new RuntimeException("OpenStack said no")).when(os).getAvailabilityZones(); final String value = ""; final String def = ""; final String openstackAuth = j.dummyCredentials(); final FormValidation actual = d.doCheckAvailabilityZone(value, def, "OSurl", false,"OSurl", openstackAuth, openstackAuth, "OSzone", "OSzone"); assertThat(actual, j.validateAs(FormValidation.ok())); }