use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsNetworkServiceTest method testFindNonOverLappingCIDRWithNon24Subnets2.
@Test
public void testFindNonOverLappingCIDRWithNon24Subnets2() {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity, createGroupNetwork(), emptyMap());
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(), null);
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[] { (byte) 76 }));
when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
when(awsClient.createEc2Client(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/20");
String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
Assert.assertEquals("10.0.76.0/24", cidr);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsNetworkServiceTest method testFindNonOverLappingCIDRWit24Vpc.
@Test
public void testFindNonOverLappingCIDRWit24Vpc() {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity, createGroupNetwork(), emptyMap());
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(), null);
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);
DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(location);
when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
when(vpc.getCidrBlock()).thenReturn("10.0.0.0/24");
when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
when(subnetsResult.getSubnets()).thenReturn(singletonList(subnet1));
when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24");
thrown.expect(CloudConnectorException.class);
thrown.expectMessage("The selected VPC has to be in a bigger CIDR range than /24");
underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsAuthenticatorTest method testAuthenticate.
private AuthenticatedContext testAuthenticate(Map<String, Object> parameters) {
CloudContext context = CloudContext.Builder.builder().withId(1L).withName("context").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("country"))).withAccountId("account").build();
CloudCredential credential = new CloudCredential("id", "alma", parameters, "acc", false);
AuthenticatedContext auth = underTest.authenticate(context, credential);
assertTrue(auth.hasParameter(AmazonEc2Client.class.getName()), "Authenticated context does not have amazonClient after authentication");
assertSame(amazonEC2Client, auth.getParameter(AmazonEc2Client.class));
return auth;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsPlatformResources method sshKeys.
@Override
public CloudSshKeys sshKeys(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters) {
Map<String, Set<CloudSshKey>> result = new HashMap<>();
if (region != null && !Strings.isNullOrEmpty(region.value())) {
CloudRegions regions = regions(cloudCredential, region, new HashMap<>(), true);
for (Region actualRegion : regions.getCloudRegions().keySet()) {
// If region is provided then should filter for those region
if (regionMatch(actualRegion, region)) {
Set<CloudSshKey> cloudSshKeys = new HashSet<>();
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential), actualRegion.value());
// create sshkey filter view
PlatformResourceSshKeyFilterView filter = new PlatformResourceSshKeyFilterView(filters);
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
// If the filtervalue is provided then we should filter only for those securitygroups
if (!Strings.isNullOrEmpty(filter.getKeyName())) {
describeKeyPairsRequest.withKeyNames(filter.getKeyName());
}
for (KeyPairInfo keyPairInfo : ec2Client.describeKeyPairs(describeKeyPairsRequest).getKeyPairs()) {
Map<String, Object> properties = new HashMap<>();
properties.put("fingerPrint", keyPairInfo.getKeyFingerprint());
cloudSshKeys.add(new CloudSshKey(keyPairInfo.getKeyName(), properties));
}
result.put(actualRegion.value(), cloudSshKeys);
}
}
}
return new CloudSshKeys(result);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsPlatformResources method regions.
@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "{ #cloudCredential?.id, #availabilityZonesNeeded }")
public CloudRegions regions(ExtendedCloudCredential cloudCredential, Region region, Map<String, String> filters, boolean availabilityZonesNeeded) {
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(cloudCredential));
Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
Map<Region, String> displayNames = new HashMap<>();
Map<Region, Coordinate> coordinates = new HashMap<>();
DescribeRegionsResult describeRegionsResult = describeRegionsResult(ec2Client);
String defaultRegion = awsDefaultZoneProvider.getDefaultZone(cloudCredential);
for (com.amazonaws.services.ec2.model.Region awsRegion : describeRegionsResult.getRegions()) {
if (!enabledRegions.contains(region(awsRegion.getRegionName()))) {
continue;
}
if (region == null || Strings.isNullOrEmpty(region.value()) || awsRegion.getRegionName().equals(region.value())) {
try {
fetchAZsIfNeeded(availabilityZonesNeeded, regionListMap, awsRegion, cloudCredential);
} catch (AmazonEC2Exception e) {
LOGGER.info("Failed to retrieve AZ from Region: {}!", awsRegion.getRegionName(), e);
}
addDisplayName(displayNames, awsRegion);
addCoordinate(coordinates, awsRegion);
}
}
if (region != null && !Strings.isNullOrEmpty(region.value())) {
defaultRegion = region.value();
}
return new CloudRegions(regionListMap, displayNames, coordinates, defaultRegion, true);
}
Aggregations