public CloudFrontLogEventHandler(AmazonCloudFormationClient cloudFormationClient, AmazonS3Client amazonS3Client, AWSWAF awsWaf) { cloudFormationClient.setRegion(Region.getRegion(Regions.US_WEST_2)); this.amazonS3Client = amazonS3Client; objectMapper = new ObjectMapper(); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); // New processors would need there own ip sets as there is a hard limit of 1000 ips and the // RateLimitingProcessor truncates the set and removes any ips from the set that it doesn't know about // see CloudFormationDefinedParams logEventProcessors.add(new RateLimitingProcessor(objectMapper, awsWaf, amazonS3Client)); logEventProcessors.add(new GoogleAnalyticsKPIProcessor()); logEventProcessors.add(new TlsVerificationProcessor()); }
@Inject public CreateCloudFrontSecurityGroupUpdaterLambdaOperation(final CloudFormationService cloudFormationService, final EnvironmentMetadata environmentMetadata, @Named(CF_OBJECT_MAPPER) final ObjectMapper cloudformationObjectMapper, AWSLambda awsLambda, AmazonS3 amazonS3) { this.cloudFormationService = cloudFormationService; this.cloudformationObjectMapper = cloudformationObjectMapper; this.environmentMetadata = environmentMetadata; this.awsLambda = awsLambda; this.amazonS3 = amazonS3; final Region region = Region.getRegion(Regions.US_EAST_1); AmazonCloudFormation amazonCloudFormation = new AmazonCloudFormationClient(); amazonCloudFormation.setRegion(region); amazonSNS = new AmazonSNSClient(); amazonSNS.setRegion(region); }
public CfSignalResourceBundle() { cloudFormationSupplier = (cfSignalResourceConfig) -> { AmazonCloudFormation amazonCloudFormation = internalCloudFormation.get(); if (amazonCloudFormation != null) { return amazonCloudFormation; } return internalCloudFormation.updateAndGet((unused) -> { AmazonCloudFormationClient amazonCloudFormationClient = new AmazonCloudFormationClient(); String awsRegion = cfSignalResourceConfig.getAwsRegion(); Region region; if (Strings.isNullOrEmpty(awsRegion)) { region = Regions.getCurrentRegion(); } else { region = Region.getRegion(Regions.fromName(awsRegion)); } amazonCloudFormationClient.setRegion(region); return amazonCloudFormationClient; }); }; }
@Test public void parseInternal_stackConfigurationWithExternallyConfiguredCloudFormationClient_returnsConfiguredStackWithExternallyConfiguredClient() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-withCustomCloudFormationClient.xml", getClass())); AmazonCloudFormation amazonCloudFormationMock = beanFactory.getBean(AmazonCloudFormation.class); when(amazonCloudFormationMock.listStackResources(new ListStackResourcesRequest().withStackName("test"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); when(amazonCloudFormationMock.describeStacks(new DescribeStacksRequest().withStackName("test"))). thenReturn(new DescribeStacksResult().withStacks(new Stack())); //Act StackResourceRegistry stackResourceRegistry = beanFactory.getBean(StackResourceRegistry.class); //Assert assertNotNull(stackResourceRegistry); assertFalse(beanFactory.containsBeanDefinition(getBeanName(AmazonCloudFormationClient.class.getName()))); verify(amazonCloudFormationMock, times(1)).listStackResources(new ListStackResourcesRequest().withStackName("test")); beanFactory.getBean("customStackTags"); verify(amazonCloudFormationMock, times(1)).describeStacks(new DescribeStacksRequest().withStackName("test")); }
private List<CloudResource> getCloudResources(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormationClient client, AmazonEC2Client amazonEC2Client, AmazonAutoScalingClient amazonASClient, boolean mapPublicIpOnLaunch) { List<CloudResource> cloudResources = new ArrayList<>(); AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); scheduleStatusChecks(stack, ac, cloudFormationClient); suspendAutoScaling(ac, stack); if (mapPublicIpOnLaunch) { Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, client); List<Group> gateways = getGatewayGroups(stack.getGroups()); for (Group gateway : gateways) { List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway); List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, client, gateway); associateElasticIpsToInstances(amazonEC2Client, eips, instanceIds); } } return cloudResources; }
private void scheduleStatusChecks(CloudStack stack, AuthenticatedContext ac, AmazonCloudFormationClient cloudFormationClient) { for (Group group : stack.getGroups()) { String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName()); LOGGER.info("Polling Auto Scaling group until new instances are ready. [stack: {}, asGroup: {}]", ac.getCloudContext().getId(), asGroupName); PollTask<Boolean> task = awsPollTaskFactory.newASGroupStatusCheckerTask(ac, asGroupName, group.getInstancesSize(), awsClient, cfStackUtil); try { Boolean statePollerResult = task.call(); if (!task.completed(statePollerResult)) { syncPollingScheduler.schedule(task); } } catch (Exception e) { throw new CloudConnectorException(e.getMessage(), e); } } }
@Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) { List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>(); try { String region = ac.getCloudContext().getLocation().getRegion().value(); AmazonCloudFormationClient amazonCFClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), region); AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), region); AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region); //contains all instances ListMultimap<String, CloudInstance> groupByInstanceGroup = groupByInstanceGroup(vms); for (String key : groupByInstanceGroup.keySet()) { List<CloudInstance> cloudInstances = groupByInstanceGroup.get(key); cloudVmMetaDataStatuses.addAll(collectGroupMetaData(ac, amazonASClient, amazonEC2Client, amazonCFClient, key, cloudInstances)); } return cloudVmMetaDataStatuses; } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } }
private List<Output> getOutputForRequest(String vpcStackName, AmazonCloudFormationClient client) { int tried = 0; while (tried < MAX_TRY) { LOGGER.info("checking vpc stack creation result, tried: " + tried + '/' + MAX_TRY); DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest(); describeStacksRequest.withStackName(vpcStackName); Stack resultStack = client.describeStacks(describeStacksRequest).getStacks().get(0); StackStatus stackStatus = StackStatus.valueOf(resultStack.getStackStatus()); if (FAILED_STATUSES.contains(stackStatus)) { LOGGER.error("stack creation failed: ", stackStatus); throw new RuntimeException(); } else if (CREATE_COMPLETE.equals(stackStatus)) { return resultStack.getOutputs(); } try { Thread.sleep(10000); } catch (InterruptedException e) { LOGGER.error("thread sleep interrupted", e); } tried++; } throw new RuntimeException("vpc creation timed out"); }
/** * Binds all the Amazon services used. */ @Override protected void configure() { final Region region = Region.getRegion(Regions.fromName(regionName)); bind(AmazonEC2.class).toInstance(createAmazonClientInstance(AmazonEC2Client.class, region)); bind(AmazonCloudFormation.class).toInstance(createAmazonClientInstance(AmazonCloudFormationClient.class, region)); bind(AmazonIdentityManagement.class).toInstance(createAmazonClientInstance(AmazonIdentityManagementClient.class, region)); bind(AWSKMS.class).toInstance(createAmazonClientInstance(AWSKMSClient.class, region)); bind(AmazonS3.class).toInstance(createAmazonClientInstance(AmazonS3Client.class, region)); bind(AmazonAutoScaling.class).toInstance(createAmazonClientInstance(AmazonAutoScalingClient.class, region)); bind(AWSSecurityTokenService.class).toInstance(createAmazonClientInstance(AWSSecurityTokenServiceClient.class, region)); bind(AWSLambda.class).toInstance(createAmazonClientInstance(AWSLambdaClient.class, region)); bind(AmazonSNS.class).toInstance(createAmazonClientInstance(AmazonSNSClient.class, region)); }
protected AmazonCloudFormationClient getCloudFormationClient() { if (null != cloudformationClient) { return cloudformationClient; } return cloudformationClient = new AmazonCloudFormationClient(getAWSCredentialsProvideChain()).withRegion(Region.getRegion(Regions.fromName(deployment.getRegion()))); }
@Test public void parseInternal_withCustomRegion_shouldConfigureDefaultClientWithCustomRegion() throws Exception { //Arrange DefaultListableBeanFactory registry = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-region.xml", getClass())); // Assert AmazonCloudFormationClient amazonCloudFormation = registry.getBean(AmazonCloudFormationClient.class); assertEquals("https://" + Region.getRegion(Regions.SA_EAST_1).getServiceEndpoint("cloudformation"), ReflectionTestUtils.getField(amazonCloudFormation, "endpoint").toString()); }
@Test public void parseInternal_withCustomRegionProvider_shouldConfigureDefaultClientWithCustomRegionReturnedByProvider() throws Exception { //Arrange DefaultListableBeanFactory registry = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); //Act reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-custom-region-provider.xml", getClass())); // Assert AmazonCloudFormationClient amazonCloudFormation = registry.getBean(AmazonCloudFormationClient.class); assertEquals("https://" + Region.getRegion(Regions.AP_SOUTHEAST_2).getServiceEndpoint("cloudformation"), ReflectionTestUtils.getField(amazonCloudFormation, "endpoint").toString()); }
public void execute() { checkParams(); AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class); UpdateStackRequest request = new UpdateStackRequest() .withStackName(stackName).withStackPolicyBody(stackPolicyBody) .withStackPolicyURL(stackPolicyURL) .withTemplateBody(templateBody).withTemplateURL(templateURL) .withStackPolicyDuringUpdateBody(stackPolicyDuringUpdateBody) .withStackPolicyDuringUpdateURL(stackPolicyDuringUpdateURL) .withUsePreviousTemplate(usePreviousTemplate); if (capabilities.size() > 0) { request.setCapabilities(capabilities); } if (parameters.size() > 0) { request.setParameters(parameters); } if (notificationArns.size() > 0) { request.setNotificationARNs(notificationArns); } try { client.updateStack(request); System.out.println("Update stack " + stackName + " request submitted."); } catch (Exception e) { throw new BuildException("Could not update stack: " + e.getMessage(), e); } }
public void execute() { checkParams(); AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class); CreateStackRequest createStackRequest = new CreateStackRequest() .withDisableRollback(disableRollback).withOnFailure(onFailure) .withStackName(stackName).withStackPolicyBody(stackPolicyBody) .withStackPolicyURL(stackPolicyURL) .withTemplateBody(templateBody).withTemplateURL(templateURL) .withTimeoutInMinutes(timeoutInMinutes); if (capabilities.size() > 0) { createStackRequest.setCapabilities(capabilities); } if (parameters.size() > 0) { createStackRequest.setParameters(parameters); } if (tags.size() > 0) { createStackRequest.setTags(tags); } try { client.createStack(createStackRequest); System.out.println("Create stack " + stackName + " request submitted."); if(waitForCreation) { WaitForStackToReachStateTask.waitForCloudFormationStackToReachStatus(client, stackName, CREATE_COMPLETE); } } catch (Exception e) { throw new BuildException( "Could not create stack " + e.getMessage(), e); } }
public void execute() { checkParams(); AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class); SetStackPolicyRequest setStackPolicyRequest = new SetStackPolicyRequest() .withStackName(stackName).withStackPolicyBody(stackPolicyBody) .withStackPolicyURL(stackPolicyURL); try { client.setStackPolicy(setStackPolicyRequest); System.out.println("Successfully set stack policy"); } catch (Exception e) { throw new BuildException("Could not set the stack policy " + e.getMessage(), e); } }
public void execute() { checkParams(); AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class); if (!waitForCloudFormationStackToReachStatus(client, stackName, status)) { throw new BuildException("The stack update or creation failed"); } }
public static boolean waitForCloudFormationStackToReachStatus( AmazonCloudFormationClient client, String stackName, String status) { int count = 0; while (true) { if (count++ == 100) { System.out .println(stackName + " never reached state " + status); return false; } try { Thread.sleep(1000 * 30); } catch (InterruptedException e) { System.out.println(e.getMessage()); return false; } String stackStatus = client .describeStacks( new DescribeStacksRequest() .withStackName(stackName)).getStacks() .get(0).getStackStatus(); if (stackStatus.equals(status)) { return true; } else if (stackStatus.contains(FAILED)) { System.out.println("The process failed with status " + stackStatus); return false; } System.out.println(stackName + " is in status " + stackStatus); } }
@BeforeClass public static void setUp() { client = new AmazonCloudFormationClient(); project = new Project(); SetUpCloudFormationTestsTask setUpTask = new SetUpCloudFormationTestsTask(); setUpTask.setProject(project); setUpTask.execute(); }
public void execute() { checkParams(); AmazonEC2Client ec2Client = getOrCreateClient(AmazonEC2Client.class); ec2Client .deleteKeyPair(new DeleteKeyPairRequest().withKeyName(keyName)); AmazonCloudFormationClient cloudFormationClient = getOrCreateClient(AmazonCloudFormationClient.class); cloudFormationClient.deleteStack(new DeleteStackRequest() .withStackName(stackName)); }
private void saveS3AccessRoleArn(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormationClient client, PersistenceNotifier resourceNotifier) { AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack); if (awsInstanceProfileView.isEnableInstanceProfileStrategy() && !awsInstanceProfileView.isInstanceProfileAvailable()) { String s3AccessRoleArn = getCreatedS3AccessRoleArn(cFStackName, client); CloudResource s3AccessRoleArnCloudResource = new Builder().type(ResourceType.S3_ACCESS_ROLE_ARN).name(s3AccessRoleArn).build(); resourceNotifier.notifyAllocation(s3AccessRoleArnCloudResource, ac.getCloudContext()); } }
private String getCreatedVpc(String cFStackName, AmazonCloudFormationClient client) { Map<String, String> outputs = getOutputs(cFStackName, client); if (outputs.containsKey(CREATED_VPC)) { return outputs.get(CREATED_VPC); } else { String outputKeyNotFound = String.format("Vpc could not be found in the Cloudformation stack('%s') output.", cFStackName); throw new CloudConnectorException(outputKeyNotFound); } }
private String getCreatedSubnet(String cFStackName, AmazonCloudFormationClient client) { Map<String, String> outputs = getOutputs(cFStackName, client); if (outputs.containsKey(CREATED_SUBNET)) { return outputs.get(CREATED_SUBNET); } else { String outputKeyNotFound = String.format("Subnet could not be found in the Cloudformation stack('%s') output.", cFStackName); throw new CloudConnectorException(outputKeyNotFound); } }
private String getCreatedS3AccessRoleArn(String cFStackName, AmazonCloudFormationClient client) { Map<String, String> outputs = getOutputs(cFStackName, client); if (outputs.containsKey(S3_ACCESS_ROLE)) { return outputs.get(S3_ACCESS_ROLE); } else { String outputKeyNotFound = String.format("S3AccessRole arn could not be found in the Cloudformation stack('%s') output.", cFStackName); throw new CloudConnectorException(outputKeyNotFound); } }
private Map<String, String> getElasticIpAllocationIds(String cFStackName, AmazonCloudFormationClient client) { Map<String, String> outputs = getOutputs(cFStackName, client); Map<String, String> elasticIpIds = outputs.entrySet().stream().filter(e -> e.getKey().startsWith(CFS_OUTPUT_EIPALLOCATION_ID)) .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); if (!elasticIpIds.isEmpty()) { return elasticIpIds; } else { String outputKeyNotFound = String.format("Allocation Id of Elastic IP could not be found in the Cloudformation stack('%s') output.", cFStackName); throw new CloudConnectorException(outputKeyNotFound); } }
private Map<String, String> getOutputs(String cFStackName, AmazonCloudFormationClient client) { DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName); String outputNotFound = String.format("Couldn't get Cloudformation stack's('%s') output", cFStackName); List<Output> cfStackOutputs = client.describeStacks(describeStacksRequest).getStacks() .stream().findFirst().orElseThrow(getCloudConnectorExceptionSupplier(outputNotFound)).getOutputs(); return cfStackOutputs.stream().collect(Collectors.toMap(Output::getOutputKey, Output::getOutputValue)); }
public String getAutoscalingGroupName(AuthenticatedContext ac, AmazonCloudFormationClient amazonCFClient, String instanceGroup) { String cFStackName = getCfStackName(ac); DescribeStackResourceResult asGroupResource = amazonCFClient.describeStackResource(new DescribeStackResourceRequest() .withStackName(cFStackName) .withLogicalResourceId(String.format("AmbariNodes%s", instanceGroup.replaceAll("_", "")))); return asGroupResource.getStackResourceDetail().getPhysicalResourceId(); }
public AmazonCloudFormationClient createCloudFormationClient(AwsCredentialView awsCredential, String regionName) { AmazonCloudFormationClient client = isRoleAssumeRequired(awsCredential) ? new AmazonCloudFormationClient(credentialClient.retrieveCachedSessionCredentials(awsCredential)) : new AmazonCloudFormationClient(createAwsCredentials(awsCredential)); client.setRegion(RegionUtils.getRegion(regionName)); return client; }
private List<CloudVmMetaDataStatus> collectGroupMetaData(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonEC2Client amazonEC2Client, AmazonCloudFormationClient amazonCFClient, String groupName, List<CloudInstance> cloudInstances) { List<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; }
@Override public void validate(AuthenticatedContext ac, CloudStack cloudStack) { AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName); String cFStackName = cfStackUtil.getCfStackName(ac); try { cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName)); throw new CloudConnectorException(String.format("Stack is already exists with the given name: %s", cFStackName)); } catch (AmazonServiceException ignored) { } }
AbstractAwsStackStatusCheckerTask(AuthenticatedContext authenticatedContext, AmazonCloudFormationClient cfClient, StackStatus successStatus, StackStatus errorStatus, List<StackStatus> stackErrorStatuses, String cloudFormationStackName, boolean cancellable) { super(authenticatedContext, cancellable); this.cfClient = cfClient; this.successStatus = successStatus; this.errorStatus = errorStatus; this.stackErrorStatuses = stackErrorStatuses; this.cloudFormationStackName = cloudFormationStackName; describeStacksRequest = new DescribeStacksRequest().withStackName(cloudFormationStackName); stackEventsRequest = new DescribeStackEventsRequest().withStackName(cloudFormationStackName); }
@AfterSuite @Parameters({ "regionName", "vpcStackName" }) public void deleteNetwork(String regionName, @Optional("it-vpc-stack") String vpcStackName) { AmazonCloudFormationClient client = new AmazonCloudFormationClient(); client.setRegion(RegionUtils.getRegion(regionName)); client.deleteStack(new DeleteStackRequest().withStackName(vpcStackName)); }
@Test @Parameters({ "networkName", "description", "publicInAccount", "regionName", "vpcStackName", "vpcName", "existingSubnet" }) public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount, String regionName, @Optional("it-vpc-stack") String vpcStackName, @Optional("it-vpc") String vpcName, boolean existingSubnet) { AmazonCloudFormationClient client = new AmazonCloudFormationClient(); client.setRegion(RegionUtils.getRegion(regionName)); Map<String, Object> networkMap = new HashMap<>(); String vpcCreationJson = existingSubnet ? "public_vpc_with_subnet.json" : "public_vpc_wihout_subnet.json"; try (InputStream vpcJsonInputStream = getClass().getResourceAsStream("/cloudformation/" + vpcCreationJson)) { String vpcCFTemplateString = IOUtils.toString(vpcJsonInputStream); CreateStackRequest stackRequest = createStackRequest(vpcStackName, vpcName, vpcCFTemplateString); client.createStack(stackRequest); List<Output> outputForRequest = getOutputForRequest(vpcStackName, client); if (existingSubnet) { networkMap.put("vpcId", outputForRequest.get(0).getOutputValue()); networkMap.put("subnetId", outputForRequest.get(1).getOutputValue()); } else { networkMap.put("vpcId", outputForRequest.get(1).getOutputValue()); networkMap.put("internetGatewayId", outputForRequest.get(0).getOutputValue()); } } catch (IOException e) { LOGGER.error("can't read vpc cloudformation template file"); throw new RuntimeException(e); } NetworkRequest networkRequest = new NetworkRequest(); networkRequest.setName(networkName); networkRequest.setDescription(description); networkRequest.setParameters(networkMap); if (!existingSubnet) { networkRequest.setSubnetCIDR("10.0.0.0/24"); } networkRequest.setCloudPlatform("AWS"); String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString(); getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true); }
public CloudFrontLogEventHandler() { this(new AmazonCloudFormationClient(), new AmazonS3Client(), new AWSWAFClient()); }
@Override protected void before() throws Throwable { try { String awsCredentialsDir = System.getProperty("aws.credentials.path"); File awsCredentialsFile = new File(awsCredentialsDir, "aws.credentials.properties"); Properties awsCredentials = new Properties(); awsCredentials.load(new FileReader(awsCredentialsFile)); String accessKey = awsCredentials.getProperty("cloud.aws.credentials.accessKey"); String secretKey = awsCredentials.getProperty("cloud.aws.credentials.secretKey"); this.cloudFormation = new AmazonCloudFormationClient(new BasicAWSCredentials(accessKey, secretKey)); YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yml")); Properties applicationProperties = yamlPropertiesFactoryBean.getObject(); this.stackName = applicationProperties.getProperty("cloud.aws.stack.name"); after(); ClassPathResource stackTemplate = new ClassPathResource("AwsIntegrationTestTemplate.json"); String templateBody = FileCopyUtils.copyToString(new InputStreamReader(stackTemplate.getInputStream())); this.cloudFormation.createStack( new CreateStackRequest() .withTemplateBody(templateBody) .withOnFailure(OnFailure.DELETE) .withStackName(this.stackName)); waitForCompletion(); System.setProperty("cloud.aws.credentials.accessKey", accessKey); System.setProperty("cloud.aws.credentials.secretKey", secretKey); } catch (Exception e) { if (!(e instanceof AssumptionViolatedException)) { Assume.assumeTrue("Can't perform AWS integration test because of: " + e.getMessage(), false); } else { throw e; } } }
protected AmazonCloudFormation amazonCloudFormationClient() { AmazonCloudFormation amazonCloudFormation = new AmazonCloudFormationClient(amazonCredentialsProvider()); amazonCloudFormation.setRegion(getRegion()); return amazonCloudFormation; }
@Override protected AmazonCloudFormation amazonCloudFormationClient() { AmazonCloudFormation amazonCloudFormation = new AmazonCloudFormationClient(awsCredentials()); amazonCloudFormation.setRegion(getRegion()); return amazonCloudFormation; }
@Bean @ConditionalOnMissingAmazonClient(AmazonCloudFormation.class) public AmazonWebserviceClientFactoryBean<AmazonCloudFormationClient> amazonCloudFormation() { return new AmazonWebserviceClientFactoryBean<>(AmazonCloudFormationClient.class, this.credentialsProvider, this.regionProvider); }
@Override public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception { createKeyPair(ac, stack); String cFStackName = cfStackUtil.getCfStackName(ac); AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName); AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName); AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork()); boolean existingVPC = awsNetworkView.isExistingVPC(); boolean existingSubnet = awsNetworkView.isExistingSubnet(); boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(awsNetworkView, amazonEC2Client); try { cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName)); LOGGER.info("Stack already exists: {}", cFStackName); } catch (AmazonServiceException ignored) { CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build(); resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext()); String cidr = stack.getNetwork().getSubnet().getCidr(); String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? findNonOverLappingCIDR(ac, stack) : cidr; AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack); ModelContext modelContext = new ModelContext() .withAuthenticatedContext(ac) .withStack(stack) .withExistingVpc(existingVPC) .withSnapshotId(getEbsSnapshotIdIfNeeded(ac, stack)) .withExistingIGW(awsNetworkView.isExistingIGW()) .withExistingSubnetCidr(existingSubnet ? getExistingSubnetCidr(ac, stack) : null) .withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null) .mapPublicIpOnLaunch(mapPublicIpOnLaunch) .withEnableInstanceProfile(awsInstanceProfileView.isEnableInstanceProfileStrategy()) .withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()) .withTemplate(stack.getTemplate()) .withDefaultSubnet(subnet); String cfTemplate = cloudFormationTemplateBuilder.build(modelContext); LOGGER.debug("CloudFormationTemplate: {}", cfTemplate); cfClient.createStack(createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate)); } LOGGER.info("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId()); AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(credentialView, regionName); PollTask<Boolean> task = awsPollTaskFactory.newAwsCreateStackStatusCheckerTask(ac, cfClient, asClient, CREATE_COMPLETE, CREATE_FAILED, ERROR_STATUSES, cFStackName); try { Boolean statePollerResult = task.call(); if (!task.completed(statePollerResult)) { syncPollingScheduler.schedule(task); } } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName); saveS3AccessRoleArn(ac, stack, cFStackName, cfClient, resourceNotifier); saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier); List<CloudResource> cloudResources = getCloudResources(ac, stack, cFStackName, cfClient, amazonEC2Client, amazonASClient, mapPublicIpOnLaunch); return check(ac, cloudResources); }
@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)); }
private List<String> getInstancesForGroup(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonCloudFormationClient client, Group group) { return cfStackUtil.getInstanceIds(amazonASClient, cfStackUtil.getAutoscalingGroupName(ac, client, group.getName())); }