Search in sources :

Example 56 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class TestAWSEnumerationTask method validateVPCInformation.

/**
 * Validates that the VPC information discovered from AWS has all the desired set of fields and
 * the association between a compute state and a network state is established correctly.
 *
 * @throws Throwable
 */
private void validateVPCInformation(String vpCId) throws Throwable {
    if (this.isAwsClientMock) {
        return;
    }
    // Get the network state that maps to this VPCID. Right now the id field of the network
    // state is set to the VPC ID, so querying the network state based on that.
    Map<String, NetworkState> networkStateMap = ProvisioningUtils.<NetworkState>getResourceStates(this.host, NetworkService.FACTORY_LINK, NetworkState.class);
    assertNotNull(networkStateMap);
    NetworkState networkState = networkStateMap.get(vpCId);
    // The network state for the VPC id of the VM should not be null
    assertNotNull(networkState);
    assertNotNull(networkState.subnetCIDR);
    assertNotNull(networkState.instanceAdapterReference);
    // This is assuming that the internet gateway is attached to the VPC by default
    assertNotNull(networkState.customProperties.get(AWS_GATEWAY_ID));
    assertNotNull(networkState.customProperties.get(AWS_VPC_ROUTE_TABLE_ID));
    assertNotNull(networkState.tagLinks);
    assertTrue(networkState.tagLinks.size() > 0);
    assertTrue(networkState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ec2_vpc.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
    List<SubnetState> subnetStates = getSubnetStates(this.host, networkState);
    assertFalse(subnetStates.isEmpty());
    subnetStates.stream().forEach(subnetState -> {
        assertNotNull(subnetState.subnetCIDR);
        assertNotNull(subnetState.zoneId);
        assertNotNull(subnetState.tagLinks);
        assertTrue(subnetState.tagLinks.size() > 0);
        assertTrue(subnetState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ec2_subnet.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
    });
}
Also used : NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 57 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState 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 58 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class TestProvisionAWSNetwork method validateAWSArtifacts.

private void validateAWSArtifacts(String networkDescriptionLink, AuthCredentialsServiceState creds) throws Throwable {
    NetworkState net = getNetworkState(networkDescriptionLink);
    AmazonEC2AsyncClient client = AWSUtils.getAsyncClient(creds, regionId, getExecutor());
    AWSNetworkClient netSVC = new AWSNetworkClient(client);
    // if any artifact is not present then an error will be thrown
    assertNotNull(netSVC.getVPC(net.customProperties.get(AWS_VPC_ID)));
    assertNotNull(netSVC.getInternetGateway(net.customProperties.get(AWS_GATEWAY_ID)));
    List<SubnetState> subnetStates = TestUtils.getSubnetStates(this.host, net);
    assertEquals(1, subnetStates.size());
    assertNotNull(netSVC.getSubnet(subnetStates.get(0).id));
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 59 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class AWSLoadBalancerService method populateVpcIdFromSubnet.

private DeferredResult<AWSLoadBalancerContext> populateVpcIdFromSubnet(AWSLoadBalancerContext context) {
    SubnetState subnetState = context.loadBalancerStateExpanded.subnets.stream().findFirst().orElse(null);
    if (subnetState == null) {
        return DeferredResult.completed(context);
    }
    Operation get = Operation.createGet(UriUtils.buildUri(getHost(), subnetState.networkLink));
    return this.sendWithDeferredResult(get, NetworkState.class).thenApply(networkState -> {
        context.vpcId = networkState.id;
        return context;
    });
}
Also used : Operation(com.vmware.xenon.common.Operation) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 60 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState 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)

Aggregations

SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)61 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)22 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)16 Operation (com.vmware.xenon.common.Operation)14 Query (com.vmware.xenon.services.common.QueryTask.Query)12 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)11 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)11 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)11 URI (java.net.URI)11 DeferredResult (com.vmware.xenon.common.DeferredResult)10 HashMap (java.util.HashMap)10 List (java.util.List)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 StatelessService (com.vmware.xenon.common.StatelessService)9 SubnetService (com.vmware.photon.controller.model.resources.SubnetService)8 UriUtils (com.vmware.xenon.common.UriUtils)8 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)7 NetworkService (com.vmware.photon.controller.model.resources.NetworkService)7 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)7