/** * Since there doesn't appear to be a first class way through the SDK at this time to get a CF export. We can * iterate through the stacks for a given output key and return the value. * @param outputKey The exported CF variable to search and retrieve the value of. * @return The value for the export if found */ public Optional<String> searchStacksForOutput(String outputKey) { DescribeStacksResult describeStacksResult = null; do { DescribeStacksRequest request = new DescribeStacksRequest(); if (describeStacksResult != null && describeStacksResult.getNextToken() != null) { request.withNextToken(describeStacksResult.getNextToken()); } describeStacksResult = cloudFormationClient.describeStacks(); for (Stack stack : describeStacksResult.getStacks()) { for (Output output : stack.getOutputs()) { if (StringUtils.equals(output.getOutputKey(), outputKey)) { return Optional.of(output.getOutputValue()); } } } } while (describeStacksResult.getNextToken() != null); return Optional.empty(); }
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"); }
@TaskAction public void writeStackOutput() throws IOException, InterruptedException { final DeployService deployService = new DeployService(config); final List<Output> output = deployService.getStackOutput(); final Properties prop = new Properties(); output.forEach(o -> prop.setProperty(o.getOutputKey(), o.getOutputValue())); try (Writer writer = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) { prop.store(writer, "Output of stack " + config.getStackName()); } }
/** * Returns the current status of the named stack. * * @param stackId Stack name. * @return Stack outputs data. */ public Map<String, String> getStackOutputs(final String stackId) { final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(stackId); final DescribeStacksResult result = cloudFormationClient.describeStacks(request); final Map<String, String> outputs = Maps.newHashMap(); if (result.getStacks().size() > 0) { outputs.putAll(result.getStacks().get(0).getOutputs().stream().collect( Collectors.toMap(Output::getOutputKey, Output::getOutputValue))); } return outputs; }
private static String getOutputValue(Stack stack, String outputKey) { for (Output output : stack.getOutputs()) { if (output.getOutputKey().equals(outputKey)) { return output.getOutputValue(); } } throw new IllegalStateException("No output '" + outputKey + "' defined in stack '" + stack.getStackName() + "'"); }
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)); }
@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 List<Output> getStackOutput() { return cloudFormationService.getOutputParameters(config.getStackName()); }
public List<Output> getOutputParameters(String stackName) { final Stack stack = describeStack(stackName).stream().findFirst() .orElseThrow(() -> new DeploymentException("Stack not found")); return stack.getOutputs(); }