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);
}
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);
}
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);
}
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));
}
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));
}
Aggregations