Search in sources :

Example 6 with StackResourceSummary

use of com.amazonaws.services.cloudformation.model.StackResourceSummary in project spring-cloud-aws by awspring.

the class StackConfigurationBeanDefinitionParserTest method stackResourceRegistry_stackConfigurationWithStaticName_stackResourceRegistryBeanExposedUnderStaticStackName.

// @checkstyle:off
@Test
void stackResourceRegistry_stackConfigurationWithStaticName_stackResourceRegistryBeanExposedUnderStaticStackName() throws Exception {
    // @checkstyle:on
    // Arrange
    GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
    AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class);
    when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("IntegrationTestStack"))).thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary()));
    applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-staticStackName.xml", getClass()));
    applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation);
    applicationContext.refresh();
    // Act
    StackResourceRegistry staticStackNameProviderBasedStackResourceRegistry = applicationContext.getBean("IntegrationTestStack", StackResourceRegistry.class);
    // Assert
    assertThat(staticStackNameProviderBasedStackResourceRegistry).isNotNull();
}
Also used : AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) StackResourceRegistry(io.awspring.cloud.core.env.stack.StackResourceRegistry) ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ListStackResourcesRequest(com.amazonaws.services.cloudformation.model.ListStackResourcesRequest) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 7 with StackResourceSummary

use of com.amazonaws.services.cloudformation.model.StackResourceSummary in project spring-cloud-aws by awspring.

the class StackResourceRegistryFactoryBean method getStackResourceSummaries.

private List<StackResourceSummary> getStackResourceSummaries(String stackName) {
    ListStackResourcesResult listStackResourcesResult = this.amazonCloudFormationClient.listStackResources(new ListStackResourcesRequest().withStackName(stackName));
    if (!StringUtils.hasText(listStackResourcesResult.getNextToken())) {
        return listStackResourcesResult.getStackResourceSummaries();
    } else {
        List<StackResourceSummary> result = new ArrayList<>(listStackResourcesResult.getStackResourceSummaries());
        while (StringUtils.hasText(listStackResourcesResult.getNextToken())) {
            listStackResourcesResult = this.amazonCloudFormationClient.listStackResources(new ListStackResourcesRequest().withStackName(stackName).withNextToken(listStackResourcesResult.getNextToken()));
            result.addAll(listStackResourcesResult.getStackResourceSummaries());
        }
        return result;
    }
}
Also used : ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ArrayList(java.util.ArrayList) ListStackResourcesRequest(com.amazonaws.services.cloudformation.model.ListStackResourcesRequest) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary)

Example 8 with StackResourceSummary

use of com.amazonaws.services.cloudformation.model.StackResourceSummary in project spring-cloud-aws by awspring.

the class StackResourceRegistryFactoryBeanTest method makeAmazonCloudFormationClient.

private static AmazonCloudFormation makeAmazonCloudFormationClient(Map<String, String> resourceIdMappings) {
    Map<String, List<StackResourceSummary>> stackResourceSummaries = new HashMap<>();
    // allow stack with no
    stackResourceSummaries.put(STACK_NAME, new ArrayList<>());
    for (Map.Entry<String, String> entry : resourceIdMappings.entrySet()) {
        String logicalResourceId = entry.getKey();
        String physicalResourceId = entry.getValue();
        String physicalStackName;
        if (logicalResourceId.contains(".")) {
            physicalStackName = resourceIdMappings.get(logicalResourceId.substring(0, logicalResourceId.lastIndexOf(".")));
            logicalResourceId = logicalResourceId.substring(logicalResourceId.lastIndexOf(".") + 1);
        } else {
            physicalStackName = STACK_NAME;
        }
        List<StackResourceSummary> list = stackResourceSummaries.computeIfAbsent(physicalStackName, k -> new ArrayList<>());
        list.add(makeStackResourceSummary(logicalResourceId, physicalResourceId));
    }
    AmazonCloudFormation amazonCloudFormationClient = mock(AmazonCloudFormation.class);
    for (Map.Entry<String, List<StackResourceSummary>> entry : stackResourceSummaries.entrySet()) {
        String stackName = entry.getKey();
        ListStackResourcesResult listStackResourcesResult = mock(ListStackResourcesResult.class);
        when(listStackResourcesResult.getStackResourceSummaries()).thenReturn(entry.getValue());
        when(amazonCloudFormationClient.listStackResources(argThat(item -> item != null && stackName.equals((item).getStackName())))).thenReturn(listStackResourcesResult);
    }
    return amazonCloudFormationClient;
}
Also used : AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) StackResourceRegistry(io.awspring.cloud.core.env.stack.StackResourceRegistry) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ListableStackResourceFactory(io.awspring.cloud.core.env.stack.ListableStackResourceFactory) HashMap(java.util.HashMap) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) ArrayList(java.util.ArrayList) Mockito.verify(org.mockito.Mockito.verify) ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) Test(org.junit.jupiter.api.Test) List(java.util.List) StackResource(io.awspring.cloud.core.env.stack.StackResource) Map(java.util.Map) Collections(java.util.Collections) ListStackResourcesRequest(com.amazonaws.services.cloudformation.model.ListStackResourcesRequest) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary)

