Search in sources :

Example 1 with TagState

use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method createTags.

/**
 * POSTs all tags for newly discovered instances. Even if some tags already exist we rely on
 * IDEMPOTENT_POST behaviour and POST them again. All tags that got created successfully are
 * stored in createdExternalTags list.
 */
private void createTags(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
    // Get all tags from the instances to be created
    Set<Tag> create = context.request.instancesToBeCreated.stream().flatMap(i -> i.getTags().stream()).collect(Collectors.toSet());
    // Put them in a set to remove the duplicates
    Set<Tag> allTags = new HashSet<>();
    allTags.addAll(create);
    // 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<>();
    allTags.stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey())).forEach(t -> {
        TagState tagState = newTagState(t.getKey(), t.getValue(), true, context.request.tenantLinks);
        Operation op = Operation.createPost(this, TagService.FACTORY_LINK).setBody(tagState);
        operations.add(op);
        tagsCreationOperationIdsMap.put(op.getId(), t);
    });
    if (operations.isEmpty()) {
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
    } else {
        OperationJoin.create(operations).setCompletion((ops, exs) -> {
            if (exs != null && !exs.isEmpty()) {
                logSevere(() -> String.format("Error creating %s external tags for compute" + "states: %s", exs.size(), Utils.toString(exs.get(0))));
            }
            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.creationStage = next;
            handleComputeStateCreateOrUpdate(context);
        }).sendWith(this);
    }
}
Also used : AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) QueryTask(com.vmware.xenon.services.common.QueryTask) InstanceDescKey(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.InstanceDescKey) AWSEnumerationUtils(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils) StringUtils(org.apache.commons.lang3.StringUtils) AWSEnumerationUtils.getKeyForComputeDescriptionFromCD(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getKeyForComputeDescriptionFromCD) Utils(com.vmware.xenon.common.Utils) Map(java.util.Map) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) 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) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) UUID(java.util.UUID) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) TagService(com.vmware.photon.controller.model.resources.TagService) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) AWSResourceType.ec2_instance(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_instance) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AWSResourceType.ec2_net_interface(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType.ec2_net_interface) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getRepresentativeListOfCDsFromInstanceList) BiConsumer(java.util.function.BiConsumer) Instance(com.amazonaws.services.ec2.model.Instance) AWSSecurityGroupEnumerationResponse(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSSecurityGroupEnumerationAdapterService.AWSSecurityGroupEnumerationResponse) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) AWSNetworkEnumerationResponse(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkEnumerationResponse) TimeUnit(java.util.concurrent.TimeUnit) AWSEnumerationUtils.getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Collections(java.util.Collections) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Tag(com.amazonaws.services.ec2.model.Tag) AdapterUtils.createDeleteOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createDeleteOperation) 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) HashSet(java.util.HashSet)

Example 2 with TagState

use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.

the class TestAWSProvisionTask method testProvision.

