use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class DataQualityAppTest method testTotals.
@Test
public void testTotals() throws Exception {
Map<String, Set<String>> testMap = new HashMap<>();
Set<String> testSet = new HashSet<>();
testSet.add("DiscreteValuesHistogram");
testMap.put("content_length", testSet);
testMap.put("status", testSet);
testMap.put("request_time", testSet);
DataQualityApp.DataQualityConfig config = new DataQualityApp.DataQualityConfig(WORKFLOW_SCHEDULE_MINUTES, getStreamSource(), "histogram", testMap);
ApplicationId appId = NamespaceId.DEFAULT.app("newApp3");
AppRequest<DataQualityApp.DataQualityConfig> appRequest = new AppRequest<>(new ArtifactSummary(appArtifact.getArtifact(), appArtifact.getVersion()), config);
ApplicationManager applicationManager = deployApplication(appId, appRequest);
MapReduceManager mrManager = applicationManager.getMapReduceManager("FieldAggregator").start();
mrManager.waitForRun(ProgramRunStatus.COMPLETED, 180, TimeUnit.SECONDS);
Map<String, Integer> expectedMap = new HashMap<>();
expectedMap.put("256", 3);
/* Test for the aggregationsGetter handler */
ServiceManager serviceManager = applicationManager.getServiceManager(DataQualityService.SERVICE_NAME).start();
serviceManager.waitForStatus(true);
URL url = new URL(serviceManager.getServiceURL(), "v1/sources/logStream/fields/content_length/aggregations/DiscreteValuesHistogram/totals");
HttpResponse httpResponse = HttpRequests.execute(HttpRequest.get(url).build());
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getResponseCode());
String response = httpResponse.getResponseBodyAsString();
Map<String, Integer> histogramMap = GSON.fromJson(response, TOKEN_TYPE_MAP_STRING_INTEGER);
Assert.assertEquals(expectedMap, histogramMap);
/* Test for the fieldsGetter handler */
url = new URL(serviceManager.getServiceURL(), "v1/sources/logStream/fields");
httpResponse = HttpRequests.execute(HttpRequest.get(url).build());
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getResponseCode());
response = httpResponse.getResponseBodyAsString();
Set<FieldDetail> outputSet = GSON.fromJson(response, TOKEN_TYPE_SET_FIELD_DETAIL);
Set<FieldDetail> expectedSet = new HashSet<>();
AggregationTypeValue aggregationTypeValue = new AggregationTypeValue("DiscreteValuesHistogram", true);
Set<AggregationTypeValue> aggregationTypeValuesList = Sets.newHashSet(aggregationTypeValue);
expectedSet.add(new FieldDetail("content_length", aggregationTypeValuesList));
expectedSet.add(new FieldDetail("request_time", aggregationTypeValuesList));
expectedSet.add(new FieldDetail("status", aggregationTypeValuesList));
Assert.assertEquals(expectedSet, outputSet);
/* Test for the aggregationTypesGetter handler */
url = new URL(serviceManager.getServiceURL(), "v1/sources/logStream/fields/content_length");
httpResponse = HttpRequests.execute(HttpRequest.get(url).build());
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpResponse.getResponseCode());
response = httpResponse.getResponseBodyAsString();
List<AggregationTypeValue> expectedAggregationTypeValuesList = new ArrayList<>();
List<AggregationTypeValue> outputAggregationTypeValuesList = GSON.fromJson(response, TOKEN_TYPE_LIST_AGGREGATION_TYPE_VALUES);
expectedAggregationTypeValuesList.add(new AggregationTypeValue("DiscreteValuesHistogram", true));
Assert.assertEquals(expectedAggregationTypeValuesList, outputAggregationTypeValuesList);
serviceManager.stop();
serviceManager.waitForRun(ProgramRunStatus.KILLED, 180, TimeUnit.SECONDS);
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class ListArtifactVersionsCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
List<ArtifactSummary> artifactSummaries;
if (scopeStr == null) {
artifactSummaries = artifactClient.listVersions(cliConfig.getCurrentNamespace(), artifactName);
} else {
ArtifactScope scope = ArtifactScope.valueOf(scopeStr.toUpperCase());
artifactSummaries = artifactClient.listVersions(cliConfig.getCurrentNamespace(), artifactName, scope);
}
Table table = Table.builder().setHeader("name", "version", "scope").setRows(artifactSummaries, new RowMaker<ArtifactSummary>() {
@Override
public List<?> makeRow(ArtifactSummary object) {
return Lists.newArrayList(object.getName(), object.getVersion(), object.getScope().name());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class ListArtifactsCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
List<ArtifactSummary> artifactSummaries;
String artifactScope = arguments.getOptional(ArgumentName.SCOPE.toString());
if (artifactScope == null) {
artifactSummaries = artifactClient.list(cliConfig.getCurrentNamespace());
} else {
artifactSummaries = artifactClient.list(cliConfig.getCurrentNamespace(), ArtifactScope.valueOf(artifactScope.toUpperCase()));
}
Table table = Table.builder().setHeader("name", "version", "scope").setRows(artifactSummaries, new RowMaker<ArtifactSummary>() {
@Override
public List<?> makeRow(ArtifactSummary object) {
return Lists.newArrayList(object.getName(), object.getVersion(), object.getScope().name());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class SystemArtifactsAuthorizationTest method testAuthorizationForSystemArtifacts.
@Test
public void testAuthorizationForSystemArtifacts() throws Exception {
artifactRepository.addSystemArtifacts();
// alice should not be able to refresh system artifacts because she does not have write privileges on the
// CDAP instance
SecurityRequestContext.setUserId(ALICE.getName());
try {
artifactRepository.addSystemArtifacts();
Assert.fail("Adding system artifacts should have failed because alice does not have write privileges on " + "the CDAP instance.");
} catch (UnauthorizedException expected) {
// expected
}
// grant alice write privileges on the CDAP instance
authorizer.grant(NamespaceId.SYSTEM, ALICE, Collections.singleton(Action.WRITE));
Assert.assertEquals(Collections.singleton(new Privilege(NamespaceId.SYSTEM, Action.WRITE)), authorizer.listPrivileges(ALICE));
// refreshing system artifacts should succeed now
artifactRepository.addSystemArtifacts();
SecurityRequestContext.setUserId("bob");
// deleting a system artifact should fail because bob does not have admin privileges on the artifact
try {
artifactRepository.deleteArtifact(SYSTEM_ARTIFACT.toId());
Assert.fail("Deleting a system artifact should have failed because alice does not have admin privileges on " + "the CDAP instance.");
} catch (UnauthorizedException expected) {
// expected
}
// grant alice admin privileges on the CDAP instance, so she can create a namespace
SecurityRequestContext.setUserId(ALICE.getName());
authorizer.grant(instance, ALICE, Collections.singleton(Action.ADMIN));
NamespaceId namespaceId = new NamespaceId("test");
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespaceId.getNamespace()).build());
authorizer.revoke(instance);
// test that system artifacts are available to everyone
List<ArtifactSummary> artifacts = artifactRepository.getArtifactSummaries(namespaceId, true);
Assert.assertEquals(1, artifacts.size());
ArtifactSummary artifactSummary = artifacts.get(0);
Assert.assertEquals(SYSTEM_ARTIFACT.getArtifact(), artifactSummary.getName());
Assert.assertEquals(SYSTEM_ARTIFACT.getVersion(), artifactSummary.getVersion());
Assert.assertEquals(SYSTEM_ARTIFACT.getNamespace(), artifactSummary.getScope().name().toLowerCase());
// test the getArtifact API
ArtifactDetail artifactDetail = artifactRepository.getArtifact(SYSTEM_ARTIFACT.toId());
co.cask.cdap.api.artifact.ArtifactId artifactId = artifactDetail.getDescriptor().getArtifactId();
Assert.assertEquals(SYSTEM_ARTIFACT.getArtifact(), artifactId.getName());
Assert.assertEquals(SYSTEM_ARTIFACT.getVersion(), artifactId.getVersion().getVersion());
Assert.assertEquals(SYSTEM_ARTIFACT.getNamespace(), artifactId.getScope().name().toLowerCase());
namespaceAdmin.delete(namespaceId);
authorizer.enforce(SYSTEM_ARTIFACT, ALICE, EnumSet.allOf(Action.class));
authorizer.enforce(NamespaceId.SYSTEM, ALICE, Action.WRITE);
// deleting system artifact should succeed as alice, because alice added the artifacts, so she should have all
// privileges on it
artifactRepository.deleteArtifact(SYSTEM_ARTIFACT.toId());
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class Upgrader method upgrade.
public boolean upgrade(ArtifactSummary oldArtifact, String oldConfigStr, UpgradeAction upgradeAction) throws Exception {
String artifactName = oldArtifact.getName();
if (!ARTIFACT_NAMES.contains(artifactName)) {
return false;
}
ArtifactVersion artifactVersion = new ArtifactVersion(oldArtifact.getVersion());
Integer majorVersion = artifactVersion.getMajor();
Integer minorVersion = artifactVersion.getMinor();
if (majorVersion == null || minorVersion == null || !shouldUpgrade(oldArtifact)) {
return false;
}
ArtifactSummary newArtifact = new ArtifactSummary(artifactName, ETLVersion.getVersion(), ArtifactScope.SYSTEM);
AppRequest<? extends ETLConfig> appRequest;
switch(artifactName) {
case BATCH_NAME:
appRequest = new AppRequest<>(newArtifact, convertBatchConfig(majorVersion, minorVersion, oldConfigStr, etlBatchContext));
break;
case REALTIME_NAME:
appRequest = new AppRequest<>(newArtifact, convertRealtimeConfig(majorVersion, minorVersion, oldConfigStr));
break;
case DATA_PIPELINE_NAME:
appRequest = new AppRequest<>(newArtifact, convertBatchConfig(majorVersion, minorVersion, oldConfigStr, dataPipelineContext));
break;
case DATA_STREAMS_NAME:
appRequest = new AppRequest<>(newArtifact, convertStreamsConfig(oldConfigStr));
break;
default:
// can never happen
throw new IllegalStateException("Unknown artifact " + artifactName);
}
return upgradeAction.upgrade(appRequest);
}
Aggregations