use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.
the class ProgramLifecycleService method createProgramOptions.
@VisibleForTesting
ProgramOptions createProgramOptions(ProgramId programId, Map<String, String> userArgs, Map<String, String> sysArgs, boolean debug) throws NotFoundException, ProfileConflictException {
ProfileId profileId = SystemArguments.getProfileIdForProgram(programId, userArgs);
Map<String, String> profileProperties = SystemArguments.getProfileProperties(userArgs);
Profile profile = profileService.getProfile(profileId, profileProperties);
if (profile.getStatus() == ProfileStatus.DISABLED) {
throw new ProfileConflictException(String.format("Profile %s in namespace %s is disabled. It cannot be " + "used to start the program %s", profileId.getProfile(), profileId.getNamespace(), programId.toString()), profileId);
}
ProvisionerDetail spec = provisioningService.getProvisionerDetail(profile.getProvisioner().getName());
if (spec == null) {
throw new NotFoundException(String.format("Provisioner '%s' not found.", profile.getProvisioner().getName()));
}
// get and add any user overrides for profile properties
Map<String, String> systemArgs = new HashMap<>(sysArgs);
// add profile properties to the system arguments
SystemArguments.addProfileArgs(systemArgs, profile);
// Set the ClusterMode. If it is NATIVE profile, then it is ON_PREMISE, otherwise is ISOLATED
// This should probably move into the provisioner later once we have a better contract for the
// provisioner to actually pick what launching mechanism it wants to use.
systemArgs.put(ProgramOptionConstants.CLUSTER_MODE, (ProfileId.NATIVE.equals(profileId) ? ClusterMode.ON_PREMISE : ClusterMode.ISOLATED).name());
ProgramSpecification programSpecification = getProgramSpecificationWithoutAuthz(programId);
if (programSpecification == null) {
throw new NotFoundException(programId);
}
addAppCDAPVersion(programId, systemArgs);
// put all the plugin requirements if any involved in the run
systemArgs.put(ProgramOptionConstants.PLUGIN_REQUIREMENTS, GSON.toJson(getPluginRequirements(programSpecification)));
return new SimpleProgramOptions(programId, new BasicArguments(systemArgs), new BasicArguments(userArgs), debug);
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.
the class SystemArgumentsTest method testGetProfileId.
@Test
public void testGetProfileId() {
// should get null profile id if the args is empty
Assert.assertFalse(SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, Collections.emptyMap()).isPresent());
Map<String, String> args = new HashMap<>();
args.put("system.log.level", "DEBUG");
args.put("system.log.leveldummyKey", "ERROR");
// Having other unrelated args should also get null profile id
Assert.assertFalse(SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).isPresent());
// without scope the profile will be considered in user scope
ProfileId expected = NamespaceId.DEFAULT.profile("MyProfile");
args.put("system.profile.name", expected.getProfile());
Assert.assertEquals(expected, SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).get());
// put a profile with scope SYSTEM, the profile we get should be in system namespace
expected = NamespaceId.SYSTEM.profile("MyProfile");
args.put("system.profile.name", expected.getScopedName());
Assert.assertEquals(expected, SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).get());
// put a profile with scope USER, the profile we get should be in the user namespace
expected = NamespaceId.DEFAULT.profile("MyProfile");
args.put("system.profile.name", expected.getScopedName());
Assert.assertEquals(expected, SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).get());
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.
the class SystemArguments method getProfileIdFromArgs.
/**
* Get the profile id from namespace and the argument. Note that the profile name in the args is initially a scoped
* name. If it is in system scope, this method will return a profile id which is in SYSTEM namespace, otherwise, it
* will be in the given namespace.
*
* @param namespaceId namespace this profile is in
* @param args arguments to look up
* @return the profile id if it is present
*/
public static Optional<ProfileId> getProfileIdFromArgs(NamespaceId namespaceId, Map<String, String> args) {
if (args.containsKey(PROFILE_NAME)) {
String scopedProfile = args.get(SystemArguments.PROFILE_NAME);
ProfileId profileId = ProfileId.fromScopedName(namespaceId, scopedProfile);
return Optional.of(profileId);
}
return Optional.empty();
}
use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.
the class PreferencesServiceTest method testAddProfileInProperties.
@Test
public void testAddProfileInProperties() throws Exception {
PreferencesService prefStore = getInjector().getInstance(PreferencesService.class);
ProfileService profileStore = getInjector().getInstance(ProfileService.class);
// put a profile unrelated property should not affect the write
Map<String, String> expected = new HashMap<>();
expected.put("unRelatedKey", "unRelatedValue");
prefStore.setProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog"), expected);
Assert.assertEquals(expected, prefStore.getProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog")));
// put something related to profile
Map<String, String> profileMap = new HashMap<>();
profileMap.put(SystemArguments.PROFILE_NAME, "userProfile");
// this set call should fail since the profile does not exist
try {
prefStore.setProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog"), profileMap);
Assert.fail();
} catch (NotFoundException e) {
// expected
}
// the pref store should remain unchanged
Assert.assertEquals(expected, prefStore.getProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog")));
// add the profile and disable it
ProfileId profileId = new ProfileId("myspace", "userProfile");
profileStore.saveProfile(profileId, Profile.NATIVE);
profileStore.disableProfile(profileId);
// this set call should fail since the profile is disabled
try {
prefStore.setProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog"), profileMap);
Assert.fail();
} catch (ProfileConflictException e) {
// expected
}
// the pref store should remain unchanged
Assert.assertEquals(expected, prefStore.getProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog")));
// enable the profile
profileStore.enableProfile(profileId);
expected = profileMap;
prefStore.setProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog"), profileMap);
Map<String, String> properties = prefStore.getProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog"));
Assert.assertEquals(expected, properties);
prefStore.deleteProperties(new ProgramId("myspace", "app", ProgramType.WORKFLOW, "prog"));
profileStore.disableProfile(profileId);
profileStore.deleteProfile(profileId);
}
use of io.cdap.cdap.proto.id.ProfileId 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);
}
Aggregations