use of software.amazon.awssdk.services.ec2.model.Vpc in project aws-doc-sdk-examples by awsdocs.
the class DescribeSecurityGroups method describeEC2SecurityGroups.
// snippet-start:[ec2.java2.describe_security_groups.main]
public static void describeEC2SecurityGroups(Ec2Client ec2, String groupId) {
try {
DescribeSecurityGroupsRequest request = DescribeSecurityGroupsRequest.builder().groupIds(groupId).build();
DescribeSecurityGroupsResponse response = ec2.describeSecurityGroups(request);
for (SecurityGroup group : response.securityGroups()) {
System.out.printf("Found Security Group with id %s, " + "vpc id %s " + "and description %s", group.groupId(), group.vpcId(), group.description());
}
} catch (Ec2Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.ec2.model.Vpc in project cloudbreak by hortonworks.
the class AwsPlatformResources method networks.
@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
Map<String, Set<CloudNetwork>> result = new HashMap<>();
Set<CloudNetwork> cloudNetworks = new HashSet<>();
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value());
// create vpc filter view
PlatformResourceVpcFilterView filter = new PlatformResourceVpcFilterView(filters);
DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest();
// If the filtervalue is provided then we should filter only for those vpc
if (!Strings.isNullOrEmpty(filter.getVpcId())) {
describeVpcsRequest.withVpcIds(filter.getVpcId());
}
for (Vpc vpc : ec2Client.describeVpcs(describeVpcsRequest).getVpcs()) {
Map<String, String> subnetMap = new HashMap<>();
List<Subnet> subnets = ec2Client.describeSubnets(createVpcDescribeRequest(vpc)).getSubnets();
Map<String, Object> properties = new HashMap<>();
properties.put("cidrBlock", vpc.getCidrBlock());
properties.put("default", vpc.getIsDefault());
properties.put("dhcpOptionsId", vpc.getDhcpOptionsId());
properties.put("instanceTenancy", vpc.getInstanceTenancy());
properties.put("state", vpc.getState());
for (Subnet subnet : subnets) {
subnetMap.put(subnet.getSubnetId(), subnet.getSubnetId());
}
cloudNetworks.add(new CloudNetwork(vpc.getVpcId(), vpc.getVpcId(), subnetMap, properties));
}
result.put(region.value(), cloudNetworks);
return new CloudNetworks(result);
}
use of software.amazon.awssdk.services.ec2.model.Vpc in project cloudbreak by hortonworks.
the class AwsResourceConnector method findNonOverLappingCIDR.
protected String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) {
AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);
DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVPC());
Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
String vpcCidr = vpc.getCidrBlock();
LOGGER.info("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr);
DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVPC())));
List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets();
List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList());
LOGGER.info("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), subnetCidrs.stream().collect(Collectors.joining(",")));
return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs);
}
use of software.amazon.awssdk.services.ec2.model.Vpc in project cloudbreak by hortonworks.
the class AwsResourceConnectorTest 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());
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);
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.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/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 software.amazon.awssdk.services.ec2.model.Vpc in project cloudbreak by hortonworks.
the class AwsResourceConnectorTest method testFindNonOverLappingCIDRWit20Vpc2.
@Test
public void testFindNonOverLappingCIDRWit20Vpc2() {
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[] { 16 }));
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/20");
when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24");
when(subnet2.getCidrBlock()).thenReturn("10.0.1.0/24");
when(subnet3.getCidrBlock()).thenReturn("10.0.2.0/24");
when(subnet4.getCidrBlock()).thenReturn("10.0.3.0/24");
String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
Assert.assertEquals("10.0.4.0/24", cidr);
}
Aggregations