Search in sources :

Example 36 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.

the class ProfileHttpHandler method writeProfile.

private void writeProfile(ProfileId profileId, FullHttpRequest request) throws BadRequestException, IOException, MethodNotAllowedException {
    ProfileCreateRequest profileCreateRequest;
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        profileCreateRequest = GSON.fromJson(reader, ProfileCreateRequest.class);
        validateProvisionerProperties(profileCreateRequest);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Unable to parse request body. Please make sure it is valid JSON", e);
    }
    String totalProcessingCpusLabel = getTotalProcessingCpusLabel(profileCreateRequest.getProvisioner());
    profileCreateRequest.getProvisioner().setTotalProcessingCpusLabel(totalProcessingCpusLabel);
    Profile profile = new Profile(profileId.getProfile(), profileCreateRequest.getLabel(), profileCreateRequest.getDescription(), profileId.getScope(), profileCreateRequest.getProvisioner());
    profileService.saveProfile(profileId, profile);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) ProfileCreateRequest(io.cdap.cdap.proto.profile.ProfileCreateRequest) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(io.cdap.cdap.common.BadRequestException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Profile(io.cdap.cdap.proto.profile.Profile)

Example 37 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.

the class ProfileHttpHandler method getProfiles.

/**
 * List the profiles in the given namespace. By default the results will not contain profiles in system scope.
 */
@GET
@Path("/namespaces/{namespace-id}/profiles")
public void getProfiles(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("includeSystem") @DefaultValue("false") String includeSystem) throws Exception {
    NamespaceId namespace = getValidatedNamespace(namespaceId);
    accessEnforcer.enforceOnParent(EntityType.PROFILE, namespace, authenticationContext.getPrincipal(), StandardPermission.LIST);
    boolean include = Boolean.valueOf(includeSystem);
    if (include) {
        accessEnforcer.enforceOnParent(EntityType.PROFILE, NamespaceId.SYSTEM, authenticationContext.getPrincipal(), StandardPermission.LIST);
    }
    List<Profile> profiles = verifyCpuLabelsProfiles(profileService.getProfiles(namespace, include), namespace);
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(profiles));
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Profile(io.cdap.cdap.proto.profile.Profile) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 38 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.

the class ProfileStore method changeProfileStatus.

private void changeProfileStatus(ProfileId profileId, ProfileStatus expectedStatus) throws NotFoundException, ProfileConflictException, IOException {
    Collection<Field<?>> fields = getProfileKeys(profileId);
    Profile oldProfile = getProfileInternal(fields);
    if (oldProfile == null) {
        throw new NotFoundException(profileId);
    }
    if (oldProfile.getStatus() == expectedStatus) {
        throw new ProfileConflictException(String.format("Profile %s already %s", profileId.getProfile(), expectedStatus.toString()), profileId);
    }
    Profile newProfile = new Profile(oldProfile.getName(), oldProfile.getLabel(), oldProfile.getDescription(), oldProfile.getScope(), expectedStatus, oldProfile.getProvisioner(), 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) 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 39 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.

the class ProfileStore method createIfNotExists.

/**
 * Add a profile if it does not exist in the store
 *
 * @param profileId the id of the profile to add
 * @param profile the information of the profile
 */
public void createIfNotExists(ProfileId profileId, Profile profile) throws IOException {
    Collection<Field<?>> keys = getProfileKeys(profileId);
    Profile newProfile = new Profile(profile.getName(), profile.getLabel(), profile.getDescription(), profile.getScope(), ProfileStatus.ENABLED, profile.getProvisioner());
    profileTable.compareAndSwap(keys, Fields.stringField(StoreDefinition.ProfileStore.PROFILE_DATA_FIELD, null), Fields.stringField(StoreDefinition.ProfileStore.PROFILE_DATA_FIELD, GSON.toJson(newProfile)));
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) Profile(io.cdap.cdap.proto.profile.Profile)

Example 40 with Profile

use of io.cdap.cdap.proto.profile.Profile in project cdap by cdapio.

the class ProfileStore method addProfileAssignment.

/**
 * Add an assignment to the profile. Assignment can only be added if the profile is ENABLED
 *
 * @param profileId the profile id
 * @param entityId the entity to add to the assgiment
 */
public void addProfileAssignment(ProfileId profileId, EntityId entityId) throws ProfileConflictException, NotFoundException, IOException {
    Collection<Field<?>> fields = getProfileKeys(profileId);
    Profile profile = getProfileInternal(fields);
    if (profile == null) {
        throw new NotFoundException(profileId);
    }
    if (profile.getStatus() == ProfileStatus.DISABLED) {
        throw new ProfileConflictException(String.format("Profile %s is DISABLED. No entity can be assigned to it.", profileId.getProfile()), profileId);
    }
    addEntityIdKey(fields, entityId);
    fields.add(Fields.stringField(StoreDefinition.ProfileStore.ENTITY_DATA_FIELD, GSON.toJson(entityId)));
    profileEntityTable.upsert(fields);
}
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)

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