/** * Create a protocol buffer MultiRequest for row mutations. * Does not propagate Action absolute position. Does not set atomic action on the created * RegionAtomic. Caller should do that if wanted. * @param regionName * @param rowMutations * @return a data-laden RegionMutation.Builder * @throws IOException */ public static RegionAction.Builder buildRegionAction(final byte [] regionName, final RowMutations rowMutations) throws IOException { RegionAction.Builder builder = getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName); ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder(); MutationProto.Builder mutationBuilder = MutationProto.newBuilder(); for (Mutation mutation: rowMutations.getMutations()) { MutationType mutateType = null; if (mutation instanceof Put) { mutateType = MutationType.PUT; } else if (mutation instanceof Delete) { mutateType = MutationType.DELETE; } else { throw new DoNotRetryIOException("RowMutations supports only put and delete, not " + mutation.getClass().getName()); } mutationBuilder.clear(); MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder); actionBuilder.clear(); actionBuilder.setMutation(mp); builder.addAction(actionBuilder.build()); } return builder; }
/** * Create a protocol buffer MultiRequest for row mutations that does not hold data. Data/Cells * are carried outside of protobuf. Return references to the Cells in <code>cells</code> param. * Does not propagate Action absolute position. Does not set atomic action on the created * RegionAtomic. Caller should do that if wanted. * @param regionName * @param rowMutations * @param cells Return in here a list of Cells as CellIterable. * @return a region mutation minus data * @throws IOException */ public static RegionAction.Builder buildNoDataRegionAction(final byte[] regionName, final RowMutations rowMutations, final List<CellScannable> cells, final RegionAction.Builder regionActionBuilder, final ClientProtos.Action.Builder actionBuilder, final MutationProto.Builder mutationBuilder) throws IOException { for (Mutation mutation: rowMutations.getMutations()) { MutationType type = null; if (mutation instanceof Put) { type = MutationType.PUT; } else if (mutation instanceof Delete) { type = MutationType.DELETE; } else { throw new DoNotRetryIOException("RowMutations supports only put and delete, not " + mutation.getClass().getName()); } mutationBuilder.clear(); MutationProto mp = ProtobufUtil.toMutationNoData(type, mutation, mutationBuilder); cells.add(mutation); actionBuilder.clear(); regionActionBuilder.addAction(actionBuilder.setMutation(mp).build()); } return regionActionBuilder; }
/** * Create a request to grant user permissions. * * @param username the short user name who to grant permissions * @param actions the permissions to be granted * @return A {@link AccessControlProtos} GrantRequest */ public static AccessControlProtos.GrantRequest buildGrantRequest( String username, AccessControlProtos.Permission.Action... actions) { AccessControlProtos.Permission.Builder ret = AccessControlProtos.Permission.newBuilder(); AccessControlProtos.GlobalPermission.Builder permissionBuilder = AccessControlProtos.GlobalPermission.newBuilder(); for (AccessControlProtos.Permission.Action a : actions) { permissionBuilder.addAction(a); } ret.setType(AccessControlProtos.Permission.Type.Global) .setGlobalPermission(permissionBuilder); return AccessControlProtos.GrantRequest.newBuilder() .setUserPermission( AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFromUtf8(username)) .setPermission(ret) ).build(); }
/** * Create a request to grant user permissions. * * @param username the short user name who to grant permissions * @param namespace optional table name the permissions apply * @param actions the permissions to be granted * @return A {@link AccessControlProtos} GrantRequest */ public static AccessControlProtos.GrantRequest buildGrantRequest( String username, String namespace, AccessControlProtos.Permission.Action... actions) { AccessControlProtos.Permission.Builder ret = AccessControlProtos.Permission.newBuilder(); AccessControlProtos.NamespacePermission.Builder permissionBuilder = AccessControlProtos.NamespacePermission.newBuilder(); for (AccessControlProtos.Permission.Action a : actions) { permissionBuilder.addAction(a); } if (namespace != null) { permissionBuilder.setNamespaceName(ByteString.copyFromUtf8(namespace)); } ret.setType(AccessControlProtos.Permission.Type.Namespace) .setNamespacePermission(permissionBuilder); return AccessControlProtos.GrantRequest.newBuilder() .setUserPermission( AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFromUtf8(username)) .setPermission(ret) ).build(); }
/** * Create a request to revoke user permissions. * * @param username the short user name whose permissions to be revoked * @param actions the permissions to be revoked * @return A {@link AccessControlProtos} RevokeRequest */ public static AccessControlProtos.RevokeRequest buildRevokeRequest( String username, AccessControlProtos.Permission.Action... actions) { AccessControlProtos.Permission.Builder ret = AccessControlProtos.Permission.newBuilder(); AccessControlProtos.GlobalPermission.Builder permissionBuilder = AccessControlProtos.GlobalPermission.newBuilder(); for (AccessControlProtos.Permission.Action a : actions) { permissionBuilder.addAction(a); } ret.setType(AccessControlProtos.Permission.Type.Global) .setGlobalPermission(permissionBuilder); return AccessControlProtos.RevokeRequest.newBuilder() .setUserPermission( AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFromUtf8(username)) .setPermission(ret) ).build(); }
/** * Create a request to revoke user permissions. * * @param username the short user name whose permissions to be revoked * @param namespace optional table name the permissions apply * @param actions the permissions to be revoked * @return A {@link AccessControlProtos} RevokeRequest */ public static AccessControlProtos.RevokeRequest buildRevokeRequest( String username, String namespace, AccessControlProtos.Permission.Action... actions) { AccessControlProtos.Permission.Builder ret = AccessControlProtos.Permission.newBuilder(); AccessControlProtos.NamespacePermission.Builder permissionBuilder = AccessControlProtos.NamespacePermission.newBuilder(); for (AccessControlProtos.Permission.Action a : actions) { permissionBuilder.addAction(a); } if (namespace != null) { permissionBuilder.setNamespaceName(ByteString.copyFromUtf8(namespace)); } ret.setType(AccessControlProtos.Permission.Type.Namespace) .setNamespacePermission(permissionBuilder); return AccessControlProtos.RevokeRequest.newBuilder() .setUserPermission( AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFromUtf8(username)) .setPermission(ret) ).build(); }
/** * Create a request to grant user permissions. * * @param username the short user name who to grant permissions * @param table optional table name the permissions apply * @param family optional column family * @param qualifier optional qualifier * @param actions the permissions to be granted * @return A {@link AccessControlProtos} GrantRequest */ public static AccessControlProtos.GrantRequest buildGrantRequest( String username, byte[] table, byte[] family, byte[] qualifier, AccessControlProtos.Permission.Action... actions) { AccessControlProtos.Permission.Builder permissionBuilder = AccessControlProtos.Permission.newBuilder(); for (AccessControlProtos.Permission.Action a : actions) { permissionBuilder.addAction(a); } if (table != null) { permissionBuilder.setTable(ByteString.copyFrom(table)); } if (family != null) { permissionBuilder.setFamily(ByteString.copyFrom(family)); } if (qualifier != null) { permissionBuilder.setQualifier(ByteString.copyFrom(qualifier)); } return AccessControlProtos.GrantRequest.newBuilder() .setPermission( AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFromUtf8(username)) .setPermission(permissionBuilder.build()) ).build(); }
/** * Create a request to revoke user permissions. * * @param username the short user name whose permissions to be revoked * @param table optional table name the permissions apply * @param family optional column family * @param qualifier optional qualifier * @param actions the permissions to be revoked * @return A {@link AccessControlProtos} RevokeRequest */ public static AccessControlProtos.RevokeRequest buildRevokeRequest( String username, byte[] table, byte[] family, byte[] qualifier, AccessControlProtos.Permission.Action... actions) { AccessControlProtos.Permission.Builder permissionBuilder = AccessControlProtos.Permission.newBuilder(); for (AccessControlProtos.Permission.Action a : actions) { permissionBuilder.addAction(a); } if (table != null) { permissionBuilder.setTable(ByteString.copyFrom(table)); } if (family != null) { permissionBuilder.setFamily(ByteString.copyFrom(family)); } if (qualifier != null) { permissionBuilder.setQualifier(ByteString.copyFrom(qualifier)); } return AccessControlProtos.RevokeRequest.newBuilder() .setPermission( AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFromUtf8(username)) .setPermission(permissionBuilder.build()) ).build(); }
/** * Convert a client Permission to a Permission proto * * @param perm the client Permission * @return the protobuf Permission */ public static AccessControlProtos.Permission toPermission(Permission perm) { AccessControlProtos.Permission.Builder builder = AccessControlProtos.Permission.newBuilder(); if (perm instanceof TablePermission) { TablePermission tablePerm = (TablePermission)perm; if (tablePerm.hasTable()) { builder.setTable(ByteString.copyFrom(tablePerm.getTable())); } if (tablePerm.hasFamily()) { builder.setFamily(ByteString.copyFrom(tablePerm.getFamily())); } if (tablePerm.hasQualifier()) { builder.setQualifier(ByteString.copyFrom(tablePerm.getQualifier())); } } for (Permission.Action a : perm.getActions()) { builder.addAction(toPermissionAction(a)); } return builder.build(); }
/** * Converts a Permission.Action proto to a client Permission.Action object. * * @param action the protobuf Action * @return the converted Action */ public static Permission.Action toPermissionAction( AccessControlProtos.Permission.Action action) { switch (action) { case READ: return Permission.Action.READ; case WRITE: return Permission.Action.WRITE; case EXEC: return Permission.Action.EXEC; case CREATE: return Permission.Action.CREATE; case ADMIN: return Permission.Action.ADMIN; } throw new IllegalArgumentException("Unknown action value "+action.name()); }
/** * Convert a client Permission.Action to a Permission.Action proto * * @param action the client Action * @return the protobuf Action */ public static AccessControlProtos.Permission.Action toPermissionAction( Permission.Action action) { switch (action) { case READ: return AccessControlProtos.Permission.Action.READ; case WRITE: return AccessControlProtos.Permission.Action.WRITE; case EXEC: return AccessControlProtos.Permission.Action.EXEC; case CREATE: return AccessControlProtos.Permission.Action.CREATE; case ADMIN: return AccessControlProtos.Permission.Action.ADMIN; } throw new IllegalArgumentException("Unknown action value "+action.name()); }
/** * Convert a client user permission to a user permission proto * * @param perm the client UserPermission * @return the protobuf UserPermission */ public static AccessControlProtos.UserPermission toUserPermission(UserPermission perm) { AccessControlProtos.Permission.Builder permissionBuilder = AccessControlProtos.Permission.newBuilder(); for (Permission.Action a : perm.getActions()) { permissionBuilder.addAction(toPermissionAction(a)); } if (perm.hasTable()) { permissionBuilder.setTable(ByteString.copyFrom(perm.getTable())); } if (perm.hasFamily()) { permissionBuilder.setFamily(ByteString.copyFrom(perm.getFamily())); } if (perm.hasQualifier()) { permissionBuilder.setQualifier(ByteString.copyFrom(perm.getQualifier())); } return AccessControlProtos.UserPermission.newBuilder() .setUser(ByteString.copyFrom(perm.getUser())) .setPermission(permissionBuilder) .build(); }
/** * Converts a user permission proto to a client user permission object. * * @param proto the protobuf UserPermission * @return the converted UserPermission */ public static UserPermission toUserPermission(AccessControlProtos.UserPermission proto) { AccessControlProtos.Permission permission = proto.getPermission(); List<Permission.Action> actions = toPermissionActions(permission.getActionList()); byte[] qualifier = null; byte[] family = null; byte[] table = null; if (permission.hasTable()) table = permission.getTable().toByteArray(); if (permission.hasFamily()) family = permission.getFamily().toByteArray(); if (permission.hasQualifier()) qualifier = permission.getQualifier().toByteArray(); return new UserPermission(proto.getUser().toByteArray(), table, family, qualifier, actions.toArray(new Permission.Action[actions.size()])); }
/** * Create a protocol buffer MutateRequest for conditioned row mutations * * @param regionName * @param row * @param family * @param qualifier * @param comparator * @param compareType * @param rowMutations * @return a mutate request * @throws IOException */ public static ClientProtos.MultiRequest buildMutateRequest( final byte[] regionName, final byte[] row, final byte[] family, final byte [] qualifier, final ByteArrayComparable comparator, final CompareType compareType, final RowMutations rowMutations) throws IOException { RegionAction.Builder builder = getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName); builder.setAtomic(true); ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder(); MutationProto.Builder mutationBuilder = MutationProto.newBuilder(); Condition condition = buildCondition( row, family, qualifier, comparator, compareType); for (Mutation mutation: rowMutations.getMutations()) { MutationType mutateType = null; if (mutation instanceof Put) { mutateType = MutationType.PUT; } else if (mutation instanceof Delete) { mutateType = MutationType.DELETE; } else { throw new DoNotRetryIOException("RowMutations supports only put and delete, not " + mutation.getClass().getName()); } mutationBuilder.clear(); MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder); actionBuilder.clear(); actionBuilder.setMutation(mp); builder.addAction(actionBuilder.build()); } ClientProtos.MultiRequest request = ClientProtos.MultiRequest.newBuilder().addRegionAction(builder.build()) .setCondition(condition).build(); return request; }