From 62a00d4807eb7e7d851055e33f50a8a48c376168 Mon Sep 17 00:00:00 2001 From: LUC DUZAN Date: Fri, 19 Jul 2024 09:38:55 +0200 Subject: [PATCH] Make /api not mandatory when setting base url (#56) --- client/client.go | 10 ++++++++-- client/client_test.go | 35 +++++++++++++++++++++++++---------- docker/initializer.json | 24 ++++++++++++------------ 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/client/client.go b/client/client.go index c847d40..a2065de 100644 --- a/client/client.go +++ b/client/client.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "regexp" "strings" "github.com/conduktor/ctl/resource" @@ -32,6 +33,11 @@ type ApiParameter struct { Insecure bool } +func uniformizeBaseUrl(baseUrl string) string { + regex := regexp.MustCompile(`(/api)?/?$`) + return regex.ReplaceAllString(baseUrl, "/api") +} + func Make(apiParameter ApiParameter) (*Client, error) { //apiKey is set later because it's not mandatory for getting the openapi and parsing different kind //or to get jwt token @@ -64,7 +70,7 @@ func Make(apiParameter ApiParameter) (*Client, error) { result := &Client{ apiKey: apiParameter.ApiKey, - baseUrl: apiParameter.BaseUrl, + baseUrl: uniformizeBaseUrl(apiParameter.BaseUrl), client: restyClient, kinds: nil, } @@ -210,7 +216,7 @@ func (client *Client) Get(kind *schema.Kind, parentPathValue []string) ([]resour } func (client *Client) Login(username, password string) (LoginResult, error) { - url := client.baseUrl + "/api/login" + url := client.baseUrl + "/login" resp, err := client.client.R().SetBody(map[string]string{"username": username, "password": password}).Post(url) if err != nil { return LoginResult{}, err diff --git a/client/client_test.go b/client/client_test.go index ebbd24e..ab33976 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -16,6 +16,21 @@ var aResource = resource.Resource{ Json: []byte(`{"apiVersion":"v1","kind":"Topic","metadata":{"name":"abc.myTopic"},"spec":{"replicationFactor":1}}`), } +func TestUniformizeBaseUrl(t *testing.T) { + validUrls := []string{ + "http://baseUrl/api/", + "http://baseUrl/api", + "http://baseUrl/", + "http://baseUrl", + } + expectedResult := "http://baseUrl/api" + for _, url := range validUrls { + finalUrl := uniformizeBaseUrl(url) + if finalUrl != expectedResult { + t.Errorf("When uniformize %s got %s expected %s", url, finalUrl, expectedResult) + } + } +} func TestApplyShouldWork(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" @@ -45,7 +60,7 @@ func TestApplyShouldWork(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "PUT", - "http://baseUrl/public/kafka/v2/cluster/local/topic", + "http://baseUrl/api/public/kafka/v2/cluster/local/topic", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")). @@ -87,7 +102,7 @@ func TestApplyWithDryModeShouldWork(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "PUT", - "http://baseUrl/public/self-serve/v1/application", + "http://baseUrl/api/public/self-serve/v1/application", "dryMode=true", httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.BodyContainsBytes(topic.Json)), @@ -131,7 +146,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "PUT", - "http://baseUrl/public/self-serve/v1/application", + "http://baseUrl/api/public/self-serve/v1/application", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.BodyContainsBytes(topic.Json)), @@ -165,7 +180,7 @@ func TestGetShouldWork(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "GET", - "http://baseUrl/public/self-serve/v1/application", + "http://baseUrl/api/public/self-serve/v1/application", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")), @@ -203,7 +218,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "GET", - "http://baseUrl/public/self-serve/v1/application", + "http://baseUrl/api/public/self-serve/v1/application", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey), responder, @@ -237,7 +252,7 @@ func TestDescribeShouldWork(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "GET", - "http://baseUrl/public/self-serve/v1/application/yo", + "http://baseUrl/api/public/self-serve/v1/application/yo", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")), @@ -276,7 +291,7 @@ func TestDescribeShouldFailIfNo2xx(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "GET", - "http://baseUrl/public/self-serve/v1/application/yo", + "http://baseUrl/api/public/self-serve/v1/application/yo", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey), responder, @@ -310,7 +325,7 @@ func TestDeleteShouldWork(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "DELETE", - "http://baseUrl/public/self-serve/v1/application/yo", + "http://baseUrl/api/public/self-serve/v1/application/yo", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")), @@ -345,7 +360,7 @@ func TestDeleteResourceShouldWork(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "DELETE", - "http://baseUrl/public/kafka/v2/cluster/local/topic/toto", + "http://baseUrl/api/public/kafka/v2/cluster/local/topic/toto", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey). And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")), @@ -382,7 +397,7 @@ func TestDeleteShouldFailOnNot2XX(t *testing.T) { httpmock.RegisterMatcherResponderWithQuery( "DELETE", - "http://baseUrl/public/self_serve/v1/api/application/yo", + "http://baseUrl/api/public/self_serve/v1/api/application/yo", nil, httpmock.HeaderIs("Authorization", "Bearer "+apiKey), responder, diff --git a/docker/initializer.json b/docker/initializer.json index c9f7a84..5a5a135 100644 --- a/docker/initializer.json +++ b/docker/initializer.json @@ -2,7 +2,7 @@ { "httpRequest": { "method": "Put", - "path": "/public/kafka/v2/cluster/my-cluster/topic", + "path": "/api/public/kafka/v2/cluster/my-cluster/topic", "headers": { "Authorization": "Bearer yo" } @@ -20,7 +20,7 @@ { "httpRequest": { "method": "Get", - "path": "/public/kafka/v2/cluster/my-cluster/topic", + "path": "/api/public/kafka/v2/cluster/my-cluster/topic", "headers": { "Authorization": "Bearer yo" } @@ -38,7 +38,7 @@ { "httpRequest": { "method": "Get", - "path": "/public/kafka/v2/cluster/my-cluster/topic/yolo", + "path": "/api/public/kafka/v2/cluster/my-cluster/topic/yolo", "headers": { "Authorization": "Bearer yo" } @@ -56,7 +56,7 @@ { "httpRequest": { "method": "Delete", - "path": "/public/kafka/v2/cluster/my-cluster/topic/yolo", + "path": "/api/public/kafka/v2/cluster/my-cluster/topic/yolo", "headers": { "Authorization": "Bearer yo" } @@ -74,7 +74,7 @@ { "httpRequest": { "method": "Delete", - "path": "/public/kafka/v2/cluster/my-cluster/topic/abcd.topic", + "path": "/api/public/kafka/v2/cluster/my-cluster/topic/abcd.topic", "headers": { "Authorization": "Bearer yo" } @@ -92,7 +92,7 @@ { "httpRequest": { "method": "Delete", - "path": "/public/kafka/v2/cluster/my-cluster/topic/abcd.myTopicWrong", + "path": "/api/public/kafka/v2/cluster/my-cluster/topic/abcd.myTopicWrong", "headers": { "Authorization": "Bearer yo" } @@ -134,7 +134,7 @@ { "httpRequest": { "method": "Get", - "path": "/token/v1/admin_tokens", + "path": "/api/token/v1/admin_tokens", "headers": { "Authorization": "Bearer yo" } @@ -152,7 +152,7 @@ { "httpRequest": { "method": "GET", - "path": "/token/v1/application_instance_tokens/my_app_instance", + "path": "/api/token/v1/application_instance_tokens/my_app_instance", "headers": { "Authorization": "Bearer yo" } @@ -170,7 +170,7 @@ { "httpRequest": { "method": "Post", - "path": "/token/v1/admin_tokens", + "path": "/api/token/v1/admin_tokens", "headers": { "Authorization": "Bearer yo" }, @@ -196,7 +196,7 @@ { "httpRequest": { "method": "Post", - "path": "/token/v1/application_instance_tokens/my_app_instance", + "path": "/api/token/v1/application_instance_tokens/my_app_instance", "headers": { "Authorization": "Bearer yo" }, @@ -222,7 +222,7 @@ { "httpRequest": { "method": "DELETE", - "path": "/token/v1/0-0-0-0-0", + "path": "/api/token/v1/0-0-0-0-0", "headers": { "Authorization": "Bearer yo" } @@ -234,7 +234,7 @@ { "httpRequest": { "method": "Get", - "path": "/public/console/v2/kafka-cluster/my_kafka_cluster", + "path": "/api/public/console/v2/kafka-cluster/my_kafka_cluster", "headers": { "Authorization": "Bearer yo_from_login" }