use of com.aliyuncs.fc.request.GetServiceRequest in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method setup.
@Before
public void setup() {
// Create or clean up everything under the test service
client = new FunctionComputeClient(REGION, ACCOUNT_ID, ACCESS_KEY, SECRET_KEY);
GetServiceRequest getSReq = new GetServiceRequest(SERVICE_NAME);
try {
client.getService(getSReq);
ListFunctionsRequest listFReq = new ListFunctionsRequest(SERVICE_NAME);
ListFunctionsResponse listFResp = client.listFunctions(listFReq);
cleanUpFunctions(SERVICE_NAME, listFResp.getFunctions());
cleanupService(SERVICE_NAME);
} catch (ClientException e) {
if (!ErrorCodes.SERVICE_NOT_FOUND.equals(e.getErrorCode())) {
throw e;
}
}
}
use of com.aliyuncs.fc.request.GetServiceRequest in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testListServices.
@Test
public void testListServices() {
final int numServices = 10;
final int limit = 3;
// Create multiple services
for (int i = 0; i < numServices; i++) {
try {
client.getService(new GetServiceRequest(SERVICE_NAME + i));
cleanupService(SERVICE_NAME + i);
} catch (ClientException e) {
if (!ErrorCodes.SERVICE_NOT_FOUND.equals(e.getErrorCode())) {
throw new RuntimeException("Cleanup failed");
}
}
CreateServiceRequest request = new CreateServiceRequest();
request.setServiceName(SERVICE_NAME + i);
request.setDescription(SERVICE_DESC_OLD);
request.setRole(ROLE);
CreateServiceResponse response = client.createService(request);
assertFalse(Strings.isNullOrEmpty(response.getRequestId()));
}
ListServicesRequest listRequest = new ListServicesRequest();
listRequest.setLimit(limit);
listRequest.setPrefix(SERVICE_NAME);
ListServicesResponse listResponse = client.listServices(listRequest);
int numCalled = 1;
String nextToken = listResponse.getNextToken();
while (nextToken != null) {
listRequest.setNextToken(nextToken);
listResponse = client.listServices(listRequest);
nextToken = listResponse.getNextToken();
numCalled++;
}
assertEquals(numServices / limit + 1, numCalled);
// Delete services
for (int i = 0; i < numServices; i++) {
cleanupService(SERVICE_NAME + i);
}
}
use of com.aliyuncs.fc.request.GetServiceRequest in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testCRUDHelper.
private void testCRUDHelper(boolean testTrigger) throws ParseException, InterruptedException {
// Create Service
CreateServiceResponse createSResp = createService(SERVICE_NAME);
assertFalse(Strings.isNullOrEmpty(createSResp.getRequestId()));
assertFalse(Strings.isNullOrEmpty(createSResp.getServiceId()));
assertEquals(SERVICE_NAME, createSResp.getServiceName());
assertEquals(SERVICE_DESC_OLD, createSResp.getDescription());
assertEquals(ROLE, createSResp.getRole());
GetServiceResponse svcOldResp = client.getService(new GetServiceRequest(SERVICE_NAME));
// Update Service
UpdateServiceRequest updateSReq = new UpdateServiceRequest(SERVICE_NAME);
updateSReq.setDescription(SERVICE_DESC_NEW);
Thread.sleep(1000L);
UpdateServiceResponse updateSResp = client.updateService(updateSReq);
verifyUpdate(svcOldResp.getServiceName(), updateSResp.getServiceName(), svcOldResp.getServiceId(), updateSResp.getServiceId(), svcOldResp.getLastModifiedTime(), updateSResp.getLastModifiedTime(), svcOldResp.getCreatedTime(), updateSResp.getCreatedTime(), svcOldResp.getDescription(), updateSResp.getDescription());
// Get Service
GetServiceRequest getSReq = new GetServiceRequest(SERVICE_NAME);
GetServiceResponse getSResp = client.getService(getSReq);
assertEquals(SERVICE_NAME, getSResp.getServiceName());
assertEquals(svcOldResp.getServiceId(), getSResp.getServiceId());
assertEquals(ROLE, getSResp.getRole());
// Create Function
CreateFunctionResponse createFResp = createFunction(FUNCTION_NAME);
assertFalse(Strings.isNullOrEmpty(createFResp.getRequestId()));
assertFalse(Strings.isNullOrEmpty(createFResp.getFunctionId()));
assertEquals(FUNCTION_NAME, createFResp.getFunctionName());
assertEquals(FUNCTION_DESC_OLD, createFResp.getDescription());
// List Functions
ListFunctionsRequest listFReq = new ListFunctionsRequest(SERVICE_NAME);
ListFunctionsResponse listFResp = client.listFunctions(listFReq);
assertFalse(Strings.isNullOrEmpty(listFResp.getRequestId()));
assertEquals(1, listFResp.getFunctions().length);
FunctionMetadata funcOld = listFResp.getFunctions()[0];
assertEquals(FUNCTION_NAME, funcOld.getFunctionName());
// Update Function
UpdateFunctionRequest updateFReq = new UpdateFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
updateFReq.setDescription(FUNCTION_DESC_NEW);
Thread.sleep(1000L);
client.updateFunction(updateFReq);
listFResp = client.listFunctions(listFReq);
assertFalse(Strings.isNullOrEmpty(listFResp.getRequestId()));
assertEquals(1, listFResp.getFunctions().length);
FunctionMetadata funcNew = listFResp.getFunctions()[0];
verifyUpdate(funcOld.getFunctionName(), funcNew.getFunctionName(), funcOld.getFunctionId(), funcNew.getFunctionId(), funcOld.getLastModifiedTime(), funcNew.getLastModifiedTime(), funcOld.getCreatedTime(), funcNew.getCreatedTime(), funcOld.getDescription(), funcNew.getDescription());
// Get Function
GetFunctionRequest getFReq = new GetFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
GetFunctionResponse getFResp = client.getFunction(getFReq);
assertFalse(Strings.isNullOrEmpty(getFResp.getRequestId()));
assertEquals(FUNCTION_NAME, getFResp.getFunctionName());
// Get Function Code
GetFunctionCodeRequest getFCReq = new GetFunctionCodeRequest(SERVICE_NAME, FUNCTION_NAME);
GetFunctionCodeResponse getFCResp = client.getFunctionCode(getFCReq);
assertFalse(Strings.isNullOrEmpty(getFResp.getRequestId()));
String crc64 = fetchFromURL(getFCResp.getCodeUrl());
assertEquals(crc64, getFCResp.getCodeChecksum());
// Invoke Function
InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
InvokeFunctionResponse invkResp = client.invokeFunction(invkReq);
assertTrue(!Strings.isNullOrEmpty(invkResp.getRequestId()));
assertEquals("hello world", new String(invkResp.getContent()));
// Invoke Function Async
invkReq.setInvocationType(Const.INVOCATION_TYPE_ASYNC);
invkResp = client.invokeFunction(invkReq);
assertEquals(HttpURLConnection.HTTP_ACCEPTED, invkResp.getStatus());
if (testTrigger) {
// Create Trigger
String tfPrefix = "prefix";
String tfSuffix = "suffix";
createTrigger(TRIGGER_NAME, tfPrefix, tfSuffix);
// List Triggers
ListTriggersRequest listTReq = new ListTriggersRequest(SERVICE_NAME, FUNCTION_NAME);
ListTriggersResponse listTResp = client.listTriggers(listTReq);
assertFalse(Strings.isNullOrEmpty(listTResp.getRequestId()));
assertEquals(1, listTResp.getTriggers().length);
TriggerMetadata triggerOld = listTResp.getTriggers()[0];
assertEquals(TRIGGER_NAME, triggerOld.getTriggerName());
// Update Trigger
String newInvocationRole = INVOCATION_ROLE + "_new";
String tfPrefixNew = "prefix_new";
String tfSuffixNew = "suffix_new";
String[] eventsNew = new String[] { "oss:ObjectCreated:PutObject" };
OSSTriggerConfig updateTriggerConfig = new OSSTriggerConfig(new String[] { "oss:ObjectCreated:PutObject" }, tfPrefixNew, tfSuffixNew);
UpdateTriggerRequest updateTReq = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
updateTReq.setInvocationRole(newInvocationRole);
updateTReq.setTriggerConfig(updateTriggerConfig);
UpdateTriggerResponse updateTResp = updateTrigger(updateTReq);
assertEquals(triggerOld.getTriggerName(), updateTResp.getTriggerName());
assertNotEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
assertEquals(triggerOld.getSourceArn(), updateTResp.getSourceArn());
Gson gson = new Gson();
OSSTriggerConfig tcOld = gson.fromJson(gson.toJson(triggerOld.getTriggerConfig()), OSSTriggerConfig.class);
OSSTriggerConfig tcNew = gson.fromJson(gson.toJson(updateTResp.getTriggerConfig()), OSSTriggerConfig.class);
assertFalse(Arrays.deepEquals(tcOld.getEvents(), tcNew.getEvents()));
assertNotEquals(tcOld.getFilter().getKey().getPrefix(), tcNew.getFilter().getKey().getPrefix());
assertNotEquals(tcOld.getFilter().getKey().getSuffix(), tcNew.getFilter().getKey().getSuffix());
assertEquals(triggerOld.getCreatedTime(), updateTResp.getCreatedTime());
assertEquals(triggerOld.getTriggerType(), updateTResp.getTriggerType());
assertNotEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
Date dateOld = DATE_FORMAT.parse(triggerOld.getLastModifiedTime());
Date dateNew = DATE_FORMAT.parse(updateTResp.getLastModifiedTime());
assertTrue(dateOld.before(dateNew));
// Get Trigger
GetTriggerRequest getTReq = new GetTriggerRequest(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
GetTriggerResponse getTResp = client.getTrigger(getTReq);
OSSTriggerConfig getTConfig = gson.fromJson(gson.toJson(getTResp.getTriggerConfig()), OSSTriggerConfig.class);
assertFalse(Strings.isNullOrEmpty(getTResp.getRequestId()));
assertEquals(TRIGGER_NAME, getTResp.getTriggerName());
assertEquals(OSS_SOURCE_ARN, getTResp.getSourceARN());
assertEquals(TRIGGER_TYPE_OSS, getTResp.getTriggerType());
assertEquals(newInvocationRole, getTResp.getInvocationRole());
assertEquals(tfPrefixNew, getTConfig.getFilter().getKey().getPrefix());
assertEquals(tfSuffixNew, getTConfig.getFilter().getKey().getSuffix());
assertTrue(Arrays.deepEquals(eventsNew, getTConfig.getEvents()));
// Delete Trigger
deleteTrigger(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
}
// Delete Function
DeleteFunctionRequest deleteFReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
int numFunctionsOld = listFResp.getFunctions().length;
DeleteFunctionResponse deleteFResp = client.deleteFunction(deleteFReq);
assertFalse(Strings.isNullOrEmpty(deleteFResp.getRequestId()));
listFResp = client.listFunctions(listFReq);
try {
getFunction(FUNCTION_NAME, listFResp.getFunctions());
fail("Function " + FUNCTION_NAME + " failed to be deleted");
} catch (RuntimeException e) {
int numFunctionsNew = (listFResp.getFunctions() == null) ? 0 : listFResp.getFunctions().length;
assertEquals(numFunctionsOld, numFunctionsNew + 1);
}
GetFunctionResponse getFResp2 = null;
try {
getFResp2 = client.getFunction(getFReq);
fail("Get Function " + FUNCTION_NAME + " should have no function returned after delete");
} catch (ClientException e) {
assertNull(getFResp2);
}
// Delete Service
DeleteServiceRequest deleteSReq = new DeleteServiceRequest(SERVICE_NAME);
DeleteServiceResponse deleteSResp = client.deleteService(deleteSReq);
assertFalse(Strings.isNullOrEmpty(deleteSResp.getRequestId()));
GetServiceResponse getSResp2 = null;
try {
getSResp2 = client.getService(getSReq);
fail("Get service " + FUNCTION_NAME + " should have no service returned after delete");
} catch (ClientException e) {
assertNull(getSResp2);
}
}
use of com.aliyuncs.fc.request.GetServiceRequest in project fc-java-sdk by aliyun.
the class FunctionComputeClientTest method testGetServiceValidate.
@Test
public void testGetServiceValidate() {
try {
GetServiceRequest request = new GetServiceRequest(null);
client.getService(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
try {
GetServiceRequest request = new GetServiceRequest("");
client.getService(request);
fail("ClientException is expected");
} catch (ClientException e) {
assertTrue(e.getMessage().contains(VALIDATE_MSG));
}
}
Aggregations