Search in sources :

Example 1 with OperationValueResultImpl

use of io.github.satr.common.OperationValueResultImpl in project intellij-idea-plugin-connector-for-aws-lambda by satr.

the class FunctionConnectorModel method getCredentialProfiles.

public OperationValueResult<List<CredentialProfileEntity>> getCredentialProfiles() {
    OperationValueResult<List<CredentialProfileEntity>> valueResult = new OperationValueResultImpl<>();
    ArrayList<CredentialProfileEntity> credentialProfilesEntries = new ArrayList<>();
    valueResult.setValue(credentialProfilesEntries);
    if (!validateCredentialProfilesExist()) {
        valueResult.addWarning("No credential profiles file found.\n To create one - please follow the instruction:\n https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html ");
        return valueResult;
    }
    ProfilesConfigFile profilesConfigFile = new ProfilesConfigFile();
    Map<String, BasicProfile> profiles = profilesConfigFile.getAllBasicProfiles();
    for (String credentialProfileName : profiles.keySet()) {
        credentialProfilesEntries.add(new CredentialProfileEntity(credentialProfileName, profiles.get(credentialProfileName)));
    }
    valueResult.addInfo("Found %d credential profiles.", credentialProfilesEntries.size());
    return valueResult;
}
Also used : OperationValueResultImpl(io.github.satr.common.OperationValueResultImpl) BasicProfile(com.amazonaws.auth.profile.internal.BasicProfile) ProfilesConfigFile(com.amazonaws.auth.profile.ProfilesConfigFile)

Example 2 with OperationValueResultImpl

use of io.github.satr.common.OperationValueResultImpl in project intellij-idea-plugin-connector-for-aws-lambda by satr.

the class FunctionConnectorModel method deleteAwsLogStreamsFor.

public OperationValueResult deleteAwsLogStreamsFor(String functionName) {
    OperationValueResult operationResult = new OperationValueResultImpl();
    LogGroup logGroup = getLogGroupForAwsLambdaFunction(functionName);
    if (logGroup == null) {
        operationResult.addError("Not found log group for the function \"%s\"", functionName);
        return operationResult;
    }
    DeleteLogGroupResult deleteLogGroupResult = awsLogClient.deleteLogGroup(new DeleteLogGroupRequest(logGroup.getLogGroupName()));
    int httpStatusCode = deleteLogGroupResult.getSdkHttpMetadata().getHttpStatusCode();
    if (httpStatusCode == HttpStatusCode.OK.getCode()) {
        return operationResult;
    }
    operationResult.addError("Operation responded with code %d", httpStatusCode);
    return operationResult;
}
Also used : OperationValueResultImpl(io.github.satr.common.OperationValueResultImpl) OperationValueResult(io.github.satr.common.OperationValueResult)

Example 3 with OperationValueResultImpl

use of io.github.satr.common.OperationValueResultImpl in project intellij-idea-plugin-connector-for-aws-lambda by satr.

the class FunctionConnectorModel method invokeFunction.

public OperationValueResult<String> invokeFunction(final String functionName, final String inputText) {
    OperationValueResult<String> operationResult = new OperationValueResultImpl<>();
    try {
        InvokeRequest invokeRequest = new InvokeRequest();
        invokeRequest.setFunctionName(functionName);
        invokeRequest.setPayload(inputText);
        final InvokeResult invokeResult = awsLambdaClient.invoke(invokeRequest);
        operationResult.addInfo("Invoked function \"%s\". Result status code: %d", functionName, invokeResult.getStatusCode());
        String functionError = invokeResult.getFunctionError();
        if (!isEmpty(functionError)) {
            operationResult.addError(functionError);
        }
        String logResult = invokeResult.getLogResult();
        if (!isEmpty(logResult)) {
            operationResult.addInfo(logResult);
        }
        ByteBuffer byteBuffer = invokeResult.getPayload();
        String rawJson = new String(byteBuffer.array(), "UTF-8");
        operationResult.setValue(rawJson);
    } catch (Exception e) {
        e.printStackTrace();
        operationResult.addError(e.getMessage());
    }
    return operationResult;
}
Also used : OperationValueResultImpl(io.github.satr.common.OperationValueResultImpl) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) ResourceNotFoundException(com.amazonaws.services.lambda.model.ResourceNotFoundException) IOException(java.io.IOException)

Example 4 with OperationValueResultImpl

use of io.github.satr.common.OperationValueResultImpl in project intellij-idea-plugin-connector-for-aws-lambda by satr.

the class FunctionConnectorModel method updateWithJar.

public OperationValueResult<FunctionEntity> updateWithJar(final FunctionEntity functionEntity, final File file) {
    final OperationValueResultImpl<FunctionEntity> operationResult = new OperationValueResultImpl<>();
    validateLambdaFunctionJarFile(file, operationResult);
    if (operationResult.failed())
        return operationResult;
    try {
        final String readOnlyAccessFileMode = "r";
        try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, readOnlyAccessFileMode);
            final FileChannel fileChannel = randomAccessFile.getChannel()) {
            return updateFunctionCode(functionEntity, fileChannel);
        }
    } catch (InvalidParameterValueException e) {
        operationResult.addError("Invalid request parameters: %s", e.getMessage());
    } catch (ResourceNotFoundException e) {
        operationResult.addError("Function not found.");
    } catch (Exception e) {
        e.printStackTrace();
        operationResult.addError(e.getMessage());
    }
    return operationResult;
}
Also used : OperationValueResultImpl(io.github.satr.common.OperationValueResultImpl) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) ResourceNotFoundException(com.amazonaws.services.lambda.model.ResourceNotFoundException) ResourceNotFoundException(com.amazonaws.services.lambda.model.ResourceNotFoundException) IOException(java.io.IOException)

Example 5 with OperationValueResultImpl

use of io.github.satr.common.OperationValueResultImpl in project intellij-idea-plugin-connector-for-aws-lambda by satr.

the class FunctionConnectorModel method updateFunctionCode.

private OperationValueResult<FunctionEntity> updateFunctionCode(final FunctionEntity functionEntity, final FileChannel fileChannel) throws IOException {
    final OperationValueResultImpl<FunctionEntity> valueResult = new OperationValueResultImpl<>();
    try {
        final MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
        buffer.load();
        final UpdateFunctionCodeRequest request = new UpdateFunctionCodeRequest().withFunctionName(functionEntity.getFunctionName()).withZipFile(buffer);
        final UpdateFunctionCodeResult updateFunctionResult = awsLambdaClient.updateFunctionCode(request);
        valueResult.setValue(new FunctionEntity(updateFunctionResult));
    } catch (IOException e) {
        e.printStackTrace();
        valueResult.addError("Update function error: %s", e.getMessage());
    }
    return valueResult;
}
Also used : OperationValueResultImpl(io.github.satr.common.OperationValueResultImpl) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Aggregations

OperationValueResultImpl (io.github.satr.common.OperationValueResultImpl)5 IOException (java.io.IOException)3 ResourceNotFoundException (com.amazonaws.services.lambda.model.ResourceNotFoundException)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 ProfilesConfigFile (com.amazonaws.auth.profile.ProfilesConfigFile)1 BasicProfile (com.amazonaws.auth.profile.internal.BasicProfile)1 OperationValueResult (io.github.satr.common.OperationValueResult)1 RandomAccessFile (java.io.RandomAccessFile)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1