Search in sources :

Example 1 with MethodNotAllowedException

use of io.cdap.cdap.common.MethodNotAllowedException in project cdap by caskdata.

the class ProfileService method deleteAllProfiles.

/**
 * Delete all profiles in a given namespace. Deleting all profiles in SYSTEM namespace is not allowed.
 *
 * @param namespaceId the id of the namespace
 */
public void deleteAllProfiles(NamespaceId namespaceId) throws MethodNotAllowedException, NotFoundException, ProfileConflictException {
    if (namespaceId.equals(NamespaceId.SYSTEM)) {
        throw new MethodNotAllowedException("Deleting all system profiles is not allowed.");
    }
    List<ProfileId> deleted = new ArrayList<>();
    TransactionRunners.run(transactionRunner, context -> {
        ProfileStore profileStore = ProfileStore.get(context);
        AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
        List<Profile> profiles = profileStore.getProfiles(namespaceId, false);
        for (Profile profile : profiles) {
            ProfileId profileId = namespaceId.profile(profile.getName());
            deleteProfile(profileStore, appMetadataStore, profileId, profile);
            deleted.add(profileId);
        }
    }, ProfileConflictException.class, NotFoundException.class);
    // delete the metrics
    for (ProfileId profileId : deleted) {
        deleteMetrics(profileId);
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) MethodNotAllowedException(io.cdap.cdap.common.MethodNotAllowedException) AppMetadataStore(io.cdap.cdap.internal.app.store.AppMetadataStore) ArrayList(java.util.ArrayList) ProfileStore(io.cdap.cdap.internal.app.store.profile.ProfileStore) Profile(io.cdap.cdap.proto.profile.Profile)

Example 2 with MethodNotAllowedException

use of io.cdap.cdap.common.MethodNotAllowedException in project cdap by caskdata.

the class ProfileService 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
 * @throws MethodNotAllowedException if trying to update the Native profile or creating a new profile when disallowed.
 */
public void saveProfile(ProfileId profileId, Profile profile) throws MethodNotAllowedException {
    TransactionRunners.run(transactionRunner, context -> {
        if (!cConf.getBoolean(Constants.Profile.UPDATE_ALLOWED)) {
            throw new MethodNotAllowedException("Compute profile creation and update are not allowed");
        }
        ProfileStore dataset = ProfileStore.get(context);
        if (profileId.equals(ProfileId.NATIVE)) {
            try {
                dataset.getProfile(profileId);
                throw new MethodNotAllowedException(String.format("Profile Native %s already exists. It cannot be updated", profileId.getScopedName()));
            } catch (NotFoundException e) {
            // if native profile is not found, we can add it to the dataset
            }
        }
        dataset.saveProfile(profileId, profile);
    }, MethodNotAllowedException.class);
}
Also used : MethodNotAllowedException(io.cdap.cdap.common.MethodNotAllowedException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProfileStore(io.cdap.cdap.internal.app.store.profile.ProfileStore)

Example 3 with MethodNotAllowedException

use of io.cdap.cdap.common.MethodNotAllowedException in project cdap by caskdata.

the class ProfileService method deleteProfile.

/**
 * Deletes the profile from the profile store. Native profile cannot be deleted.
 * Other profile deletion must satisfy the following:
 * 1. Profile must exist and must be DISABLED
 * 2. Profile must not be assigned to any entities. Profiles can be assigned to an entity by setting a preference
 *    or a schedule property.
 * 3. There must be no active program runs using this profile
 *
 * @param profileId the id of the profile to delete
 * @throws NotFoundException if the profile is not found
 * @throws ProfileConflictException if the profile is enabled
 * @throws MethodNotAllowedException if trying to delete the Native profile
 */
public void deleteProfile(ProfileId profileId) throws MethodNotAllowedException, NotFoundException, ProfileConflictException {
    if (profileId.equals(ProfileId.NATIVE)) {
        throw new MethodNotAllowedException(String.format("Profile Native %s cannot be deleted.", profileId.getScopedName()));
    }
    TransactionRunners.run(transactionRunner, context -> {
        ProfileStore profileStore = ProfileStore.get(context);
        Profile profile = profileStore.getProfile(profileId);
        AppMetadataStore appMetadataStore = AppMetadataStore.create(context);
        deleteProfile(profileStore, appMetadataStore, profileId, profile);
    }, NotFoundException.class, ProfileConflictException.class);
    deleteMetrics(profileId);
}
Also used : MethodNotAllowedException(io.cdap.cdap.common.MethodNotAllowedException) AppMetadataStore(io.cdap.cdap.internal.app.store.AppMetadataStore) ProfileStore(io.cdap.cdap.internal.app.store.profile.ProfileStore) Profile(io.cdap.cdap.proto.profile.Profile)

Aggregations

MethodNotAllowedException (io.cdap.cdap.common.MethodNotAllowedException)3 ProfileStore (io.cdap.cdap.internal.app.store.profile.ProfileStore)3 AppMetadataStore (io.cdap.cdap.internal.app.store.AppMetadataStore)2 Profile (io.cdap.cdap.proto.profile.Profile)2 NotFoundException (io.cdap.cdap.common.NotFoundException)1 ProfileId (io.cdap.cdap.proto.id.ProfileId)1 ArrayList (java.util.ArrayList)1