Search in sources :

Example 16 with Vpc

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

the class AWSNetworkClient method getMainRouteTable.

/**
 * Get the main route table for a given VPC
 */
public RouteTable getMainRouteTable(String vpcId) {
    // build filter list
    List<Filter> filters = new ArrayList<>();
    filters.add(AWSUtils.getFilter(AWSUtils.AWS_FILTER_VPC_ID, vpcId));
    filters.add(AWSUtils.getFilter(AWS_MAIN_ROUTE_ASSOCIATION, "true"));
    DescribeRouteTablesRequest req = new DescribeRouteTablesRequest().withFilters(filters);
    DescribeRouteTablesResult res = this.client.describeRouteTables(req);
    List<RouteTable> routeTables = res.getRouteTables();
    return routeTables.isEmpty() ? null : routeTables.get(0);
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) DescribeRouteTablesRequest(com.amazonaws.services.ec2.model.DescribeRouteTablesRequest) Filter(com.amazonaws.services.ec2.model.Filter) ArrayList(java.util.ArrayList) DescribeRouteTablesResult(com.amazonaws.services.ec2.model.DescribeRouteTablesResult)

Example 17 with Vpc

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

the class AWSEnumerationUtils method mapInstanceToComputeState.

/**
 * Maps the instance discovered on AWS to a local compute state that will be persisted.
 */
public static ComputeState mapInstanceToComputeState(ServiceHost host, Instance instance, String parentComputeLink, String placementComputeLink, String resourcePoolLink, String existingEndpointLink, Set<String> endpointLinks, String computeDescriptionLink, Set<URI> parentCDStatsAdapterReferences, Set<String> internalTagLinks, String regionId, String zoneId, List<String> tenantLinks, List<Tag> createdExternalTags, Boolean isNewState, List<String> diskLinks) {
    ComputeState computeState = new ComputeState();
    computeState.id = instance.getInstanceId();
    computeState.name = instance.getInstanceId();
    computeState.parentLink = parentComputeLink;
    computeState.computeHostLink = parentComputeLink;
    computeState.type = ComputeType.VM_GUEST;
    computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
    computeState.regionId = regionId;
    computeState.zoneId = zoneId;
    computeState.instanceType = instance.getInstanceType();
    computeState.instanceAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_INSTANCE_ADAPTER);
    computeState.enumerationAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_ENUMERATION_CREATION_ADAPTER);
    computeState.statsAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_STATS_ADAPTER);
    computeState.statsAdapterReferences = parentCDStatsAdapterReferences;
    computeState.resourcePoolLink = resourcePoolLink;
    if (computeState.endpointLinks == null) {
        computeState.endpointLinks = new HashSet<>();
    }
    computeState.endpointLinks.addAll(endpointLinks);
    // assign existing one, if exists
    if (existingEndpointLink != null) {
        computeState.endpointLink = existingEndpointLink;
    } else {
        computeState.endpointLink = endpointLinks.iterator().next();
    }
    // Compute descriptions are looked up by the instanceType in the local list of CDs.
    computeState.descriptionLink = computeDescriptionLink;
    computeState.hostName = instance.getPublicDnsName();
    // TODO VSYM-375 for adding disk information
    computeState.address = instance.getPublicIpAddress();
    computeState.powerState = AWSUtils.mapToPowerState(instance.getState());
    computeState.customProperties = new HashMap<>();
    computeState.customProperties.put(CUSTOM_OS_TYPE, getNormalizedOSType(instance));
    computeState.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
    computeState.customProperties.put(ComputeProperties.PLACEMENT_LINK, placementComputeLink);
    // Network State. Create one network state mapping to each VPC that is discovered during
    // enumeration.
    computeState.customProperties.put(AWS_VPC_ID, instance.getVpcId());
    computeState.tagLinks = new HashSet<>();
    // PATCH to update tagLinks of existing disks.
    if (!instance.getTags().isEmpty() && isNewState) {
        // we have already made sure that the tags exist and we can build their links ourselves
        computeState.tagLinks = instance.getTags().stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey()) && createdExternalTags.contains(t)).map(t -> newTagState(t.getKey(), t.getValue(), true, tenantLinks)).map(TagFactoryService::generateSelfLink).collect(Collectors.toSet());
        if (computeState.tagLinks != null && computeState.tagLinks.contains(null)) {
            host.log(Level.SEVERE, "Null tag link inserted in new ComputeState for instance ID: %s", instance.getInstanceId());
            host.log(Level.SEVERE, "Removing null tag link from new ComputeState");
            computeState.tagLinks.remove(null);
        }
    }
    // The name of the compute state is the value of the AWS_TAG_NAME tag
    String nameTag = getTagValue(instance.getTags(), AWS_TAG_NAME);
    if (nameTag != null && !nameTag.equals(EMPTY_STRING)) {
        computeState.name = nameTag;
    }
    // append internal tagLinks to any existing ones
    if (internalTagLinks != null) {
        computeState.tagLinks.addAll(internalTagLinks);
    }
    if (instance.getLaunchTime() != null) {
        computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
    }
    if (diskLinks != null && !diskLinks.isEmpty()) {
        computeState.diskLinks = new ArrayList<>();
        computeState.diskLinks.addAll(diskLinks);
    }
    computeState.tenantLinks = tenantLinks;
    return computeState;
}
Also used : ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) QueryTask(com.vmware.xenon.services.common.QueryTask) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) Query(com.vmware.xenon.services.common.QueryTask.Query) TagFactoryService(com.vmware.photon.controller.model.resources.TagFactoryService) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) WINDOWS_PLATFORM(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.WINDOWS_PLATFORM) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) Instance(com.amazonaws.services.ec2.model.Instance) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Collection(java.util.Collection) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) UUID(java.util.UUID) AWS_VPC_ID(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) Tag(com.amazonaws.services.ec2.model.Tag) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) URI_PATH_CHAR(com.vmware.xenon.common.UriUtils.URI_PATH_CHAR) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TagFactoryService(com.vmware.photon.controller.model.resources.TagFactoryService)

