@Test public void testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets() { String networkCidr = "0.0.0.0/16"; Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC)); AmazonCloudFormationRetryClient cloudFormationRetryClient = mock(AmazonCloudFormationRetryClient.class); AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class); AmazonEC2Client ec2Client = mock(AmazonEC2Client.class); Map<String, String> output = createOutput(); NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets); List<SubnetRequest> subnetRequestList = createSubnetRequestList(); Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet()); when(awsClient.createAccess(any(), any())).thenReturn(ec2Client); when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList); when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cloudFormationRetryClient); when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient); when(cfClient.waiters()).thenReturn(cfWaiters); when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter); when(cfStackUtil.getOutputs(NETWORK_ID, cloudFormationRetryClient)).thenReturn(output); when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets); CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest); verify(awsClient).createCloudFormationRetryClient(any(AwsCredentialView.class), eq(REGION.value())); verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value())); verify(creationWaiter, times(1)).run(any()); verify(cfStackUtil).getOutputs(NETWORK_ID, cloudFormationRetryClient); verify(awsTaggingService, never()).prepareCloudformationTags(any(), any()); verify(cloudFormationRetryClient, never()).createStack(any(CreateStackRequest.class)); assertEquals(VPC_ID, actual.getNetworkId()); assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size()); }
@Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential()); AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkRequest.getRegion().value()); List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest); String cfStackName = networkRequest.getStackName(); try { cloudFormationRetryClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName)); LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName); return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationRetryClient, subnetRequests); } catch (AmazonServiceException e) { if (networkDoesNotExist(e)) { LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage()); String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests); return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationRetryClient, cloudFormationTemplate, subnetRequests); } else { throw new CloudConnectorException("Failed to create network.", e); } } }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential()); AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkRequest.getRegion().value()); List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest); String cfStackName = networkRequest.getStackName(); try { cloudFormationRetryClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName)); LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName); return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationRetryClient, subnetRequests); } catch (AmazonServiceException e) { if (networkDoesNotExist(e)) { LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage()); String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests); return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationRetryClient, cloudFormationTemplate, subnetRequests); } else { throw new CloudConnectorException("Failed to create network.", e); } } } }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential()); AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkRequest.getRegion().value()); List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest); String cfStackName = networkRequest.getStackName(); try { cloudFormationRetryClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName)); LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName); return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationRetryClient, subnetRequests); } catch (AmazonServiceException e) { if (networkDoesNotExist(e)) { LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage()); String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests); return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationRetryClient, cloudFormationTemplate, subnetRequests); } else { throw new CloudConnectorException("Failed to create network.", e); } } }  }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential()); AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkRequest.getRegion().value()); List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest); String cfStackName = networkRequest.getStackName(); try { cloudFormationRetryClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName)); LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName); return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationRetryClient, subnetRequests); } catch (AmazonServiceException e) { if (networkDoesNotExist(e)) { LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage()); String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests); return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationRetryClient, cloudFormationTemplate, subnetRequests); } else { throw new CloudConnectorException("Failed to create network.", e); } } }  @Override CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest); @Override NetworkCidr getNetworkCidr(Network network, CloudCredential credential); @Override SubnetSelectionResult filterSubnets(Collection<CloudSubnet> subnetMetas, SubnetSelectionParameters subnetSelectionParameters); @Override int subnetCountInDifferentAzMin(); @Override int subnetCountInDifferentAzMax(); @Override void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest); @Override Platform platform(); @Override Variant variant(); }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential()); AmazonCloudFormationRetryClient cloudFormationRetryClient = getCloudFormationRetryClient(credentialView, networkRequest.getRegion().value()); List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest); String cfStackName = networkRequest.getStackName(); try { cloudFormationRetryClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName)); LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName); return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationRetryClient, subnetRequests); } catch (AmazonServiceException e) { if (networkDoesNotExist(e)) { LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage()); String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests); return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationRetryClient, cloudFormationTemplate, subnetRequests); } else { throw new CloudConnectorException("Failed to create network.", e); } } }  @Override CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest); @Override NetworkCidr getNetworkCidr(Network network, CloudCredential credential); @Override SubnetSelectionResult filterSubnets(Collection<CloudSubnet> subnetMetas, SubnetSelectionParameters subnetSelectionParameters); @Override int subnetCountInDifferentAzMin(); @Override int subnetCountInDifferentAzMax(); @Override void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest); @Override Platform platform(); @Override Variant variant();  }