Search in sources :

Example 51 with Subnet

use of com.amazonaws.services.ec2.model.Subnet in project photon-model by vmware.

the class AWSNetworkService method createSubnetState.

/**
 * Create SubnetState representing AWS Subnet instance.
 */
private void createSubnetState(Subnet subnet, AWSNetworkContext context, AWSNetworkStage next) {
    SubnetState subnetState = new SubnetState();
    subnetState.id = subnet.getSubnetId();
    subnetState.name = subnet.getSubnetId();
    subnetState.subnetCIDR = subnet.getCidrBlock();
    subnetState.networkLink = context.network.documentSelfLink;
    subnetState.tenantLinks = context.network.tenantLinks;
    sendRequest(Operation.createPost(this.getHost(), SubnetService.FACTORY_LINK).setBody(subnetState).setCompletion((o, e) -> {
        if (e != null) {
            handleStages(context, e);
            return;
        }
        handleStages(context, next);
    }));
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ProvisionNetworkTaskState(com.vmware.photon.controller.model.tasks.ProvisionNetworkTaskService.ProvisionNetworkTaskState) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) HashMap(java.util.HashMap) AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) Query(com.vmware.xenon.services.common.QueryTask.Query) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) RouteTable(com.amazonaws.services.ec2.model.RouteTable) Subnet(com.amazonaws.services.ec2.model.Subnet) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) NetworkInstanceRequest(com.vmware.photon.controller.model.adapterapi.NetworkInstanceRequest) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) AWS_VPC_ROUTE_TABLE_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ROUTE_TABLE_ID) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) AWS_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID) AWS_GATEWAY_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_GATEWAY_ID) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) DeferredResult(com.vmware.xenon.common.DeferredResult) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 52 with Subnet

use of com.amazonaws.services.ec2.model.Subnet in project wildfly-camel by wildfly-extras.

the class EC2Utils method getSubnetId.

public static String getSubnetId(AmazonEC2Client ec2Client) {
    Subnet subnet = null;
    for (Subnet aux : ec2Client.describeSubnets().getSubnets()) {
        System.out.println();
        if (aux.getState().equals("available") && aux.getAvailabilityZone().startsWith("eu-west-1")) {
            subnet = aux;
            break;
        }
    }
    Assert.assertNotNull("Subnet not null", subnet);
    return subnet.getSubnetId();
}
Also used : Subnet(com.amazonaws.services.ec2.model.Subnet)

Example 53 with Subnet

use of com.amazonaws.services.ec2.model.Subnet in project cloudbreak by hortonworks.

the class AwsPlatformResources method networks.

@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Map<String, Set<CloudNetwork>> result = new HashMap<>();
    Set<CloudNetwork> cloudNetworks = new HashSet<>();
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value());
    // create vpc filter view
    PlatformResourceVpcFilterView filter = new PlatformResourceVpcFilterView(filters);
    DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest();
    // If the filtervalue is provided then we should filter only for those vpc
    if (!Strings.isNullOrEmpty(filter.getVpcId())) {
        describeVpcsRequest.withVpcIds(filter.getVpcId());
    }
    for (Vpc vpc : ec2Client.describeVpcs(describeVpcsRequest).getVpcs()) {
        Map<String, String> subnetMap = new HashMap<>();
        List<Subnet> subnets = ec2Client.describeSubnets(createVpcDescribeRequest(vpc)).getSubnets();
        Map<String, Object> properties = new HashMap<>();
        properties.put("cidrBlock", vpc.getCidrBlock());
        properties.put("default", vpc.getIsDefault());
        properties.put("dhcpOptionsId", vpc.getDhcpOptionsId());
        properties.put("instanceTenancy", vpc.getInstanceTenancy());
        properties.put("state", vpc.getState());
        for (Subnet subnet : subnets) {
            subnetMap.put(subnet.getSubnetId(), subnet.getSubnetId());
        }
        cloudNetworks.add(new CloudNetwork(vpc.getVpcId(), vpc.getVpcId(), subnetMap, properties));
    }
    result.put(region.value(), cloudNetworks);
    return new CloudNetworks(result);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) PlatformResourceVpcFilterView(com.sequenceiq.cloudbreak.cloud.model.view.PlatformResourceVpcFilterView) Vpc(com.amazonaws.services.ec2.model.Vpc) CloudNetworks(com.sequenceiq.cloudbreak.cloud.model.CloudNetworks) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Subnet(com.amazonaws.services.ec2.model.Subnet) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) HashSet(java.util.HashSet)

