Search in sources :

Example 16 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class PreferencesServiceTest method testProfileAssignment.

@Test
public void testProfileAssignment() throws Exception {
    PreferencesService preferencesService = getInjector().getInstance(PreferencesService.class);
    ProfileService profileService = getInjector().getInstance(ProfileService.class);
    ProfileId myProfile = NamespaceId.DEFAULT.profile("myProfile");
    profileService.saveProfile(myProfile, Profile.NATIVE);
    // add properties with profile information
    Map<String, String> prop = new HashMap<>();
    prop.put(SystemArguments.PROFILE_NAME, ProfileId.NATIVE.getScopedName());
    ApplicationId myApp = NamespaceId.DEFAULT.app("myApp");
    ProgramId myProgram = myApp.workflow("myProgram");
    preferencesService.setProperties(prop);
    preferencesService.setProperties(NamespaceId.DEFAULT, prop);
    preferencesService.setProperties(myApp, prop);
    preferencesService.setProperties(myProgram, prop);
    // the assignment should be there for these entities
    Set<EntityId> expected = new HashSet<>();
    expected.add(new InstanceId(""));
    expected.add(NamespaceId.DEFAULT);
    expected.add(myApp);
    expected.add(myProgram);
    Assert.assertEquals(expected, profileService.getProfileAssignments(ProfileId.NATIVE));
    // setting an empty property is actually deleting the assignment
    prop.clear();
    preferencesService.setProperties(myApp, prop);
    expected.remove(myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(ProfileId.NATIVE));
    // set my program to use a different profile, should update both profiles
    prop.put(SystemArguments.PROFILE_NAME, myProfile.getScopedName());
    preferencesService.setProperties(myProgram, prop);
    expected.remove(myProgram);
    Assert.assertEquals(expected, profileService.getProfileAssignments(ProfileId.NATIVE));
    Assert.assertEquals(Collections.singleton(myProgram), profileService.getProfileAssignments(myProfile));
    // delete all preferences
    preferencesService.deleteProperties();
    preferencesService.deleteProperties(NamespaceId.DEFAULT);
    preferencesService.deleteProperties(myApp);
    preferencesService.deleteProperties(myProgram);
    Assert.assertEquals(Collections.emptySet(), profileService.getProfileAssignments(ProfileId.NATIVE));
    Assert.assertEquals(Collections.emptySet(), profileService.getProfileAssignments(myProfile));
    profileService.disableProfile(myProfile);
    profileService.deleteProfile(myProfile);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) EntityId(io.cdap.cdap.proto.id.EntityId) ProfileService(io.cdap.cdap.internal.profile.ProfileService) HashMap(java.util.HashMap) InstanceId(io.cdap.cdap.proto.id.InstanceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class PreferencesService method addProperties.

/**
 * Set instance level preferences if they are not already set. Only adds the properties that don't already exist.
 *
 * @param properties the preferences to add
 * @return the preference keys that were added
 */
public Set<String> addProperties(Map<String, String> properties) throws NotFoundException, ProfileConflictException, BadRequestException {
    InstanceId instanceId = new InstanceId("");
    Set<String> added = new HashSet<>();
    TransactionRunners.run(transactionRunner, context -> {
        ProfileStore profileStore = ProfileStore.get(context);
        PreferencesTable preferencesTable = new PreferencesTable(context);
        Map<String, String> oldProperties = preferencesTable.getPreferences(instanceId).getProperties();
        Map<String, String> newProperties = new HashMap<>(properties);
        added.addAll(Sets.difference(newProperties.keySet(), oldProperties.keySet()));
        newProperties.putAll(oldProperties);
        setConfig(profileStore, preferencesTable, instanceId, newProperties);
    }, NotFoundException.class, ProfileConflictException.class, BadRequestException.class);
    return added;
}
Also used : InstanceId(io.cdap.cdap.proto.id.InstanceId) HashMap(java.util.HashMap) ProfileStore(io.cdap.cdap.internal.app.store.profile.ProfileStore) HashSet(java.util.HashSet)

Example 18 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class PreferencesHttpHandler method getInstancePrefs.

// Instance Level Properties
@Path("/preferences")
@GET
public void getInstancePrefs(HttpRequest request, HttpResponder responder) throws Exception {
    InstanceId instanceId = new InstanceId("");
    accessEnforcer.enforce(instanceId, authenticationContext.getPrincipal(), StandardPermission.GET);
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(preferencesService.getProperties()));
}
Also used : InstanceId(io.cdap.cdap.proto.id.InstanceId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 19 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class MonitorHandlerAuthorizationTest method testGetBootStatusAuthorization.

@Test
public void testGetBootStatusAuthorization() throws Exception {
    InstanceId instanceId = InstanceId.SELF;
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(instanceId, EntityType.SYSTEM_SERVICE), Arrays.asList(StandardPermission.LIST));
    HttpRequest request = mock(HttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.getBootStatus(request, responder);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.getBootStatus(request, responder);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpResponder(io.cdap.http.HttpResponder) InstanceId(io.cdap.cdap.proto.id.InstanceId) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 20 with InstanceId

use of io.cdap.cdap.proto.id.InstanceId in project cdap by caskdata.

the class PreferencesFetcherInternalTest method testGetPreferences.

@Test
public void testGetPreferences() throws Exception {
    PreferencesFetcher fetcher = getPreferencesFetcher(fetcherType);
    PreferencesDetail preferences = null;
    EntityId entityId = null;
    // Used to keep track of preferences being set in order to facilitate clean up at the end of the test
    List<String> preferenceURIList = new ArrayList<>();
    // Get preferences on instance, but none was set.
    entityId = new InstanceId("");
    preferences = fetcher.get(entityId, false);
    Assert.assertEquals(Collections.emptyMap(), preferences.getProperties());
    Assert.assertFalse(preferences.getResolved());
    // Set preferences on instance and fetch again.
    Map<String, String> instanceProperties = ImmutableMap.of("instance-key1", "instance-val1");
    setPreferences(getPreferenceURI(), instanceProperties, 200);
    preferenceURIList.add(getPreferenceURI());
    entityId = new InstanceId("");
    preferences = fetcher.get(entityId, false);
    Assert.assertEquals(instanceProperties, preferences.getProperties());
    Assert.assertFalse(preferences.getResolved());
    Assert.assertTrue(preferences.getSeqId() > 0);
    // Deploy the application.
    String namespace = TEST_NAMESPACE1;
    String appName = AllProgramsApp.NAME;
    deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, namespace);
    // Get preferences on the application, but none was set.
    entityId = new ApplicationId(namespace, appName);
    preferences = fetcher.get(entityId, false);
    Assert.assertEquals(Collections.emptyMap(), preferences.getProperties());
    Assert.assertFalse(preferences.getResolved());
    // Get resolved preferences on the application, preferences on instance should be returned.
    entityId = new ApplicationId(namespace, appName);
    preferences = fetcher.get(entityId, true);
    Assert.assertEquals(instanceProperties, preferences.getProperties());
    Assert.assertTrue(preferences.getResolved());
    Assert.assertTrue(preferences.getSeqId() > 0);
    // Set preferences on application and fetch again, resolved preferences should be returned.
    Map<String, String> appProperties = ImmutableMap.of("app-key1", "app-val1");
    setPreferences(getPreferenceURI(namespace, appName), appProperties, 200);
    preferenceURIList.add(getPreferenceURI(namespace, appName));
    preferences = fetcher.get(entityId, true);
    Map<String, String> resolvedProperites = new HashMap<>();
    resolvedProperites.putAll(instanceProperties);
    resolvedProperites.putAll(appProperties);
    Assert.assertEquals(resolvedProperites, preferences.getProperties());
    Assert.assertTrue(preferences.getResolved());
    Assert.assertTrue(preferences.getSeqId() > 0);
    // Cleanup: delete preferences that were set
    for (String uri : preferenceURIList) {
        deletePreferences(uri, 200);
    }
    // Cleanup: delete the app
    Assert.assertEquals(200, doDelete(getVersionedAPIPath("apps/", Constants.Gateway.API_VERSION_3_TOKEN, namespace)).getResponseCode());
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) EntityId(io.cdap.cdap.proto.id.EntityId) InstanceId(io.cdap.cdap.proto.id.InstanceId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Aggregations

InstanceId (io.cdap.cdap.proto.id.InstanceId)32 Test (org.junit.Test)16 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)10 HashSet (java.util.HashSet)8 ProfileService (io.cdap.cdap.internal.profile.ProfileService)6 EntityId (io.cdap.cdap.proto.id.EntityId)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 HashMap (java.util.HashMap)6 Path (javax.ws.rs.Path)6 Service (com.google.common.util.concurrent.Service)4 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)4 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)4 PreferencesDetail (io.cdap.cdap.proto.PreferencesDetail)4 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)4 HttpResponder (io.cdap.http.HttpResponder)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 AbstractModule (com.google.inject.AbstractModule)2