Example usage for com.amazonaws.services.cloudformation.model StackEvent getPhysicalResourceId

List of usage examples for com.amazonaws.services.cloudformation.model StackEvent getPhysicalResourceId

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation.model StackEvent getPhysicalResourceId.

Prototype


public String getPhysicalResourceId() 

Source Link

Document

The name or unique identifier associated with the physical instance of the resource.

Usage

From source file:com.deploymentio.cfnstacker.CloudFormationClient.java

License:Apache License

/**
 * Gets all non-progress events for stack that were generated after a
 * certain time. This method will ignore any "throttling" error from AWS and
 * return empty results./*  www.  jav  a2s.co  m*/
 * 
 * @param stackId unique ID for the stack
 * @param startDate only events after this time are considered
 * @return a list of stack events
 */
public List<StackEvent> getStackEvents(String stackId, Date startDate, OperationTracker tracker,
        int checkIntervalSeconds) {

    ArrayList<StackEvent> events = new ArrayList<StackEvent>();
    DescribeStackEventsResult result = null;
    String nextToken = null;

    doLoop: do {
        try {
            result = client.describeStackEvents(new DescribeStackEventsRequest().withStackName(stackId));
        } catch (AmazonServiceException ase) {
            if ("Throttling".equals(ase.getErrorCode())) {
                logger.warn("Got a throttling error from AWS while calling describeStackEvents()");
                break;
            } else {
                throw ase;
            }
        }
        nextToken = result.getNextToken();

        for (StackEvent evt : result.getStackEvents()) {

            // break out if we start seeing events older than our start date
            if (!evt.getTimestamp().after(startDate)) {
                if (logger.isTraceEnabled()) {
                    logger.trace(createStackEventLogMessage(evt, startDate, "Saw event older than startdate"));
                }
                break doLoop;
            }

            // mark that an event was generated
            if (tracker != null) {
                tracker.markEventsGenerated(stackId);
            }

            // ignore IN_PROGRESS events
            if (!evt.getResourceStatus().endsWith("_IN_PROGRESS")) {
                if (logger.isTraceEnabled()) {
                    logger.trace(createStackEventLogMessage(evt, startDate, "Adding event"));
                }
                events.add(evt);
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace(createStackEventLogMessage(evt, startDate, "Ignorning event"));
                }
            }

            // start tracking a sub-stack if we come across one
            if (tracker != null && evt.getResourceType().equals("AWS::CloudFormation::Stack")
                    && !evt.getPhysicalResourceId().equals(stackId)) {
                tracker.track(this, evt.getLogicalResourceId(), evt.getPhysicalResourceId(),
                        checkIntervalSeconds);
            }
        }

    } while (!StringUtils.isEmpty(nextToken));

    // sort the events
    Collections.sort(events, new Comparator<StackEvent>() {
        @Override
        public int compare(StackEvent e1, StackEvent e2) {
            return e1.getTimestamp().compareTo(e2.getTimestamp());
        }
    });

    return events;
}

From source file:com.deploymentio.cfnstacker.OperationTracker.java

License:Apache License

/**
 * Adds a new ongoing stack operation to track. If a task (based on
 * taskName) is already tracked, it is silently ignored.
 * /*from www .  j  av  a 2  s.  c  o  m*/
 * @param client
 *            the CFN client
 * @param stackName
 *            stack name
 * @param stackId
 *            stack ID
 * @param checkIntervalSeconds
 *            how often to check the stack operation's progress
 */
public OperationTracker track(final CloudFormationClient client, final String stackName, final String stackId,
        final int checkIntervalSeconds) {

    if (stackNames.add(stackName)) {
        logger.debug("Tracking Stack: Name=" + stackName + " ID=" + stackId);
        Future<String> future = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                // set the time to be a minute in the past - this is to account for any
                // time differences between the local clock and clock on CFN servers
                Date startTime = new Date(System.currentTimeMillis() - 60000);
                while (true) {

                    if (logger.isTraceEnabled()) {
                        logger.trace(
                                "Waiting for completion: StartDate=" + DateUtils.formatISO8601Date(startTime));
                    }

                    // display all events and look for the final event for the stack itself
                    boolean getOutLater = false;
                    for (StackEvent evt : client.getStackEvents(stackId, startTime, OperationTracker.this,
                            checkIntervalSeconds)) {

                        if (evt.getResourceType().equals("AWS::CloudFormation::Stack")) {

                            if (evt.getPhysicalResourceId().equals(stackId)) {
                                getOutLater = true;
                            }
                        }

                        // record the latest timestamp
                        if (evt.getTimestamp().after(startTime))
                            startTime = evt.getTimestamp();

                        if (logger.isDebugEnabled()) {
                            logger.info("EventDate=" + DateUtils.formatISO8601Date(evt.getTimestamp())
                                    + " Stack=" + stackName + " Type=" + evt.getResourceType() + " ID="
                                    + evt.getLogicalResourceId() + " Status=" + evt.getResourceStatus());
                        } else {
                            logger.info("Stack=" + stackName + " Type=" + evt.getResourceType() + " ID="
                                    + evt.getLogicalResourceId() + " Status=" + evt.getResourceStatus());
                        }
                    }

                    if (getOutLater) {
                        break;
                    } else {
                        Thread.sleep(checkIntervalSeconds * 1000);
                    }
                }

                return stackId;
            }
        });
        trackerRecords.put(stackId, new ProgressTrackerRecord(stackName, future));
    } else {
        logger.trace("Ignoring Stack: Name=" + stackName);
    }

    return this;
}