Search in sources :

Example 21 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPlugin.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
    Id.Artifact parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
    int limitNumber = Integer.valueOf(limit);
    limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {

        @Override
        public boolean apply(ArtifactId input) {
            // by default, the scoped namespace is for USER scope
            return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
        }
    };
    try {
        SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, parentArtifactId, pluginType, pluginName, predicate, limitNumber, sortOrder);
        List<PluginInfo> pluginInfos = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            PluginClass pluginClass = pluginsEntry.getValue();
            pluginInfos.add(new PluginInfo(pluginClass.getName(), pluginClass.getType(), pluginClass.getDescription(), pluginClass.getClassName(), pluginArtifactSummary, pluginClass.getProperties(), pluginClass.getEndpoints()));
        }
        responder.sendJson(HttpResponseStatus.OK, pluginInfos);
    } catch (PluginNotExistsException e) {
        responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactVersionRange(co.cask.cdap.api.artifact.ArtifactVersionRange) IOException(java.io.IOException) PluginEndpoint(co.cask.cdap.internal.app.runtime.plugin.PluginEndpoint) ArtifactSortOrder(co.cask.cdap.proto.artifact.ArtifactSortOrder) Predicate(com.google.common.base.Predicate) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginInfo(co.cask.cdap.proto.artifact.PluginInfo) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 22 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactStore method clear.

/**
   * Clear all data in the given namespace. Used only in unit tests.
   *
   * @param namespace the namespace to delete data in
   * @throws IOException if there was some problem deleting the data
   */
@VisibleForTesting
void clear(final NamespaceId namespace) throws IOException {
    final Id.Namespace namespaceId = namespace.toId();
    namespacedLocationFactory.get(namespace).append(ARTIFACTS_PATH).delete(true);
    try {
        transactional.execute(new TxRunnable() {

            @Override
            public void run(DatasetContext context) throws Exception {
                // delete all rows about artifacts in the namespace
                Table metaTable = getMetaTable(context);
                Row row;
                try (Scanner scanner = metaTable.scan(scanArtifacts(namespace))) {
                    while ((row = scanner.next()) != null) {
                        metaTable.delete(row.getRow());
                    }
                }
                // delete all rows about artifacts in the namespace and the plugins they have access to
                Scan pluginsScan = new Scan(Bytes.toBytes(String.format("%s:%s:", PLUGIN_PREFIX, namespace.getNamespace())), Bytes.toBytes(String.format("%s:%s;", PLUGIN_PREFIX, namespace.getNamespace())));
                try (Scanner scanner = metaTable.scan(pluginsScan)) {
                    while ((row = scanner.next()) != null) {
                        metaTable.delete(row.getRow());
                    }
                }
                // delete app classes in this namespace
                try (Scanner scanner = metaTable.scan(scanAppClasses(namespace))) {
                    while ((row = scanner.next()) != null) {
                        metaTable.delete(row.getRow());
                    }
                }
                // delete plugins in this namespace from system artifacts
                // for example, if there was an artifact in this namespace that extends a system artifact
                Scan systemPluginsScan = new Scan(Bytes.toBytes(String.format("%s:%s:", PLUGIN_PREFIX, Id.Namespace.SYSTEM.getId())), Bytes.toBytes(String.format("%s:%s;", PLUGIN_PREFIX, Id.Namespace.SYSTEM.getId())));
                try (Scanner scanner = metaTable.scan(systemPluginsScan)) {
                    while ((row = scanner.next()) != null) {
                        for (Map.Entry<byte[], byte[]> columnVal : row.getColumns().entrySet()) {
                            // the column is the id of the artifact the plugin is from
                            ArtifactColumn column = ArtifactColumn.parse(columnVal.getKey());
                            // if the plugin artifact is in the namespace we're deleting, delete this column.
                            if (column.artifactId.getNamespace().equals(namespaceId)) {
                                metaTable.delete(row.getRow(), column.getColumn());
                            }
                        }
                    }
                }
            }
        });
    } catch (TransactionFailureException e) {
        throw Transactions.propagate(e, IOException.class);
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) Table(co.cask.cdap.api.dataset.table.Table) IOException(java.io.IOException) TransactionFailureException(org.apache.tephra.TransactionFailureException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactAlreadyExistsException(co.cask.cdap.common.ArtifactAlreadyExistsException) TransactionConflictException(org.apache.tephra.TransactionConflictException) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) TransactionFailureException(org.apache.tephra.TransactionFailureException) TxRunnable(co.cask.cdap.api.TxRunnable) Scan(co.cask.cdap.api.dataset.table.Scan) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetId(co.cask.cdap.proto.id.DatasetId) Row(co.cask.cdap.api.dataset.table.Row) DatasetContext(co.cask.cdap.api.data.DatasetContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 23 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class ArtifactRepository method addSystemArtifacts.

/**
   * Scan all files in the local system artifact directory, looking for jar files and adding them as system artifacts.
   * If the artifact already exists it will not be added again unless it is a snapshot version.
   *
   * @throws IOException if there was some IO error adding the system artifacts
   */
public void addSystemArtifacts() throws Exception {
    // to add system artifacts, users should have write privileges on the system namespace
    Principal principal = authenticationContext.getPrincipal();
    authorizationEnforcer.enforce(NamespaceId.SYSTEM, principal, Action.WRITE);
    // scan the directory for artifact .jar files and config files for those artifacts
    List<SystemArtifactInfo> systemArtifacts = new ArrayList<>();
    for (File systemArtifactDir : systemArtifactDirs) {
        for (File jarFile : DirUtils.listFiles(systemArtifactDir, "jar")) {
            // parse id from filename
            Id.Artifact artifactId;
            try {
                artifactId = Id.Artifact.parse(Id.Namespace.SYSTEM, jarFile.getName());
            } catch (IllegalArgumentException e) {
                LOG.warn(String.format("Skipping system artifact '%s' because the name is invalid: ", e.getMessage()));
                continue;
            }
            // first revoke any orphane privileges
            co.cask.cdap.proto.id.ArtifactId artifact = artifactId.toEntityId();
            privilegesManager.revoke(artifact);
            // then grant all on the artifact
            privilegesManager.grant(artifact, principal, EnumSet.allOf(Action.class));
            // check for a corresponding .json config file
            String artifactFileName = jarFile.getName();
            String configFileName = artifactFileName.substring(0, artifactFileName.length() - ".jar".length()) + ".json";
            File configFile = new File(systemArtifactDir, configFileName);
            try {
                // read and parse the config file if it exists. Otherwise use an empty config with the artifact filename
                ArtifactConfig artifactConfig = configFile.isFile() ? configReader.read(artifactId.getNamespace(), configFile) : new ArtifactConfig();
                validateParentSet(artifactId, artifactConfig.getParents());
                validatePluginSet(artifactConfig.getPlugins());
                systemArtifacts.add(new SystemArtifactInfo(artifactId, jarFile, artifactConfig));
            } catch (InvalidArtifactException e) {
                LOG.warn(String.format("Could not add system artifact '%s' because it is invalid.", artifactFileName), e);
                // since adding artifact failed, revoke privileges, since they may be orphane now
                privilegesManager.revoke(artifact);
            }
        }
    }
    // taking advantage of the fact that we only have 1 level of dependencies
    // so we can add all the parents first, then we know its safe to add everything else
    // add all parents
    Set<Id.Artifact> parents = new HashSet<>();
    for (SystemArtifactInfo child : systemArtifacts) {
        Id.Artifact childId = child.getArtifactId();
        for (SystemArtifactInfo potentialParent : systemArtifacts) {
            Id.Artifact potentialParentId = potentialParent.getArtifactId();
            // skip if we're looking at ourselves
            if (childId.equals(potentialParentId)) {
                continue;
            }
            if (child.getConfig().hasParent(potentialParentId)) {
                parents.add(potentialParentId);
            }
        }
    }
    // add all parents first
    for (SystemArtifactInfo systemArtifact : systemArtifacts) {
        if (parents.contains(systemArtifact.getArtifactId())) {
            addSystemArtifact(systemArtifact);
        }
    }
    // add children next
    for (SystemArtifactInfo systemArtifact : systemArtifacts) {
        if (!parents.contains(systemArtifact.getArtifactId())) {
            addSystemArtifact(systemArtifact);
        }
    }
}
Also used : Action(co.cask.cdap.proto.security.Action) ArtifactConfig(co.cask.cdap.common.conf.ArtifactConfig) ArrayList(java.util.ArrayList) ArtifactId(co.cask.cdap.api.artifact.ArtifactId) Id(co.cask.cdap.proto.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) EntityId(co.cask.cdap.proto.id.EntityId) File(java.io.File) Principal(co.cask.cdap.proto.security.Principal) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) HashSet(java.util.HashSet)

Example 24 with Id

use of co.cask.cdap.proto.Id in project cdap by caskdata.

the class NamespaceHttpHandler method create.

@PUT
@Path("/namespaces/{namespace-id}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    Id.Namespace namespace;
    try {
        namespace = Id.Namespace.from(namespaceId);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Namespace id can contain only alphanumeric characters or '_'.");
    }
    NamespaceMeta metadata = getNamespaceMeta(request);
    if (isReserved(namespaceId)) {
        throw new BadRequestException(String.format("Cannot create the namespace '%s'. '%s' is a reserved namespace.", namespaceId, namespaceId));
    }
    NamespaceMeta.Builder builder = metadata == null ? new NamespaceMeta.Builder() : new NamespaceMeta.Builder(metadata);
    builder.setName(namespace);
    NamespaceMeta finalMetadata = builder.build();
    try {
        namespaceAdmin.create(finalMetadata);
        responder.sendString(HttpResponseStatus.OK, String.format("Namespace '%s' created successfully.", namespaceId));
    } catch (AlreadyExistsException e) {
        responder.sendString(HttpResponseStatus.OK, String.format("Namespace '%s' already exists.", namespaceId));
    }
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

Id (co.cask.cdap.proto.Id)24 NamespaceId (co.cask.cdap.proto.id.NamespaceId)19 IOException (java.io.IOException)15 Path (javax.ws.rs.Path)14 ArtifactId (co.cask.cdap.proto.id.ArtifactId)13 GET (javax.ws.rs.GET)6 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)5 ProgramId (co.cask.cdap.proto.id.ProgramId)5 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)4 PluginClass (co.cask.cdap.api.plugin.PluginClass)4 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)4 BadRequestException (co.cask.cdap.common.BadRequestException)4 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)4 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)3 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)3 ArtifactAlreadyExistsException (co.cask.cdap.common.ArtifactAlreadyExistsException)3 ArtifactRangeNotFoundException (co.cask.cdap.common.ArtifactRangeNotFoundException)3 NotFoundException (co.cask.cdap.common.NotFoundException)3 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)3 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)3