Search in sources :

Example 1 with InstanceProfileCredentialsProvider

use of com.aliyuncs.auth.InstanceProfileCredentialsProvider in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testClientCredentialProvider.

/**
 * run only on aliyun ecs, and that ecs need bind a RAM Role
 */
public void testClientCredentialProvider() {
    // init CredentialProvider
    String roleName = "ECSAccessingFCTestRole";
    InstanceProfileCredentialsProvider credsProvider = new InstanceProfileCredentialsProvider(roleName);
    // init fc client
    Config config = new Config(REGION, ACCOUNT_ID, credsProvider, false);
    FunctionComputeClient fcClient = new FunctionComputeClient(config);
    client = fcClient;
    // Create a service
    try {
        createService(SERVICE_NAME, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // create a function
    try {
        createFunction(FUNCTION_NAME);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // Invoke the function with a string as function event parameter, Sync mode
    InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    String payload = "Hello FunctionCompute!";
    invkReq.setPayload(payload.getBytes());
    InvokeFunctionResponse invkResp = client.invokeFunction(invkReq);
    System.out.println(new String(invkResp.getContent()));
    cleanUpFunctions(SERVICE_NAME);
    cleanupService(SERVICE_NAME);
}
Also used : InstanceProfileCredentialsProvider(com.aliyuncs.auth.InstanceProfileCredentialsProvider) Config(com.aliyuncs.fc.config.Config) SignURLConfig(com.aliyuncs.fc.auth.SignURLConfig) NasMountConfig(com.aliyuncs.fc.model.NasConfig.NasMountConfig) FunctionComputeClient(com.aliyuncs.fc.client.FunctionComputeClient) JSONException(org.json.JSONException) ParseException(java.text.ParseException) ClientException(com.aliyuncs.fc.exceptions.ClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 2 with InstanceProfileCredentialsProvider

use of com.aliyuncs.auth.InstanceProfileCredentialsProvider in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testClientCredentialProviderMock.

@Test
public void testClientCredentialProviderMock() {
    // init CredentialProvider
    String ak = "ak";
    String sk = "sk";
    String stsToken = "sts_token";
    BasicSessionCredentials creds = new BasicSessionCredentials(ak, sk, stsToken);
    // mock
    InstanceProfileCredentialsProvider credsProvider = mock(InstanceProfileCredentialsProvider.class);
    try {
        when(credsProvider.getCredentials()).thenReturn(creds);
    } catch (com.aliyuncs.exceptions.ClientException e) {
        e.printStackTrace();
    }
    // init fc client
    Config config = new Config(REGION, ACCOUNT_ID, credsProvider, false);
    FunctionComputeClient fcClient = new FunctionComputeClient(config);
    client = fcClient;
    // Create a service
    try {
        createService(SERVICE_NAME);
    } catch (Exception e) {
    }
    assertEquals(creds.getAccessKeyId(), config.getAccessKeyID());
    assertEquals(creds.getAccessKeySecret(), config.getAccessKeySecret());
    assertEquals(creds.getSessionToken(), config.getSecurityToken());
}
Also used : InstanceProfileCredentialsProvider(com.aliyuncs.auth.InstanceProfileCredentialsProvider) BasicSessionCredentials(com.aliyuncs.auth.BasicSessionCredentials) Config(com.aliyuncs.fc.config.Config) SignURLConfig(com.aliyuncs.fc.auth.SignURLConfig) NasMountConfig(com.aliyuncs.fc.model.NasConfig.NasMountConfig) FunctionComputeClient(com.aliyuncs.fc.client.FunctionComputeClient) JSONException(org.json.JSONException) ParseException(java.text.ParseException) ClientException(com.aliyuncs.fc.exceptions.ClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Aggregations

InstanceProfileCredentialsProvider (com.aliyuncs.auth.InstanceProfileCredentialsProvider)2 SignURLConfig (com.aliyuncs.fc.auth.SignURLConfig)2 FunctionComputeClient (com.aliyuncs.fc.client.FunctionComputeClient)2 Config (com.aliyuncs.fc.config.Config)2 ClientException (com.aliyuncs.fc.exceptions.ClientException)2 NasMountConfig (com.aliyuncs.fc.model.NasConfig.NasMountConfig)2 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ParseException (java.text.ParseException)2 JSONException (org.json.JSONException)2 BasicSessionCredentials (com.aliyuncs.auth.BasicSessionCredentials)1