Skip to content

Commit

Permalink
Tsuru Proxy V2 URLs (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior authored Sep 13, 2024
1 parent eab456b commit 3ba6d0f
Show file tree
Hide file tree
Showing 25 changed files with 72 additions and 74 deletions.
6 changes: 3 additions & 3 deletions pkg/rpaas/client/access_control_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpaas/client/access_control_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down Expand Up @@ -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)
},
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpaas/client/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpaas/client/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
},
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/rpaas/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/rpaas/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
},
Expand All @@ -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)
Expand All @@ -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)
},
},
Expand Down Expand Up @@ -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"]}]`)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
},
Expand All @@ -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)
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpaas/client/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/rpaas/client/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/rpaas/client/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/rpaas/client/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions pkg/rpaas/client/extra_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 3ba6d0f

Please sign in to comment.