Example 54 with Subnet

use of com.amazonaws.services.ec2.model.Subnet in project cloudbreak by hortonworks.

the class AwsResourceConnector method launch.

@Override
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception {
    createKeyPair(ac, stack);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName);
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    boolean existingVPC = awsNetworkView.isExistingVPC();
    boolean existingSubnet = awsNetworkView.isExistingSubnet();
    boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
    try {
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        LOGGER.info("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        String cidr = stack.getNetwork().getSubnet().getCidr();
        String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? findNonOverLappingCIDR(ac, stack) : cidr;
        AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
        ModelContext modelContext = new ModelContext().withAuthenticatedContext(ac).withStack(stack).withExistingVpc(existingVPC).withSnapshotId(getEbsSnapshotIdIfNeeded(ac, stack)).withExistingIGW(awsNetworkView.isExistingIGW()).withExistingSubnetCidr(existingSubnet ? getExistingSubnetCidr(ac, stack) : null).withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(awsInstanceProfileView.isEnableInstanceProfileStrategy()).withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()).withTemplate(stack.getTemplate()).withDefaultSubnet(subnet);
        String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
    }
    LOGGER.info("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(credentialView, regionName);
    PollTask<Boolean> task = awsPollTaskFactory.newAwsCreateStackStatusCheckerTask(ac, cfClient, asClient, CREATE_COMPLETE, CREATE_FAILED, ERROR_STATUSES, cFStackName);
    try {
        Boolean statePollerResult = task.call();
        if (!task.completed(statePollerResult)) {
            syncPollingScheduler.schedule(task);
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
    saveS3AccessRoleArn(ac, stack, cFStackName, cfClient, resourceNotifier);
    saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
    List<CloudResource> cloudResources = getCloudResources(ac, stack, cFStackName, cfClient, amazonEC2Client, amazonASClient, mapPublicIpOnLaunch);
    return check(ac, cloudResources);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 55 with Subnet

use of com.amazonaws.services.ec2.model.Subnet in project cloudbreak by hortonworks.

the class AwsResourceConnector method findNonOverLappingCIDR.

protected String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);
    DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVPC());
    Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
    String vpcCidr = vpc.getCidrBlock();
    LOGGER.info("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr);
    DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVPC())));
    List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets();
    List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList());
    LOGGER.info("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), subnetCidrs.stream().collect(Collectors.joining(",")));
    return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Filter(com.amazonaws.services.ec2.model.Filter) Vpc(com.amazonaws.services.ec2.model.Vpc) Subnet(com.amazonaws.services.ec2.model.Subnet) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest)

Aggregations

DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)26 Subnet (com.amazonaws.services.ec2.model.Subnet)22 Vpc (com.amazonaws.services.ec2.model.Vpc)22 HashMap (java.util.HashMap)22 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)21 Test (org.junit.Test)20 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)17 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)16 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)14 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)14 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)14 Group (com.sequenceiq.cloudbreak.cloud.model.Group)14 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)14 Location (com.sequenceiq.cloudbreak.cloud.model.Location)14 Network (com.sequenceiq.cloudbreak.cloud.model.Network)14 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)14 ArrayList (java.util.ArrayList)12 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)11 Filter (com.amazonaws.services.ec2.model.Filter)9 HashSet (java.util.HashSet)7