Search in sources :

Example 1 with CloudVmMetaDataStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus in project cloudbreak by hortonworks.

the class AwsMetadataCollector method collectGroupMetaData.

private Collection<CloudVmMetaDataStatus> collectGroupMetaData(AuthenticatedContext ac, AmazonAutoScaling amazonASClient, AmazonEC2 amazonEC2Client, AmazonCloudFormation amazonCFClient, String groupName, Iterable<CloudInstance> cloudInstances) {
    Collection<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
    String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, groupName);
    List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
    DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
    DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
    // contains instances with instanceId
    Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(cloudInstances);
    // contains instances with privateId (without instanceId)
    Queue<CloudInstance> untrackedInstances = untrackedInstances(cloudInstances);
    for (Reservation reservation : instancesResult.getReservations()) {
        LOGGER.info("Number of instances found in reservation: {}", reservation.getInstances().size());
        for (Instance instance : reservation.getInstances()) {
            String instanceId = instance.getInstanceId();
            CloudInstance cloudInstance = ensureInstanceTag(mapByInstanceId, instance, instanceId, untrackedInstances, amazonEC2Client);
            if (cloudInstance != null) {
                CloudInstanceMetaData md = new CloudInstanceMetaData(instance.getPrivateIpAddress(), instance.getPublicIpAddress());
                CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md);
                cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus);
            }
        }
    }
    return cloudVmMetaDataStatuses;
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)

Example 2 with CloudVmMetaDataStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus in project cloudbreak by hortonworks.

the class AwsMetaDataCollectorTest method collectNewOneGroup.

@Test
public void collectNewOneGroup() {
    List<CloudInstance> vms = new ArrayList<>();
    List<Volume> volumes = new ArrayList<>();
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
    when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
    List<String> gatewayIds = Collections.singletonList("i-1");
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
    when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
    when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
    Instance instance = Mockito.mock(Instance.class);
    when(instance.getInstanceId()).thenReturn("i-1");
    when(instance.getPrivateIpAddress()).thenReturn("privateIp");
    when(instance.getPublicIpAddress()).thenReturn("publicIp");
    List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance));
    when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
    AuthenticatedContext ac = authenticatedContext();
    List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
    verify(amazonEC2Client).createTags(any(CreateTagsRequest.class));
    Assert.assertEquals(1, statuses.size());
    Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
    Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp());
    Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp());
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Reservation(com.amazonaws.services.ec2.model.Reservation) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.Test)

Example 3 with CloudVmMetaDataStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus in project cloudbreak by hortonworks.

the class AwsMetaDataCollectorTest method collectAlreadyTaggedOneGroup.

@Test
public void collectAlreadyTaggedOneGroup() {
    List<CloudInstance> vms = new ArrayList<>();
    List<Volume> volumes = new ArrayList<>();
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
    when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
    List<String> gatewayIds = Collections.singletonList("i-1");
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
    when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
    when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
    Instance instance = Mockito.mock(Instance.class);
    when(instance.getInstanceId()).thenReturn("i-1");
    when(instance.getPrivateIpAddress()).thenReturn("privateIp");
    when(instance.getPublicIpAddress()).thenReturn("publicIp");
    Tag tag = new Tag();
    tag.setKey("cbname");
    tag.setValue("somevalue");
    when(instance.getTags()).thenReturn(Collections.singletonList(tag));
    List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance));
    when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
    AuthenticatedContext ac = authenticatedContext();
    List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
    verify(amazonEC2Client, never()).createTags(any(CreateTagsRequest.class));
    Assert.assertEquals(1, statuses.size());
    Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
    Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp());
    Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp());
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Reservation(com.amazonaws.services.ec2.model.Reservation) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.Test)

Example 4 with CloudVmMetaDataStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus in project cloudbreak by hortonworks.

the class AwsMetaDataCollectorTest method collectNewOldIsTagged.

