Search in sources :

Example 31 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by caskdata.

the class AuthorizableTest method testProfile.

@Test
public void testProfile() {
    ProfileId profileId = new ProfileId("ns", "test_secure");
    Authorizable authorizable = Authorizable.fromEntityId(profileId);
    Assert.assertEquals(profileId.toString(), authorizable.toString());
    String widcardId = profileId.toString().replace("est", "*es?t");
    Assert.assertEquals(widcardId, Authorizable.fromString(widcardId).toString());
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) Test(org.junit.Test)

Example 32 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

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 33 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileMetricServiceTest method testRoundingLogic.

@Test
public void testRoundingLogic() throws Exception {
    ProgramRunId runId = NamespaceId.DEFAULT.app("round").workflow("round").run(RunIds.generate());
    ProfileId profileId = NamespaceId.DEFAULT.profile("roundProfile");
    MetricsCollectionService collectionService = injector.getInstance(MetricsCollectionService.class);
    MetricStore metricStore = injector.getInstance(MetricStore.class);
    ProfileMetricService scheduledService = new ProfileMetricService(collectionService, runId, profileId, 1, 1);
    // start and stop the service, the metric should still go up by 1
    scheduledService.startUp();
    scheduledService.shutDown();
    Tasks.waitFor(1L, () -> getMetric(metricStore, runId, profileId, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
    scheduledService.startUp();
    // set the start up time to 90 seconds before the current time
    scheduledService.setStartUpTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - 90);
    // 90 seconds should round up to 2 mins, so emit 1 min and test the rounding logic
    scheduledService.emitMetric();
    scheduledService.shutDown();
    // the metric should go up by 2
    Tasks.waitFor(3L, () -> getMetric(metricStore, runId, profileId, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
    scheduledService.startUp();
    // set the start up time to 65 seconds before the current time
    scheduledService.setStartUpTime(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - 65);
    // 65 seconds should round down to 1 min, so emit 1 min and test the rest seconds are ignored
    scheduledService.emitMetric();
    scheduledService.shutDown();
    // the metric should go up by 1
    Tasks.waitFor(4L, () -> getMetric(metricStore, runId, profileId, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) MetricStore(io.cdap.cdap.api.metrics.MetricStore) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Example 34 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileServiceTest method testProfileUpdateDisallowed.

@Test(expected = MethodNotAllowedException.class)
public void testProfileUpdateDisallowed() throws Exception {
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    profileService.saveProfile(profileId, profile);
    CConfiguration cConfWithProfileCreationDisabled = CConfiguration.copy(cConf);
    cConfWithProfileCreationDisabled.setBoolean(Constants.Profile.UPDATE_ALLOWED, false);
    ProfileService service = new ProfileService(cConfWithProfileCreationDisabled, getInjector().getInstance(MetricsSystemClient.class), getInjector().getInstance(TransactionRunner.class));
    Profile newProfile = new Profile("MyProfile", "label", "my new profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // Update the profile
    service.saveProfile(profileId, newProfile);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 35 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileServiceTest method testProfileService.

@Test
public void testProfileService() throws Exception {
    // get non-existing profile
    try {
        profileService.getProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    // delete non-existing profile
    try {
        profileService.deleteProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // add a profile
    profileService.saveProfile(profileId, expected);
    // get the profile
    Assert.assertEquals(expected, profileService.getProfile(profileId));
    // add a profile which already exists, should succeed and the profile property should be updated
    expected = new Profile("MyProfile", "label", "my 2nd profile for updating", new ProvisionerInfo("anotherProvisioner", Collections.emptyList()));
    profileService.saveProfile(profileId, expected);
    Assert.assertEquals(expected, profileService.getProfile(profileId));
    // add another profile to default namespace
    ProfileId profileId2 = NamespaceId.DEFAULT.profile("MyProfile2");
    Profile profile2 = new Profile("MyProfile2", "label", "my 2nd profile for testing", new ProvisionerInfo("anotherProvisioner", PROPERTY_SUMMARIES));
    profileService.saveProfile(profileId2, profile2);
    // add default profile
    profileService.saveProfile(ProfileId.NATIVE, Profile.NATIVE);
    // get all profiles
    List<Profile> profiles = ImmutableList.of(expected, profile2, Profile.NATIVE);
    Assert.assertEquals(profiles, profileService.getProfiles(NamespaceId.DEFAULT, true));
    // by default the profile status should be enabled
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId2).getStatus());
    // by default the profile will be enabled, so enable it will throw a ProfileConflictException
    try {
        profileService.enableProfile(profileId);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    // disable the profile should success
    profileService.disableProfile(profileId);
    // check the profile status to the disabled
    Assert.assertEquals(ProfileStatus.DISABLED, profileService.getProfile(profileId).getStatus());
    // disable again should throw ProfileConflictException
    try {
        profileService.disableProfile(profileId);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    // enable should work this time
    profileService.enableProfile(profileId);
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
    // delete the second profile should fail since it is enabled
    try {
        profileService.deleteProfile(profileId2);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    profileService.disableProfile(profileId2);
    profileService.deleteProfile(profileId2);
    Assert.assertEquals(ImmutableList.of(expected), profileService.getProfiles(NamespaceId.DEFAULT, false));
    // add one and delete all profiles
    profileService.saveProfile(profileId2, profile2);
    profileService.disableProfile(profileId);
    profileService.disableProfile(profileId2);
    profileService.deleteAllProfiles(NamespaceId.DEFAULT);
    Assert.assertEquals(Collections.EMPTY_LIST, profileService.getProfiles(NamespaceId.DEFAULT, false));
    // try to enable and disable an non-existing profile should throw NotFoundException
    try {
        profileService.enableProfile(profileId);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    try {
        profileService.disableProfile(profileId);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) NotFoundException(io.cdap.cdap.common.NotFoundException) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Aggregations

ProfileId (io.cdap.cdap.proto.id.ProfileId)112 Test (org.junit.Test)68 Profile (io.cdap.cdap.proto.profile.Profile)50 ProgramId (io.cdap.cdap.proto.id.ProgramId)26 ProvisionerInfo (io.cdap.cdap.proto.provisioner.ProvisionerInfo)26 Path (javax.ws.rs.Path)22 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)16 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)14 HashMap (java.util.HashMap)14 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)12 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)12 MetricStore (io.cdap.cdap.api.metrics.MetricStore)10 NotFoundException (io.cdap.cdap.common.NotFoundException)10 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)10 ProfileService (io.cdap.cdap.internal.profile.ProfileService)10 ArrayList (java.util.ArrayList)10 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)8 ScheduleId (io.cdap.cdap.proto.id.ScheduleId)8 POST (javax.ws.rs.POST)8 Store (io.cdap.cdap.app.store.Store)6