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);
}
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;
}
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()));
}
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);
}
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());
}
Aggregations