Example 9 with StackResourceSummary

use of com.amazonaws.services.cloudformation.model.StackResourceSummary in project spring-cloud-aws by awspring.

the class StackResourceRegistryFactoryBeanTest method makeStackResourceSummary.

private static StackResourceSummary makeStackResourceSummary(String logicalResourceId, String physicalResourceId) {
    StackResourceSummary stackResourceSummary = mock(StackResourceSummary.class);
    when(stackResourceSummary.getLogicalResourceId()).thenReturn(logicalResourceId);
    when(stackResourceSummary.getPhysicalResourceId()).thenReturn(physicalResourceId);
    when(stackResourceSummary.getResourceType()).thenReturn(logicalResourceId.endsWith("Stack") ? "AWS::CloudFormation::Stack" : "Amazon::SES::Test");
    return stackResourceSummary;
}
Also used : StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary)

Example 10 with StackResourceSummary

use of com.amazonaws.services.cloudformation.model.StackResourceSummary in project cloudbreak by hortonworks.

the class AwsLoadBalancerLaunchService method setLoadBalancerMetadata.

@VisibleForTesting
void setLoadBalancerMetadata(List<AwsLoadBalancer> awsLoadBalancers, ListStackResourcesResult result) {
    List<StackResourceSummary> summaries = result.getStackResourceSummaries();
    for (AwsLoadBalancer loadBalancer : awsLoadBalancers) {
        LOGGER.debug("Processing load balancer {}", loadBalancer.getName());
        for (AwsListener listener : loadBalancer.getListeners()) {
            LOGGER.debug("Processing listener {} and target group {}", listener.getName(), listener.getTargetGroup().getName());
            Optional<StackResourceSummary> targetGroupSummary = summaries.stream().filter(stackResourceSummary -> listener.getTargetGroup().getName().equals(stackResourceSummary.getLogicalResourceId())).findFirst();
            if (targetGroupSummary.isEmpty()) {
                throw new CloudConnectorException(String.format("Could not create load balancer listeners: target group %s not found.", listener.getTargetGroup().getName()));
            }
            if (StringUtils.isEmpty(targetGroupSummary.get().getPhysicalResourceId())) {
                throw new CloudConnectorException(String.format("Could not create load balancer listeners: target group %s arn not found.", listener.getTargetGroup().getName()));
            }
            listener.getTargetGroup().setArn(targetGroupSummary.get().getPhysicalResourceId());
            LOGGER.debug("Found arn {} for target group {}", listener.getTargetGroup().getArn(), listener.getTargetGroup().getName());
        }
        Optional<StackResourceSummary> loadBalancerSummary = summaries.stream().filter(stackResourceSummary -> loadBalancer.getName().equals(stackResourceSummary.getLogicalResourceId())).findFirst();
        if (loadBalancerSummary.isEmpty()) {
            throw new CloudConnectorException(String.format("Could not create load balancer listeners: load balancer %s not found.", loadBalancer.getName()));
        }
        if (StringUtils.isEmpty(loadBalancerSummary.get().getPhysicalResourceId())) {
            throw new CloudConnectorException(String.format("Could not create load balancer listeners: load balancer %s arn not found.", loadBalancer.getName()));
        }
        loadBalancer.setArn(loadBalancerSummary.get().getPhysicalResourceId());
        loadBalancer.validateListenerConfigIsSet();
        LOGGER.debug("Found arn {} for load balancer {}", loadBalancer.getArn(), loadBalancer.getName());
    }
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AwsCloudFormationErrorMessageProvider(com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Service(org.springframework.stereotype.Service) Collectors.mapping(java.util.stream.Collectors.mapping) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) Network(com.sequenceiq.cloudbreak.cloud.model.Network) Waiter(com.amazonaws.waiters.Waiter) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) Logger(org.slf4j.Logger) AwsStackRequestHelper(com.sequenceiq.cloudbreak.cloud.aws.AwsStackRequestHelper) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceStatus(com.amazonaws.services.cloudformation.model.ResourceStatus) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors(java.util.stream.Collectors) ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) AwsModelService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsModelService) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) Optional(java.util.Optional) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ELASTIC_LOAD_BALANCER(com.sequenceiq.common.api.type.ResourceType.ELASTIC_LOAD_BALANCER) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

StackResourceSummary (com.amazonaws.services.cloudformation.model.StackResourceSummary)34 ListStackResourcesResult (com.amazonaws.services.cloudformation.model.ListStackResourcesResult)17 ListStackResourcesRequest (com.amazonaws.services.cloudformation.model.ListStackResourcesRequest)14 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)13 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)13 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)12 Network (com.sequenceiq.cloudbreak.cloud.model.Network)12 Test (org.junit.Test)12 Test (org.junit.jupiter.api.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 GroupNetwork (com.sequenceiq.cloudbreak.cloud.model.GroupNetwork)11 AmazonCloudFormation (com.amazonaws.services.cloudformation.AmazonCloudFormation)9 AwsLoadBalancer (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)6 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 ArrayList (java.util.ArrayList)5 GenericXmlApplicationContext (org.springframework.context.support.GenericXmlApplicationContext)5 AwsListener (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener)4 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)4