// Creates a AWS instance via a provision task.
@Test
public void testProvision() throws Throwable {
    initResourcePoolAndComputeHost();
    // create a AWS VM compute resoruce
    boolean addNonExistingSecurityGroup = true;
    this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm1", zoneId, regionId, null, /* tagLinks */
    this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
    // set placement link
    String zoneId = TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier;
    ComputeState zoneComputeState = createAWSComputeHost(this.host, this.endpointState, zoneId, regionId, this.isAwsClientMock, this.awsMockEndpointReference, null);
    zoneComputeState.id = zoneId;
    zoneComputeState = TestUtils.doPatch(this.host, zoneComputeState, ComputeState.class, UriUtils.buildUri(this.host, zoneComputeState.documentSelfLink));
    if (this.vmState.customProperties == null) {
        this.vmState.customProperties = new HashMap<>();
    }
    this.vmState.customProperties.put(PLACEMENT_LINK, zoneComputeState.documentSelfLink);
    TestUtils.doPatch(this.host, this.vmState, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
    // kick off a provision task to do the actual VM creation
    ProvisionComputeTaskState provisionTask = new ProvisionComputeTaskService.ProvisionComputeTaskState();
    provisionTask.computeLink = this.vmState.documentSelfLink;
    provisionTask.isMockRequest = this.isMock;
    provisionTask.taskSubStage = ProvisionComputeTaskState.SubStage.CREATING_HOST;
    // Wait for default request timeout in minutes for the machine to be powered ON before
    // reporting failure to the parent task.
    provisionTask.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.MINUTES.toMicros(AWS_VM_REQUEST_TIMEOUT_MINUTES);
    provisionTask.tenantLinks = this.endpointState.tenantLinks;
    provisionTask = TestUtils.doPost(this.host, provisionTask, ProvisionComputeTaskState.class, UriUtils.buildUri(this.host, ProvisionComputeTaskService.FACTORY_LINK));
    this.host.waitForFinishedTask(ProvisionComputeTaskState.class, provisionTask.documentSelfLink);
    // check that the VM has been created
    ProvisioningUtils.queryComputeInstances(this.host, 3);
    if (!this.isMock) {
        ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
        List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
        Instance instance = instances.get(0);
        assertTags(Collections.emptySet(), instance, this.vmState.name);
        assertVmNetworksConfiguration(instance);
        assertStorageConfiguration(this.client, instance, compute);
        assertEquals(zoneId, instance.getPlacement().getAvailabilityZone());
    }
    this.host.setTimeoutSeconds(600);
    this.host.waitFor("Error waiting for stats with default collection windows", () -> {
        try {
            this.host.log(Level.INFO, "Issuing stats request for VM with default collection window.");
            issueStatsRequest(this.vmState, null);
        } catch (Throwable t) {
            return false;
        }
        return true;
    });
    // store the network links and disk links for removal check later
    List<String> resourcesToDelete = new ArrayList<>();
    if (this.vmState.diskLinks != null) {
        resourcesToDelete.addAll(this.vmState.diskLinks);
    }
    if (this.vmState.networkInterfaceLinks != null) {
        resourcesToDelete.addAll(this.vmState.networkInterfaceLinks);
    }
    // delete vm
    TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host);
    // validates the local documents of network links and disk links have been removed
    verifyRemovalOfResourceState(this.host, resourcesToDelete);
    // create another AWS VM
    List<String> instanceIdList = new ArrayList<>();
    Set<TagState> tags = createTags(null, "testProvisionKey1", "testProvisionValue1", "testProvisionKey2", "testProvisionValue2");
    Set<String> tagLinks = tags.stream().map(t -> t.documentSelfLink).collect(Collectors.toSet());
    addNonExistingSecurityGroup = false;
    this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm2", TestAWSSetupUtils.zoneId, regionId, tagLinks, this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
    TestAWSSetupUtils.provisionMachine(this.host, this.vmState, this.isMock, instanceIdList);
    if (!this.isMock) {
        ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
        List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
        assertTags(tags, instances.get(0), this.vmState.name);
        assertVmNetworksConfiguration(instances.get(0));
        assertStorageConfiguration(this.client, instances.get(0), compute);
        // reach out to AWS and get the current state
        TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
    }
    // delete just the local representation of the resource
    TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host, true);
    if (!this.isMock) {
        try {
            TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
        } finally {
            TestAWSSetupUtils.deleteVMsUsingEC2Client(this.client, this.host, instanceIdList);
            deleteSecurityGroupUsingEC2Client(this.client, this.host, this.sgToCleanUp);
        }
    }
    this.vmState = null;
    this.sgToCleanUp = null;
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) TestAWSSetupUtils.verifyRemovalOfResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.verifyRemovalOfResourceState) Map(java.util.Map) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) After(org.junit.After) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) Collector(java.util.stream.Collector) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) AwsNicSpecs(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) AWS_VM_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) UUID(java.util.UUID) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Collectors(java.util.stream.Collectors) VOLUME_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.VOLUME_TYPE) TestAWSSetupUtils.avalabilityZoneIdentifier(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.avalabilityZoneIdentifier) TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client) List(java.util.List) TagService(com.vmware.photon.controller.model.resources.TagService) Tag(com.amazonaws.services.ec2.model.Tag) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) DiskService(com.vmware.photon.controller.model.resources.DiskService) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client) DISK_IOPS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DISK_IOPS) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) TestAWSSetupUtils.zoneId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.zoneId) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) TestAWSSetupUtils.getAwsInstancesByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) PLACEMENT_LINK(com.vmware.photon.controller.model.ComputeProperties.PLACEMENT_LINK) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) DEVICE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_TYPE) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) TestAWSSetupUtils.getCompute(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getCompute) Test(org.junit.Test) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) TimeUnit(java.util.concurrent.TimeUnit) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Test(org.junit.Test)

Example 3 with TagState

use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.

the class TestAWSProvisionTask method assertTags.

