public ManagementAddressResolveResult resolve()

in locations/jclouds/src/main/java/org/apache/brooklyn/location/jclouds/DefaultConnectivityResolver.java [169:260]


    public ManagementAddressResolveResult resolve(
            JcloudsLocation location, NodeMetadata node, ConfigBag config, ConnectivityResolverOptions options) {
        LOG.debug("{} resolving management parameters for {}, node={}, config={}, options={}",
                new Object[]{this, location, node, config, options});
        final Stopwatch timer = Stopwatch.createStarted();
        // Should only be null in tests.
        final Entity contextEntity = getContextEntity(config);
        if (contextEntity==null && !BrooklynVersion.isDevelopmentEnvironment()) {
            LOG.debug("No context entity found in config or current task when resolving "+location);
        }
        if (shouldPublishNetworks() && !options.isRebinding() && contextEntity != null) {
            publishNetworks(node, contextEntity);
        }
        HostAndPort hapChoice = null;
        LoginCredentials credChoice = null;

        final Iterable<HostAndPort> managementCandidates = getManagementCandidates(location, node, config, options);
        Iterable<LoginCredentials> credentialCandidates = Collections.emptyList();
        if (!Iterables.isEmpty(managementCandidates)) {
            credentialCandidates = getCredentialCandidates(location, node, options, config);

            // Try each pair of address and credential until one succeeds.
            if (shouldCheckCredentials() && options.pollForReachableAddresses()) {
                for (HostAndPort hap : managementCandidates) {
                    for (LoginCredentials cred : credentialCandidates) {
                        LOG.trace("Testing host={} with credential={}", hap, cred);
                        if (checkCredential(location, hap, cred, config, options.isWindows())) {
                            hapChoice = hap;
                            credChoice = cred;
                            break;
                        }
                    }
                    if (hapChoice != null) break;
                }
            } else if (shouldCheckCredentials()) {
                LOG.debug("{} set on {} but pollForFirstReachableAddress={}",
                        new Object[]{CHECK_CREDENTIALS.getName(), this, options.pollForReachableAddresses()});
            }
        }

        if (hapChoice == null) {
            LOG.trace("Choosing first management candidate given node={} and mode={}", node, getNetworkMode());
            hapChoice = Iterables.getFirst(managementCandidates, null);
        }
        if (hapChoice == null) {
            LOG.trace("Choosing first address of node={} in mode={}", node, getNetworkMode());
            final Iterator<String> hit = getResolvableAddressesWithMode(node).iterator();
            if (hit.hasNext()) HostAndPort.fromHost(hit.next());
        }

        if (hapChoice == null) {
            LOG.error("None of the addresses of node {} are reachable in mode {}", new Object[]{node, getNetworkMode()});
            throw new IllegalStateException("Could not determine management address for node: " + node + " in mode: " + getNetworkMode());
        }
        
        if (credChoice == null) {
            credChoice = Iterables.getFirst(credentialCandidates, null);
            if (credChoice == null) {
                throw new IllegalStateException("No credentials configured for " + location);
            }
        }

        if (contextEntity != null) {
            contextEntity.sensors().set(Attributes.ADDRESS, hapChoice.getHost());
        }

        // Treat AWS as a special case because the DNS fully qualified hostname in AWS is
        // (normally?!) a good way to refer to the VM from both inside and outside of the region.
        if (!isNetworkModeSet() && !options.isWindows()) {
            final boolean lookupAwsHostname = Boolean.TRUE.equals(config.get(JcloudsLocationConfig.LOOKUP_AWS_HOSTNAME));
            String provider = config.get(JcloudsLocationConfig.CLOUD_PROVIDER);
            if (provider == null) {
                provider = location.getProvider();
            }
            if (options.waitForConnectable() && "aws-ec2".equals(provider) && lookupAwsHostname) {
                // getHostnameAws sshes to the machine and curls 169.254.169.254/latest/meta-data/public-hostname.
                try {
                    LOG.debug("Resolving AWS hostname of {}", location);
                    String result = location.getHostnameAws(hapChoice, credChoice, config);
                    hapChoice = HostAndPort.fromParts(result, hapChoice.getPort());
                    LOG.debug("Resolved AWS hostname of {}: {}", location, result);
                } catch (Exception e) {
                    LOG.debug("Failed to resolve AWS hostname of " + location, e);
                }
            }
        }

        ManagementAddressResolveResult result = new ManagementAddressResolveResult(hapChoice, credChoice);
        LOG.debug("{} resolved management parameters for {} in {}: {}",
                new Object[]{this, location, Duration.of(timer), result});
        return result;
    }