SlaveOptionsDescriptor extends OsAuthDescriptor<SlaveOptions> { @Restricted(DoNotUse.class) @InjectOsAuth @RequirePOST public ComboBoxModel doFillAvailabilityZoneItems( @QueryParameter String availabilityZone, @QueryParameter String endPointUrl, @QueryParameter boolean ignoreSsl, @QueryParameter String credentialsId, @QueryParameter String zone ) { Jenkins.get().checkPermission(Jenkins.ADMINISTER); final ComboBoxModel m = new ComboBoxModel(); try { OpenstackCredential openstackCredential = OpenstackCredentials.getCredential(credentialsId); if (haveAuthDetails(endPointUrl, openstackCredential, zone)) { final Openstack openstack = Openstack.Factory.get(endPointUrl, ignoreSsl, openstackCredential, zone); for (final AvailabilityZone az : openstack.getAvailabilityZones()) { final String value = az.getZoneName(); m.add(value); } } } catch (AuthenticationException | FormValidation | ConnectionException ex) { LOGGER.log(Level.FINEST, "Openstack call failed", ex); } catch (Exception ex) { LOGGER.log(Level.SEVERE, ex.getMessage(), ex); } return m; } 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 doFillAvailabilityZoneItemsGivenAZsThenPopulatesList() { final AvailabilityZone az1 = mock(AvailabilityZone.class, "az1"); final AvailabilityZone az2 = mock(AvailabilityZone.class, "az2"); final String openstackAuth = j.dummyCredentials(); 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 ComboBoxModel actual = d.doFillAvailabilityZoneItems("az2Name", "OSurl", false, openstackAuth, "OSzone"); assertEquals(2, actual.size()); final String az1Option = actual.get(0); assertThat(az1Option, equalTo("az1Name")); final String az2Option = actual.get(1); assertThat(az2Option, equalTo("az2Name")); }
@Test public void doFillAvailabilityZoneItemsGivenNoSupportForAZsThenGivesEmptyList() { final Openstack os = j.fakeOpenstackFactory(); final String openstackAuth = j.dummyCredentials(); doThrow(new RuntimeException("OpenStack said no")).when(os).getAvailabilityZones(); final ComboBoxModel actual = d.doFillAvailabilityZoneItems("az2Name", "OSurl", false, openstackAuth, "OSzone"); assertEquals(0, actual.size()); }