Search in sources :

Example 31 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsPublicKeyConnector method exists.

@Override
public boolean exists(PublicKeyDescribeRequest request) {
    LOGGER.debug("Describe public key {} in {} region on AWS", request.getPublicKeyId(), request.getRegion());
    AwsCredentialView awsCredential = new AwsCredentialView(request.getCredential());
    try {
        AmazonEc2Client client = awsClient.createEc2Client(awsCredential, request.getRegion());
        return exists(client, request.getPublicKeyId());
    } catch (Exception e) {
        String errorMessage = String.format("Failed to describe public key [%s:'%s', region: '%s'], detailed message: %s", getType(awsCredential), getAwsId(awsCredential), request.getRegion(), e.getMessage());
        LOGGER.error(errorMessage, e);
    }
    return false;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)

Example 32 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNetworkCfTemplateProviderTest method testProvideWhenPrivateSubnetsAreDisabledAndInterfaceServicesWithDifferentAzs.

@Test
public void testProvideWhenPrivateSubnetsAreDisabledAndInterfaceServicesWithDifferentAzs() throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File("src/test/resources/json/aws-cf-network-publicsubnet-vpcendpoints-differentazs.json"));
    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(createDescribeVpcEndpointServicesResultWithDifferentAzs());
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(false, PrivateEndpointType.USE_VPC_ENDPOINT);
    List<SubnetRequest> subnetRequestList = createPublicSubnetRequestList();
    String actual = underTest.provide(networkCreationRequest, subnetRequestList);
    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNetworkCfTemplateProviderTest method testProvideWhenOnlyPublicSubnetsAndInterfaceServicesWithDifferentAzs.

@Test
public void testProvideWhenOnlyPublicSubnetsAndInterfaceServicesWithDifferentAzs() throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File("src/test/resources/json/aws-cf-network-publicsubnet-vpcendpoints-differentazs.json"));
    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(createDescribeVpcEndpointServicesResultWithDifferentAzs());
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(true, PrivateEndpointType.USE_VPC_ENDPOINT);
    List<SubnetRequest> subnetRequestList = createPublicSubnetRequestList();
    String actual = underTest.provide(networkCreationRequest, subnetRequestList);
    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 34 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNetworkCfTemplateProviderTest method testProvideWhenPrivateSubnetCreationEnabled.

@ParameterizedTest
@MethodSource("privateSubnetArguments")
public void testProvideWhenPrivateSubnetCreationEnabled(String expectedTemplate, List<String> gatewayServices, List<String> interfaceServices, DescribeVpcEndpointServicesResult describeVpcEndpointServicesResult) throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File(expectedTemplate));
    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(describeVpcEndpointServicesResult);
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(true, PrivateEndpointType.USE_VPC_ENDPOINT);
    List<SubnetRequest> subnetRequestList = createPrivateAndPublicSubnetRequestList();
    ReflectionTestUtils.setField(underTest, "gatewayServices", gatewayServices);
    ReflectionTestUtils.setField(underTest, "interfaceServices", interfaceServices);
    String actual = underTest.provide(networkCreationRequest, subnetRequestList);
    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(freemarker.template.Template) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 35 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testGetNetworkCidrWithDuplicatedCidr.

@Test
public void testGetNetworkCidrWithDuplicatedCidr() {
    String existingVpc = "vpc-1";
    String cidrBlock = "10.0.0.0/16";
    Network network = new Network(null, Map.of(NetworkConstants.VPC_ID, existingVpc, "region", "us-west-2"));
    CloudCredential credential = new CloudCredential();
    AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
    DescribeVpcsResult describeVpcsResult = describeVpcsResult(cidrBlock, cidrBlock);
    describeVpcsResult.getVpcs().get(0).getCidrBlockAssociationSet().add(new VpcCidrBlockAssociation().withCidrBlock(cidrBlock));
    when(awsClient.createEc2Client(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client);
    when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult);
    NetworkCidr result = underTest.getNetworkCidr(network, credential);
    assertEquals(cidrBlock, result.getCidr());
    assertEquals(1, result.getCidrs().size());
    assertEquals(cidrBlock, result.getCidrs().get(0));
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) VpcCidrBlockAssociation(com.amazonaws.services.ec2.model.VpcCidrBlockAssociation) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Test(org.junit.Test)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)97 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)44 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)41 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)29 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)29 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)28 HashMap (java.util.HashMap)28 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)23 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)23 Network (com.sequenceiq.cloudbreak.cloud.model.Network)23 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)22 AmazonServiceException (com.amazonaws.AmazonServiceException)21 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)21 List (java.util.List)21 Vpc (com.amazonaws.services.ec2.model.Vpc)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20