Search in sources :

Example 1 with KeyParser

use of org.platformlayer.crypto.KeyParser in project platformlayer by platformlayer.

the class GoogleComputeClientFactory method getComputeClient.

public GoogleComputeClient getComputeClient(GoogleCloud cloud) throws OpsException {
    KeyParser parser = new KeyParser();
    Object parsed = parser.parse(cloud.serviceAccountKey.plaintext());
    PrivateKey privateKey;
    if (parsed == null) {
        throw new OpsException("Cannot parse private key");
    } else if (parsed instanceof PrivateKey) {
        privateKey = (PrivateKey) parsed;
    } else {
        throw new OpsException("Expected private key, found: " + parsed.getClass().getSimpleName());
    }
    // Build service account credential.
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY).setServiceAccountId(cloud.serviceAccountId).setServiceAccountScopes(ComputeScopes.COMPUTE).setServiceAccountPrivateKey(privateKey).build();
    Compute compute = new Compute(HTTP_TRANSPORT, JSON_FACTORY, credential);
    return new GoogleComputeClient(platformLayerClient, compute, cloud.projectId);
}
Also used : OpsException(org.platformlayer.ops.OpsException) PrivateKey(java.security.PrivateKey) Compute(com.google.api.services.compute.Compute) KeyParser(org.platformlayer.crypto.KeyParser) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential)

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 Compute (com.google.api.services.compute.Compute)1 PrivateKey (java.security.PrivateKey)1 KeyParser (org.platformlayer.crypto.KeyParser)1 OpsException (org.platformlayer.ops.OpsException)1