Search in sources :

Example 26 with EndpointState

use of com.vmware.photon.controller.model.resources.EndpointService.EndpointState in project photon-model by vmware.

the class EndpointAdapterUtils method configureEndpoint.

private static void configureEndpoint(StatelessService service, EndpointConfigRequest body, BiConsumer<AuthCredentialsServiceState, Retriever> credEnhancer, BiConsumer<ComputeDescription, Retriever> descEnhancer, BiConsumer<ComputeState, Retriever> compEnhancer, BiConsumer<EndpointState, Retriever> endpointEnhancer) {
    TaskManager tm = new TaskManager(service, body.taskReference, body.resourceLink());
    Consumer<Throwable> onFailure = tm::patchTaskToFailure;
    Consumer<Operation> onSuccess = (op) -> {
        EndpointState endpoint = op.getBody(EndpointState.class);
        op.complete();
        AuthCredentialsServiceState authState = new AuthCredentialsServiceState();
        Map<String, String> props = new HashMap<>(body.endpointProperties);
        props.put(MOCK_REQUEST, String.valueOf(body.isMockRequest));
        props.put(ENDPOINT_REFERENCE_URI, body.resourceReference.toString());
        Retriever r = Retriever.of(props);
        try {
            credEnhancer.accept(authState, r);
            ComputeDescription cd = new ComputeDescription();
            descEnhancer.accept(cd, r);
            ComputeState cs = new ComputeState();
            cs.powerState = PowerState.ON;
            compEnhancer.accept(cs, r);
            EndpointState es = new EndpointState();
            es.endpointProperties = new HashMap<>();
            es.regionId = r.get(EndpointConfigRequest.REGION_KEY).orElse(null);
            endpointEnhancer.accept(es, r);
            Stream<Operation> operations = Stream.of(Pair.of(authState, endpoint.authCredentialsLink), Pair.of(cd, endpoint.computeDescriptionLink), Pair.of(cs, endpoint.computeLink), Pair.of(es, endpoint.documentSelfLink)).map((p) -> Operation.createPatch(createInventoryUri(service.getHost(), p.right)).setBody(p.left).setReferer(service.getUri()));
            applyChanges(tm, service, endpoint, operations);
        } catch (Exception e) {
            tm.patchTaskToFailure(e);
        }
    };
    AdapterUtils.getServiceState(service, body.resourceReference, onSuccess, onFailure);
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) HashMap(java.util.HashMap) ServiceEndpointLocator(com.vmware.photon.controller.model.util.ServiceEndpointLocator) Level(java.util.logging.Level) Utils(com.vmware.xenon.common.Utils) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) INVENTORY_SERVICE(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster.INVENTORY_SERVICE) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE_CODE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE_CODE) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) Consumer(java.util.function.Consumer) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils.buildUri(com.vmware.xenon.common.UriUtils.buildUri) Optional(java.util.Optional) PhotonModelAdaptersRegistryService(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Operation(com.vmware.xenon.common.Operation) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Stream(java.util.stream.Stream) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with EndpointState

use of com.vmware.photon.controller.model.resources.EndpointService.EndpointState in project photon-model by vmware.

the class EndpointAdapterUtils method getEndpointLinks.

private static DeferredResult<List<String>> getEndpointLinks(ServiceHost host, Query endpointQuery, List<String> credentialsLinks, String endpointType, List<String> queryTaskTenantLinks) {
    if (credentialsLinks.isEmpty()) {
        return DeferredResult.completed(Collections.emptyList());
    }
    Query.Builder qBuilder = Builder.create().addKindFieldClause(EndpointState.class).addFieldClause(EndpointState.FIELD_NAME_ENDPOINT_TYPE, endpointType).addInClause(EndpointState.FIELD_NAME_AUTH_CREDENTIALS_LINK, credentialsLinks);
    if (endpointQuery != null) {
        qBuilder.addClause(endpointQuery);
    }
    QueryTop<EndpointState> queryEndpoints = new QueryTop<>(host, qBuilder.build(), EndpointState.class, queryTaskTenantLinks).setQueryTaskTenantLinks(queryTaskTenantLinks).setMaxResultsLimit(1);
    queryEndpoints.setClusterType(INVENTORY_SERVICE);
    return queryEndpoints.collectLinks(Collectors.toList());
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Query(com.vmware.xenon.services.common.QueryTask.Query) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop)

Example 28 with EndpointState

use of com.vmware.photon.controller.model.resources.EndpointService.EndpointState in project photon-model by vmware.

the class AzureSubscriptionEndpointsEnumerationServiceTest method testAddSecondAzureSubscription.

private void testAddSecondAzureSubscription() throws Throwable {
    // Request for creating computes for 1 Azure Subscriptions
    AzureSubscription subscription = getAzureSubscription(SUBSCRIPTION_ID_2, ACCOUNT_ID_2);
    createAzureEndpointsForSubscriptions(Collections.singletonList(subscription));
    // Query the Endpoints to assert
    ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, EndpointService.FACTORY_LINK));
    Assert.assertEquals(3, result.documents.size());
    // Assert the created Endpoint and other resources
    result.documents.remove(this.endpointLink);
    // Remove the endpoints created earlier
    this.createdEndpointLinks.forEach(endpoint -> result.documents.remove(endpoint));
    EndpointState endpointStateCreated = Utils.fromJson(result.documents.values().iterator().next(), EndpointState.class);
    assertCreatedEndpoint(endpointStateCreated, SUBSCRIPTION_ID_2);
    this.createdEndpointLinks.add(endpointStateCreated.documentSelfLink);
    // Assert the root compute under the endpoint
    ComputeState computeStateCreated = getServiceSynchronously(endpointStateCreated.computeLink, ComputeState.class);
    assertCreatedComputeState(computeStateCreated, SUBSCRIPTION_ID_2, ACCOUNT_ID_2);
    // Assert the partial AuthCredentialsState
    AuthCredentialsServiceState authCreated = getServiceSynchronously(endpointStateCreated.authCredentialsLink, AuthCredentialsServiceState.class);
    assertAuthCredentialState(authCreated, SUBSCRIPTION_ID_2);
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

