use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileStore method deleteProfile.
/**
* Deletes the profile from the profile store
*
* @param profileId the id of the profile to delete
* @throws NotFoundException if the profile is not found
* @throws ProfileConflictException if the profile is enabled
*/
public void deleteProfile(ProfileId profileId) throws NotFoundException, ProfileConflictException, IOException {
Collection<Field<?>> fields = getProfileKeys(profileId);
Profile value = getProfileInternal(fields);
if (value == null) {
throw new NotFoundException(profileId);
}
if (value.getStatus() == ProfileStatus.ENABLED) {
throw new ProfileConflictException(String.format("Profile %s in namespace %s is currently enabled. A profile can " + "only be deleted if it is disabled", profileId.getProfile(), profileId.getNamespace()), profileId);
}
profileTable.delete(getProfileKeys(profileId));
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileStore method saveProfile.
/**
* Save the profile to the profile store. By default the profile status will be enabled.
*
* @param profileId the id of the profile to save
* @param profile the information of the profile
*/
public void saveProfile(ProfileId profileId, Profile profile) throws IOException {
Collection<Field<?>> fields = getProfileKeys(profileId);
Profile oldProfile = getProfileInternal(fields);
Profile newProfile = new Profile(profile.getName(), profile.getLabel(), profile.getDescription(), profile.getScope(), oldProfile == null ? ProfileStatus.ENABLED : oldProfile.getStatus(), profile.getProvisioner(), oldProfile == null ? profile.getCreatedTsSeconds() : oldProfile.getCreatedTsSeconds());
fields.add(Fields.stringField(StoreDefinition.ProfileStore.PROFILE_DATA_FIELD, GSON.toJson(newProfile)));
profileTable.upsert(fields);
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileHttpHandlerTest method testSystemProfiles.
@Test
public void testSystemProfiles() throws Exception {
Assert.assertEquals(Collections.singletonList(Profile.NATIVE), listSystemProfiles(HttpURLConnection.HTTP_OK));
Profile p1 = new Profile("p1", "label", "desc", EntityScope.SYSTEM, new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
Optional<Profile> p1Optional = getSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
Assert.assertTrue(p1Optional.isPresent());
Assert.assertEquals(p1, p1Optional.get());
// check list contains both native and p1
Set<Profile> expected = new HashSet<>();
expected.add(Profile.NATIVE);
expected.add(p1);
Set<Profile> actual = new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK));
Assert.assertEquals(expected, actual);
// check that they're both visible to namespaces
Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
// check we can add a profile with the same name in a namespace
Profile p2 = new Profile(p1.getName(), p1.getLabel(), p1.getDescription(), EntityScope.USER, p1.getProvisioner());
ProfileId p2Id = NamespaceId.DEFAULT.profile(p2.getName());
putProfile(p2Id, p2, HttpURLConnection.HTTP_OK);
// check that all are visible to the namespace
expected.add(p2);
Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
// check that namespaced profile is not visible in system list
expected.remove(p2);
Assert.assertEquals(expected, new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK)));
disableProfile(p2Id, HttpURLConnection.HTTP_OK);
deleteProfile(p2Id, HttpURLConnection.HTTP_OK);
disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
doAs(READ_WRITE_USER_NAME, () -> {
listSystemProfiles(HttpURLConnection.HTTP_OK);
listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
});
doAs(READ_ONLY_SYSTEM_USER_NAME, () -> {
listSystemProfiles(HttpURLConnection.HTTP_OK);
listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
});
doAs(READ_ONLY_USER_NAME, () -> {
listSystemProfiles(HttpURLConnection.HTTP_FORBIDDEN);
listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_FORBIDDEN);
listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_FORBIDDEN);
putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
});
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileHttpHandlerTest method testListAndGetProfiles.
@Test
public void testListAndGetProfiles() throws Exception {
// no profile should be there in default namespace
List<Profile> profiles = listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
Assert.assertEquals(Collections.emptyList(), profiles);
// try to list all profiles including system namespace before putting a new one, there should only exist a default
// profile
profiles = listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
Assert.assertEquals(Collections.singletonList(Profile.NATIVE), profiles);
// test get single profile endpoint
ProfileId profileId = NamespaceId.DEFAULT.profile("p1");
Profile expected = new Profile("p1", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
Profile actual = getProfile(profileId, HttpURLConnection.HTTP_OK).get();
Assert.assertEquals(expected, actual);
// get a nonexisting profile should get a not found code
getProfile(NamespaceId.DEFAULT.profile("nonExisting"), HttpURLConnection.HTTP_NOT_FOUND);
doAs(READ_ONLY_USER_NAME, () -> {
listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
getProfile(profileId, HttpURLConnection.HTTP_OK);
});
doAs(NO_ACCESS_USER_NAME, () -> {
listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_FORBIDDEN);
getProfile(profileId, HttpURLConnection.HTTP_FORBIDDEN);
});
}
use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.
the class ProfileHttpHandlerTest method testEnableDisableProfile.
@Test
public void testEnableDisableProfile() throws Exception {
Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
ProfileId profileId = NamespaceId.DEFAULT.profile(expected.getName());
// enable and disable a non-existing profile should give a 404
enableProfile(profileId, HttpURLConnection.HTTP_NOT_FOUND);
disableProfile(profileId, HttpURLConnection.HTTP_NOT_FOUND);
// put the profile
putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
// by default the status should be enabled
Assert.assertEquals(ProfileStatus.ENABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
// enable it again should give a 409
enableProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
// disable should work
disableProfile(profileId, HttpURLConnection.HTTP_OK);
Assert.assertEquals(ProfileStatus.DISABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
// disable again should give a 409
disableProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
// enable should work
enableProfile(profileId, HttpURLConnection.HTTP_OK);
Assert.assertEquals(ProfileStatus.ENABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
// now delete should not work since we have the profile enabled
deleteProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
// disable and delete
disableProfile(profileId, HttpURLConnection.HTTP_OK);
deleteProfile(profileId, HttpURLConnection.HTTP_OK);
}
Aggregations