Search in sources :

Example 41 with Profile

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));
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) Profile(io.cdap.cdap.proto.profile.Profile)

Example 42 with Profile

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);
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) Profile(io.cdap.cdap.proto.profile.Profile)

Example 43 with Profile

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);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with Profile

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);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 45 with Profile

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);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Aggregations

Profile (io.cdap.cdap.proto.profile.Profile)86 ProfileId (io.cdap.cdap.proto.id.ProfileId)50 Test (org.junit.Test)48 ProvisionerInfo (io.cdap.cdap.proto.provisioner.ProvisionerInfo)32 NotFoundException (io.cdap.cdap.common.NotFoundException)16 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)14 ProgramId (io.cdap.cdap.proto.id.ProgramId)14 Field (io.cdap.cdap.spi.data.table.field.Field)14 ArrayList (java.util.ArrayList)14 ProvisionerPropertyValue (io.cdap.cdap.proto.provisioner.ProvisionerPropertyValue)10 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)10 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)8 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)6 Store (io.cdap.cdap.app.store.Store)6 DefaultStore (io.cdap.cdap.internal.app.store.DefaultStore)6 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)6 Injector (com.google.inject.Injector)4