Example 18 with Vpc

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

the class AWSSecurityGroupClient method getDefaultSecurityGroup.

public SecurityGroup getDefaultSecurityGroup(String vpcId) {
    SecurityGroup cellGroup = null;
    DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest().withFilters(new Filter("group-name", Collections.singletonList(DEFAULT_SECURITY_GROUP_NAME)));
    if (vpcId != null) {
        req.withFilters(new Filter("vpc-id", Collections.singletonList(vpcId)));
    }
    DescribeSecurityGroupsResult cellGroups = this.client.describeSecurityGroups(req);
    if (cellGroups != null && !cellGroups.getSecurityGroups().isEmpty()) {
        cellGroup = cellGroups.getSecurityGroups().get(0);
    }
    return cellGroup;
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) Filter(com.amazonaws.services.ec2.model.Filter) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)

Example 19 with Vpc

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

the class AWSSecurityGroupClient method getSecurityGroup.

public SecurityGroup getSecurityGroup(String name, String vpcId) {
    SecurityGroup cellGroup = null;
    DescribeSecurityGroupsRequest req = new DescribeSecurityGroupsRequest().withFilters(new Filter("group-name", Collections.singletonList(name)));
    if (vpcId != null) {
        req.withFilters(new Filter("vpc-id", Collections.singletonList(vpcId)));
    }
    DescribeSecurityGroupsResult cellGroups = this.client.describeSecurityGroups(req);
    if (cellGroups != null && !cellGroups.getSecurityGroups().isEmpty()) {
        cellGroup = cellGroups.getSecurityGroups().get(0);
    }
    return cellGroup;
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) Filter(com.amazonaws.services.ec2.model.Filter) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)

Example 20 with Vpc

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

the class TestAWSSetupUtils method setUpTestVpc.

public static void setUpTestVpc(AmazonEC2AsyncClient client, Map<String, Object> awsTestContext, boolean isMock, String zoneId) {
    // If the pre-set VPC does not exist, get the test VPC for the given account and use it in the tests.
    if (!isMock && !vpcIdExists(client, AWS_DEFAULT_VPC_ID)) {
        String vpcId = createorGetVPCForAccount(client);
        awsTestContext.put(VPC_KEY, vpcId);
        Subnet subnet = createOrGetSubnet(client, AWS_DEFAULT_SUBNET_CIDR, vpcId, zoneId);
        awsTestContext.put(SUBNET_KEY, subnet.getSubnetId());
        String internetGatewayId = createOrGetInternetGatewayForGivenVPC(client, vpcId);
        awsTestContext.put(INTERNET_GATEWAY_KEY, internetGatewayId);
        SecurityGroup sg = createOrGetDefaultSecurityGroupForGivenVPC(client, vpcId);
        awsTestContext.put(SECURITY_GROUP_KEY, sg.getGroupId());
        awsTestContext.put(SECURITY_GROUP_NAME_KEY, sg.getGroupName());
        NetSpec network = new NetSpec(vpcId, vpcId, AWS_DEFAULT_VPC_CIDR);
        List<NetSpec> subnets = new ArrayList<>();
        subnets.add(new NetSpec(subnet.getSubnetId(), AWS_DEFAULT_SUBNET_NAME, subnet.getCidrBlock(), zoneId == null ? TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier : zoneId));
        NicSpec nicSpec = NicSpec.create().withSubnetSpec(subnets.get(0)).withDynamicIpAssignment();
        awsTestContext.put(NIC_SPECS_KEY, new AwsNicSpecs(network, Collections.singletonList(nicSpec)));
        return;
    }
    awsTestContext.put(VPC_KEY, AWS_DEFAULT_VPC_ID);
    awsTestContext.put(NIC_SPECS_KEY, SINGLE_NIC_SPEC);
    awsTestContext.put(SUBNET_KEY, AWS_DEFAULT_SUBNET_ID);
    awsTestContext.put(SECURITY_GROUP_KEY, AWS_DEFAULT_GROUP_ID);
    awsTestContext.put(SECURITY_GROUP_NAME_KEY, AWS_DEFAULT_GROUP_NAME);
}
Also used : NetSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NetSpec) ArrayList(java.util.ArrayList) Subnet(com.amazonaws.services.ec2.model.Subnet) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) NicSpec(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs.NicSpec)

Aggregations

Vpc (com.amazonaws.services.ec2.model.Vpc)27 HashMap (java.util.HashMap)25 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)21 Test (org.junit.Test)21 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)19 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)18 ArrayList (java.util.ArrayList)15 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 Filter (com.amazonaws.services.ec2.model.Filter)12 Subnet (com.amazonaws.services.ec2.model.Subnet)10 HashSet (java.util.HashSet)8 RouteTable (com.amazonaws.services.ec2.model.RouteTable)7 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)7