use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileService method getProfile.
/**
* Get the profile information about the given profile with property overrides. If a non-editable property is
* in the overrides, it will be ignored, but a message will be logged.
*
* @param profileId the id of the profile to look up
* @param overrides overrides to the profile properties
* @return the profile information about the given profile
* @throws NotFoundException if the profile is not found
*/
public Profile getProfile(ProfileId profileId, Map<String, String> overrides) throws NotFoundException {
Profile storedProfile = getProfile(profileId);
List<ProvisionerPropertyValue> properties = new ArrayList<>();
Set<String> remainingOverrides = new HashSet<>(overrides.keySet());
// add all properties from the stored profile
for (ProvisionerPropertyValue storedProperty : storedProfile.getProvisioner().getProperties()) {
String propertyName = storedProperty.getName();
String storedVal = storedProperty.getValue();
if (!storedProperty.isEditable()) {
if (overrides.containsKey(propertyName)) {
LOG.info("Profile property {} cannot be edited. The original value will be used.", propertyName);
}
properties.add(storedProperty);
} else {
String val = overrides.getOrDefault(propertyName, storedVal);
properties.add(new ProvisionerPropertyValue(propertyName, val, true));
}
remainingOverrides.remove(propertyName);
}
// add all remaining overrides
for (String propertyName : remainingOverrides) {
properties.add(new ProvisionerPropertyValue(propertyName, overrides.get(propertyName), true));
}
ProvisionerInfo provisionerInfo = new ProvisionerInfo(storedProfile.getProvisioner().getName(), properties);
return new Profile(storedProfile.getName(), storedProfile.getLabel(), storedProfile.getDescription(), storedProfile.getScope(), storedProfile.getStatus(), provisionerInfo, storedProfile.getCreatedTsSeconds());
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class SystemProfileCreator method execute.
@Override
public void execute(Arguments arguments) {
Profile profile = new Profile(arguments.name, arguments.getLabel(), arguments.getDescription(), EntityScope.SYSTEM, arguments.getProvisioner());
profileService.createIfNotExists(arguments.getId(), profile);
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileServiceTest method testProfileUpdateDisallowed.
@Test(expected = MethodNotAllowedException.class)
public void testProfileUpdateDisallowed() throws Exception {
ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
Profile profile = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
profileService.saveProfile(profileId, profile);
CConfiguration cConfWithProfileCreationDisabled = CConfiguration.copy(cConf);
cConfWithProfileCreationDisabled.setBoolean(Constants.Profile.UPDATE_ALLOWED, false);
ProfileService service = new ProfileService(cConfWithProfileCreationDisabled, getInjector().getInstance(MetricsSystemClient.class), getInjector().getInstance(TransactionRunner.class));
Profile newProfile = new Profile("MyProfile", "label", "my new profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
// Update the profile
service.saveProfile(profileId, newProfile);
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileServiceTest method testProfileService.
@Test
public void testProfileService() throws Exception {
// get non-existing profile
try {
profileService.getProfile(NamespaceId.DEFAULT.profile("nonExisting"));
Assert.fail();
} catch (NotFoundException e) {
// expected
}
// delete non-existing profile
try {
profileService.deleteProfile(NamespaceId.DEFAULT.profile("nonExisting"));
Assert.fail();
} catch (NotFoundException e) {
// expected
}
ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
// add a profile
profileService.saveProfile(profileId, expected);
// get the profile
Assert.assertEquals(expected, profileService.getProfile(profileId));
// add a profile which already exists, should succeed and the profile property should be updated
expected = new Profile("MyProfile", "label", "my 2nd profile for updating", new ProvisionerInfo("anotherProvisioner", Collections.emptyList()));
profileService.saveProfile(profileId, expected);
Assert.assertEquals(expected, profileService.getProfile(profileId));
// add another profile to default namespace
ProfileId profileId2 = NamespaceId.DEFAULT.profile("MyProfile2");
Profile profile2 = new Profile("MyProfile2", "label", "my 2nd profile for testing", new ProvisionerInfo("anotherProvisioner", PROPERTY_SUMMARIES));
profileService.saveProfile(profileId2, profile2);
// add default profile
profileService.saveProfile(ProfileId.NATIVE, Profile.NATIVE);
// get all profiles
List<Profile> profiles = ImmutableList.of(expected, profile2, Profile.NATIVE);
Assert.assertEquals(profiles, profileService.getProfiles(NamespaceId.DEFAULT, true));
// by default the profile status should be enabled
Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId2).getStatus());
// by default the profile will be enabled, so enable it will throw a ProfileConflictException
try {
profileService.enableProfile(profileId);
Assert.fail();
} catch (ProfileConflictException e) {
// expected
}
// disable the profile should success
profileService.disableProfile(profileId);
// check the profile status to the disabled
Assert.assertEquals(ProfileStatus.DISABLED, profileService.getProfile(profileId).getStatus());
// disable again should throw ProfileConflictException
try {
profileService.disableProfile(profileId);
Assert.fail();
} catch (ProfileConflictException e) {
// expected
}
// enable should work this time
profileService.enableProfile(profileId);
Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
// delete the second profile should fail since it is enabled
try {
profileService.deleteProfile(profileId2);
Assert.fail();
} catch (ProfileConflictException e) {
// expected
}
profileService.disableProfile(profileId2);
profileService.deleteProfile(profileId2);
Assert.assertEquals(ImmutableList.of(expected), profileService.getProfiles(NamespaceId.DEFAULT, false));
// add one and delete all profiles
profileService.saveProfile(profileId2, profile2);
profileService.disableProfile(profileId);
profileService.disableProfile(profileId2);
profileService.deleteAllProfiles(NamespaceId.DEFAULT);
Assert.assertEquals(Collections.EMPTY_LIST, profileService.getProfiles(NamespaceId.DEFAULT, false));
// try to enable and disable an non-existing profile should throw NotFoundException
try {
profileService.enableProfile(profileId);
Assert.fail();
} catch (NotFoundException e) {
// expected
}
try {
profileService.disableProfile(profileId);
Assert.fail();
} catch (NotFoundException e) {
// expected
}
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileServiceTest method testAddDeleteAssignments.
@Test
public void testAddDeleteAssignments() throws Exception {
ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
Profile profile1 = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
profileService.saveProfile(myProfile, profile1);
// add a profile assignment and verify
Set<EntityId> expected = new HashSet<>();
expected.add(NamespaceId.DEFAULT);
profileService.addProfileAssignment(myProfile, NamespaceId.DEFAULT);
Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
// add more and verify
InstanceId instanceId = new InstanceId("");
ApplicationId myApp = NamespaceId.DEFAULT.app("myApp");
ProgramId myProgram = myApp.workflow("myProgram");
expected.add(instanceId);
expected.add(myApp);
expected.add(myProgram);
profileService.addProfileAssignment(myProfile, instanceId);
profileService.addProfileAssignment(myProfile, myApp);
profileService.addProfileAssignment(myProfile, myProgram);
Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
// add same entity id should not affect
profileService.addProfileAssignment(myProfile, myApp);
Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
// delete one and verify
expected.remove(myApp);
profileService.removeProfileAssignment(myProfile, myApp);
Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
// delete all
for (EntityId entityId : expected) {
profileService.removeProfileAssignment(myProfile, entityId);
}
expected.clear();
Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
// delete again should not affect
profileService.removeProfileAssignment(myProfile, myApp);
Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
profileService.disableProfile(myProfile);
profileService.deleteProfile(myProfile);
}
Aggregations