private void assertTags(Set<TagState> expectedTagStates, Instance instance, String instanceName) {
    Set<Tag> expectedTags = expectedTagStates.stream().map(ts -> new Tag(ts.key, ts.value)).collect(Collectors.toSet());
    Set<Tag> actualTags = new HashSet<>(instance.getTags());
    // account for the name tag
    assertEquals(expectedTags.size() + 1, actualTags.size());
    assertTrue(actualTags.containsAll(expectedTags));
    Tag nameTag = new Tag(AWSConstants.AWS_TAG_NAME, instanceName);
    assertTrue(actualTags.contains(nameTag));
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) TestAWSSetupUtils.createAWSComputeHost(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSComputeHost) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.createAWSResourcePool(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSResourcePool) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) DEVICE_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_NAME) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) AWSBlockDeviceNameMapper(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSBlockDeviceNameMapper) TestAWSSetupUtils.verifyRemovalOfResourceState(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.verifyRemovalOfResourceState) Map(java.util.Map) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) After(org.junit.After) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) Collector(java.util.stream.Collector) ProvisioningUtils(com.vmware.photon.controller.model.tasks.ProvisioningUtils) AwsNicSpecs(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) AWS_VM_REQUEST_TIMEOUT_MINUTES(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AWS_VM_REQUEST_TIMEOUT_MINUTES) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) UUID(java.util.UUID) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) Collectors(java.util.stream.Collectors) VOLUME_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.VOLUME_TYPE) TestAWSSetupUtils.avalabilityZoneIdentifier(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.avalabilityZoneIdentifier) TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getSecurityGroupsIdUsingEC2Client) List(java.util.List) TagService(com.vmware.photon.controller.model.resources.TagService) Tag(com.amazonaws.services.ec2.model.Tag) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) DiskService(com.vmware.photon.controller.model.resources.DiskService) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) InstanceNetworkInterface(com.amazonaws.services.ec2.model.InstanceNetworkInterface) TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.deleteSecurityGroupUsingEC2Client) DISK_IOPS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DISK_IOPS) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) TestAWSSetupUtils.zoneId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.zoneId) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) TestAWSSetupUtils.getAwsInstancesByIds(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getAwsInstancesByIds) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) PLACEMENT_LINK(com.vmware.photon.controller.model.ComputeProperties.PLACEMENT_LINK) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) DEVICE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.DEVICE_TYPE) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) TestAWSSetupUtils.createAWSAuthentication(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSAuthentication) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) TestAWSSetupUtils.getCompute(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.getCompute) Test(org.junit.Test) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) TimeUnit(java.util.concurrent.TimeUnit) TestAWSSetupUtils.createAWSVMResource(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.createAWSVMResource) Rule(org.junit.Rule) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) TestAWSSetupUtils.regionId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.regionId) TestAWSSetupUtils.setAwsClientMockInfo(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setAwsClientMockInfo) IpPermission(com.amazonaws.services.ec2.model.IpPermission) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) Tag(com.amazonaws.services.ec2.model.Tag) HashSet(java.util.HashSet)

Example 4 with TagState

use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method createNetworkInternalTagStates.

/**
 * Create internal tag states for networks.
 */
private void createNetworkInternalTagStates(NetworkEnumContext context, NetworkEnumStages next) {
    TagState internalTypeTag = newTagState(PhotonModelConstants.TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE, false, context.parentCompute.tenantLinks);
    // operation to create tag "type" for subnets.
    Operation.createPost(this, TagService.FACTORY_LINK).setBody(internalTypeTag).setCompletion((o, e) -> {
        if (e != null) {
            logSevere("Error creating internal type tag for networks %s", e.getMessage());
        } else {
            context.networkInternalTagsMap.put(PhotonModelConstants.TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE);
            context.networkInternalTagLinksSet.add(internalTypeTag.documentSelfLink);
        }
        handleSubStage(context, next);
    }).sendWith(this);
}
Also used : QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) LifecycleState(com.vmware.photon.controller.model.support.LifecycleState) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_INSTANCE_ADAPTER_REFERENCE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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) NETWORK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) Stream(java.util.stream.Stream) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) AddressSpace(com.vmware.photon.controller.model.adapters.azure.model.network.AddressSpace) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Subnet(com.vmware.photon.controller.model.adapters.azure.model.network.Subnet) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) TagState(com.vmware.photon.controller.model.resources.TagService.TagState)

Example 5 with TagState

use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.

the class ResourceUtilsTest method testExpandTags.

