private static String createSecurityGroupOnDefaultVPC(AWSInstanceContext aws) { String vpcId = null; // get the subnet cidr (if any) String subnetCidr = null; // in case subnet will be obtained from the default vpc, the security group should // as well be created there Vpc defaultVPC = getDefaultVPC(aws); if (defaultVPC != null) { vpcId = defaultVPC.getVpcId(); subnetCidr = defaultVPC.getCidrBlock(); } // no subnet or no vpc is not an option... if (subnetCidr == null || vpcId == null) { throw new AmazonServiceException("default VPC not found"); } return new AWSSecurityGroupClient(aws.amazonEC2Client) .createDefaultSecurityGroupWithDefaultRules(defaultVPC); }
public String createDefaultSecurityGroupWithDefaultRules(Vpc vpc) { String groupId; try { groupId = createDefaultSecurityGroup(vpc.getVpcId()); addIngressRules(groupId, getDefaultRules(vpc.getCidrBlock())); } catch (AmazonServiceException t) { if (t.getMessage().contains( DEFAULT_SECURITY_GROUP_NAME)) { groupId = getSecurityGroup(DEFAULT_SECURITY_GROUP_NAME, vpc.getVpcId()).getGroupId(); } else { throw t; } } return groupId; }
@Override protected void consumeSuccess(DescribeVpcsRequest request, DescribeVpcsResult result) { URI adapterUri = AdapterUriUtil.buildAdapterUri(getHost(), AWSUriPaths.AWS_NETWORK_ADAPTER); for (Vpc resultVPC : result.getVpcs()) { NetworkState networkState = mapVPCToNetworkState(resultVPC, this.context.request.regionId, this.context.request.request.resourcePoolLink, this.context.request.request.endpointLink, this.context.request.endpointAuth.documentSelfLink, this.context.request.parentComputeLink, this.context.request.tenantLinks, adapterUri); if (networkState.subnetCIDR == null) { logWarning(() -> String.format("AWS did not return CIDR information for VPC %s", resultVPC.toString())); } this.context.awsVpcs.put(resultVPC.getVpcId(), resultVPC); this.context.vpcs.put(resultVPC.getVpcId(), networkState); } }
public VpcDTO(final Vpc vpc) { this.vpcId = vpc.getVpcId(); this.cidrBlock = vpc.getCidrBlock(); this.state = vpc.getState(); this.tags.addAll( vpc.getTags() .stream() .map(TagDTO::new) .collect(Collectors.toList())); this.name = vpc.getTags() .stream() .filter(t -> t.getKey().equals("Name")) .findFirst() .map(Tag::getValue) .orElse("n/a"); }
@Override @Cacheable(value = CachingConfiguration.VPC_CACHE, key = "#vpcId", condition = "#bypassCache == false") public Vpc getVpcInRegion(final String vpcId, final String region, boolean bypassCache) { Preconditions.checkArgument(StringUtils.isNotBlank(vpcId), "vpcId may not be null or blank"); Preconditions.checkArgument(StringUtils.isNotBlank(region), "region may not be null or blank"); LOG.info("Retrieving VPC {} in region {} ({})", vpcId, region, bypassCache); DescribeVpcsRequest request = new DescribeVpcsRequest() .withVpcIds(vpcId); DescribeVpcsResult result = getClientForRegion(region).describeVpcs(request); List<Vpc> results = result.getVpcs(); if (results.size() != 1) { throw new IllegalArgumentException("Did not get expected result"); } return results.get(0); }
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); }
private String calculateSubnet(String stackName, Vpc vpc, List<String> subnetCidrs) { SubnetInfo vpcInfo = new SubnetUtils(vpc.getCidrBlock()).getInfo(); String[] cidrParts = vpcInfo.getCidrSignature().split("/"); int netmask = Integer.parseInt(cidrParts[cidrParts.length - 1]); int netmaskBits = CIDR_PREFIX - netmask; if (netmaskBits <= 0) { throw new CloudConnectorException("The selected VPC has to be in a bigger CIDR range than /24"); } int numberOfSubnets = Double.valueOf(Math.pow(2, netmaskBits)).intValue(); int targetSubnet = 0; if (stackName != null) { byte[] b = stackName.getBytes(Charset.forName("UTF-8")); for (byte ascii : b) { targetSubnet += ascii; } } targetSubnet = Long.valueOf(targetSubnet % numberOfSubnets).intValue(); String cidr = getSubnetCidrInRange(vpc, subnetCidrs, targetSubnet, numberOfSubnets); if (cidr == null) { cidr = getSubnetCidrInRange(vpc, subnetCidrs, 0, targetSubnet); } if (cidr == null) { throw new CloudConnectorException("Cannot find non-overlapping CIDR range"); } return cidr; }
public Vpc getCopyOfVpc(ProjectAndEnv projectAndEnv) { if (idCache.containsKey(projectAndEnv)) { String vpcId = idCache.get(projectAndEnv); logger.info(String.format("Cache hit for %s, found VPC ID %s", projectAndEnv, vpcId)); return getVpcById(vpcId); } else { logger.info(String.format("Checking for TAGs %s:%s and %s:%s to find VPC", AwsFacade.PROJECT_TAG, projectAndEnv.getProject(), AwsFacade.ENVIRONMENT_TAG, projectAndEnv.getEnv())); Vpc result = findVpcUsingProjectAndEnv(projectAndEnv); if (result==null) { logger.error("Could not find VPC for " + projectAndEnv); } else { idCache.put(projectAndEnv, result.getVpcId()); } return result; } }
private Vpc findVpcUsingProjectAndEnv(ProjectAndEnv key) { List<Vpc> vpcs = cloudClient.describeVpcs(); for(Vpc vpc : vpcs) { String vpcId = vpc.getVpcId(); String possibleProject = getTagByName(vpc, AwsFacade.PROJECT_TAG); if (key.getProject().equals(possibleProject)) { logger.debug(String.format("Found Possible VPC with %s:%s ID is %s", AwsFacade.PROJECT_TAG, possibleProject, vpcId)); String possibleEnv = getTagByName(vpc, AwsFacade.ENVIRONMENT_TAG); logger.debug(String.format("Found Possible VPC with %s:%s ID is %s", AwsFacade.ENVIRONMENT_TAG, possibleEnv, vpcId)); if (key.getEnv().equals(possibleEnv)) { logger.info("Matched tags, vpc id is " + vpcId); return vpc; } } } return null; }
public void initAllTags(String vpcId, ProjectAndEnv projectAndEnv) throws CannotFindVpcException { Vpc vpc = getVpcById(vpcId); if (vpc==null) { throw new CannotFindVpcException(vpcId); } logger.info("Initialise tags for VPC " + vpcId); List<Tag> tags = new LinkedList<Tag>(); Tag indexTag = new Tag(AwsFacade.INDEX_TAG, "0"); Tag projectTag = new Tag(AwsFacade.PROJECT_TAG, projectAndEnv.getProject()); Tag envTag = new Tag(AwsFacade.ENVIRONMENT_TAG, projectAndEnv.getEnv()); tags.add(indexTag); tags.add(projectTag); tags.add(envTag); setTags(vpcId, tags); }
private StackNameAndId applyTemplate(File file, ProjectAndEnv projAndEnv, Collection<Parameter> userParameters, Tagging tagging) throws CfnAssistException, IOException, InterruptedException { logger.info(format("Applying template %s for %s", file.getAbsoluteFile(), projAndEnv)); Vpc vpcForEnv = findVpcForEnv(projAndEnv); List<TemplateParameter> declaredParameters = validateTemplate(file); List<PopulatesParameters> populators = new LinkedList<>(); populators.add(new CfnBuiltInParams(vpcForEnv.getVpcId())); populators.add(new AutoDiscoverParams(file, vpcRepository, cfnRepository)); populators.add(new EnvVarParams()); ParameterFactory parameterFactory = new ParameterFactory(populators); if (projAndEnv.hasComment()) { tagging.setCommentTag(projAndEnv.getComment()); } String contents = loadFileContents(file); if (isUpdate(file)) { logger.info("Request to update a stack, filename is " + file.getAbsolutePath()); return updateStack(projAndEnv, userParameters, declaredParameters, contents, parameterFactory); } else { return createStack(file, projAndEnv, userParameters, declaredParameters, contents, parameterFactory,tagging); } }
private void addTitle(Vpc vpc, Diagram diagram) { String title = vpc.getVpcId(); List<Tag> tags = vpc.getTags(); String name = AmazonVPCFacade.getNameFromTags(tags); if (!name.isEmpty()) { title = title + String.format(" (%s)", name); } String project = AmazonVPCFacade.getValueFromTag(tags, AwsFacade.PROJECT_TAG); if (!project.isEmpty()) { title = title + String.format(" PROJECT=%s", project); } String env = AmazonVPCFacade.getValueFromTag(tags, AwsFacade.ENVIRONMENT_TAG); if (!env.isEmpty()) { title = title + String.format(" ENV=%s", env); } diagram.addTitle(title); }
protected StackNameAndId setUpdateExpectations(String stackName, String filename, List<TemplateParameter> templateParameters, Collection<Parameter> parameters) throws CfnAssistException, InterruptedException, IOException { String stackId = "stackId"; Stack stack = new Stack().withStackId(stackId); StackNameAndId stackNameAndId = new StackNameAndId(stackName, stackId); String contents = EnvironmentSetupForTests.loadFile(filename); EasyMock.expect(vpcRepository.getCopyOfVpc(projectAndEnv)).andReturn(new Vpc().withVpcId(VPC_ID)); EasyMock.expect(cfnRepository.validateStackTemplate(contents)).andReturn(templateParameters); EasyMock.expect(cfnRepository.updateStack(contents, parameters, monitor, stackName)).andReturn(stackNameAndId); EasyMock.expect(monitor.waitForUpdateFinished(stackNameAndId)).andReturn(StackStatus.UPDATE_COMPLETE.toString()); EasyMock.expect(cfnRepository.updateSuccess(stackNameAndId)).andReturn(stack); EasyMock.expect(cloudRepository.getZones()).andReturn(zones); return stackNameAndId; }
@Test public void testUploadArtifactsToS3AndAutopopulateAsParameters() { Main main = new Main(CLIArgBuilder.createSubnetStackWithArtifactUpload(BUILD_NUMBER, testName)); int result = main.parse(); deletesStacks.ifPresent("CfnAssist9987TestsubnetWithS3Param"); assertEquals("deploy failed", 0, result); Vpc vpcId = vpcRepository.getCopyOfVpc(projectAndEnv); List<Subnet> subnets = EnvironmentSetupForTests.getSubnetFors(ec2Client, vpcId); assertEquals(1, subnets.size()); List<Tag> tags = subnets.get(0).getTags(); List<Tag> expectedTags = new LinkedList<>(); expectedTags.add(new Tag().withKey("urlATag").withValue(EnvironmentSetupForTests.S3_PREFIX+"/"+KEY_A)); expectedTags.add(new Tag().withKey("urlBTag").withValue(EnvironmentSetupForTests.S3_PREFIX+"/"+KEY_B)); assertTrue(tags.containsAll(expectedTags)); }
@Test public void shouldThrowOnCreateWhenStackExistsAndNotRolledBack() throws IOException, CfnAssistException, InterruptedException { String stackName = "CfnAssistTestsimpleStack"; String filename = FilesForTesting.SIMPLE_STACK; String contents = EnvironmentSetupForTests.loadFile(filename); String stackId = "stackId"; StackNameAndId stackNameAndId = new StackNameAndId(stackName, stackId); List<TemplateParameter> templateParameters = new LinkedList<>(); EasyMock.expect(vpcRepository.getCopyOfVpc(projectAndEnv)).andReturn(new Vpc().withVpcId(VPC_ID)); EasyMock.expect(cfnRepository.validateStackTemplate(contents)).andReturn(templateParameters); // stack in rolled back status so delete it EasyMock.expect(cfnRepository.getStackStatus(stackName)).andReturn(CREATE_COMP_STATUS); EasyMock.expect(cfnRepository.getStackNameAndId(stackName)).andReturn(stackNameAndId); replayAll(); try { aws.applyTemplate(filename, projectAndEnv); } catch(DuplicateStackException expected) { // expected } verifyAll(); }
private StackNameAndId SetCreateExpectations(String stackName, String contents, List<TemplateParameter> templateParameters, Collection<Parameter> creationParameters, String comment, Collection<Output> outputs, Map<String, AvailabilityZone> zones) throws CfnAssistException, InterruptedException { StackNameAndId stackNameAndId = new StackNameAndId(stackName, "stackId"); Stack stack = new Stack().withStackId("stackId"); if (outputs.size()>0) { stack.setOutputs(outputs); } EasyMock.expect(vpcRepository.getCopyOfVpc(projectAndEnv)).andReturn(new Vpc().withVpcId(VPC_ID)); EasyMock.expect(cfnRepository.validateStackTemplate(contents)).andReturn(templateParameters); EasyMock.expect(cfnRepository.getStackStatus(stackName)).andReturn(""); Tagging tagging = new Tagging(); tagging.setCommentTag(comment); EasyMock.expect(cfnRepository.createStack(projectAndEnv, contents, stackName, creationParameters, monitor, tagging)). andReturn(stackNameAndId); EasyMock.expect(cloudRepository.getZones()).andReturn(zones); EasyMock.expect(monitor.waitForCreateFinished(stackNameAndId)).andReturn(CREATE_COMP_STATUS); EasyMock.expect(identityProvider.getUserId()).andReturn(user); CFNAssistNotification notification = new CFNAssistNotification(stackName, CREATE_COMP_STATUS, user); EasyMock.expect(notificationSender.sendNotification(notification)).andReturn("sendMessageID"); EasyMock.expect(cfnRepository.createSuccess(stackNameAndId)).andReturn(stack); return stackNameAndId; }
@Test public void shouldGetVpcByProjectAndEnvironmentTags() { List<Vpc> vpcs = new LinkedList<>(); List<Tag> tags = EnvironmentSetupForTests.createExpectedEc2Tags(projectAndEnv,""); vpcs.add(new Vpc().withVpcId("firstWrongId")); vpcs.add(new Vpc().withVpcId("correctId").withTags(tags)); List<Tag> wrongTags = EnvironmentSetupForTests.createExpectedEc2Tags(EnvironmentSetupForTests.getAltProjectAndEnv(), ""); vpcs.add(new Vpc().withVpcId("wrongId").withTags(wrongTags)); EasyMock.expect(cloudClient.describeVpcs()).andReturn(vpcs); replayAll(); Vpc result = repository.getCopyOfVpc(projectAndEnv); assertEquals("correctId", result.getVpcId()); verifyAll(); }
@Test public void shouldMatchVpcAndFindDetlaIndexTag() throws CannotFindVpcException { List<Vpc> vpcs = new LinkedList<>(); List<Tag> matchingTags = EnvironmentSetupForTests.createExpectedEc2Tags(projectAndEnv,""); matchingTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_DELTA","004422")); List<Tag> wrongTags = EnvironmentSetupForTests.createExpectedEc2Tags(EnvironmentSetupForTests.getAltProjectAndEnv(), ""); wrongTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_DELTA","005555")); vpcs.add(new Vpc().withVpcId("correctId").withTags(matchingTags)); vpcs.add(new Vpc().withVpcId("firstWrongId").withTags(wrongTags)); EasyMock.expect(cloudClient.describeVpcs()).andReturn(vpcs); replayAll(); String result = repository.getVpcIndexTag(projectAndEnv); assertEquals("004422", result); verifyAll(); }
@Test public void shouldTestSomething() throws CannotFindVpcException { List<Vpc> vpcs = new LinkedList<>(); List<Tag> matchingTags = EnvironmentSetupForTests.createExpectedEc2Tags(projectAndEnv,""); matchingTags.add(EnvironmentSetupForTests.createEc2Tag("tagName","correctValue")); List<Tag> wrongTags = EnvironmentSetupForTests.createExpectedEc2Tags(EnvironmentSetupForTests.getAltProjectAndEnv(), ""); wrongTags.add(EnvironmentSetupForTests.createEc2Tag("tagName","wrongValue")); vpcs.add(new Vpc().withVpcId("correctId").withTags(matchingTags)); vpcs.add(new Vpc().withVpcId("firstWrongId").withTags(wrongTags)); EasyMock.expect(cloudClient.describeVpcs()).andReturn(vpcs); replayAll(); String result = repository.getVpcTag("tagName", projectAndEnv); assertEquals(result, "correctValue"); verifyAll(); }
@Test public void shouldTestSomegthing() throws CannotFindVpcException { List<Tag> expectedTags = new LinkedList<>(); expectedTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_DELTA", "0")); expectedTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_PROJECT", "CfnAssist")); expectedTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_ENV", "Test")); EasyMock.expect(cloudClient.describeVpc("vpcID11")).andReturn(new Vpc().withCidrBlock("cidrBlock")); List<String> resources = new LinkedList<>(); resources.add("vpcID11"); cloudClient.addTagsToResources(resources, expectedTags); EasyMock.expectLastCall(); replayAll(); repository.initAllTags("vpcID11", projectAndEnv); verifyAll(); }
@Test public void shouldTestSetCfnDeltaIndex() throws CannotFindVpcException { List<Vpc> vpcs = new LinkedList<>(); List<Tag> initialTags = EnvironmentSetupForTests.createExpectedEc2Tags(projectAndEnv,""); initialTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_DELTA","initialValue")); vpcs.add(new Vpc().withVpcId("correctId").withTags(initialTags)); EasyMock.expect(cloudClient.describeVpcs()).andReturn(vpcs); List<String> resources = new LinkedList<>(); resources.add("correctId"); List<Tag> tags = new LinkedList<>(); tags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_DELTA","newIndexValue")); cloudClient.addTagsToResources(resources, tags); EasyMock.expectLastCall(); replayAll(); repository.setVpcIndexTag(projectAndEnv, "newIndexValue"); verifyAll(); }
@Test public void shouldTestSomethign() { List<Vpc> vpcs = new LinkedList<>(); List<Tag> initialTags = EnvironmentSetupForTests.createExpectedEc2Tags(projectAndEnv,""); initialTags.add(EnvironmentSetupForTests.createEc2Tag("CFN_ASSIST_DELTA","initialValue")); vpcs.add(new Vpc().withVpcId("correctId").withTags(initialTags)); EasyMock.expect(cloudClient.describeVpcs()).andReturn(vpcs); List<Tag> tags = new LinkedList<>(); tags.add(EnvironmentSetupForTests.createEc2Tag("someKey","someValue")); List<String> resources = new LinkedList<>(); resources.add("correctId"); cloudClient.addTagsToResources(resources, tags); EasyMock.expectLastCall(); replayAll(); repository.setVpcTag(projectAndEnv, "someKey", "someValue"); verifyAll(); }
@Test public void ShouldUseTagIfMoreThanOneELB() throws TooManyELBException { String typeTag = "expectedType"; List<Tag> lb1Tags = new LinkedList<>(); lb1Tags.add(new Tag().withKey(AwsFacade.TYPE_TAG).withValue("someNonMatchingTag")); List<Tag> lb2Tags = new LinkedList<>(); lb2Tags.add(new Tag().withKey(AwsFacade.TYPE_TAG).withValue(typeTag)); List<LoadBalancerDescription> lbs = new LinkedList<>(); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb1Name").withVPCId("vpcId")); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb2Name").withVPCId("vpcId")); Vpc vpc = new Vpc().withVpcId("vpcId"); EasyMock.expect(vpcRepository.getCopyOfVpc(projAndEnv)).andReturn(vpc); EasyMock.expect(elbClient.describeLoadBalancers()).andReturn(lbs); EasyMock.expect(elbClient.getTagsFor("lb1Name")).andReturn(lb1Tags); EasyMock.expect(elbClient.getTagsFor("lb2Name")).andReturn(lb2Tags); replayAll(); LoadBalancerDescription result = elbRepository.findELBFor(projAndEnv, typeTag); verifyAll(); assertEquals("lb2Name", result.getLoadBalancerName()); }
@Test public void ShouldThrowIfMoreThanOneELBAndNoMatchingTags() { List<Tag> tags = new LinkedList<>(); tags.add(new Tag().withKey("someOtherTag").withValue("someOtherValue")); List<LoadBalancerDescription> lbs = new LinkedList<>(); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb1Name").withVPCId("vpcId")); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb2Name").withVPCId("vpcId")); Vpc vpc = new Vpc().withVpcId("vpcId"); EasyMock.expect(vpcRepository.getCopyOfVpc(projAndEnv)).andReturn(vpc); EasyMock.expect(elbClient.describeLoadBalancers()).andReturn(lbs); EasyMock.expect(elbClient.getTagsFor("lb1Name")).andReturn(new LinkedList<>()); EasyMock.expect(elbClient.getTagsFor("lb2Name")).andReturn(tags); replayAll(); try { elbRepository.findELBFor(projAndEnv,"notMatchingAnLB"); fail("should have thrown"); } catch(TooManyELBException expectedException) { // no op } verifyAll(); }
@Test public void shouldGetInstancesForTheLB() throws TooManyELBException { String vpcId = "myVPC"; Instance insA = new Instance().withInstanceId("instanceA"); // associated List<LoadBalancerDescription> theLB = new LinkedList<>(); theLB.add(new LoadBalancerDescription().withVPCId(vpcId). withInstances(insA). withLoadBalancerName("lbName").withDNSName("dnsName")); EasyMock.expect(vpcRepository.getCopyOfVpc(projAndEnv)).andStubReturn(new Vpc().withVpcId(vpcId)); EasyMock.expect(elbClient.describeLoadBalancers()).andReturn(theLB); replayAll(); List<Instance> result = elbRepository.findInstancesAssociatedWithLB(projAndEnv,"typeNotUsedWhenOneMatchingLB"); verifyAll(); assertEquals(1, result.size()); assertEquals("instanceA", result.get(0).getInstanceId()); }
/** * Test describing vpcs. */ @Test(timeout = TIMEOUT_LEVEL1) public final void describeAllNetworksTest() { log.info("Start describing vpcs test"); List<Vpc> vpcs = describeVpcs(); Assert.assertNotNull("vpcs should not be null", vpcs); Assert.assertNotNull("vpc id should not be null", vpcs.get(0).getVpcId()); log.info("Vpc Sizes " + vpcs.size()); log.info("Start describing vpcs test"); List<Subnet> subnets = getSubnets(); Assert.assertNotNull("vpcs should not be null", subnets); Assert.assertNotNull("vpc id should not be null", subnets.get(0).getSubnetId()); log.info("Subnets Sizes " + subnets.size()); log.info("Start describing vpcs test"); List<InternetGateway> internetGateways = getInternetGateways(); Assert.assertNotNull("vpcs should not be null", internetGateways); Assert.assertNotNull("vpc id should not be null", internetGateways.get(0).getInternetGatewayId()); log.info("Subnets Sizes " + internetGateways.size()); }
/** * Gets the default VPC */ public static Vpc getDefaultVPC(AWSInstanceContext aws) { DescribeVpcsResult result = aws.amazonEC2Client.describeVpcs(); List<Vpc> vpcs = result.getVpcs(); for (Vpc vpc : vpcs) { if (vpc.isDefault()) { return vpc; } } return null; }
public Vpc getVPC(String vpcId) { DescribeVpcsRequest req = new DescribeVpcsRequest().withVpcIds(vpcId); DescribeVpcsResult result = this.client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); if (vpcs != null && vpcs.size() == 1) { return vpcs.get(0); } return null; }
/** * Get the default VPC - return null if no default specified */ public Vpc getDefaultVPC() { DescribeVpcsRequest req = new DescribeVpcsRequest(); DescribeVpcsResult result = this.client.describeVpcs(req); List<Vpc> vpcs = result.getVpcs(); for (Vpc vpc : vpcs) { if (vpc.isDefault()) { return vpc; } } return null; }
/** * Allocate an elastic IP address */ public DeferredResult<String> allocateElasticIPAddress() { AllocateAddressRequest req = new AllocateAddressRequest() .withDomain(DomainType.Vpc); String message = "Allocate AWS Elastic IP Address for use with instances in a VPC."; AWSDeferredResultAsyncHandler<AllocateAddressRequest, AllocateAddressResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message); this.client.allocateAddressAsync(req, handler); return handler.toDeferredResult() .thenApply(AllocateAddressResult::getAllocationId); }
public static NetworkState mapVPCToNetworkState(Vpc vpc, String regionId, String resourcePoolLink, String endpointLink, String authCredentialsLink, String parentComputeLink, List<String> tenantLinks, URI adapterUri) { if (vpc == null) { throw new IllegalArgumentException("Cannot map VPC to network state for null instance"); } NetworkState networkState = new NetworkState(); networkState.id = vpc.getVpcId(); // calculate vpc name if (vpc.getTags() == null) { networkState.name = vpc.getVpcId(); } else { networkState.name = vpc.getTags().stream() .filter(tag -> tag.getKey().equals(AWS_TAG_NAME)) .map(tag -> tag.getValue()).findFirst() .orElse(vpc.getVpcId()); } networkState.subnetCIDR = vpc.getCidrBlock(); networkState.regionId = regionId; networkState.resourcePoolLink = resourcePoolLink; networkState.endpointLink = endpointLink; if (networkState.endpointLinks == null) { networkState.endpointLinks = new HashSet<>(); } networkState.endpointLinks.add(endpointLink); networkState.authCredentialsLink = authCredentialsLink; networkState.instanceAdapterReference = adapterUri; networkState.tenantLinks = tenantLinks; networkState.computeHostLink = parentComputeLink; networkState.customProperties = new HashMap<>(); networkState.customProperties.put("defaultInstance", String.valueOf(vpc.isDefault())); return networkState; }
/** * Return true if vpcId exists. */ public static boolean vpcIdExists(AmazonEC2AsyncClient client, String vpcId) { List<Vpc> vpcs = client.describeVpcs() .getVpcs() .stream() .filter(vpc -> vpc.getVpcId().equals(vpcId)) .collect(Collectors.toList()); return vpcs != null && !vpcs.isEmpty(); }
@Test public void testGetMainRouteTable() throws Throwable { Vpc defVPC = this.netClient.getDefaultVPC(); assertTrue(defVPC != null); RouteTable routeTable = this.netClient.getMainRouteTable(defVPC.getVpcId()); assertTrue(routeTable != null); }
@RequestMapping(value = "/vpcs/{vpcId}/detail") public VpcDTO getVpcDetail(@RequestParam("region") final String region, @PathVariable("vpcId") final String vpcId, @RequestParam(value = "bypassCache", required = false) boolean bypassCache) { Vpc vpc = vpcService.getVpcInRegion(vpcId, region, bypassCache); return new VpcDetailDTO(vpc, vpcService.getSubnetsForVpcInRegion(vpcId, region, bypassCache), vpcService.getRouteTablesForVpcInRegion(vpcId, region, bypassCache)); }
public VpcDetailDTO(final Vpc vpc, final List<Subnet> subnets, final List<RouteTable> routeTables) { super(vpc); final Map<String, SubnetDetailDTO> subnetDetails = new HashMap<>(); subnetDetails.putAll( subnets.stream() .map(SubnetDetailDTO::new) .collect(Collectors.toMap(s -> s.getSubnetId(), identity()))); LOG.trace("Details map: {}", subnetDetails); routeTables.stream() .map(RouteTableDTO::new) .forEach(rt -> rt.getAssociations().forEach(assoc -> { SubnetDetailDTO dto = subnetDetails.get(assoc.getSubnetId()); if (dto == null) { if (LOG.isTraceEnabled()) { LOG.trace("RT: {}, Assoc.SubnetID: {}, Assocs: {}", rt.getRouteTableId(), assoc.getSubnetId(), rt.getAssociations()); } return; } dto.setRouteTableId(rt.getRouteTableId()); dto.getRoutes().addAll(rt.getRoutes()); })); this.subnets.addAll(subnetDetails.values()); }
@Override @Cacheable(value = CachingConfiguration.VPC_LISTS_CACHE, key = "#region", condition = "#bypassCache == false") public List<Vpc> getVpcsInRegion(final String region, boolean bypassCache) { Preconditions.checkArgument(StringUtils.isNotBlank(region), "region may not be null or blank"); LOG.info("Retrieving all VPCs in region {} ({})", region, bypassCache); DescribeVpcsResult result = getClientForRegion(region).describeVpcs(); return result.getVpcs(); }
public Vpc createVPC() throws InterruptedException { logger.info("create VPC"); String vpcId = ec2.createVpc(new CreateVpcRequest().withCidrBlock("10.0.0.0/16")).getVpc().getVpcId(); while (true) { Threads.sleepRoughly(Duration.ofSeconds(20)); DescribeVpcsResult result = ec2.describeVpcs(new DescribeVpcsRequest().withVpcIds(vpcId)); Vpc remoteVPC = result.getVpcs().get(0); if ("available".equals(remoteVPC.getState())) { enableVPCDNS(vpcId); return remoteVPC; } } }
private String getSubnetCidrInRange(Vpc vpc, List<String> subnetCidrs, int start, int end) { SubnetInfo vpcInfo = new SubnetUtils(vpc.getCidrBlock()).getInfo(); String lowProbe = incrementIp(vpcInfo.getLowAddress()); String highProbe = new SubnetUtils(toSubnetCidr(lowProbe)).getInfo().getHighAddress(); // start from the target subnet for (int i = 0; i < start - 1; i++) { lowProbe = incrementIp(lowProbe); highProbe = incrementIp(highProbe); } boolean foundProbe = false; for (int i = start; i < end; i++) { boolean overlapping = false; for (String subnetCidr : subnetCidrs) { SubnetInfo subnetInfo = new SubnetUtils(subnetCidr).getInfo(); if (isInRange(lowProbe, subnetInfo) || isInRange(highProbe, subnetInfo)) { overlapping = true; break; } } if (overlapping) { lowProbe = incrementIp(lowProbe); highProbe = incrementIp(highProbe); } else { foundProbe = true; break; } } if (foundProbe && isInRange(highProbe, vpcInfo)) { String subnet = toSubnetCidr(lowProbe); LOGGER.info("The following subnet cidr found: {} for VPC: {}", subnet, vpc.getVpcId()); return subnet; } else { return null; } }
@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); }
@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); }