From 3ba6d0f3341eccdd26ad9e76444c03978bb79646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Fri, 13 Sep 2024 14:17:46 -0300 Subject: [PATCH] Tsuru Proxy V2 URLs (#151) --- pkg/rpaas/client/access_control_list.go | 6 ++--- pkg/rpaas/client/access_control_list_test.go | 6 ++--- pkg/rpaas/client/block.go | 6 ++--- pkg/rpaas/client/block_test.go | 6 ++--- pkg/rpaas/client/certificate.go | 10 +++---- pkg/rpaas/client/certificate_test.go | 18 ++++++------- pkg/rpaas/client/debug.go | 2 +- pkg/rpaas/client/debug_test.go | 3 ++- pkg/rpaas/client/exec.go | 2 +- pkg/rpaas/client/exec_test.go | 3 ++- pkg/rpaas/client/extra_files.go | 10 +++---- pkg/rpaas/client/extra_files_test.go | 12 ++++----- pkg/rpaas/client/info.go | 2 +- pkg/rpaas/client/info_test.go | 2 +- pkg/rpaas/client/internal_client.go | 28 +++++++++----------- pkg/rpaas/client/log.go | 2 +- pkg/rpaas/client/log_test.go | 4 +-- pkg/rpaas/client/route.go | 6 ++--- pkg/rpaas/client/route_test.go | 6 ++--- pkg/rpaas/client/scale.go | 2 +- pkg/rpaas/client/scale_test.go | 2 +- pkg/rpaas/client/start.go | 2 +- pkg/rpaas/client/start_test.go | 2 +- pkg/rpaas/client/stop.go | 2 +- pkg/rpaas/client/stop_test.go | 2 +- 25 files changed, 72 insertions(+), 74 deletions(-) diff --git a/pkg/rpaas/client/access_control_list.go b/pkg/rpaas/client/access_control_list.go index 1cd4ac490..5bbddd9a9 100644 --- a/pkg/rpaas/client/access_control_list.go +++ b/pkg/rpaas/client/access_control_list.go @@ -31,7 +31,7 @@ func (c *client) AddAccessControlList(ctx context.Context, instance, host string body := bytes.NewReader(b) pathName := fmt.Sprintf("/resources/%s/acl", instance) - req, err := c.newRequest("POST", pathName, body, instance) + req, err := c.newRequest("POST", pathName, body) if err != nil { return err } @@ -66,7 +66,7 @@ func (c *client) RemoveAccessControlList(ctx context.Context, instance, host str body := bytes.NewReader(b) pathName := fmt.Sprintf("/resources/%s/acl", instance) - req, err := c.newRequest("DELETE", pathName, body, instance) + req, err := c.newRequest("DELETE", pathName, body) if err != nil { return err } @@ -90,7 +90,7 @@ func (c *client) ListAccessControlList(ctx context.Context, instance string) ([] } pathName := fmt.Sprintf("/resources/%s/acl", instance) - req, err := c.newRequest("GET", pathName, nil, instance) + req, err := c.newRequest("GET", pathName, nil) if err != nil { return nil, err } diff --git a/pkg/rpaas/client/access_control_list_test.go b/pkg/rpaas/client/access_control_list_test.go index b629417e2..af0b70e50 100644 --- a/pkg/rpaas/client/access_control_list_test.go +++ b/pkg/rpaas/client/access_control_list_test.go @@ -38,7 +38,7 @@ func TestClientThroughTsuru_AddAccessControlList(t *testing.T) { port: 80, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/acl"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/acl", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusCreated) }, @@ -81,7 +81,7 @@ func TestClientThroughTsuru_RemoveAccessControlList(t *testing.T) { port: 80, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "DELETE") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/acl"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/acl", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusNoContent) }, @@ -129,7 +129,7 @@ func TestClientThroughTsuru_ListAccessControlList(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/acl"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/acl", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) fmt.Fprintf(w, `[{"host": "some-host.com", "port": 443}, {"host": "some-host2.com", "port": 80}]}`) w.WriteHeader(http.StatusOK) diff --git a/pkg/rpaas/client/block.go b/pkg/rpaas/client/block.go index fda90b3fd..e5b30bf8e 100644 --- a/pkg/rpaas/client/block.go +++ b/pkg/rpaas/client/block.go @@ -48,7 +48,7 @@ func (c *client) UpdateBlock(ctx context.Context, args UpdateBlockArgs) error { body := strings.NewReader(b) pathName := fmt.Sprintf("/resources/%s/block", args.Instance) - req, err := c.newRequest("POST", pathName, body, args.Instance) + req, err := c.newRequest("POST", pathName, body) if err != nil { return err } @@ -84,7 +84,7 @@ func (c *client) DeleteBlock(ctx context.Context, args DeleteBlockArgs) error { } pathName := fmt.Sprintf("/resources/%s/block/%s", args.Instance, args.Name) - req, err := c.newRequest("DELETE", pathName, nil, args.Instance) + req, err := c.newRequest("DELETE", pathName, nil) if err != nil { return err } @@ -115,7 +115,7 @@ func (c *client) ListBlocks(ctx context.Context, args ListBlocksArgs) ([]types.B } pathName := fmt.Sprintf("/resources/%s/block", args.Instance) - req, err := c.newRequest("GET", pathName, nil, args.Instance) + req, err := c.newRequest("GET", pathName, nil) if err != nil { return nil, err } diff --git a/pkg/rpaas/client/block_test.go b/pkg/rpaas/client/block_test.go index 3a7be6a01..f4196e720 100644 --- a/pkg/rpaas/client/block_test.go +++ b/pkg/rpaas/client/block_test.go @@ -52,7 +52,7 @@ func TestClientThroughTsuru_UpdateBlock(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/block"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/block", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) assert.Equal(t, "block_name=http&content=%23+NGINX+configuration+block", getBody(t, r)) @@ -114,7 +114,7 @@ func TestClientThroughTsuru_DeleteBlock(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "DELETE") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/block/http"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/block/http", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusOK) }, @@ -187,7 +187,7 @@ func TestClientThroughTsuru_ListBlocks(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/block"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/block", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) fmt.Fprintf(w, `{"blocks": [{"block_name": "http", "content": "Some HTTP conf"}, {"block_name": "server", "content": "Some server conf"}]}`) w.WriteHeader(http.StatusOK) diff --git a/pkg/rpaas/client/certificate.go b/pkg/rpaas/client/certificate.go index 1ff79485b..c8d45dd28 100644 --- a/pkg/rpaas/client/certificate.go +++ b/pkg/rpaas/client/certificate.go @@ -77,7 +77,7 @@ func (c *client) UpdateCertificate(ctx context.Context, args UpdateCertificateAr body := strings.NewReader(buffer.String()) pathName := fmt.Sprintf("/resources/%s/certificate", args.Instance) - req, err := c.newRequest("POST", pathName, body, args.Instance) + req, err := c.newRequest("POST", pathName, body) req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", w.Boundary())) if err != nil { return err @@ -110,7 +110,7 @@ func (c *client) DeleteCertificate(ctx context.Context, args DeleteCertificateAr args.Name = url.QueryEscape(args.Name) pathName := fmt.Sprintf("/resources/%s/certificate/%s", args.Instance, args.Name) - req, err := c.newRequest("DELETE", pathName, nil, args.Instance) + req, err := c.newRequest("DELETE", pathName, nil) if err != nil { return err } @@ -132,7 +132,7 @@ func (c *client) ListCertManagerRequests(ctx context.Context, instance string) ( return nil, ErrMissingInstance } - req, err := c.newRequest("GET", fmt.Sprintf("/resources/%s/cert-manager", instance), nil, instance) + req, err := c.newRequest("GET", fmt.Sprintf("/resources/%s/cert-manager", instance), nil) if err != nil { return nil, err } @@ -174,7 +174,7 @@ func (c *client) UpdateCertManager(ctx context.Context, args UpdateCertManagerAr return err } - req, err := c.newRequest("POST", fmt.Sprintf("/resources/%s/cert-manager", args.Instance), &body, args.Instance) + req, err := c.newRequest("POST", fmt.Sprintf("/resources/%s/cert-manager", args.Instance), &body) if err != nil { return err } @@ -203,7 +203,7 @@ func (c *client) DeleteCertManager(ctx context.Context, instance, issuer string) data.Set("issuer", issuer) } - req, err := c.newRequestWithQueryString("DELETE", fmt.Sprintf("/resources/%s/cert-manager", instance), nil, instance, data) + req, err := c.newRequestWithQueryString("DELETE", fmt.Sprintf("/resources/%s/cert-manager", instance), nil, data) if err != nil { return err } diff --git a/pkg/rpaas/client/certificate_test.go b/pkg/rpaas/client/certificate_test.go index e79f80c5b..1a4494c18 100644 --- a/pkg/rpaas/client/certificate_test.go +++ b/pkg/rpaas/client/certificate_test.go @@ -58,7 +58,7 @@ func TestClientThroughTsuru_UpdateCertificate(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/certificate"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/certificate", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Equal(t, "multipart/form-data; boundary=\"custom-boundary\"", r.Header.Get("Content-Type")) assert.Equal(t, "--custom-boundary\r\nContent-Disposition: form-data; name=\"cert\"; filename=\"cert.pem\"\r\nContent-Type: application/octet-stream\r\n\r\nmy certificate\r\n--custom-boundary\r\nContent-Disposition: form-data; name=\"key\"; filename=\"key.pem\"\r\nContent-Type: application/octet-stream\r\n\r\nmy key\r\n--custom-boundary\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n\r\n--custom-boundary--\r\n", getBody(t, r)) @@ -113,7 +113,7 @@ func TestClientThroughTsuru_DeleteCertificate(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "DELETE") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/certificate/"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/certificate/", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusOK) }, @@ -126,7 +126,7 @@ func TestClientThroughTsuru_DeleteCertificate(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "DELETE") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/certificate/my-certificate"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/certificate/my-certificate", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Equal(t, "", getBody(t, r)) w.WriteHeader(http.StatusOK) @@ -151,7 +151,7 @@ func TestClientThroughTsuru_DeleteCertificate(t *testing.T) { Name: "my certificate", }, handler: func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/certificate/my+certificate"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/certificate/my+certificate", FakeTsuruService, "my-instance"), r.URL.RequestURI()) w.WriteHeader(http.StatusOK) }, }, @@ -186,7 +186,7 @@ func TestClientThroughTsuru_ListCertManagerRequests(t *testing.T) { instance: "my-instance", handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/cert-manager", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) fmt.Fprintf(w, `[{"issuer": "my-issuer", "dnsNames": ["www.example.com", "web.example.com"], "ipAddresses": ["169.196.254.100"]}, {"issuer": "my-issuer-1", "dnsNames": ["*.test"]}]`) @@ -208,7 +208,7 @@ func TestClientThroughTsuru_ListCertManagerRequests(t *testing.T) { instance: "my-instance", handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/cert-manager", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusInternalServerError) @@ -255,7 +255,7 @@ func TestClientThroughTsuru_UpdateCertManager(t *testing.T) { }, }, handler: func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/cert-manager", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "POST", r.Method) assert.Equal(t, "application/json", r.Header.Get("Content-Type")) var cm types.CertManager @@ -313,7 +313,7 @@ func TestClientThroughTsuru_DeleteCertManager(t *testing.T) { "when removing a Cert Manager request with no issuer provided": { instance: "my-instance", handler: func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/cert-manager", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "DELETE", r.Method) w.WriteHeader(http.StatusOK) }, @@ -323,7 +323,7 @@ func TestClientThroughTsuru_DeleteCertManager(t *testing.T) { instance: "my-instance", issuer: "lets-encrypt", handler: func(w http.ResponseWriter, r *http.Request) { - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager&issuer=lets-encrypt"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/cert-manager?issuer=lets-encrypt", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "DELETE", r.Method) w.WriteHeader(http.StatusOK) }, diff --git a/pkg/rpaas/client/debug.go b/pkg/rpaas/client/debug.go index 63b89984c..80471fc54 100644 --- a/pkg/rpaas/client/debug.go +++ b/pkg/rpaas/client/debug.go @@ -27,7 +27,7 @@ func (c *client) Debug(ctx context.Context, args DebugArgs) (*websocket.Conn, er return nil, err } - serverAddress := c.formatURL(fmt.Sprintf("/resources/%s/debug", args.Instance), args.Instance) + serverAddress := c.formatURL(fmt.Sprintf("/resources/%s/debug", args.Instance)) u, err := url.Parse(serverAddress) if err != nil { return nil, err diff --git a/pkg/rpaas/client/debug_test.go b/pkg/rpaas/client/debug_test.go index 39fde0a8a..cb0361ec2 100644 --- a/pkg/rpaas/client/debug_test.go +++ b/pkg/rpaas/client/debug_test.go @@ -45,7 +45,6 @@ func TestClientThroughTsuru_Debug(t *testing.T) { assert.True(t, websocket.IsWebSocketUpgrade(r)) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) expectedQS := url.Values{} - expectedQS.Set("callback", "/resources/my-instance/debug") expectedQS.Set("ws", "true") expectedQS.Set("command", "bash") expectedQS.Set("pod", "pod-1") @@ -56,6 +55,8 @@ func TestClientThroughTsuru_Debug(t *testing.T) { expectedQS.Set("interactive", "true") expectedQS.Set("image", "my-image/test") assert.Equal(t, expectedQS, r.URL.Query()) + assert.Equal(t, "/1.20/services/rpaasv2/resources/my-instance/debug", r.URL.Path) + w.WriteHeader(http.StatusBadRequest) }, expectedCalled: true, diff --git a/pkg/rpaas/client/exec.go b/pkg/rpaas/client/exec.go index a42d23baf..fbbd2e3ac 100644 --- a/pkg/rpaas/client/exec.go +++ b/pkg/rpaas/client/exec.go @@ -31,7 +31,7 @@ func (c *client) Exec(ctx context.Context, args ExecArgs) (*websocket.Conn, erro return nil, err } - serverAddress := c.formatURL(fmt.Sprintf("/resources/%s/exec", args.Instance), args.Instance) + serverAddress := c.formatURL(fmt.Sprintf("/resources/%s/exec", args.Instance)) u, err := url.Parse(serverAddress) if err != nil { return nil, err diff --git a/pkg/rpaas/client/exec_test.go b/pkg/rpaas/client/exec_test.go index d50180ef9..198d5bed6 100644 --- a/pkg/rpaas/client/exec_test.go +++ b/pkg/rpaas/client/exec_test.go @@ -51,7 +51,6 @@ func TestClientThroughTsuru_Exec(t *testing.T) { assert.True(t, websocket.IsWebSocketUpgrade(r)) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) expectedQS := url.Values{} - expectedQS.Set("callback", "/resources/my-instance/exec") expectedQS.Set("ws", "true") expectedQS.Set("command", "bash") expectedQS.Set("pod", "pod-1") @@ -61,6 +60,8 @@ func TestClientThroughTsuru_Exec(t *testing.T) { expectedQS.Set("tty", "true") expectedQS.Set("interactive", "true") assert.Equal(t, expectedQS, r.URL.Query()) + assert.Equal(t, "/1.20/services/rpaasv2/resources/my-instance/exec", r.URL.Path) + w.WriteHeader(http.StatusBadRequest) }, expectedCalled: true, diff --git a/pkg/rpaas/client/extra_files.go b/pkg/rpaas/client/extra_files.go index 89e39d699..607e49928 100644 --- a/pkg/rpaas/client/extra_files.go +++ b/pkg/rpaas/client/extra_files.go @@ -81,7 +81,7 @@ func (c *client) AddExtraFiles(ctx context.Context, args ExtraFilesArgs) error { body := strings.NewReader(buffer.String()) pathName := fmt.Sprintf("/resources/%s/files", args.Instance) - req, err := c.newRequest(http.MethodPost, pathName, body, args.Instance) + req, err := c.newRequest(http.MethodPost, pathName, body) req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", w.Boundary())) if err != nil { return err @@ -111,7 +111,7 @@ func (c *client) UpdateExtraFiles(ctx context.Context, args ExtraFilesArgs) erro body := strings.NewReader(buffer.String()) pathName := fmt.Sprintf("/resources/%s/files", args.Instance) - req, err := c.newRequest(http.MethodPut, pathName, body, args.Instance) + req, err := c.newRequest(http.MethodPut, pathName, body) req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", w.Boundary())) if err != nil { return err @@ -141,7 +141,7 @@ func (c *client) DeleteExtraFiles(ctx context.Context, args DeleteExtraFilesArgs body := bytes.NewReader(b) pathName := fmt.Sprintf("/resources/%s/files", args.Instance) - req, err := c.newRequest(http.MethodDelete, pathName, body, args.Instance) + req, err := c.newRequest(http.MethodDelete, pathName, body) if err != nil { return err } @@ -165,7 +165,7 @@ func (c *client) ListExtraFiles(ctx context.Context, args ListExtraFilesArgs) ([ } pathName := fmt.Sprintf("/resources/%s/files?show-content=%s", args.Instance, strconv.FormatBool(args.ShowContent)) - req, err := c.newRequest(http.MethodGet, pathName, nil, args.Instance) + req, err := c.newRequest(http.MethodGet, pathName, nil) if err != nil { return nil, err } @@ -193,7 +193,7 @@ func (c *client) GetExtraFile(ctx context.Context, args GetExtraFileArgs) (types } pathName := fmt.Sprintf("/resources/%s/files/%s", args.Instance, args.FileName) - req, err := c.newRequest(http.MethodGet, pathName, nil, args.Instance) + req, err := c.newRequest(http.MethodGet, pathName, nil) if err != nil { return types.RpaasFile{}, err } diff --git a/pkg/rpaas/client/extra_files_test.go b/pkg/rpaas/client/extra_files_test.go index 4044c81d8..c814f43c2 100644 --- a/pkg/rpaas/client/extra_files_test.go +++ b/pkg/rpaas/client/extra_files_test.go @@ -56,7 +56,7 @@ func TestClientThroughTsuru_DeleteExtraFiles(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "DELETE") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/files"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/files", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Equal(t, "application/json", r.Header.Get("Content-Type")) assert.Equal(t, `["f1","f2"]`, getBody(t, r)) @@ -135,7 +135,7 @@ func TestClientThroughTsuru_AddExtraFiles(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/files"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/files", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Contains(t, r.Header.Get("Content-Type"), "multipart/form-data") @@ -220,7 +220,7 @@ func TestClientThroughTsuru_UpdateExtraFiles(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "PUT") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/files"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/files", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Contains(t, r.Header.Get("Content-Type"), "multipart/form-data") @@ -292,7 +292,7 @@ func TestClientThroughTsuru_GetExtraFiles(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/files/some-file"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/files/some-file", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.Header().Set("Content-type", "application/json") file := types.RpaasFile{ @@ -355,7 +355,7 @@ func TestClientThroughTsuru_ListExtraFiles(t *testing.T) { expectedFiles: []types.RpaasFile{{Name: "f1"}, {Name: "f2"}}, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/files?show-content=false"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/files?%s", FakeTsuruService, "my-instance", "show-content=false"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.Header().Set("Content-type", "application/json") files := []types.RpaasFile{ @@ -390,7 +390,7 @@ func TestClientThroughTsuru_ListExtraFiles(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/files?show-content=true"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/files?%s", FakeTsuruService, "my-instance", "show-content=true"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.Header().Set("Content-type", "application/json") files := []types.RpaasFile{ diff --git a/pkg/rpaas/client/info.go b/pkg/rpaas/client/info.go index 175004a58..8ea2962fc 100644 --- a/pkg/rpaas/client/info.go +++ b/pkg/rpaas/client/info.go @@ -27,7 +27,7 @@ func (c *client) Info(ctx context.Context, args InfoArgs) (*types.InstanceInfo, } pathName := fmt.Sprintf("/resources/%s/info", args.Instance) - req, err := c.newRequest("GET", pathName, nil, args.Instance) + req, err := c.newRequest("GET", pathName, nil) if err != nil { return nil, err } diff --git a/pkg/rpaas/client/info_test.go b/pkg/rpaas/client/info_test.go index 51efcadd2..cdd938129 100644 --- a/pkg/rpaas/client/info_test.go +++ b/pkg/rpaas/client/info_test.go @@ -32,7 +32,7 @@ func TestClientThroughTsuru_Info(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/info"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/info", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) fmt.Fprintf(w, `{"address":[{"hostname":"some-host","ip":"0.0.0.0"},{"hostname":"some-host2","ip":"0.0.0.1"}],"replicas":5,"plan":"basic","locations":[{"path":"some-path","destination":"some-destination"}],"binds":[{"name":"some-name","host":"some-host"},{"name":"some-name2","host":"some-host2"}],"team":"some team","name":"my-instance","description":"some description","tags":["tag1","tag2","tag3"]}`) w.WriteHeader(http.StatusOK) diff --git a/pkg/rpaas/client/internal_client.go b/pkg/rpaas/client/internal_client.go index bb10c01b1..c81b74052 100644 --- a/pkg/rpaas/client/internal_client.go +++ b/pkg/rpaas/client/internal_client.go @@ -190,12 +190,12 @@ func (c *client) SetService(service string) (Client, error) { return &newClient, nil } -func (c *client) newRequest(method, pathName string, body io.Reader, instance string) (*http.Request, error) { - return c.newRequestWithQueryString(method, pathName, body, instance, nil) +func (c *client) newRequest(method, pathName string, body io.Reader) (*http.Request, error) { + return c.newRequestWithQueryString(method, pathName, body, nil) } -func (c *client) newRequestWithQueryString(method, pathName string, body io.Reader, instance string, qs url.Values) (*http.Request, error) { - url := c.formatURLWithQueryString(pathName, instance, qs) +func (c *client) newRequestWithQueryString(method, pathName string, body io.Reader, qs url.Values) (*http.Request, error) { + url := c.formatURLWithQueryString(pathName, qs) request, err := http.NewRequest(method, url, body) if err != nil { return nil, err @@ -229,30 +229,26 @@ func (c *client) do(ctx context.Context, request *http.Request) (*http.Response, return c.client.Do(request.WithContext(ctx)) } -func (c *client) formatURL(pathName, instance string) string { +func (c *client) formatURL(pathName string) string { if !c.throughTsuru { return fmt.Sprintf("%s%s", c.rpaasAddress, pathName) } - return fmt.Sprintf("%s/services/%s/proxy/%s?callback=%s", c.tsuruTarget, c.tsuruService, instance, pathName) + return fmt.Sprintf("%s/1.20/services/%s%s", c.tsuruTarget, c.tsuruService, pathName) } -func (c *client) formatURLWithQueryString(pathName, instance string, qs url.Values) string { +func (c *client) formatURLWithQueryString(pathName string, qs url.Values) string { qsData := qs.Encode() - if !c.throughTsuru { - if qsData != "" { - qsData = "?" + qsData - } - - return fmt.Sprintf("%s%s%s", c.rpaasAddress, pathName, qsData) + if qsData != "" { + qsData = "?" + qsData } - if qsData != "" { - qsData = "&" + qsData + if !c.throughTsuru { + return fmt.Sprintf("%s%s%s", c.rpaasAddress, pathName, qsData) } - return fmt.Sprintf("%s/services/%s/proxy/%s?callback=%s%s", c.tsuruTarget, c.tsuruService, instance, pathName, qsData) + return fmt.Sprintf("%s/1.20/services/%s%s%s", c.tsuruTarget, c.tsuruService, pathName, qsData) } func unmarshalBody(resp *http.Response, dst interface{}) error { diff --git a/pkg/rpaas/client/log.go b/pkg/rpaas/client/log.go index 04f01428a..88e84ad25 100644 --- a/pkg/rpaas/client/log.go +++ b/pkg/rpaas/client/log.go @@ -30,7 +30,7 @@ func (c *client) Log(ctx context.Context, args LogArgs) error { httpClient := *c.client httpClient.Timeout = time.Duration(0) - serverAddress := c.formatURL(fmt.Sprintf("/resources/%s/log", args.Instance), args.Instance) + serverAddress := c.formatURL(fmt.Sprintf("/resources/%s/log", args.Instance)) u, err := url.Parse(serverAddress) if err != nil { return err diff --git a/pkg/rpaas/client/log_test.go b/pkg/rpaas/client/log_test.go index e6afac2d6..500586b93 100644 --- a/pkg/rpaas/client/log_test.go +++ b/pkg/rpaas/client/log_test.go @@ -27,7 +27,7 @@ func TestClientThroughTsuru_Log(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "%2Fresources%2Fmy-instance%2Flog&color=false&follow=false"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/log?%s", FakeTsuruService, "my-instance", "color=false&follow=false"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusOK) }, @@ -43,7 +43,7 @@ func TestClientThroughTsuru_Log(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "%2Fresources%2Fmy-instance%2Flog&color=false&container=some-container&follow=true&lines=10&pod=some-pod"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/log?color=false&container=some-container&follow=true&lines=10&pod=some-pod", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusOK) }, diff --git a/pkg/rpaas/client/route.go b/pkg/rpaas/client/route.go index 1d6e42101..38c9880a5 100644 --- a/pkg/rpaas/client/route.go +++ b/pkg/rpaas/client/route.go @@ -37,7 +37,7 @@ func (c *client) DeleteRoute(ctx context.Context, args DeleteRouteArgs) error { values := url.Values{} values.Set("path", args.Path) body := strings.NewReader(values.Encode()) - req, err := c.newRequest("DELETE", pathName, body, args.Instance) + req, err := c.newRequest("DELETE", pathName, body) if err != nil { return err } @@ -70,7 +70,7 @@ func (c *client) ListRoutes(ctx context.Context, args ListRoutesArgs) ([]types.R } pathName := fmt.Sprintf("/resources/%s/route", args.Instance) - req, err := c.newRequest("GET", pathName, nil, args.Instance) + req, err := c.newRequest("GET", pathName, nil) if err != nil { return nil, err } @@ -126,7 +126,7 @@ func (c *client) UpdateRoute(ctx context.Context, args UpdateRouteArgs) error { body := strings.NewReader(b) pathName := fmt.Sprintf("/resources/%s/route", args.Instance) - req, err := c.newRequest("POST", pathName, body, args.Instance) + req, err := c.newRequest("POST", pathName, body) if err != nil { return err } diff --git a/pkg/rpaas/client/route_test.go b/pkg/rpaas/client/route_test.go index d9fae8fa4..aa7c3ae33 100644 --- a/pkg/rpaas/client/route_test.go +++ b/pkg/rpaas/client/route_test.go @@ -54,7 +54,7 @@ func TestClientThroughTsuru_DeleteRoute(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "DELETE") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/route"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/route", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) assert.Equal(t, "path=%2Fcustom%2Fpath", getBody(t, r)) @@ -122,7 +122,7 @@ func TestClientThroughTsuru_ListRoutes(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "GET") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/route"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/route", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) fmt.Fprintf(w, `{"paths": [{"path": "/static", "destination": "static.apps.tsuru.example.com"}, {"path": "/login", "destination": "login.apps.tsuru.example.com", "https_only": true}, {"path": "/custom/path", "content": "# some NGINX configuration"}]}`) w.WriteHeader(http.StatusOK) @@ -185,7 +185,7 @@ func TestClientThroughTsuru_UpdateRoute(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/route"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/route", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) expected := url.Values{ "path": []string{"/app"}, diff --git a/pkg/rpaas/client/scale.go b/pkg/rpaas/client/scale.go index ca7d58e45..bdd5c1efe 100644 --- a/pkg/rpaas/client/scale.go +++ b/pkg/rpaas/client/scale.go @@ -33,7 +33,7 @@ func (c *client) Scale(ctx context.Context, args ScaleArgs) error { values := url.Values{} values.Set("quantity", fmt.Sprint(args.Replicas)) body := strings.NewReader(values.Encode()) - req, err := c.newRequest("POST", pathName, body, args.Instance) + req, err := c.newRequest("POST", pathName, body) if err != nil { return err } diff --git a/pkg/rpaas/client/scale_test.go b/pkg/rpaas/client/scale_test.go index feca5f1aa..b64bfcb83 100644 --- a/pkg/rpaas/client/scale_test.go +++ b/pkg/rpaas/client/scale_test.go @@ -49,7 +49,7 @@ func TestClientThroughTsuru_Scale(t *testing.T) { }, handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/scale"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/scale", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) assert.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type")) assert.Equal(t, "quantity=777", getBody(t, r)) diff --git a/pkg/rpaas/client/start.go b/pkg/rpaas/client/start.go index dd18febb0..b5cfe9ae5 100644 --- a/pkg/rpaas/client/start.go +++ b/pkg/rpaas/client/start.go @@ -16,7 +16,7 @@ func (c *client) Start(ctx context.Context, instance string) error { } pathName := fmt.Sprintf("/resources/%s/start", instance) - req, err := c.newRequest("POST", pathName, nil, instance) + req, err := c.newRequest("POST", pathName, nil) if err != nil { return err } diff --git a/pkg/rpaas/client/start_test.go b/pkg/rpaas/client/start_test.go index 4e9ef9886..78686a536 100644 --- a/pkg/rpaas/client/start_test.go +++ b/pkg/rpaas/client/start_test.go @@ -35,7 +35,7 @@ func TestClientThroughTsuru_Start(t *testing.T) { instance: "my-instance", handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/start"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/start", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusOK) }, diff --git a/pkg/rpaas/client/stop.go b/pkg/rpaas/client/stop.go index 201e31657..6a33f54fe 100644 --- a/pkg/rpaas/client/stop.go +++ b/pkg/rpaas/client/stop.go @@ -16,7 +16,7 @@ func (c *client) Stop(ctx context.Context, instance string) error { } pathName := fmt.Sprintf("/resources/%s/stop", instance) - req, err := c.newRequest("POST", pathName, nil, instance) + req, err := c.newRequest("POST", pathName, nil) if err != nil { return err } diff --git a/pkg/rpaas/client/stop_test.go b/pkg/rpaas/client/stop_test.go index 5b5f9d6b9..334b9df46 100644 --- a/pkg/rpaas/client/stop_test.go +++ b/pkg/rpaas/client/stop_test.go @@ -35,7 +35,7 @@ func TestClientThroughTsuru_Stop(t *testing.T) { instance: "my-instance", handler: func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, "POST") - assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/stop"), r.URL.RequestURI()) + assert.Equal(t, fmt.Sprintf("/1.20/services/%s/resources/%s/stop", FakeTsuruService, "my-instance"), r.URL.RequestURI()) assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization")) w.WriteHeader(http.StatusOK) },