@Test
public void testExpandTags() throws Throwable {
    TagState tag1 = new TagState();
    tag1.key = "A";
    tag1.value = "1";
    tag1 = postServiceSynchronously(TagService.FACTORY_LINK, tag1, TagState.class);
    TagState tag2 = new TagState();
    tag2.key = "A";
    tag2.value = "2";
    tag2 = postServiceSynchronously(TagService.FACTORY_LINK, tag2, TagState.class);
    TagState tag3 = new TagState();
    tag3.key = "A";
    tag3.value = "3";
    tag3 = postServiceSynchronously(TagService.FACTORY_LINK, tag3, TagState.class);
    // validate expansion on POST
    ComputeState compute = new ComputeState();
    compute.descriptionLink = "cdLink";
    compute.tagLinks = new HashSet<>();
    compute.tagLinks.add(tag1.documentSelfLink);
    compute.tagLinks.add(tag2.documentSelfLink);
    compute = postServiceSynchronously(ComputeService.FACTORY_LINK, compute, ComputeState.class);
    Collection<String> tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
    // validate tags cannot be modified directly
    compute.expandedTags.remove(1);
    assertEquals(1, compute.expandedTags.size());
    putServiceSynchronously(compute.documentSelfLink, compute);
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
    // validate expansion on PUT
    compute.tagLinks.remove(tag2.documentSelfLink);
    compute.tagLinks.add(tag3.documentSelfLink);
    putServiceSynchronously(compute.documentSelfLink, compute);
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(2, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n3")));
    // validate expansion on PATCH
    ComputeState patchState = new ComputeState();
    patchState.tagLinks = new HashSet<>();
    patchState.tagLinks.add(tag2.documentSelfLink);
    compute = patchServiceSynchronously(compute.documentSelfLink, patchState, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(3, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2", "A\n3")));
    // validate expansion through custom PATCH body
    Map<String, Collection<Object>> itemsToRemove = new HashMap<>();
    itemsToRemove.put(ResourceState.FIELD_NAME_TAG_LINKS, Arrays.asList(tag2.documentSelfLink, tag3.documentSelfLink));
    patchServiceSynchronously(compute.documentSelfLink, ServiceStateCollectionUpdateRequest.create(null, itemsToRemove));
    compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
    tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
    assertEquals(1, tags.size());
    assertTrue(tags.containsAll(Arrays.asList("A\n1")));
    // validate query (case-insensitive) (Note: only 1 tag can be found with Xenon 1.6.1)
    Query tagQuery = Query.Builder.create().addFieldClause(TagInfo.COMPOSITE_FIELD_NAME_TAG, "a*", MatchType.WILDCARD).build();
    QueryTask tagQueryTask = QueryTask.Builder.createDirectTask().setQuery(tagQuery).addOption(QueryOption.EXPAND_CONTENT).build();
    tagQueryTask = postServiceSynchronously(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, tagQueryTask, QueryTask.class);
    assertEquals(1, tagQueryTask.results.documentLinks.size());
    assertEquals(1, tagQueryTask.results.documents.size());
    assertEquals(compute.documentSelfLink, tagQueryTask.results.documentLinks.get(0));
    ComputeState foundCompute = Utils.fromJson(tagQueryTask.results.documents.values().iterator().next(), ComputeState.class);
    assertEquals(1, foundCompute.expandedTags.size());
    assertEquals("A\n1", foundCompute.expandedTags.get(0).tag);
}
Also used : Arrays(java.util.Arrays) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) ArrayList(java.util.ArrayList) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) Utils(com.vmware.xenon.common.Utils) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Assert.assertNull(org.junit.Assert.assertNull) TagInfo(com.vmware.photon.controller.model.resources.ResourceState.TagInfo) UriUtils(com.vmware.xenon.common.UriUtils) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Assert.assertEquals(org.junit.Assert.assertEquals) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) HashMap(java.util.HashMap) Collection(java.util.Collection) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Aggregations

TagState (com.vmware.photon.controller.model.resources.TagService.TagState)31 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)18 Operation (com.vmware.xenon.common.Operation)15 Query (com.vmware.xenon.services.common.QueryTask.Query)15 HashSet (java.util.HashSet)14 Map (java.util.Map)14 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)13 Utils (com.vmware.xenon.common.Utils)13 TagService (com.vmware.photon.controller.model.resources.TagService)12 QueryTask (com.vmware.xenon.services.common.QueryTask)12 List (java.util.List)12 Set (java.util.Set)12 StatelessService (com.vmware.xenon.common.StatelessService)11 Collectors (java.util.stream.Collectors)11 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)10 UriUtils (com.vmware.xenon.common.UriUtils)10 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)8 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)8 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)8