Search in sources :

Example 1 with ImageState

use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.

the class TestVSphereImageEnumerationTask method testRefresh.

@Test
public void testRefresh() throws Throwable {
    // Create a resource pool where the VM will be housed
    this.resourcePool = createResourcePool();
    this.auth = createAuth();
    this.computeHostDescription = createComputeDescription();
    this.computeHost = createComputeHost(this.computeHostDescription);
    EndpointState ep = createEndpointState(this.computeHost, this.computeHostDescription);
    this.endpoint = TestUtils.doPost(this.host, ep, EndpointState.class, UriUtils.buildUri(this.host, EndpointService.FACTORY_LINK));
    doRefresh();
    captureFactoryState("initial");
    Query q = Query.Builder.create().addKindFieldClause(ImageState.class).build();
    ImageState anImage = null;
    try {
        anImage = findFirstMatching(q, ImageState.class);
    } catch (Exception ignore) {
    }
    if (anImage != null) {
        assertFalse(anImage.tenantLinks.isEmpty());
    }
    doRefresh();
    captureFactoryState("updated");
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Query(com.vmware.xenon.services.common.QueryTask.Query) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) Test(org.junit.Test)

Example 2 with ImageState

use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.

the class ImageEnumerationTaskService method deletePublicImagesByEndpointType.

private DeferredResult<SendImageEnumerationAdapterContext> deletePublicImagesByEndpointType(SendImageEnumerationAdapterContext ctx) {
    QueryByPages<ImageState> queryAll = new QueryByPages<ImageState>(getHost(), publicImagesByEndpointTypeQuery(ctx).build(), ImageState.class, null).setQueryTaskTenantLinks(ctx.taskState.tenantLinks);
    queryAll.setMaxPageSize(QueryUtils.DEFAULT_MAX_RESULT_LIMIT);
    queryAll.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
    final List<DeferredResult<Operation>> deleteDRs = new ArrayList<>();
    queryAll.queryLinks(imageLink -> {
        Operation delOp = Operation.createDelete(UriUtils.buildUri(getHost(), imageLink));
        DeferredResult<Operation> delDR = sendWithDeferredResult(delOp).whenComplete((op, e) -> {
            final String msg = "Deleting '%s' public image state [%s]";
            if (e != null) {
                // Be tolerant on individual images delete
                log(Level.WARNING, () -> String.format(msg + ": FAILED with %s", ctx.taskState.endpointType, imageLink, Utils.toString(e)));
            } else {
                log(Level.FINEST, () -> String.format(msg + ": SUCCESS", ctx.taskState.endpointType, imageLink));
            }
        });
        deleteDRs.add(delDR);
    });
    return DeferredResult.allOf(deleteDRs).thenApply(ignore -> ctx);
}
Also used : ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 3 with ImageState

use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.

the class ResourceGroomerTaskService method populateEndpointLinksByDocumentLinks.

/**
 * Helper for creating corresponding object and parsing the response for given documentKind
 * to store selfLink and endpointLink.
 */
