Search in sources :

Example 41 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class LoadArtifactCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    File artifactFile = resolver.resolvePathToFile(arguments.get(ArgumentName.LOCAL_FILE_PATH.toString()));
    String name = arguments.getOptional(ArgumentName.ARTIFACT_NAME.toString());
    String version = arguments.getOptional(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactId artifactId;
    if (name == null && version != null) {
        throw new IllegalArgumentException("If a version is specified, name must also be specified.");
    } else if (name != null && version == null) {
        throw new IllegalArgumentException("If a name is specified, a version must also be specified.");
    } else if (name == null) {
        artifactId = new ArtifactId(cliConfig.getCurrentNamespace().getNamespace(), artifactFile.getName());
    } else {
        artifactId = cliConfig.getCurrentNamespace().artifact(name, version);
    }
    String configPath = arguments.getOptional(ArgumentName.ARTIFACT_CONFIG_FILE.toString());
    NamespaceId namespace = artifactId.getParent();
    if (configPath == null) {
        artifactClient.add(namespace, artifactId.getEntityName(), Files.newInputStreamSupplier(artifactFile), artifactId.getVersion());
    } else {
        File configFile = resolver.resolvePathToFile(configPath);
        ArtifactConfig artifactConfig = configReader.read(Id.Namespace.fromEntityId(namespace), configFile);
        artifactClient.add(namespace, artifactId.getEntityName(), Files.newInputStreamSupplier(artifactFile), artifactId.getVersion(), artifactConfig.getParents(), artifactConfig.getPlugins());
        Map<String, String> properties = artifactConfig.getProperties();
        if (properties != null && !properties.isEmpty()) {
            artifactClient.writeProperties(artifactId, properties);
        }
    }
    output.printf("Successfully added artifact with name '%s'\n", artifactId.getEntityName());
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactConfig(co.cask.cdap.common.conf.ArtifactConfig) NamespaceId(co.cask.cdap.proto.id.NamespaceId) File(java.io.File)

Example 42 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class GetArtifactPropertiesCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactId artifactId = cliConfig.getCurrentNamespace().artifact(artifactName, artifactVersion);
    String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
    ArtifactInfo info;
    if (scopeStr == null) {
        info = artifactClient.getArtifactInfo(artifactId);
    } else {
        ArtifactScope scope = ArtifactScope.valueOf(scopeStr.toUpperCase());
        info = artifactClient.getArtifactInfo(artifactId, scope);
    }
    List<Map.Entry<String, String>> rows = new ArrayList<>(info.getProperties().size());
    rows.addAll(info.getProperties().entrySet());
    Table table = Table.builder().setHeader("key", "value").setRows(rows, new RowMaker<Map.Entry<String, String>>() {

        @Override
        public List<String> makeRow(Map.Entry<String, String> entry) {
            List<String> columns = new ArrayList<>(2);
            columns.add(entry.getKey());
            columns.add(entry.getValue());
            return columns;
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : ArtifactScope(co.cask.cdap.api.artifact.ArtifactScope) Table(co.cask.cdap.cli.util.table.Table) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactInfo(co.cask.cdap.api.artifact.ArtifactInfo) RowMaker(co.cask.cdap.cli.util.RowMaker) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 43 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class ListArtifactPluginTypesCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactId artifactId = cliConfig.getCurrentNamespace().artifact(artifactName, artifactVersion);
    List<String> types;
    String scopeStr = arguments.getOptional(ArgumentName.SCOPE.toString());
    if (scopeStr == null) {
        types = artifactClient.getPluginTypes(artifactId);
    } else {
        types = artifactClient.getPluginTypes(artifactId, ArtifactScope.valueOf(scopeStr.toUpperCase()));
    }
    Table table = Table.builder().setHeader("plugin type").setRows(types, new RowMaker<String>() {

        @Override
        public List<?> makeRow(String object) {
            return Lists.newArrayList(object);
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : Table(co.cask.cdap.cli.util.table.Table) ArtifactId(co.cask.cdap.proto.id.ArtifactId) RowMaker(co.cask.cdap.cli.util.RowMaker)

Example 44 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class ArtifactHttpHandler method getProperty.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/properties/{property}")
public void getProperty(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("property") String key, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
    NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
    ArtifactId artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
    try {
        ArtifactDetail detail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
        responder.sendString(HttpResponseStatus.OK, detail.getMeta().getProperties().get(key));
    } catch (IOException e) {
        LOG.error("Exception reading property for artifact {}.", artifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading properties for artifact.");
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 45 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactProperties.

@POST
@Path("/namespaces/{namespace-id}/artifactproperties")
public void getArtifactProperties(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    NamespaceId namespace = validateAndGetNamespace(namespaceId);
    List<ArtifactPropertiesRequest> propertyRequests;
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        propertyRequests = GSON.fromJson(reader, BATCH_ARTIFACT_PROPERTIES_REQUEST);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Unable to parse request: " + e.getMessage(), e);
    }
    List<ArtifactSummaryProperties> result = new ArrayList<>(propertyRequests.size());
    for (ArtifactPropertiesRequest propertiesRequest : propertyRequests) {
        NamespaceId requestNamespace = propertiesRequest.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespace;
        ArtifactId artifactId = validateAndGetArtifactId(requestNamespace, propertiesRequest.getName(), propertiesRequest.getVersion());
        ArtifactDetail artifactDetail;
        try {
            artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(artifactId));
        } catch (ArtifactNotFoundException e) {
            continue;
        }
        Map<String, String> properties = artifactDetail.getMeta().getProperties();
        Map<String, String> filteredProperties = new HashMap<>(propertiesRequest.getProperties().size());
        for (String propertyKey : propertiesRequest.getProperties()) {
            if (properties.containsKey(propertyKey)) {
                filteredProperties.put(propertyKey, properties.get(propertyKey));
            }
        }
        result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), propertiesRequest.getVersion(), propertiesRequest.getScope(), filteredProperties));
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
Also used : InputStreamReader(java.io.InputStreamReader) ArtifactId(co.cask.cdap.proto.id.ArtifactId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) JsonSyntaxException(com.google.gson.JsonSyntaxException) ArtifactPropertiesRequest(co.cask.cdap.proto.artifact.ArtifactPropertiesRequest) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ArtifactSummaryProperties(co.cask.cdap.proto.artifact.ArtifactSummaryProperties) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

ArtifactId (co.cask.cdap.proto.id.ArtifactId)105 Test (org.junit.Test)45 NamespaceId (co.cask.cdap.proto.id.NamespaceId)40 IOException (java.io.IOException)29 Path (javax.ws.rs.Path)29 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)24 ApplicationId (co.cask.cdap.proto.id.ApplicationId)22 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)15 PluginClass (co.cask.cdap.api.plugin.PluginClass)15 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)14 AppRequest (co.cask.cdap.proto.artifact.AppRequest)14 ProgramId (co.cask.cdap.proto.id.ProgramId)14 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)13 File (java.io.File)13 Id (co.cask.cdap.common.id.Id)11 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)11 Map (java.util.Map)10 BadRequestException (co.cask.cdap.common.BadRequestException)9 ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)8 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)8