Search in sources :

Example 51 with Vpc

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

the class AWSSecurityGroupClient method createSecurityGroup.

public String createSecurityGroup(String name, String description, String vpcId) {
    CreateSecurityGroupRequest req = new CreateSecurityGroupRequest().withDescription(description).withGroupName(name);
    // set vpc for the security group if provided
    if (vpcId != null) {
        req = req.withVpcId(vpcId);
    }
    CreateSecurityGroupResult result = this.client.createSecurityGroup(req);
    return result.getGroupId();
}
Also used : CreateSecurityGroupResult(com.amazonaws.services.ec2.model.CreateSecurityGroupResult) CreateSecurityGroupRequest(com.amazonaws.services.ec2.model.CreateSecurityGroupRequest)

Example 52 with Vpc

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

the class TestAWSEnumerationTask method testTagEnumeration.

@Test
public void testTagEnumeration() throws Throwable {
    if (this.isMock) {
        return;
    }
    setUpTestVolume(this.host, this.client, this.awsTestContext, this.isMock);
    this.snapshotId = (String) this.awsTestContext.get(TestAWSSetupUtils.SNAPSHOT_KEY);
    this.ebsBlockDevice = new EbsBlockDevice().withSnapshotId(this.snapshotId);
    this.blockDeviceMapping = new BlockDeviceMapping().withDeviceName(BLOCK_DEVICE_NAME).withEbs(this.ebsBlockDevice);
    this.diskId = (String) this.awsTestContext.get(TestAWSSetupUtils.DISK_KEY);
    this.host.log("Running test: " + this.currentTestName.getMethodName());
    // VM tags
    Tag tag1 = new Tag(VM_TAG_KEY_1, VM_TAG_VALUE_1);
    Tag tag2 = new Tag(VM_TAG_KEY_2, VM_TAG_VALUE_2);
    Tag tag3 = new Tag(VM_TAG_KEY_3, VM_TAG_VALUE_3);
    List<Tag> vmTags = Arrays.asList(tag1, tag2, tag3);
    // SG tag
    List<Tag> sgTags = new ArrayList<>();
    sgTags.add(new Tag(INITIAL_SG_TAG, INITIAL_SG_TAG));
    // Network tag
    List<Tag> networkTags = new ArrayList<>();
    networkTags.add(new Tag(INITIAL_VPC_TAG, INITIAL_VPC_TAG));
    // Subnet tag
    List<Tag> subnetTags = new ArrayList<>();
    subnetTags.add(new Tag(INITIAL_SUBNET_TAG, INITIAL_SUBNET_TAG));
    // Disk tag
    List<Tag> diskTags = new ArrayList<>();
    diskTags.add(new Tag(INITIAL_DISK_TAG, INITIAL_DISK_TAG));
    try {
        String linuxVMId1 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
        this.instancesToCleanUp.add(linuxVMId1);
        waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
        // Tag the first VM with a name and add some additional tags
        tagResourcesWithName(this.client, VM_NAME, linuxVMId1);
        List<Tag> linuxVMId1Tags = Arrays.asList(tag1, tag2);
        // tag vm, default SG, VPC, Subnet and Disk
        tagResources(this.client, linuxVMId1Tags, linuxVMId1);
        tagResources(this.client, sgTags, this.securityGroupId);
        tagResources(this.client, networkTags, this.vpcId);
        tagResources(this.client, subnetTags, this.subnetId);
        tagResources(this.client, diskTags, this.diskId);
        enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
        String linuxVMId2 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
        this.instancesToCleanUp.add(linuxVMId2);
        waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
        // Name the second VM and add some tags
        tagResourcesWithName(this.client, VM_UPDATED_NAME, linuxVMId2);
        List<Tag> linuxVMId2Tags = Arrays.asList(tag2, tag3);
        tagResources(this.client, linuxVMId2Tags, linuxVMId2);
        // Un-tag the resources
        unTagResources(this.client, sgTags, this.securityGroupId);
        unTagResources(this.client, networkTags, this.vpcId);
        unTagResources(this.client, subnetTags, this.subnetId);
        unTagResources(this.client, diskTags, this.diskId);
        // re-init tag arrays
        sgTags = new ArrayList<>();
        networkTags = new ArrayList<>();
        subnetTags = new ArrayList<>();
        diskTags = new ArrayList<>();
        // new key-value set remotely should result in a new tag state created locally
        // and a new tag link added to the SecurityGroupState, NetworkState, SubnetState and
        // DiskState
        sgTags.add(new Tag(SECONDARY_SG_TAG, SECONDARY_SG_TAG));
        networkTags.add(new Tag(SECONDARY_VPC_TAG, SECONDARY_VPC_TAG));
        subnetTags.add(new Tag(SECONDARY_SUBNET_TAG, SECONDARY_SUBNET_TAG));
        diskTags.add(new Tag(SECONDARY_DISK_TAG, SECONDARY_DISK_TAG));
        // tag again default SG, VPC, Subnet and Disk
        tagResources(this.client, diskTags, this.diskId);
        tagResources(this.client, sgTags, this.securityGroupId);
        tagResources(this.client, networkTags, this.vpcId);
        tagResources(this.client, subnetTags, this.subnetId);
        enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
        validateComputeName(linuxVMId1, VM_NAME);
        validateComputeName(linuxVMId2, VM_UPDATED_NAME);
        // Validate tag states number
        int allTagsNumber = vmTags.size() + sgTags.size() + networkTags.size() + subnetTags.size() + diskTags.size();
        queryDocumentsAndAssertExpectedCount(this.host, allTagsNumber, TagService.FACTORY_LINK, false);
        ServiceDocumentQueryResult serviceDocumentQueryResult = queryAllFactoryResources(this.host, TagService.FACTORY_LINK);
        Map<String, TagState> tagsMap = new HashMap<>();
        for (Entry<String, Object> entry : serviceDocumentQueryResult.documents.entrySet()) {
            tagsMap.put(entry.getKey(), Utils.fromJson(entry.getValue(), TagState.class));
        }
        // validate security group tags
        Map<String, SecurityGroupState> allSecurityGroupStatesMap = ProvisioningUtils.<SecurityGroupState>getResourceStates(this.host, SecurityGroupService.FACTORY_LINK, SecurityGroupState.class);
        SecurityGroupState defaultSgState = allSecurityGroupStatesMap.get(this.securityGroupId);
        // ensure one link is deleted and one new is added to the sg state. One additional
        // link is an internal tag.
        assertNotNull(defaultSgState.tagLinks);
        assertEquals("Wrong number of security-group tag links found.", 1 + internalTagsCount1, defaultSgState.tagLinks.size());
        // validate vpc tags
        Map<String, NetworkState> allNetworkStatesMap = ProvisioningUtils.<NetworkState>getResourceStates(this.host, NetworkService.FACTORY_LINK, NetworkState.class);
        NetworkState defaultNetworkState = allNetworkStatesMap.get(this.vpcId);
        // ensure one link is deleted and one new is added to the network state. One additional
        // link is an internal tag.
        assertEquals("Wrong number of network tag links found.", 1 + internalTagsCount1, defaultNetworkState.tagLinks.size());
        // validate subnet tags
        Map<String, SubnetState> allSubnetStatesMap = ProvisioningUtils.<SubnetState>getResourceStates(this.host, SubnetService.FACTORY_LINK, SubnetState.class);
        SubnetState defaultSubnetState = allSubnetStatesMap.get(this.subnetId);
        // ensure one link is deleted and one new is added to the subnet state. One additional
        // link is an internal tag.
        assertEquals("Wrong number of subnet tag links found.", 1 + internalTagsCount1, defaultSubnetState.tagLinks.size());
        // validate disk tags
        Map<String, DiskState> allDiskStatesMap = ProvisioningUtils.<DiskState>getResourceStates(this.host, DiskService.FACTORY_LINK, DiskState.class);
        DiskState defaultDiskState = allDiskStatesMap.get(this.diskId);
        // ensure one link is deleted and one new is added to the disk state
        assertEquals("Wrong number of disk tag links found.", 1 + internalTagsCount1, defaultDiskState.tagLinks.size());
        // ensure EBS disk has an internal type tag set
        assertTrue(defaultDiskState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ebs_block.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
        // validate vm tags
        Map<Tag, String> vmTagLinks = new HashMap<>();
        for (Tag tag : vmTags) {
            for (TagState tagState : tagsMap.values()) {
                if (tagState.key.equals(tag.getKey())) {
                    vmTagLinks.put(tag, tagState.documentSelfLink);
                }
            }
        }
        ComputeState linuxVMId1ComputeState = getComputeByAWSId(this.host, linuxVMId1);
        // compute has 2 remote tags + 1 local tag
        assertEquals(linuxVMId1Tags.size() + internalTagsCount1, linuxVMId1ComputeState.tagLinks.size());
        for (Tag tag : linuxVMId1Tags) {
            assertTrue(linuxVMId1ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
        }
        ComputeState linuxVMId2ComputeState = getComputeByAWSId(this.host, linuxVMId2);
        assertEquals(linuxVMId2Tags.size() + internalTagsCount1, linuxVMId2ComputeState.tagLinks.size());
        for (Tag tag : linuxVMId2Tags) {
            assertTrue(linuxVMId2ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
        }
    } catch (Throwable t) {
        this.host.log("Exception occurred during test execution: %s", t.getMessage());
        if (t instanceof AssertionError) {
            fail("Assert exception occurred during test execution: " + t.getMessage());
        }
    } finally {
        // un-tag default SG
        unTagResources(this.client, sgTags, this.securityGroupId);
        // un-tag default VPC
        unTagResources(this.client, networkTags, this.vpcId);
        // un-tag default Subnet
        unTagResources(this.client, subnetTags, this.subnetId);
        // un-tag default Disk
        unTagResources(this.client, diskTags, this.diskId);
        tearDownTestDisk(this.client, this.host, this.awsTestContext, this.isMock);
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) HashMap(java.util.HashMap) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) TestAWSSetupUtils.deleteVMsOnThisEndpoint(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteVMsOnThisEndpoint) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) Tag(com.amazonaws.services.ec2.model.Tag) Test(org.junit.Test)

Example 53 with Vpc

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

the class TestAWSNetworkService method testEnvironmentCreation.

/*
     * Test covers the necessary elements for a successful environment creation
     * These environmental elements are necessary before any VM instances can be
     * created
     *
     * - Internet Gateway
     * - VPC
     * - Subnet
     * - Route to IG
     *
     */
@Test
public void testEnvironmentCreation() throws Throwable {
    boolean attached = false;
    String gatewayID = this.netClient.createInternetGateway();
    assertTrue(gatewayID != null);
    String vpcID = this.netClient.createVPC(AWS_DEFAULT_SUBNET_CIDR);
    assertTrue(vpcID != null);
    String subnetID = this.netClient.createSubnet(AWS_DEFAULT_SUBNET_CIDR, vpcID).getSubnetId();
    this.netClient.attachInternetGateway(vpcID, gatewayID);
    InternetGateway gw = this.netClient.getInternetGateway(gatewayID);
    List<InternetGatewayAttachment> attachments = gw.getAttachments();
    // ensure we are attached to newly created vpc
    for (InternetGatewayAttachment attachment : attachments) {
        if (attachment.getVpcId().equalsIgnoreCase(vpcID)) {
            attached = true;
            break;
        }
    }
    assertTrue(attached);
    RouteTable routeTable = this.netClient.getMainRouteTable(vpcID);
    this.netClient.createInternetRoute(gatewayID, routeTable.getRouteTableId(), "0.0.0.0/0");
    // remove resources
    this.netClient.detachInternetGateway(vpcID, gatewayID);
    this.netClient.deleteInternetGateway(gatewayID);
    this.netClient.deleteSubnet(subnetID);
    this.netClient.deleteVPC(vpcID);
}
Also used : RouteTable(com.amazonaws.services.ec2.model.RouteTable) InternetGateway(com.amazonaws.services.ec2.model.InternetGateway) InternetGatewayAttachment(com.amazonaws.services.ec2.model.InternetGatewayAttachment) Test(org.junit.Test)

Example 54 with Vpc

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

the class AWSInstanceContext method getVPCs.

/**
 * For every NIC lookup associated AWS VPC as specified by
 * {@code AWSNicContext.networkState.id}. If any of the VPCs is not found then complete with an
 * exception.
 */
private DeferredResult<AWSInstanceContext> getVPCs(AWSInstanceContext context) {
    if (context.nics.isEmpty()) {
        return DeferredResult.completed(context);
    }
    List<DeferredResult<DescribeVpcsResult>> getVpcDRs = new ArrayList<>();
    for (AWSNicContext nicCtx : context.nics) {
        DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList(nicCtx.networkState.id)));
        String msg = "Getting AWS VPC [" + nicCtx.networkState.id + "/" + nicCtx.networkState.name + "/" + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.child.name + "] VM";
        AWSDeferredResultAsyncHandler<DescribeVpcsRequest, DescribeVpcsResult> handler = new AWSDeferredResultAsyncHandler<DescribeVpcsRequest, DescribeVpcsResult>(this.service, msg) {

            @Override
            protected DeferredResult<DescribeVpcsResult> consumeSuccess(DescribeVpcsRequest request, DescribeVpcsResult result) {
                if (result.getVpcs().isEmpty()) {
                    String msg = String.format("VPC with [%s] id is not found in AWS for [%s] NIC of [%s] VM.", nicCtx.networkState.id, nicCtx.nicStateWithDesc.name, context.child.name);
                    return DeferredResult.failed(new IllegalStateException(msg));
                }
                nicCtx.vpc = result.getVpcs().get(0);
                return DeferredResult.completed(result);
            }
        };
        context.amazonEC2Client.describeVpcsAsync(vpcRequest, handler);
        getVpcDRs.add(handler.toDeferredResult());
    }
    return DeferredResult.allOf(getVpcDRs).handle((all, exc) -> {
        if (exc != null) {
            String msg = String.format("Error getting VPCs from AWS for [%s] VM.", context.child.name);
            throw new IllegalStateException(msg, exc);
        }
        return context;
    });
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Filter(com.amazonaws.services.ec2.model.Filter) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) ArrayList(java.util.ArrayList) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 55 with Vpc

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

the class TestAWSSetupUtils method getEC2InstanceIdsAssociatedWithVpcId.

/**
 * Get a list of all EC2 instance ids associated with a given VPC id.
 */
public static List<String> getEC2InstanceIdsAssociatedWithVpcId(AmazonEC2AsyncClient client, String vpcId) {
    DescribeInstancesRequest req = new DescribeInstancesRequest();
    if (vpcId != null) {
        req.withFilters(new Filter(AWS_VPC_ID_FILTER, Collections.singletonList(vpcId)));
    }
    DescribeInstancesResult instancesResult = client.describeInstances(req);
    return instancesResult == null ? Collections.emptyList() : instancesResult.getReservations().get(0).getInstances().stream().map(instance -> instance.getInstanceId()).collect(Collectors.toList());
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

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