Search in sources :

Example 46 with ProfileId

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

the class PreferencesHttpHandlerTest method testSetPreferenceWithProfiles.

@Test
public void testSetPreferenceWithProfiles() throws Exception {
    // put my profile
    ProfileId myProfile = new ProfileId(TEST_NAMESPACE1, "MyProfile");
    putProfile(myProfile, Profile.NATIVE, 200);
    // put some properties with my profile, it should work fine
    Map<String, String> properties = new HashMap<>();
    properties.put("1st key", "1st value");
    properties.put("2nd key", "2nd value");
    properties.put(SystemArguments.PROFILE_NAME, "USER:MyProfile");
    Map<String, String> expected = ImmutableMap.copyOf(properties);
    setPreferences(getPreferenceURI(TEST_NAMESPACE1), properties, 200);
    Assert.assertEquals(expected, getPreferences(getPreferenceURI(TEST_NAMESPACE1), false, 200));
    // put some property with non-existing profile, it should fail with 404
    properties.put(SystemArguments.PROFILE_NAME, "NonExisting");
    setPreferences(getPreferenceURI(TEST_NAMESPACE1), properties, 404);
    Assert.assertEquals(expected, getPreferences(getPreferenceURI(TEST_NAMESPACE1), false, 200));
    // disable the profile and put again, it should fail with 409
    disableProfile(myProfile, 200);
    properties.put(SystemArguments.PROFILE_NAME, "USER:MyProfile");
    setPreferences(getPreferenceURI(TEST_NAMESPACE1), properties, 409);
    Assert.assertEquals(expected, getPreferences(getPreferenceURI(TEST_NAMESPACE1), false, 200));
    deletePreferences(getPreferenceURI(TEST_NAMESPACE1), 200);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 47 with ProfileId

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

the class ProfileHttpHandlerTest method testSystemProfiles.

@Test
public void testSystemProfiles() throws Exception {
    Assert.assertEquals(Collections.singletonList(Profile.NATIVE), listSystemProfiles(HttpURLConnection.HTTP_OK));
    Profile p1 = new Profile("p1", "label", "desc", EntityScope.SYSTEM, new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
    Optional<Profile> p1Optional = getSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    Assert.assertTrue(p1Optional.isPresent());
    Assert.assertEquals(p1, p1Optional.get());
    // check list contains both native and p1
    Set<Profile> expected = new HashSet<>();
    expected.add(Profile.NATIVE);
    expected.add(p1);
    Set<Profile> actual = new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK));
    Assert.assertEquals(expected, actual);
    // check that they're both visible to namespaces
    Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
    // check we can add a profile with the same name in a namespace
    Profile p2 = new Profile(p1.getName(), p1.getLabel(), p1.getDescription(), EntityScope.USER, p1.getProvisioner());
    ProfileId p2Id = NamespaceId.DEFAULT.profile(p2.getName());
    putProfile(p2Id, p2, HttpURLConnection.HTTP_OK);
    // check that all are visible to the namespace
    expected.add(p2);
    Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
    // check that namespaced profile is not visible in system list
    expected.remove(p2);
    Assert.assertEquals(expected, new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK)));
    disableProfile(p2Id, HttpURLConnection.HTTP_OK);
    deleteProfile(p2Id, HttpURLConnection.HTTP_OK);
    disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    doAs(READ_WRITE_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_OK);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    });
    doAs(READ_ONLY_SYSTEM_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_OK);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
    doAs(READ_ONLY_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_FORBIDDEN);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_FORBIDDEN);
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with ProfileId

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

the class ProfileHttpHandlerTest method testListAndGetProfiles.

