use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.
the class SpannerStructuredTable method increment.
@Override
public void increment(Collection<Field<?>> keys, String column, long amount) throws InvalidFieldException, IllegalArgumentException {
if (schema.isPrimaryKeyColumn(column)) {
throw new IllegalArgumentException("Cannot use increment on a primary key field");
}
FieldType.Type type = schema.getType(column);
if (type == null) {
throw new InvalidFieldException(schema.getTableId(), column, "Column " + column + " does not exist");
}
if (type != FieldType.Type.LONG) {
throw new IllegalArgumentException(String.format("Trying to increment a column of type %s. Only %s column type can be incremented", type, FieldType.Type.LONG));
}
fieldValidator.validatePrimaryKeys(keys, false);
StructuredRow existing = read(keys, Collections.singleton(column)).orElse(null);
List<Field<?>> fields = new ArrayList<>(keys);
fields.add(Fields.longField(column, amount + (existing == null ? 0L : Objects.requireNonNull(existing.getLong(column)))));
if (existing == null) {
// Insert a new row if there is no existing row
insert(fields);
} else {
// Update the row by incrementing the amount
update(fields);
}
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.
the class OAuthStore method writeProvider.
/**
* Create/update an OAuth provider.
*
* @param oauthProvider {@link OAuthProvider} to write
* @throws OAuthStoreException if the write fails
*/
public void writeProvider(OAuthProvider oauthProvider) throws OAuthStoreException {
try {
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
table.upsert(getRow(oauthProvider));
}, TableNotFoundException.class, InvalidFieldException.class);
} catch (TableNotFoundException e) {
throw new OAuthStoreException("OAuth provider table not found", e);
} catch (InvalidFieldException e) {
throw new OAuthStoreException("OAuth provider object fields do not match table", e);
}
String namespace = NamespaceId.SYSTEM.getNamespace();
try {
secureStoreManager.put(namespace, getClientCredsKey(oauthProvider.getName()), GSON.toJson(oauthProvider.getClientCredentials()), "OAuth client creds", Collections.emptyMap());
} catch (IOException e) {
throw new OAuthStoreException("Failed to write to OAuth provider secure storage", e);
} catch (Exception e) {
throw new OAuthStoreException("Namespace \"" + namespace + "\" does not exist", e);
}
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by cdapio.
the class OAuthStore method getProvider.
/**
* Get an OAuth provider.
*
* @param name name of {@link OAuthProvider} to read
* @throws OAuthStoreException if the read fails
*/
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
OAuthClientCredentials clientCreds;
try {
String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
} catch (IOException e) {
throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
} catch (Exception e) {
return Optional.empty();
}
try {
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
Optional<StructuredRow> row = table.read(getKey(name));
return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
}, TableNotFoundException.class, InvalidFieldException.class);
} catch (TableNotFoundException e) {
throw new OAuthStoreException("OAuth provider table not found", e);
} catch (InvalidFieldException e) {
throw new OAuthStoreException("OAuth provider object fields do not match table", e);
}
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
the class OAuthStore method writeProvider.
/**
* Create/update an OAuth provider.
*
* @param oauthProvider {@link OAuthProvider} to write
* @throws OAuthStoreException if the write fails
*/
public void writeProvider(OAuthProvider oauthProvider) throws OAuthStoreException {
try {
TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
table.upsert(getRow(oauthProvider));
}, TableNotFoundException.class, InvalidFieldException.class);
} catch (TableNotFoundException e) {
throw new OAuthStoreException("OAuth provider table not found", e);
} catch (InvalidFieldException e) {
throw new OAuthStoreException("OAuth provider object fields do not match table", e);
}
String namespace = NamespaceId.SYSTEM.getNamespace();
try {
secureStoreManager.put(namespace, getClientCredsKey(oauthProvider.getName()), GSON.toJson(oauthProvider.getClientCredentials()), "OAuth client creds", Collections.emptyMap());
} catch (IOException e) {
throw new OAuthStoreException("Failed to write to OAuth provider secure storage", e);
} catch (Exception e) {
throw new OAuthStoreException("Namespace \"" + namespace + "\" does not exist", e);
}
}
use of io.cdap.cdap.spi.data.InvalidFieldException in project cdap by caskdata.
the class OAuthStore method getProvider.
/**
* Get an OAuth provider.
*
* @param name name of {@link OAuthProvider} to read
* @throws OAuthStoreException if the read fails
*/
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
OAuthClientCredentials clientCreds;
try {
String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
} catch (IOException e) {
throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
} catch (Exception e) {
return Optional.empty();
}
try {
return TransactionRunners.run(transactionRunner, context -> {
StructuredTable table = context.getTable(TABLE_ID);
Optional<StructuredRow> row = table.read(getKey(name));
return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
}, TableNotFoundException.class, InvalidFieldException.class);
} catch (TableNotFoundException e) {
throw new OAuthStoreException("OAuth provider table not found", e);
} catch (InvalidFieldException e) {
throw new OAuthStoreException("OAuth provider object fields do not match table", e);
}
}
Aggregations