Skip to content

Commit

Permalink
Add API translation method for setting admin object metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCreosote committed Oct 25, 2023
1 parent 2582975 commit ef0430d
Show file tree
Hide file tree
Showing 4 changed files with 519 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/us/kbase/workspace/AlterAdminObjectMetadataParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

package us.kbase.workspace;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
* <p>Original spec-file type: AlterAdminObjectMetadataParams</p>
* <pre>
* Input parameters for the alter_admin_object_metadata method.
* updates - the metadata updates to apply to the objects. If the same object is specified
* twice in the list, the update order is unspecified.
* </pre>
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"updates"
})
public class AlterAdminObjectMetadataParams {

@JsonProperty("updates")
private List<ObjectMetadataUpdate> updates;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("updates")
public List<ObjectMetadataUpdate> getUpdates() {
return updates;
}

@JsonProperty("updates")
public void setUpdates(List<ObjectMetadataUpdate> updates) {
this.updates = updates;
}

public AlterAdminObjectMetadataParams withUpdates(List<ObjectMetadataUpdate> updates) {
this.updates = updates;
return this;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperties(String name, Object value) {
this.additionalProperties.put(name, value);
}

@Override
public String toString() {
return ((((("AlterAdminObjectMetadataParams"+" [updates=")+ updates)+", additionalProperties=")+ additionalProperties)+"]");
}

}
163 changes: 163 additions & 0 deletions src/us/kbase/workspace/ObjectMetadataUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@

package us.kbase.workspace;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;


/**
* <p>Original spec-file type: ObjectMetadataUpdate</p>
* <pre>
* An object metadata update specification.
* Required arguments:
* ObjectIdentity oi - the object to be altered
* One or both of the following arguments are required:
* usermeta new - metadata to assign to the workspace. Duplicate keys will
* be overwritten.
* list<string> remove - these keys will be removed from the workspace
* metadata key/value pairs.
* </pre>
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"oi",
"new",
"remove"
})
public class ObjectMetadataUpdate {

/**
* <p>Original spec-file type: ObjectIdentity</p>
* <pre>
* An object identifier.
* Select an object by either:
* One, and only one, of the numerical id or name of the workspace.
* ws_id wsid - the numerical ID of the workspace.
* ws_name workspace - the name of the workspace.
* AND
* One, and only one, of the numerical id or name of the object.
* obj_id objid- the numerical ID of the object.
* obj_name name - name of the object.
* OPTIONALLY
* obj_ver ver - the version of the object.
* OR an object reference string:
* obj_ref ref - an object reference string.
* </pre>
*
*/
@JsonProperty("oi")
private ObjectIdentity oi;
@JsonProperty("new")
private Map<String, String> _new;
@JsonProperty("remove")
private List<String> remove;
private Map<java.lang.String, Object> additionalProperties = new HashMap<java.lang.String, Object>();

/**
* <p>Original spec-file type: ObjectIdentity</p>
* <pre>
* An object identifier.
* Select an object by either:
* One, and only one, of the numerical id or name of the workspace.
* ws_id wsid - the numerical ID of the workspace.
* ws_name workspace - the name of the workspace.
* AND
* One, and only one, of the numerical id or name of the object.
* obj_id objid- the numerical ID of the object.
* obj_name name - name of the object.
* OPTIONALLY
* obj_ver ver - the version of the object.
* OR an object reference string:
* obj_ref ref - an object reference string.
* </pre>
*
*/
@JsonProperty("oi")
public ObjectIdentity getOi() {
return oi;
}

/**
* <p>Original spec-file type: ObjectIdentity</p>
* <pre>
* An object identifier.
* Select an object by either:
* One, and only one, of the numerical id or name of the workspace.
* ws_id wsid - the numerical ID of the workspace.
* ws_name workspace - the name of the workspace.
* AND
* One, and only one, of the numerical id or name of the object.
* obj_id objid- the numerical ID of the object.
* obj_name name - name of the object.
* OPTIONALLY
* obj_ver ver - the version of the object.
* OR an object reference string:
* obj_ref ref - an object reference string.
* </pre>
*
*/
@JsonProperty("oi")
public void setOi(ObjectIdentity oi) {
this.oi = oi;
}

public ObjectMetadataUpdate withOi(ObjectIdentity oi) {
this.oi = oi;
return this;
}

@JsonProperty("new")
public Map<String, String> getNew() {
return _new;
}

@JsonProperty("new")
public void setNew(Map<String, String> _new) {
this._new = _new;
}

public ObjectMetadataUpdate withNew(Map<String, String> _new) {
this._new = _new;
return this;
}

@JsonProperty("remove")
public List<String> getRemove() {
return remove;
}

@JsonProperty("remove")
public void setRemove(List<String> remove) {
this.remove = remove;
}

public ObjectMetadataUpdate withRemove(List<String> remove) {
this.remove = remove;
return this;
}

@JsonAnyGetter
public Map<java.lang.String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperties(java.lang.String name, Object value) {
this.additionalProperties.put(name, value);
}

@Override
public java.lang.String toString() {
return ((((((((("ObjectMetadataUpdate"+" [oi=")+ oi)+", _new=")+ _new)+", remove=")+ remove)+", additionalProperties=")+ additionalProperties)+"]");
}

}
67 changes: 67 additions & 0 deletions src/us/kbase/workspace/kbase/WorkspaceServerMethods.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.kbase.workspace.kbase;

