Search in sources :

Example 1 with Profile

use of co.cask.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileStore method add.

/**
 * Add the profile to the profile store.
 *
 * @param profileId the id of the profile to add
 * @param profile the information of the profile
 * @throws IOException if there was an IO error adding the profile
 * @throws AlreadyExistsException if the profile already exists
 */
public void add(ProfileId profileId, Profile profile) throws IOException, AlreadyExistsException {
    Transactionals.execute(transactional, context -> {
        MetadataStoreDataset mds = getMDS(context);
        // make sure that a profile doesn't exist
        MDSKey rowKey = getRowKey(profileId);
        Profile value = mds.get(rowKey, Profile.class);
        if (value != null) {
            throw new AlreadyExistsException(profileId, String.format("Profile '%s' already exists.", profile.getName()));
        }
        mds.write(rowKey, profile);
    }, IOException.class, AlreadyExistsException.class);
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) MetadataStoreDataset(co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Profile(co.cask.cdap.proto.profile.Profile)

Example 2 with Profile

use of co.cask.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileStore method delete.

/**
 * Deletes the profile from the profile store
 *
 * @param profileId the id of the profile to delete
 * @throws IOException if there was an IO error deleting the profile
 * @throws NotFoundException if the profile is not found
 */
public void delete(ProfileId profileId) throws IOException, NotFoundException {
    Transactionals.execute(transactional, context -> {
        MetadataStoreDataset mds = getMDS(context);
        MDSKey rowKey = getRowKey(profileId);
        Profile value = getMDS(context).get(rowKey, Profile.class);
        if (value == null) {
            throw new NotFoundException(profileId);
        }
        mds.delete(getRowKey(profileId));
    }, IOException.class, NotFoundException.class);
}
Also used : MetadataStoreDataset(co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) NotFoundException(co.cask.cdap.common.NotFoundException) Profile(co.cask.cdap.proto.profile.Profile)

Example 3 with Profile

use of co.cask.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileHttpHandler method writeProfile.

/**
 * Write a profile in a namespace.
 */
@PUT
@Path("/profiles/{profile-name}")
public void writeProfile(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("profile-name") String profileName) throws BadRequestException, IOException, AlreadyExistsException {
    ProfileCreateRequest profileCreateRequest;
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        // TODO: validate the profileCreateRequest, the provisoner should exist and the property should be correct
        profileCreateRequest = GSON.fromJson(reader, ProfileCreateRequest.class);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Request body is invalid json: " + e.getMessage(), e);
    }
    ProfileId profileId = new ProfileId(namespaceId, profileName);
    Profile profile = new Profile(profileName, profileCreateRequest.getDescription(), profileId.getNamespaceId().equals(NamespaceId.SYSTEM) ? EntityScope.SYSTEM : EntityScope.USER, profileCreateRequest.getProvisioner());
    profileStore.add(profileId, profile);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : ProfileId(co.cask.cdap.proto.id.ProfileId) JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) ProfileCreateRequest(co.cask.cdap.proto.profile.ProfileCreateRequest) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Profile(co.cask.cdap.proto.profile.Profile) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 4 with Profile

use of co.cask.cdap.proto.profile.Profile in project cdap by caskdata.

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("/profiles")
public void getProfiles(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("includeSystem") @DefaultValue("false") String includeSystem) throws IOException {
    List<Profile> profiles = new ArrayList<>();
    NamespaceId namespace = new NamespaceId(namespaceId);
    boolean include = Boolean.valueOf(includeSystem);
    if (include && !namespace.equals(NamespaceId.SYSTEM)) {
        profiles.addAll(profileStore.getProfiles(NamespaceId.SYSTEM));
    }
    profiles.addAll(profileStore.getProfiles(namespace));
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(profiles));
}
Also used : ArrayList(java.util.ArrayList) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Profile(co.cask.cdap.proto.profile.Profile) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with Profile

use of co.cask.cdap.proto.profile.Profile in project cdap by caskdata.

the class ProfileStoreTest method testProfileStore.

@Test
public void testProfileStore() throws Exception {
    // get non-existing profile
    try {
        profileStore.getProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    // delete non-existing profile
    try {
        profileStore.delete(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile expected = new Profile("MyProfile", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // add a profile
    profileStore.add(profileId, expected);
    // get the profile
    Assert.assertEquals(expected, profileStore.getProfile(profileId));
    // add a profile which already exists
    try {
        profileStore.add(profileId, new Profile("MyProfile", "my another profile", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES)));
        Assert.fail();
    } catch (AlreadyExistsException e) {
    // expected
    }
    // add another profile to default namespace
    ProfileId profileId2 = NamespaceId.DEFAULT.profile("MyProfile2");
    Profile profile2 = new Profile("MyProfile2", "my 2nd profile for testing", new ProvisionerInfo("anotherProvisioner", PROPERTY_SUMMARIES));
    profileStore.add(profileId2, profile2);
    // get all profiles
    List<Profile> profiles = ImmutableList.of(expected, profile2);
    Assert.assertEquals(profiles, profileStore.getProfiles(NamespaceId.DEFAULT));
    // delete the second profile
    profileStore.delete(profileId2);
    Assert.assertEquals(ImmutableList.of(expected), profileStore.getProfiles(NamespaceId.DEFAULT));
    // add one and delete all profiles
    profileStore.add(profileId2, profile2);
    profileStore.deleteAll(NamespaceId.DEFAULT);
    Assert.assertEquals(Collections.EMPTY_LIST, profileStore.getProfiles(NamespaceId.DEFAULT));
}
Also used : ProfileId(co.cask.cdap.proto.id.ProfileId) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProvisionerInfo(co.cask.cdap.proto.profile.ProvisionerInfo) NotFoundException(co.cask.cdap.common.NotFoundException) Profile(co.cask.cdap.proto.profile.Profile) Test(org.junit.Test)

Aggregations

Profile (co.cask.cdap.proto.profile.Profile)5 AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)2 NotFoundException (co.cask.cdap.common.NotFoundException)2 MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)2 MetadataStoreDataset (co.cask.cdap.data2.dataset2.lib.table.MetadataStoreDataset)2 ProfileId (co.cask.cdap.proto.id.ProfileId)2 Path (javax.ws.rs.Path)2 BadRequestException (co.cask.cdap.common.BadRequestException)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 ProfileCreateRequest (co.cask.cdap.proto.profile.ProfileCreateRequest)1 ProvisionerInfo (co.cask.cdap.proto.profile.ProvisionerInfo)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 Test (org.junit.Test)1