use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class DefaultNamespaceAdminTest method testConfigUpdate.
@Test
public void testConfigUpdate() throws Exception {
String namespace = "custompaceNamespace";
NamespaceId namespaceId = new NamespaceId(namespace);
// check that root directory for a namespace cannot be updated
// create the custom directory since the namespace is being created with custom root directory it needs to exist
String customRoot = "/some/custom/dir";
Location customlocation = baseLocationFactory.create(customRoot);
Assert.assertTrue(customlocation.mkdirs());
NamespaceMeta nsMeta = new NamespaceMeta.Builder().setName(namespaceId).setRootDirectory(customRoot).build();
namespaceAdmin.create(nsMeta);
Assert.assertTrue(namespaceAdmin.exists(namespaceId));
// Updating the root directory for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setRootDirectory("/newloc").build());
Assert.fail();
} catch (BadRequestException e) {
//expected
}
// Updating the HBase namespace for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setHBaseNamespace("custns").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// Updating the hive database for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setHiveDatabase("newDB").build());
Assert.fail();
} catch (BadRequestException e) {
//expected
}
// removing the root directory mapping for a namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setRootDirectory("").build());
Assert.fail();
} catch (BadRequestException e) {
//expected
}
// updating the principal for an existing namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setPrincipal("newPrincipal").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// updating the keytabURI for an existing namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setKeytabURI("/new/keytab/uri").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// updating the groupname for an existing namespace should fail
try {
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setGroupName("anotherGroup").build());
Assert.fail();
} catch (BadRequestException e) {
// expected
}
// Although disabling explore impersonation should be allowed
Assert.assertTrue(namespaceAdmin.get(nsMeta.getNamespaceId()).getConfig().isExploreAsPrincipal());
namespaceAdmin.updateProperties(nsMeta.getNamespaceId(), new NamespaceMeta.Builder(nsMeta).setExploreAsPrincipal(false).build());
Assert.assertFalse(namespaceAdmin.get(nsMeta.getNamespaceId()).getConfig().isExploreAsPrincipal());
//clean up
namespaceAdmin.delete(namespaceId);
Locations.deleteQuietly(customlocation);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ArtifactClient method add.
/**
* Add an artifact.
*
* @param namespace the namespace to add the artifact to
* @param artifactName the name of the artifact to add
* @param artifactContents an input supplier for the contents of the artifact
* @param artifactVersion the version of the artifact to add. If null, the version will be derived from the
* manifest of the artifact
* @param parentArtifacts the set of artifacts this artifact extends
* @param additionalPlugins the set of plugins contained in the artifact that cannot be determined
* through jar inspection. This set should include any classes that are plugins but could
* not be annotated as such. For example, 3rd party classes like jdbc drivers fall into
* this category.
* @throws ArtifactAlreadyExistsException if the artifact already exists
* @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
* @throws ArtifactRangeNotFoundException if the parent artifacts do not exist
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void add(NamespaceId namespace, String artifactName, InputSupplier<? extends InputStream> artifactContents, @Nullable String artifactVersion, @Nullable Set<ArtifactRange> parentArtifacts, @Nullable Set<PluginClass> additionalPlugins) throws ArtifactAlreadyExistsException, BadRequestException, IOException, UnauthenticatedException, ArtifactRangeNotFoundException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(namespace, String.format("artifacts/%s", artifactName));
HttpRequest.Builder requestBuilder = HttpRequest.post(url);
if (artifactVersion != null) {
requestBuilder.addHeader("Artifact-Version", artifactVersion);
}
if (parentArtifacts != null && !parentArtifacts.isEmpty()) {
requestBuilder.addHeader("Artifact-Extends", Joiner.on('/').join(parentArtifacts));
}
if (additionalPlugins != null && !additionalPlugins.isEmpty()) {
requestBuilder.addHeader("Artifact-Plugins", GSON.toJson(additionalPlugins));
}
HttpRequest request = requestBuilder.withBody(artifactContents).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_CONFLICT, HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
int responseCode = response.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_CONFLICT) {
throw new ArtifactAlreadyExistsException(response.getResponseBodyAsString());
} else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ArtifactRangeNotFoundException(parentArtifacts);
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ArtifactClient method deleteProperty.
/**
* Delete a property for an artifact. If the property does not exist, this will be a no-op.
*
* @param artifactId the artifact to delete a property from
* @param key the property to delete
* @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws ArtifactNotFoundException if the artifact does not exist
* @throws IOException if a network error occurred
*/
public void deleteProperty(ArtifactId artifactId, String key) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
String path = String.format("artifacts/%s/versions/%s/properties/%s", artifactId.getArtifact(), artifactId.getVersion(), key);
URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
HttpRequest.Builder requestBuilder = HttpRequest.delete(url);
HttpRequest request = requestBuilder.build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
int responseCode = response.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ArtifactNotFoundException(artifactId);
} else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ArtifactClient method delete.
/**
* Delete an artifact.
*
* @param artifactId the artifact to delete
*
* @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void delete(ArtifactId artifactId) throws IOException, UnauthenticatedException, BadRequestException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(artifactId.getParent(), String.format("artifacts/%s/versions/%s", artifactId.getArtifact(), artifactId.getVersion()));
HttpRequest request = HttpRequest.delete(url).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST);
int responseCode = response.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
}
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class ApplicationClient method update.
/**
* Update an existing app to use a different artifact version or config.
*
* @param appId the id of the application to update
* @param updateRequest the request to update the application with
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws NotFoundException if the app or requested artifact could not be found
* @throws BadRequestException if the request is invalid
*/
public void update(ApplicationId appId, AppRequest<?> updateRequest) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(appId.getParent(), String.format("apps/%s/update", appId.getApplication()));
HttpRequest request = HttpRequest.post(url).withBody(GSON.toJson(updateRequest)).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
int responseCode = response.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException("app or app artifact");
} else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
}
}
Aggregations