import static java.util.Objects.requireNonNull;
import static us.kbase.common.utils.ServiceUtils.checkAddlArgs;
import static us.kbase.workspace.kbase.ArgUtils.checkLong;
import static us.kbase.workspace.kbase.ArgUtils.chooseInstant;
Expand Down Expand Up @@ -29,6 +30,9 @@
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.kbase.auth.AuthException;
import us.kbase.auth.AuthToken;
import us.kbase.auth.ConfigurableAuthService;
Expand All @@ -42,6 +46,7 @@
import us.kbase.typedobj.idref.IdReferenceHandlerSetFactory;
import us.kbase.typedobj.idref.IdReferenceHandlerSetFactoryBuilder;
import us.kbase.typedobj.idref.IdReferencePermissionHandlerSet;
import us.kbase.workspace.AlterAdminObjectMetadataParams;
import us.kbase.workspace.CreateWorkspaceParams;
import us.kbase.workspace.GetObjectInfo3Params;
import us.kbase.workspace.GetObjectInfo3Results;
Expand All @@ -53,6 +58,7 @@
import us.kbase.workspace.ListWorkspaceInfoParams;
import us.kbase.workspace.ObjectData;
import us.kbase.workspace.ObjectIdentity;
import us.kbase.workspace.ObjectMetadataUpdate;
import us.kbase.workspace.ObjectSaveData;
import us.kbase.workspace.SaveObjectsParams;
import us.kbase.workspace.SetGlobalPermissionsParams;
Expand All @@ -61,11 +67,13 @@
import us.kbase.workspace.WorkspacePermissions;
import us.kbase.workspace.database.DependencyStatus;
import us.kbase.workspace.database.ListObjectsParameters;
import us.kbase.workspace.database.MetadataUpdate;
import us.kbase.workspace.database.ObjectIDNoWSNoVer;
import us.kbase.workspace.database.ObjectIdentifier;
import us.kbase.workspace.database.ObjectInformation;
import us.kbase.workspace.database.Permission;
import us.kbase.workspace.database.RefLimit;
import us.kbase.workspace.database.ResolvedObjectID;
import us.kbase.workspace.database.User;
import us.kbase.workspace.database.UserWorkspaceIDs;
import us.kbase.workspace.database.Workspace;
Expand Down Expand Up @@ -105,6 +113,10 @@ public WorkspaceServerMethods(
this.auth = auth;
}

private static Logger getLogger() {
return LoggerFactory.getLogger(WorkspaceServerMethods.class);
}

/** Get the core workspace instance underlying this server -> core translation layer.
* @return the workspace.
*/
Expand Down Expand Up @@ -640,4 +652,59 @@ Long, Map<String, String>>> getObjectHistory(
final ObjectIdentifier oi = processObjectIdentifier(object);
return objInfoToTuple(ws.getObjectHistory(user, oi, asAdmin), true, false);
}

/** Set administrative metadata on an object. This method is reserver for full workspace
* administrators only and should not be exposed in a public API.
* @param params the method parameters.
* @throws NoSuchObjectException if one of the objects doesn't exist.
* @throws CorruptWorkspaceDBException if the workspace database is corrupt.
* @throws WorkspaceCommunicationException if a communication error occurs contacting the
* database.
* @throws InaccessibleObjectException if one of the objects is inaccessible.
*/
public void setAdminObjectMetadata(final AlterAdminObjectMetadataParams params)
// TODO CODE corrupt & comm exceptions should be unchecked, there's no recovery
// and it's not the user's fault
throws WorkspaceCommunicationException, InaccessibleObjectException,
CorruptWorkspaceDBException, NoSuchObjectException {
checkAddlArgs(
requireNonNull(params, "params").getAdditionalProperties(), params.getClass());
if (params.getUpdates() == null || params.getUpdates().isEmpty()) {
throw new IllegalArgumentException("updates list cannot be empty");
}
final Map<ObjectIdentifier, MetadataUpdate> update = new HashMap<>();
final ListIterator<ObjectMetadataUpdate> iter = params.getUpdates().listIterator();
while (iter.hasNext()) {
final ObjectMetadataUpdate u = iter.next();
try {
if (u == null) {
throw new NullPointerException(
ObjectMetadataUpdate.class.getSimpleName() + " cannot be null");
}
checkAddlArgs(u.getAdditionalProperties(), ObjectMetadataUpdate.class);
final MetadataUpdate mu = new MetadataUpdate(
new WorkspaceUserMetadata(u.getNew()), u.getRemove());
if (!mu.hasUpdate()) {
throw new IllegalArgumentException("A metadata update is required");
}
update.put(processObjectIdentifier(u.getOi()), mu);
} catch (NullPointerException | IllegalArgumentException | MetadataException e) {
// TODO CODE user caused exceptions should be checked & have custom classes
// in preparation for adding error codes. Will need to do this if
// methods are converted to a REST-like API so 400s and 500s can be
// distinguished
throw new IllegalArgumentException(String.format(
"Error processing update index %s: %s",
iter.previousIndex(), e.getMessage()), e);
}
}
final Map<ObjectIdentifier, ResolvedObjectID> objs = ws.setAdminObjectMetadata(update);
for (final ResolvedObjectID r: objs.values()) {
getLogger().info("Object {}/{}/{}",
r.getWorkspaceIdentifier().getID(),
r.getId(),
r.getVersion()
);
}
}
}
Loading

0 comments on commit ef0430d

Please sign in to comment.