Search in sources :

Example 1 with SUBNET_ID

use of com.sequenceiq.cloudbreak.common.network.NetworkConstants.SUBNET_ID in project cloudbreak by hortonworks.

the class AzureMetadataCollector method collect.

@Override
@Retryable(backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 10000), maxAttempts = 5)
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> knownInstances) {
    LOGGER.debug("Starting to collect vm metadata.");
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    String resourceGroup = azureUtils.getTemplateResource(resources).getName();
    AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
    Map<String, InstanceTemplate> templateMap = getTemplateMap(vms, authenticatedContext);
    Map<String, VirtualMachine> virtualMachinesByName = azureVirtualMachineService.getVirtualMachinesByName(azureClient, resourceGroup, templateMap.keySet());
    azureVirtualMachineService.refreshInstanceViews(virtualMachinesByName);
    try {
        for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
            VirtualMachine vm = virtualMachinesByName.get(instance.getKey());
            // TODO: network interface is lazy, so we will fetch it for every instances
            if (vm != null) {
                NetworkInterface networkInterface = vm.getPrimaryNetworkInterface();
                String subnetId = networkInterface.primaryIPConfiguration().subnetName();
                Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceGroup, vm.name());
                String publicIp = azureVmPublicIpProvider.getPublicIp(networkInterface);
                String instanceId = instance.getKey();
                String localityIndicator = Optional.ofNullable(faultDomainCount).map(domainCount -> getLocalityIndicator(domainCount, authenticatedContext.getCloudContext(), instance.getValue(), resourceGroup)).orElse(null);
                CloudInstanceMetaData md = new CloudInstanceMetaData(networkInterface.primaryPrivateIP(), publicIp, localityIndicator);
                InstanceTemplate template = templateMap.get(instanceId);
                if (template != null) {
                    Map<String, Object> params = new HashMap<>(1);
                    params.put(SUBNET_ID, subnetId);
                    params.put(CloudInstance.INSTANCE_NAME, vm.computerName());
                    CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, subnetId, null, params);
                    CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                    results.add(new CloudVmMetaDataStatus(status, md));
                }
            }
        }
    } catch (RuntimeException e) {
        LOGGER.debug("Failed to collect vm metadata due to an exception: ", e);
        throw new CloudConnectorException(e.getMessage(), e);
    }
    LOGGER.debug("Metadata collection finished with result {}", results);
    return results;
}
Also used : NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureLoadBalancer(com.sequenceiq.cloudbreak.cloud.azure.loadbalancer.AzureLoadBalancer) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArrayList(java.util.ArrayList) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) SUBNET_ID(com.sequenceiq.cloudbreak.common.network.NetworkConstants.SUBNET_ID) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Service(org.springframework.stereotype.Service) Map(java.util.Map) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Retryable(org.springframework.retry.annotation.Retryable) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) InstanceStoreMetadata(com.sequenceiq.cloudbreak.cloud.model.InstanceStoreMetadata) Logger(org.slf4j.Logger) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) MetadataCollector(com.sequenceiq.cloudbreak.cloud.MetadataCollector) Collectors(java.util.stream.Collectors) Backoff(org.springframework.retry.annotation.Backoff) List(java.util.List) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Entry(java.util.Map.Entry) Optional(java.util.Optional) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) HashMap(java.util.HashMap) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) Retryable(org.springframework.retry.annotation.Retryable)

Aggregations

VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)1 NetworkInterface (com.microsoft.azure.management.network.NetworkInterface)1 MetadataCollector (com.sequenceiq.cloudbreak.cloud.MetadataCollector)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 AzureLoadBalancer (com.sequenceiq.cloudbreak.cloud.azure.loadbalancer.AzureLoadBalancer)1 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)1 CloudLoadBalancerMetadata (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)1 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)1 InstanceStatus (com.sequenceiq.cloudbreak.cloud.model.InstanceStatus)1 InstanceStoreMetadata (com.sequenceiq.cloudbreak.cloud.model.InstanceStoreMetadata)1 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)1 SUBNET_ID (com.sequenceiq.cloudbreak.common.network.NetworkConstants.SUBNET_ID)1 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)1 ArrayList (java.util.ArrayList)1