Search in sources :

Example 1 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class AwsResourceConnector method upscale.

@Override
public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) {
    resumeAutoScaling(ac, stack);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    List<Group> scaledGroups = getScaledGroups(stack);
    for (Group group : scaledGroups) {
        String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName());
        amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest().withAutoScalingGroupName(asGroupName).withMaxSize(group.getInstancesSize()).withDesiredCapacity(group.getInstancesSize()));
        LOGGER.info("Updated Auto Scaling group's desiredCapacity: [stack: '{}', to: '{}']", ac.getCloudContext().getId(), resources.size());
    }
    scheduleStatusChecks(stack, ac, cloudFormationClient);
    suspendAutoScaling(ac, stack);
    boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(new AwsNetworkView(stack.getNetwork()), amazonEC2Client);
    List<Group> gateways = getGatewayGroups(scaledGroups);
    if (mapPublicIpOnLaunch && !gateways.isEmpty()) {
        String cFStackName = getCloudFormationStackResource(resources).getName();
        Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, cloudFormationClient);
        for (Group gateway : gateways) {
            List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway);
            List<String> freeEips = getFreeIps(eips, amazonEC2Client);
            List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, cloudFormationClient, gateway);
            List<String> newInstances = instanceIds.stream().filter(iid -> gateway.getInstances().stream().noneMatch(inst -> iid.equals(inst.getInstanceId()))).collect(Collectors.toList());
            associateElasticIpsToInstances(amazonEC2Client, freeEips, newInstances);
        }
    }
    return singletonList(new CloudResourceStatus(getCloudFormationStackResource(resources), ResourceStatus.UPDATED));
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest) ROLLBACK_IN_PROGRESS(com.amazonaws.services.cloudformation.model.StackStatus.ROLLBACK_IN_PROGRESS) SuspendProcessesRequest(com.amazonaws.services.autoscaling.model.SuspendProcessesRequest) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) ROLLBACK_FAILED(com.amazonaws.services.cloudformation.model.StackStatus.ROLLBACK_FAILED) AdjustmentType(com.sequenceiq.cloudbreak.api.model.AdjustmentType) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Retry(com.sequenceiq.cloudbreak.service.Retry) SyncPollingScheduler(com.sequenceiq.cloudbreak.cloud.scheduler.SyncPollingScheduler) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DELETE_FAILED(com.amazonaws.services.cloudformation.model.StackStatus.DELETE_FAILED) AwsPollTaskFactory(com.sequenceiq.cloudbreak.cloud.aws.task.AwsPollTaskFactory) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) StackStatus(com.amazonaws.services.cloudformation.model.StackStatus) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) InstanceGroupType(com.sequenceiq.cloudbreak.api.model.InstanceGroupType) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Lists(com.google.common.collect.Lists) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Service(org.springframework.stereotype.Service) Image(com.amazonaws.services.ec2.model.Image) Filter(com.amazonaws.services.ec2.model.Filter) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DELETE_COMPLETE(com.amazonaws.services.cloudformation.model.StackStatus.DELETE_COMPLETE) CREATE_FAILED(com.amazonaws.services.cloudformation.model.StackStatus.CREATE_FAILED) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) Parameter(com.amazonaws.services.cloudformation.model.Parameter) ROLLBACK_COMPLETE(com.amazonaws.services.cloudformation.model.StackStatus.ROLLBACK_COMPLETE) IOException(java.io.IOException) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) ImportKeyPairRequest(com.amazonaws.services.ec2.model.ImportKeyPairRequest) Configuration(freemarker.template.Configuration) InetAddresses(com.google.common.net.InetAddresses) SubnetUtils(org.apache.commons.net.util.SubnetUtils) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) OnFailure(com.amazonaws.services.cloudformation.model.OnFailure) TlsInfo(com.sequenceiq.cloudbreak.cloud.model.TlsInfo) AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CREATE_COMPLETE(com.amazonaws.services.cloudformation.model.StackStatus.CREATE_COMPLETE) Collection(java.util.Collection) Output(com.amazonaws.services.cloudformation.model.Output) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) Collectors(java.util.stream.Collectors) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) ActionWentFailException(com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Entry(java.util.Map.Entry) Optional(java.util.Optional) AmazonAutoScaling(com.amazonaws.services.autoscaling.AmazonAutoScaling) HashMap(java.util.HashMap) Inject(javax.inject.Inject) DeleteKeyPairRequest(com.amazonaws.services.ec2.model.DeleteKeyPairRequest) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) Charset(java.nio.charset.Charset) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) Qualifier(org.springframework.beans.factory.annotation.Qualifier) ResumeProcessesRequest(com.amazonaws.services.autoscaling.model.ResumeProcessesRequest) PollTask(com.sequenceiq.cloudbreak.cloud.task.PollTask) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) Subnet(com.amazonaws.services.ec2.model.Subnet) ResourceConnector(com.sequenceiq.cloudbreak.cloud.ResourceConnector) Logger(org.slf4j.Logger) ResourceType(com.sequenceiq.cloudbreak.common.type.ResourceType) Address(com.amazonaws.services.ec2.model.Address) Vpc(com.amazonaws.services.ec2.model.Vpc) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) Collections(java.util.Collections) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 2 with AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext 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 AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext 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 AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext 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 AuthenticatedContext

use of com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext in project cloudbreak by hortonworks.

the class AwsResourceConnectorTest method testFindNonOverLappingCIDRWithNon24Subnets.

@Test
public void testFindNonOverLappingCIDRWithNon24Subnets() {
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
    Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    Map<String, Object> networkParameters = new HashMap<>();
    networkParameters.put("vpcId", "vpc-12345678");
    networkParameters.put("internetGatewayId", "igw-12345678");
    Network network = new Network(new Subnet(null), networkParameters);
    CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudContext cloudContext = mock(CloudContext.class);
    Location location = mock(Location.class);
    Vpc vpc = mock(Vpc.class);
    DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
    AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
    com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
    DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location);
    when(cloudContext.getName()).thenReturn(new String(new byte[] { 23 }));
    when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
    when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
    when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
    when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
    when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
    when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
    when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
    when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
    when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
    when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
    when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/24");
    String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
    Assert.assertEquals("10.0.49.0/24", cidr);
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Vpc(com.amazonaws.services.ec2.model.Vpc) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Location(com.sequenceiq.cloudbreak.cloud.model.Location) Test(org.junit.Test)

Aggregations

AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)52 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)34 Test (org.junit.Test)26 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)20 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)19 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)18 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)17 Location (com.sequenceiq.cloudbreak.cloud.model.Location)17 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)15 Vpc (com.amazonaws.services.ec2.model.Vpc)15 Group (com.sequenceiq.cloudbreak.cloud.model.Group)15 Network (com.sequenceiq.cloudbreak.cloud.model.Network)15 HashMap (java.util.HashMap)15 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)14 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)14 ArrayList (java.util.ArrayList)11 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)10 Event (reactor.bus.Event)10 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)8 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)8