@Test
public void testListAndGetProfiles() throws Exception {
    // no profile should be there in default namespace
    List<Profile> profiles = listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.emptyList(), profiles);
    // try to list all profiles including system namespace before putting a new one, there should only exist a default
    // profile
    profiles = listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.singletonList(Profile.NATIVE), profiles);
    // test get single profile endpoint
    ProfileId profileId = NamespaceId.DEFAULT.profile("p1");
    Profile expected = new Profile("p1", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
    Profile actual = getProfile(profileId, HttpURLConnection.HTTP_OK).get();
    Assert.assertEquals(expected, actual);
    // get a nonexisting profile should get a not found code
    getProfile(NamespaceId.DEFAULT.profile("nonExisting"), HttpURLConnection.HTTP_NOT_FOUND);
    doAs(READ_ONLY_USER_NAME, () -> {
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
        getProfile(profileId, HttpURLConnection.HTTP_OK);
    });
    doAs(NO_ACCESS_USER_NAME, () -> {
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_FORBIDDEN);
        getProfile(profileId, HttpURLConnection.HTTP_FORBIDDEN);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 49 with ProfileId

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

the class ProfileHttpHandlerTest method testEnableDisableProfile.

@Test
public void testEnableDisableProfile() throws Exception {
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    ProfileId profileId = NamespaceId.DEFAULT.profile(expected.getName());
    // enable and disable a non-existing profile should give a 404
    enableProfile(profileId, HttpURLConnection.HTTP_NOT_FOUND);
    disableProfile(profileId, HttpURLConnection.HTTP_NOT_FOUND);
    // put the profile
    putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
    // by default the status should be enabled
    Assert.assertEquals(ProfileStatus.ENABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
    // enable it again should give a 409
    enableProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
    // disable should work
    disableProfile(profileId, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(ProfileStatus.DISABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
    // disable again should give a 409
    disableProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
    // enable should work
    enableProfile(profileId, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(ProfileStatus.ENABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
    // now delete should not work since we have the profile enabled
    deleteProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
    // disable and delete
    disableProfile(profileId, HttpURLConnection.HTTP_OK);
    deleteProfile(profileId, HttpURLConnection.HTTP_OK);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 50 with ProfileId

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

the class PreferencesService method setConfig.

/**
 * Validate the profile status is enabled and set the preferences
 */
private void setConfig(ProfileStore profileStore, PreferencesTable preferencesTable, EntityId entityId, Map<String, String> propertyMap) throws NotFoundException, ProfileConflictException, BadRequestException, IOException {
    boolean isInstanceLevel = entityId.getEntityType().equals(EntityType.INSTANCE);
    NamespaceId namespaceId = isInstanceLevel ? NamespaceId.SYSTEM : ((NamespacedEntityId) entityId).getNamespaceId();
    // validate the profile and publish the necessary metadata change if the profile exists in the property
    Optional<ProfileId> profile = SystemArguments.getProfileIdFromArgs(namespaceId, propertyMap);
    if (profile.isPresent()) {
        ProfileId profileId = profile.get();
        // setting a USER scoped profile
        if (isInstanceLevel && !propertyMap.get(SystemArguments.PROFILE_NAME).startsWith(EntityScope.SYSTEM.name())) {
            throw new BadRequestException(String.format("Cannot set profile %s at the instance level. " + "Only system profiles can be set at the instance level. " + "The profile property must look like SYSTEM:[profile-name]", propertyMap.get(SystemArguments.PROFILE_NAME)));
        }
        if (profileStore.getProfile(profileId).getStatus() == ProfileStatus.DISABLED) {
            throw new ProfileConflictException(String.format("Profile %s in namespace %s is disabled. It cannot be " + "assigned to any programs or schedules", profileId.getProfile(), profileId.getNamespace()), profileId);
        }
    }
    // need to get old property and check if it contains profile information
    Map<String, String> oldProperties = preferencesTable.getPreferences(entityId).getProperties();
    // get the old profile information from the previous properties
    Optional<ProfileId> oldProfile = SystemArguments.getProfileIdFromArgs(namespaceId, oldProperties);
    long seqId = preferencesTable.setPreferences(entityId, propertyMap);
    // After everything is set, publish the update message and add the association if profile is present
    if (profile.isPresent()) {
        profileStore.addProfileAssignment(profile.get(), entityId);
    }
    // if old properties has the profile, remove the association
    if (oldProfile.isPresent()) {
        profileStore.removeProfileAssignment(oldProfile.get(), entityId);
    }
    // if new profiles do not have profile information but old profiles have, it is same as deletion of the profile
    if (profile.isPresent()) {
        adminEventPublisher.publishProfileAssignment(entityId, seqId);
    } else if (oldProfile.isPresent()) {
        adminEventPublisher.publishProfileUnAssignment(entityId, seqId);
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException)

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