//Execute a specific operation on an entity. Returns a []byte (of a json object) that should be unmarshalled to a specific entity func (entityApi *EntityApi) Execute(id string, operation string, body []byte, options map[string]string) ([]byte, error) { optionsCopy := map[string]string{} for k, v := range options { optionsCopy[k] = v } optionsCopy["operation"] = operation endpoint := entityApi.buildEndpoint() if id != "" { endpoint = endpoint + "/" + id } request := api.CcaRequest{ Method: api.POST, Body: body, Endpoint: endpoint, Options: optionsCopy, } response, err := entityApi.apiClient.Do(request) if err != nil { return nil, err } else if response.IsError() { return nil, api.CcaErrorResponse(*response) } return entityApi.taskService.PollResponse(response, DEFAULT_POLLING_INTERVAL) }
func TestGetTaskReturnErrorIfHasCcaErrors(t *testing.T) { //given ctrl := gomock.NewController(t) defer ctrl.Finish() mockCcaClient := api_mocks.NewMockApiClient(ctrl) taskService := TaskApi{ apiClient: mockCcaClient, } ccaResponse := api.CcaResponse{ StatusCode: 400, Errors: []api.CcaError{api.CcaError{}}, } mockCcaClient.EXPECT().Do(api.CcaRequest{ Method: api.GET, Endpoint: "tasks/" + TEST_TASK_ID, }).Return(&ccaResponse, nil) //when task, err := taskService.Get(TEST_TASK_ID) //then assert.Nil(t, task) assert.Equal(t, api.CcaErrorResponse(ccaResponse), err) }
//Poll an the Task API. Blocks until success or failure func (taskApi *TaskApi) PollResponse(response *api.CcaResponse, milliseconds time.Duration) ([]byte, error) { if strings.EqualFold(response.TaskStatus, SUCCESS) { return response.Data, nil } else if strings.EqualFold(response.TaskStatus, FAILED) { return nil, api.CcaErrorResponse(*response) } return taskApi.Poll(response.TaskId, milliseconds) }
//Get an entity list. Returns a []byte (of a json object) that should be unmarshalled to a specific entity func (entityApi *EntityApi) List(options map[string]string) ([]byte, error) { request := api.CcaRequest{ Method: api.GET, Endpoint: entityApi.buildEndpoint(), Options: options, } response, err := entityApi.apiClient.Do(request) if err != nil { return nil, err } else if response.IsError() { return nil, api.CcaErrorResponse(*response) } return response.Data, nil }
//Get. Returns a []byte (of a json object) that should be unmarshalled to a specific entity func (configurationApi *ConfigurationApi) Get(id string, options map[string]string) ([]byte, error) { request := api.CcaRequest{ Method: api.GET, Endpoint: configurationApi.buildEndpoint() + "/" + id, Options: options, } response, err := configurationApi.apiClient.Do(request) if err != nil { return nil, err } else if response.IsError() { return nil, api.CcaErrorResponse(*response) } return response.Data, nil }
//Create a new entity described in the body parameter (json object). Returns a []byte (of a json object) that should be unmarshalled to a specific entity func (entityApi *EntityApi) Create(body []byte, options map[string]string) ([]byte, error) { request := api.CcaRequest{ Method: api.POST, Body: body, Endpoint: entityApi.buildEndpoint(), Options: options, } response, err := entityApi.apiClient.Do(request) if err != nil { return nil, err } else if response.IsError() { return nil, api.CcaErrorResponse(*response) } return entityApi.taskService.PollResponse(response, DEFAULT_POLLING_INTERVAL) }
//Create as described in the body parameter (json object). Returns a []byte (of a json object) that should be unmarshalled to a specific entity func (configurationApi *ConfigurationApi) Create(body []byte, options map[string]string) ([]byte, error) { request := api.CcaRequest{ Method: api.POST, Body: body, Endpoint: configurationApi.buildEndpoint(), Options: options, } response, err := configurationApi.apiClient.Do(request) if err != nil { return nil, err } else if response.IsError() { return nil, api.CcaErrorResponse(*response) } return response.Data, nil }
func TestExistsReturnFalseIfInstanceDoesntExist(t *testing.T) { //given ctrl := gomock.NewController(t) defer ctrl.Finish() mockEntityService := services_mocks.NewMockEntityService(ctrl) instanceService := InstanceApi{ entityService: mockEntityService, } mockApiError := api.CcaErrorResponse(api.CcaResponse{StatusCode: api.NOT_FOUND}) mockEntityService.EXPECT().Get(TEST_INSTANCE_ID, gomock.Any()).Return([]byte(`{}`), mockApiError) //when exists, err := instanceService.Exists(TEST_INSTANCE_ID) //then assert.Nil(t, err) assert.False(t, exists) }
//Retrieve a Task with sepecified id func (taskApi *TaskApi) Find(id string) (*Task, error) { request := api.CcaRequest{ Method: api.GET, Endpoint: "tasks/" + id, } response, err := taskApi.apiClient.Do(request) if err != nil { return nil, err } else if len(response.Errors) > 0 { return nil, api.CcaErrorResponse(*response) } data := response.Data taskMap := map[string]*json.RawMessage{} json.Unmarshal(data, &taskMap) task := Task{} json.Unmarshal(*taskMap["id"], &task.Id) json.Unmarshal(*taskMap["status"], &task.Status) json.Unmarshal(*taskMap["created"], &task.Created) if val, ok := taskMap["result"]; ok { task.Result = []byte(*val) } return &task, nil }