private static void populateEndpointLinksByDocumentLinks(Map<String, Object> documents, Map<String, Set<String>> endpointLinksByDocumentLinks, Map<String, String> endpointLinkByDocumentLinks) {
    for (Object document : documents.values()) {
        ServiceDocument doc = Utils.fromJson(document, ServiceDocument.class);
        Set<String> endpointLinks = new HashSet<>();
        if (doc.documentKind.equals(COMPUTE_STATE_DOCUMENT_KIND)) {
            ComputeState state = Utils.fromJson(document, ComputeState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(DISK_STATE_DOCUMENT_KIND)) {
            DiskState state = Utils.fromJson(document, DiskState.class);
            if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
                // skip resources that have never been attached to a particular endpoint
                continue;
            }
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(COMPUTE_DESCRIPTION_DOCUMENT_KIND)) {
            ComputeDescription state = Utils.fromJson(document, ComputeDescription.class);
            // only deleting discovered resources
            if (!(state.customProperties != null && (ResourceEnumerationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK)) || EndpointAllocationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK))))) {
                continue;
            }
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(NETWORK_STATE_DOCUMENT_KIND)) {
            NetworkState state = Utils.fromJson(document, NetworkState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(NETWORK_INTERFACE_STATE_DOCUMENT_KIND)) {
            NetworkInterfaceState state = Utils.fromJson(document, NetworkInterfaceState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(SECURITY_GROUP_STATE_DOCUMENT_KIND)) {
            SecurityGroupState state = Utils.fromJson(document, SecurityGroupState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(SUBNET_STATE_DOCUMENT_KIND)) {
            SubnetState state = Utils.fromJson(document, SubnetState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(LOAD_BALANCER_DOCUMENT_KIND)) {
            LoadBalancerState state = Utils.fromJson(document, LoadBalancerState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(STORAGE_DESCRIPTION_DOCUMENT_KIND)) {
            StorageDescription state = Utils.fromJson(document, StorageDescription.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(RESOURCE_GROUP_DOCUMENT_KIND)) {
            ResourceGroupState state = Utils.fromJson(document, ResourceGroupState.class);
            if (state.customProperties != null && state.customProperties.containsKey(ResourceGroupService.PROPERTY_NAME_IS_USER_CREATED)) {
                continue;
            }
            if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
                // skip resources that have never been attached to a particular endpoint
                continue;
            }
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(IMAGE_STATE_KIND)) {
            ImageState state = Utils.fromJson(document, ImageState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(ROUTER_STATE_KIND)) {
            RouterState state = Utils.fromJson(document, RouterState.class);
            if (state.endpointLinks != null) {
                state.endpointLinks.remove(null);
                endpointLinks.addAll(state.endpointLinks);
            }
            endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
            endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
        } else if (doc.documentKind.equals(AUTH_CREDENTIALS_SERVICE_STATE_KIND)) {
            AuthCredentialsServiceState state = Utils.fromJson(document, AuthCredentialsServiceState.class);
            if (state.customProperties != null && state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK) != null) {
                endpointLinkByDocumentLinks.put(state.documentSelfLink, state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK));
            } else {
                endpointLinkByDocumentLinks.put(state.documentSelfLink, EMPTY_STRING);
            }
        }
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) RouterState(com.vmware.photon.controller.model.resources.RouterService.RouterState) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) LoadBalancerState(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) HashSet(java.util.HashSet) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState)

Example 4 with ImageState

use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.

the class EndpointRemovalTaskServiceTest method createPrivateImageState.

private static void createPrivateImageState(BaseModelTest test, String endpointLink, List<String> tenantLinks) throws Throwable {
    ImageState image = new ImageState();
    image.name = "disk";
    image.tenantLinks = tenantLinks;
    image.endpointLink = endpointLink;
    image.endpointLinks = new HashSet<String>();
    image.endpointLinks.add(endpointLink);
    test.postServiceSynchronously(ImageService.FACTORY_LINK, image, ImageState.class);
}
Also used : ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState)

Example 5 with ImageState

use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.

the class TestAzureImageEnumerationTask method createImageState.

/**
 * @param endpoint
 *            the end-point of the image to create
 * @param epRegion
 *            a flag indicating whether to create the image in the region of the end-point or in
 *            a different region
 * @param isPublic
 *            a flag indicating whether to create public or private image
 * @return the actual image created
 */
private ImageState createImageState(EndpointState endpoint, boolean epRegion, boolean isPublic) throws Throwable {
    ImageState image = new ImageState();
    if (isPublic == PUBLIC) {
        image.endpointType = endpoint.endpointType;
    } else {
        image.endpointLink = endpoint.documentSelfLink;
        image.tenantLinks = endpoint.tenantLinks;
    }
    image.endpointLinks = new HashSet<>();
    image.endpointLinks.add(endpoint.documentSelfLink);
    image.id = "dummy-" + this.currentTestName.getMethodName();
    image.regionId = epRegion ? ENDPOINT_REGION : ENDPOINT_REGION + "_diff";
    return postServiceSynchronously(ImageService.FACTORY_LINK, image, ImageState.class);
}
Also used : ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState)

Aggregations

ImageState (com.vmware.photon.controller.model.resources.ImageService.ImageState)31 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)12 Test (org.junit.Test)12 List (java.util.List)9 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)7 HashSet (java.util.HashSet)7 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)6 QueryTop (com.vmware.photon.controller.model.query.QueryUtils.QueryTop)6 DiskConfiguration (com.vmware.photon.controller.model.resources.ImageService.ImageState.DiskConfiguration)6 ImageEnumerationTaskState (com.vmware.photon.controller.model.tasks.ImageEnumerationTaskService.ImageEnumerationTaskState)6 AzureBaseTest (com.vmware.photon.controller.model.adapters.azure.base.AzureBaseTest)5 BaseModelTest (com.vmware.photon.controller.model.helpers.BaseModelTest)5 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)5 ImageService (com.vmware.photon.controller.model.resources.ImageService)5 Operation (com.vmware.xenon.common.Operation)5 Utils (com.vmware.xenon.common.Utils)5 Builder (com.vmware.xenon.services.common.QueryTask.Query.Builder)5 DeferredResult (com.vmware.xenon.common.DeferredResult)4 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)3 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)3