Example 29 with EndpointState

use of com.vmware.photon.controller.model.resources.EndpointService.EndpointState in project photon-model by vmware.

the class AzureSubscriptionEndpointsEnumerationServiceTest method setUp.

@Before
public void setUp() throws Exception {
    this.host = VerificationHost.create(0);
    this.isMock = true;
    this.createdEndpointLinks = new ArrayList<>();
    try {
        this.host.start();
        PhotonModelServices.startServices(this.host);
        PhotonModelMetricServices.startServices(this.host);
        PhotonModelAdaptersRegistryAdapters.startServices(this.host);
        PhotonModelTaskServices.startServices(this.host);
        this.host.startService(new AzureSubscriptionEndpointsEnumerationService());
        AzureAdaptersTestUtils.startServicesSynchronouslyEaAzure(this.host);
        this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelMetricServices.LINKS);
        this.host.waitForServiceAvailable(AzureSubscriptionEndpointsEnumerationService.SELF_LINK);
        this.host.setTimeoutSeconds(600);
        // Create an Azure endpoint which will act as Azure EA endpoint
        EndpointState ep = createEndpointState();
        EndpointAllocationTaskState validateEndpoint = new EndpointAllocationTaskState();
        validateEndpoint.endpointState = ep;
        validateEndpoint.options = this.isMock ? EnumSet.of(TaskOption.IS_MOCK) : null;
        validateEndpoint.tenantLinks = Collections.singletonList(TENANT_ID);
        EndpointAllocationTaskState outTask = TestUtils.doPost(this.host, validateEndpoint, EndpointAllocationTaskState.class, UriUtils.buildUri(this.host, EndpointAllocationTaskService.FACTORY_LINK));
        this.host.waitForFinishedTask(EndpointAllocationTaskState.class, outTask.documentSelfLink);
        EndpointAllocationTaskState taskState = getServiceSynchronously(outTask.documentSelfLink, EndpointAllocationTaskState.class);
        this.endpointLink = taskState.endpointState.documentSelfLink;
        this.computeLink = taskState.endpointState.computeLink;
        this.compute = getServiceSynchronously(this.computeLink, ComputeState.class);
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EndpointAllocationTaskState(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState) Before(org.junit.Before)

Example 30 with EndpointState

use of com.vmware.photon.controller.model.resources.EndpointService.EndpointState in project photon-model by vmware.

the class AzureSubscriptionEndpointCreationServiceTest method setUp.

@Before
public void setUp() throws Exception {
    try {
        this.host.setMaintenanceIntervalMicros(TimeUnit.MILLISECONDS.toMicros(10));
        PhotonModelServices.startServices(this.host);
        PhotonModelAdaptersRegistryAdapters.startServices(this.host);
        PhotonModelMetricServices.startServices(this.host);
        PhotonModelTaskServices.startServices(this.host);
        this.host.startService(new AzureSubscriptionEndpointCreationService());
        AzureAdaptersTestUtils.startServicesSynchronouslyEaAzure(this.host);
        this.host.setTimeoutSeconds(300);
        this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
        this.host.waitForServiceAvailable(AzureSubscriptionEndpointCreationService.SELF_LINK);
        EndpointState ep = createEndpointState();
        // Create the Azure Ea Endpoint for the test
        EndpointAllocationTaskState validateEndpoint = new EndpointAllocationTaskState();
        validateEndpoint.endpointState = ep;
        validateEndpoint.options = this.isMock ? EnumSet.of(TaskOption.IS_MOCK) : null;
        validateEndpoint.tenantLinks = Collections.singletonList(TENANT_ID);
        EndpointAllocationTaskState outTask = TestUtils.doPost(this.host, validateEndpoint, EndpointAllocationTaskState.class, UriUtils.buildUri(this.host, EndpointAllocationTaskService.FACTORY_LINK));
        this.host.waitForFinishedTask(EndpointAllocationTaskState.class, outTask.documentSelfLink);
        EndpointAllocationTaskState taskState = getServiceSynchronously(outTask.documentSelfLink, EndpointAllocationTaskState.class);
        this.eaEndPointLink = taskState.endpointState.documentSelfLink;
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) EndpointAllocationTaskState(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState) Before(org.junit.Before)

Aggregations

EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)69 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)23 Operation (com.vmware.xenon.common.Operation)22 Test (org.junit.Test)16 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)15 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)14 EndpointAllocationTaskState (com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState)13 URI (java.net.URI)13 HashMap (java.util.HashMap)13 Query (com.vmware.xenon.services.common.QueryTask.Query)12 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)11 UriUtils (com.vmware.xenon.common.UriUtils)11 Utils (com.vmware.xenon.common.Utils)11 List (java.util.List)11 ImageState (com.vmware.photon.controller.model.resources.ImageService.ImageState)10 ServiceDocument (com.vmware.xenon.common.ServiceDocument)10 Collectors (java.util.stream.Collectors)10 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)9