@Test
public void collectNewOldIsTagged() {
    List<CloudInstance> vms = new ArrayList<>();
    List<Volume> volumes = new ArrayList<>();
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
    when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
    when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
    when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
    List<String> gatewayIds = Arrays.asList("i-1", "i-new");
    when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
    when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
    when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
    Instance instance1 = Mockito.mock(Instance.class);
    when(instance1.getInstanceId()).thenReturn("i-1");
    when(instance1.getPrivateIpAddress()).thenReturn("privateIp1");
    when(instance1.getPublicIpAddress()).thenReturn("publicIp1");
    Tag tag = new Tag();
    tag.setKey("cbname");
    tag.setValue("somevalue");
    when(instance1.getTags()).thenReturn(Collections.singletonList(tag));
    Instance instance2 = Mockito.mock(Instance.class);
    when(instance2.getInstanceId()).thenReturn("i-new");
    when(instance2.getPrivateIpAddress()).thenReturn("privateIp2");
    when(instance2.getPublicIpAddress()).thenReturn("publicIp2");
    List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2));
    when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
    AuthenticatedContext ac = authenticatedContext();
    List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
    verify(amazonEC2Client, times(1)).createTags(any(CreateTagsRequest.class));
    Assert.assertEquals(1, statuses.size());
    Assert.assertEquals("i-new", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
    Assert.assertEquals("privateIp2", statuses.get(0).getMetaData().getPrivateIp());
    Assert.assertEquals("publicIp2", statuses.get(0).getMetaData().getPublicIp());
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Reservation(com.amazonaws.services.ec2.model.Reservation) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) Tag(com.amazonaws.services.ec2.model.Tag) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Test(org.junit.Test)

Example 5 with CloudVmMetaDataStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus in project cloudbreak by hortonworks.

the class AzureMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    CloudResource resource = azureUtils.getTemplateResource(resources);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
    String resourceName = resource.getName();
    Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> azureUtils.getPrivateInstanceId(resourceName, from.getGroupName(), Long.toString(from.getPrivateId())));
    try {
        for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
            AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
            VirtualMachine vm = azureClient.getVirtualMachine(resourceName, instance.getKey());
            String subnetId = vm.getPrimaryNetworkInterface().primaryIPConfiguration().subnetName();
            String privateIp = null;
            String publicIp = null;
            Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceName, vm.name());
            String platform = authenticatedContext.getCloudContext().getPlatform().value();
            String location = authenticatedContext.getCloudContext().getLocation().getRegion().value();
            String hostgroupNm = instance.getValue().getGroupName();
            StringBuilder localityIndicatorBuilder = new StringBuilder();
            localityIndicatorBuilder.append(LOCALITY_SEPARATOR).append(platform).append(LOCALITY_SEPARATOR).append(location).append(LOCALITY_SEPARATOR).append(resourceName).append(LOCALITY_SEPARATOR).append(hostgroupNm).append(LOCALITY_SEPARATOR).append(faultDomainCount);
            AzureUtils.removeBlankSpace(localityIndicatorBuilder);
            List<String> networkInterfaceIdList = vm.networkInterfaceIds();
            for (String networkInterfaceId : networkInterfaceIdList) {
                NetworkInterface networkInterface = azureClient.getNetworkInterfaceById(networkInterfaceId);
                privateIp = networkInterface.primaryPrivateIP();
                PublicIPAddress publicIpAddress = networkInterface.primaryIPConfiguration().getPublicIPAddress();
                List<LoadBalancerBackend> backends = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerBackends();
                List<LoadBalancerInboundNatRule> inboundNatRules = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerInboundNatRules();
                if (!backends.isEmpty() || !inboundNatRules.isEmpty()) {
                    publicIp = azureClient.getLoadBalancerIps(resource.getName(), azureUtils.getLoadBalancerId(resource.getName())).get(0);
                }
                if (publicIpAddress != null && publicIpAddress.ipAddress() != null) {
                    publicIp = publicIpAddress.ipAddress();
                }
            }
            String instanceId = instance.getKey();
            CloudInstanceMetaData md = new CloudInstanceMetaData(privateIp, publicIp, faultDomainCount == null ? null : localityIndicatorBuilder.toString());
            InstanceTemplate template = templateMap.get(instanceId);
            if (template != null) {
                Map<String, Object> params = new HashMap<>(1);
                params.put(CloudInstance.SUBNET_ID, subnetId);
                // TODO use shhkey here
                CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, params);
                CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                results.add(new CloudVmMetaDataStatus(status, md));
            }
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) LoadBalancerInboundNatRule(com.microsoft.azure.management.network.LoadBalancerInboundNatRule) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) LoadBalancerBackend(com.microsoft.azure.management.network.LoadBalancerBackend) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Aggregations

CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)43 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20 ArrayList (java.util.ArrayList)18 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)13 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)12 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)10 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)9 Service (spark.Service)8 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)7 Map (java.util.Map)7 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)6 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)6 Instance (com.amazonaws.services.ec2.model.Instance)6 Reservation (com.amazonaws.services.ec2.model.Reservation)6 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)6 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)6 AmbariClusterResponse (com.sequenceiq.it.spark.ambari.AmbariClusterResponse)6 EmptyAmbariResponse (com.sequenceiq.it.spark.ambari.EmptyAmbariResponse)6 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)5 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)5