Search in sources :

Example 21 with Vpc

use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.

the class AWSUtils method getDefaultVPC.

/**
 * Gets the default VPC
 */
public static Vpc getDefaultVPC(AWSInstanceContext aws) {
    DescribeVpcsResult result = aws.amazonEC2Client.describeVpcs();
    List<Vpc> vpcs = result.getVpcs();
    for (Vpc vpc : vpcs) {
        if (vpc.isDefault()) {
            return vpc;
        }
    }
    return null;
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) Vpc(com.amazonaws.services.ec2.model.Vpc)

Example 22 with Vpc

use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.

the class AWSUtils method createSecurityGroupOnDefaultVPC.

// method create a security group in the VPC from custom properties or the default VPC
private static String createSecurityGroupOnDefaultVPC(AWSInstanceContext aws) {
    String vpcId = null;
    // get the subnet cidr (if any)
    String subnetCidr = null;
    // in case subnet will be obtained from the default vpc, the security group should
    // as well be created there
    Vpc defaultVPC = getDefaultVPC(aws);
    if (defaultVPC != null) {
        vpcId = defaultVPC.getVpcId();
        subnetCidr = defaultVPC.getCidrBlock();
    }
    // no subnet or no vpc is not an option...
    if (subnetCidr == null || vpcId == null) {
        throw new AmazonServiceException("default VPC not found");
    }
    return new AWSSecurityGroupClient(aws.amazonEC2Client).createDefaultSecurityGroupWithDefaultRules(defaultVPC);
}
Also used : AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) Vpc(com.amazonaws.services.ec2.model.Vpc) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 23 with Vpc

use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.

the class AWSNetworkStateEnumerationAdapterService method createTags.

/**
 * Gets the Networks and Subnets tags information and creates TagState for each tag
 */
private void createTags(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
    // Collect all tags in a List
    List<Tag> allNetworkAndSubnetsTags = context.awsVpcs.values().stream().filter(vpc -> !context.localNetworkStateMap.containsKey(vpc.getVpcId())).flatMap(vpc -> vpc.getTags().stream()).collect(Collectors.toList());
    allNetworkAndSubnetsTags.addAll(context.awsSubnets.values().stream().filter(subnet -> !context.localSubnetStateMap.containsKey(subnet.getSubnetId())).flatMap(subnet -> subnet.getTags().stream()).collect(Collectors.toList()));
    // POST each of the tags. If a tag exists it won't be created again. We don't want the name
    // tags, so filter them out
    List<Operation> operations = new ArrayList<>();
    Map<Long, Tag> tagsCreationOperationIdsMap = new ConcurrentHashMap<>();
    allNetworkAndSubnetsTags.stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey())).forEach(t -> {
        TagState tagState = newTagState(t.getKey(), t.getValue(), true, context.request.tenantLinks);
        Operation createTagOp = Operation.createPost(this, TagService.FACTORY_LINK).setBody(tagState);
        operations.add(createTagOp);
        tagsCreationOperationIdsMap.put(createTagOp.getId(), t);
    });
    if (operations.isEmpty()) {
        context.networkCreationStage = next;
        handleNetworkStateChanges(context);
    } else {
        OperationJoin.create(operations).setCompletion((ops, exs) -> {
            if (exs != null && !exs.isEmpty()) {
                this.logWarning("Failure creating external tags for network and subnets: %s", exs.get(0).getMessage());
            }
            ops.values().stream().filter(operation -> operation.getStatusCode() == Operation.STATUS_CODE_OK || operation.getStatusCode() == Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
                if (tagsCreationOperationIdsMap.containsKey(operation.getId())) {
                    context.createdExternalTags.add(tagsCreationOperationIdsMap.get(operation.getId()));
                }
            });
            context.networkCreationStage = next;
            handleNetworkStateChanges(context);
        }).sendWith(this);
    }
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) AWSNetworkUtils.mapVPCToNetworkState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapVPCToNetworkState) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) AWSResourceType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AWSNetworkUtils.createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.createQueryToGetExistingNetworkStatesFilteredByDiscoveredVPCs) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) SubnetStateWithParentVpcId(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkStateCreationContext.SubnetStateWithParentVpcId) AWS_ATTACHMENT_VPC_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_ATTACHMENT_VPC_FILTER) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) AWS_MAIN_ROUTE_ASSOCIATION(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_MAIN_ROUTE_ASSOCIATION) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) AWSNetworkUtils.mapSubnetToSubnetState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.mapSubnetToSubnetState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) AWS_GATEWAY_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_GATEWAY_ID) List(java.util.List) AWS_FILTER_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_FILTER_VPC_ID) DescribeRouteTablesRequest(com.amazonaws.services.ec2.model.DescribeRouteTablesRequest) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) DescribeRouteTablesResult(com.amazonaws.services.ec2.model.DescribeRouteTablesResult) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) BiConsumer(java.util.function.BiConsumer) Filter(com.amazonaws.services.ec2.model.Filter) RouteTable(com.amazonaws.services.ec2.model.RouteTable) Subnet(com.amazonaws.services.ec2.model.Subnet) AWS_VPC_ROUTE_TABLE_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ROUTE_TABLE_ID) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Vpc(com.amazonaws.services.ec2.model.Vpc) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) AWSNetworkUtils.createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkUtils.createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets) NumericRange.createLessThanRange(com.vmware.xenon.services.common.QueryTask.NumericRange.createLessThanRange) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AWS_VPC_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ArrayList(java.util.ArrayList) Tag(com.amazonaws.services.ec2.model.Tag) AdapterUtils.createPatchOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPatchOperation) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) TagState(com.vmware.photon.controller.model.resources.TagService.TagState)

