diff --git a/README.md b/README.md index a20a18b..8f1dfe1 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,9 @@ endpoint, err := underTest.CreateEndpoint(endpoints.Endpoint{ ### Changelog +- v1.3.2 + - fix client custom endpoint headers bug + - improve tests - v1.3 - unnecessary resource updates bug fix. - support tags in alerts diff --git a/endpoints/client_endpoints_create.go b/endpoints/client_endpoints_create.go index ccc35bb..3a8830d 100644 --- a/endpoints/client_endpoints_create.go +++ b/endpoints/client_endpoints_create.go @@ -37,7 +37,7 @@ func buildCreateEndpointRequest(endpoint Endpoint) map[string]interface{} { for k, v := range headers { headerStrings = append(headerStrings, fmt.Sprintf("%s=%s", k, v)) } - headerString := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(headerStrings)), ","), "[]") + headerString := strings.Join(headerStrings, ",") createEndpoint[fldEndpointHeaders] = headerString createEndpoint[fldEndpointBodyTemplate] = endpoint.BodyTemplate } diff --git a/endpoints/endpoints_bigpanda_integration_test.go b/endpoints/endpoints_bigpanda_integration_test.go index 026341b..2c691de 100644 --- a/endpoints/endpoints_bigpanda_integration_test.go +++ b/endpoints/endpoints_bigpanda_integration_test.go @@ -1,5 +1,3 @@ -// +build integration - package endpoints_test import ( @@ -26,7 +24,7 @@ func createBigPandaEndpoint() endpoints.Endpoint { return endpoints.Endpoint{ Title: "bigpandaendpoint", Description: "description", - EndpointType: "big-panda", + EndpointType: "bigpanda", ApiToken: "api_token", AppKey: "app_key", } @@ -36,7 +34,7 @@ func updateBigPandaEndpoint() endpoints.Endpoint { return endpoints.Endpoint{ Title: "bigpandaupdatedendpoint", Description: "updated description", - EndpointType: "big-panda", + EndpointType: "bigpanda", ApiToken: "updated_api_token", AppKey: "updated_app_key", } diff --git a/endpoints/endpoints_create_custom_endpoint_test.go b/endpoints/endpoints_create_custom_endpoint_test.go new file mode 100644 index 0000000..088fa61 --- /dev/null +++ b/endpoints/endpoints_create_custom_endpoint_test.go @@ -0,0 +1,55 @@ +package endpoints_test + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" + + "github.com/logzio/logzio_terraform_client/endpoints" + "github.com/stretchr/testify/assert" +) + +func TestEndpoints_CreateCustomEndpoint(t *testing.T) { + underTest, err, teardown := setupEndpointsTest() + defer teardown() + + endpointId := int64(1234567) + + mux.HandleFunc("/v1/endpoints/custom", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + assert.Equal(t, http.MethodPost, r.Method) + jsonBytes, _ := ioutil.ReadAll(r.Body) + var target map[string]interface{} + err = json.Unmarshal(jsonBytes, &target) + assert.NoError(t, err) + assert.Contains(t, target, "title") + assert.Contains(t, target, "description") + assert.Contains(t, target, "url") + assert.Contains(t, target, "headers") + Headers := strings.Split(fmt.Sprint(target["headers"]), ",") + assert.Equal(t, 2, len(Headers)) + assert.Equal(t, strings.Split(Headers[1], "=")[1], "two words") + + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, fixture("create_endpoint.json")) + }) + + if assert.NoError(t, err) { + endpoint, err := underTest.CreateEndpoint(endpoints.Endpoint{ + Title: "testCreateCustomEndpoint", + Method: "POST", + Description: "my description", + Url: "https://jsonplaceholder.typicode.com/todos/1", + EndpointType: endpoints.EndpointTypeCustom, + Headers: map[string]string{"hello": "there", "header": "two words"}, + BodyTemplate: map[string]string{"hello": "there", "header": "two"}, + }) + assert.NoError(t, err) + assert.NotNil(t, endpoint) + assert.Equal(t, endpointId, endpoint.Id) + } +} diff --git a/endpoints/endpoints_custom_integration_test.go b/endpoints/endpoints_custom_integration_test.go index 9d55e97..6174d89 100644 --- a/endpoints/endpoints_custom_integration_test.go +++ b/endpoints/endpoints_custom_integration_test.go @@ -1,5 +1,3 @@ -// +build integration - package endpoints_test import ( @@ -15,7 +13,7 @@ func TestIntegrationEndpoints_CustomCreateUpdate(t *testing.T) { Title: "testCreateCustomEndpoint", Method: "POST", Description: "my description", - Url: "https://this.is.com/some/other/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "custom", Headers: map[string]string{"hello": "there", "header": "two"}, BodyTemplate: map[string]string{"hello": "there", "header": "two"}, @@ -27,7 +25,7 @@ func TestIntegrationEndpoints_CustomCreateUpdate(t *testing.T) { Title: "testCreateUpdateCustomEndpoint", Method: "POST", Description: "my description update", - Url: "https://this.is.com/some/updated/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "custom", Headers: map[string]string{"hello": "there", "header": "two"}, BodyTemplate: map[string]string{"hello": "there", "header": "two"}, @@ -45,7 +43,7 @@ func TestIntegrationEndpoints_CustomCreateDuplicate(t *testing.T) { Title: "testCustomDuplicateEndpoint", Method: "POST", Description: "my description", - Url: "https://this.is.com/some/other/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "custom", Headers: map[string]string{"hello": "there", "header": "two"}, BodyTemplate: map[string]string{"hello": "there", "header": "two"}, @@ -57,7 +55,7 @@ func TestIntegrationEndpoints_CustomCreateDuplicate(t *testing.T) { Title: "testCustomDuplicateEndpoint", Method: "POST", Description: "my description", - Url: "https://this.is.com/some/other/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "custom", Headers: map[string]string{"hello": "there", "header": "two"}, BodyTemplate: map[string]string{"hello": "there", "header": "two"}, diff --git a/endpoints/endpoints_datadog_integration_test.go b/endpoints/endpoints_datadog_integration_test.go index 9bafe9f..7bf6606 100644 --- a/endpoints/endpoints_datadog_integration_test.go +++ b/endpoints/endpoints_datadog_integration_test.go @@ -1,5 +1,3 @@ -// +build integration - package endpoints_test import ( @@ -26,7 +24,7 @@ func createDataDogEndpoint() endpoints.Endpoint { return endpoints.Endpoint{ Title: "datadogendpoint", Description: "description", - EndpointType: "data-dog", + EndpointType: "datadog", ApiKey: "api_key", } } @@ -35,7 +33,7 @@ func updateDataDogEndpoint() endpoints.Endpoint { return endpoints.Endpoint{ Title: "datadogupdatedendpoint", Description: "updated description", - EndpointType: "data-dog", + EndpointType: "datadog", ApiKey: "updated_api_key", } } diff --git a/endpoints/endpoints_pagerduty_integration_test.go b/endpoints/endpoints_pagerduty_integration_test.go index fb42ccb..94d62ef 100644 --- a/endpoints/endpoints_pagerduty_integration_test.go +++ b/endpoints/endpoints_pagerduty_integration_test.go @@ -1,5 +1,3 @@ -// +build integration - package endpoints_test import ( @@ -28,7 +26,7 @@ func createPagerDutyEndpoint() endpoints.Endpoint { return endpoints.Endpoint{ Title: "pagerdutyvalidEndpoint", Description: "my description", - EndpointType: "pager-duty", + EndpointType: "pagerduty", ServiceKey: "my_service_key", } } diff --git a/endpoints/endpoints_slack_integration_test.go b/endpoints/endpoints_slack_integration_test.go index f447154..28cb6f7 100644 --- a/endpoints/endpoints_slack_integration_test.go +++ b/endpoints/endpoints_slack_integration_test.go @@ -1,5 +1,3 @@ -// +build integration - package endpoints_test import ( @@ -18,7 +16,7 @@ func TestIntegrationEndpoints_CreateDeleteGetValidEndpoint(t *testing.T) { endpoint, err = underTest.CreateEndpoint(endpoints.Endpoint{ Title: "slackcreatedeletevalidendpoint", Description: "my description", - Url: "https://this.is.com/some/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "slack", }) assert.Nil(t, err) @@ -42,14 +40,14 @@ func TestIntegrationEndpoints_CreateDuplicateEndpoint(t *testing.T) { endpoint, err = underTest.CreateEndpoint(endpoints.Endpoint{ Title: "slackcreateduplicateendpoint", Description: "my description", - Url: "https://this.is.com/some/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "slack", }) if assert.NoError(t, err) { duplicate, err := underTest.CreateEndpoint(endpoints.Endpoint{ Title: "slackcreateduplicateendpoint", Description: "my description", - Url: "https://this.is.com/some/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "slack", }) assert.Error(t, err) @@ -66,7 +64,7 @@ func TestIntegrationEndpoints_ListEndpoints(t *testing.T) { endpoint, err := underTest.CreateEndpoint(endpoints.Endpoint{ Title: "slacklistendpoints", Description: "my description", - Url: "https://this.is.com/some/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "slack", }) list, err := underTest.ListEndpoints() @@ -101,7 +99,7 @@ func TestIntegrationEndpoints_UpdateEndpoint(t *testing.T) { endpoint, err = underTest.CreateEndpoint(endpoints.Endpoint{ Title: "slackupdatedendpoint", Description: "my description", - Url: "https://this.is.com/some/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "slack", }) assert.NoError(t, err) @@ -110,7 +108,7 @@ func TestIntegrationEndpoints_UpdateEndpoint(t *testing.T) { updatedEndpoint, err := underTest.UpdateEndpoint(endpoint.Id, endpoints.Endpoint{ Title: "slackupdatedupdatedendpoint", Description: "my updated description", - Url: "https://this.is.com/some/other/webhook", + Url: "https://jsonplaceholder.typicode.com/todos/1", EndpointType: "slack", }) assert.NoError(t, err) diff --git a/endpoints/endpoints_victorops_integration_test.go b/endpoints/endpoints_victorops_integration_test.go index ae93e6a..2f1408f 100644 --- a/endpoints/endpoints_victorops_integration_test.go +++ b/endpoints/endpoints_victorops_integration_test.go @@ -1,5 +1,3 @@ -// +build integration - package endpoints_test import (