Example 24 with Vpc

use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.

the class AWSNetworkClient method getDefaultVPC.

/**
 * Get the default VPC - return null if no default specified
 */
public Vpc getDefaultVPC() {
    DescribeVpcsRequest req = new DescribeVpcsRequest();
    DescribeVpcsResult result = this.client.describeVpcs(req);
    List<Vpc> vpcs = result.getVpcs();
    for (Vpc vpc : vpcs) {
        if (vpc.isDefault()) {
            return vpc;
        }
    }
    return null;
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Vpc(com.amazonaws.services.ec2.model.Vpc)

Example 25 with Vpc

use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.

the class AWSNetworkUtils method mapVPCToNetworkState.

public static NetworkState mapVPCToNetworkState(Vpc vpc, String regionId, String resourcePoolLink, String endpointLink, String authCredentialsLink, String parentComputeLink, List<String> tenantLinks, URI adapterUri) {
    if (vpc == null) {
        throw new IllegalArgumentException("Cannot map VPC to network state for null instance");
    }
    NetworkState networkState = new NetworkState();
    networkState.id = vpc.getVpcId();
    // calculate vpc name
    if (vpc.getTags() == null) {
        networkState.name = vpc.getVpcId();
    } else {
        networkState.name = vpc.getTags().stream().filter(tag -> tag.getKey().equals(AWS_TAG_NAME)).map(tag -> tag.getValue()).findFirst().orElse(vpc.getVpcId());
    }
    networkState.subnetCIDR = vpc.getCidrBlock();
    networkState.regionId = regionId;
    networkState.resourcePoolLink = resourcePoolLink;
    networkState.endpointLink = endpointLink;
    if (networkState.endpointLinks == null) {
        networkState.endpointLinks = new HashSet<>();
    }
    networkState.endpointLinks.add(endpointLink);
    networkState.authCredentialsLink = authCredentialsLink;
    networkState.instanceAdapterReference = adapterUri;
    networkState.tenantLinks = tenantLinks;
    networkState.computeHostLink = parentComputeLink;
    networkState.customProperties = new HashMap<>();
    networkState.customProperties.put("defaultInstance", String.valueOf(vpc.isDefault()));
    return networkState;
}
Also used : AWSEnumerationUtils.getTagValue(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getTagValue) Arrays(java.util.Arrays) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) StringUtil(io.netty.util.internal.StringUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) Subnet(com.amazonaws.services.ec2.model.Subnet) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) StatelessService(com.vmware.xenon.common.StatelessService) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Vpc(com.amazonaws.services.ec2.model.Vpc) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Set(java.util.Set) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) List(java.util.List) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Aggregations

Vpc (com.amazonaws.services.ec2.model.Vpc)27 HashMap (java.util.HashMap)21 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)20 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)18 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)17 Test (org.junit.Test)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)8 Subnet (com.amazonaws.services.ec2.model.Subnet)7 DescribeVpcsRequest (com.amazonaws.services.ec2.model.DescribeVpcsRequest)6 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)5 Filter (com.amazonaws.services.ec2.model.Filter)5 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)5