Skip to content

Commit

Permalink
Fix MFKE routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ducvm29 committed Sep 6, 2024
1 parent 2e79156 commit 7edd991
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 70 deletions.
2 changes: 1 addition & 1 deletion commons/api_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ var ApiPath = struct {
)
},
GetFKEOSVersion: func(vpcId string, platform string) string {
return fmt.Sprintf("v1/xplat/fke/vpc/%s/m-fke/%s/get_k8s_versions", vpcId, platform)
return fmt.Sprintf("/v1/xplat/fke/vpc/%s/m-fke/%s/get_k8s_versions", vpcId, platform)
},
}
24 changes: 12 additions & 12 deletions commons/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func NewClientWithURL(apiKey, apiUrl, region string, tenantName string) (*Client
return client, nil
}

func (c *Client) prepareClientURL(requestURL string) *url.URL {
func (c *Client) PrepareClientURL(requestURL string) *url.URL {
u, _ := url.Parse(c.BaseURL.String() + requestURL)
return u
}

func (c *Client) sendRequest(req *http.Request) ([]byte, error) {
func (c *Client) SendRequest(req *http.Request) ([]byte, error) {
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.UserAgent)
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -123,18 +123,18 @@ func (c *Client) sendRequest(req *http.Request) ([]byte, error) {

// SendGetRequest sends a correctly authenticated get request to the API server
func (c *Client) SendGetRequest(requestURL string) ([]byte, error) {
u := c.prepareClientURL(requestURL)
u := c.PrepareClientURL(requestURL)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}

return c.sendRequest(req)
return c.SendRequest(req)
}

// SendPostRequest sends a correctly authenticated post request to the API server
func (c *Client) SendPostRequest(requestURL string, params interface{}) ([]byte, error) {
u := c.prepareClientURL(requestURL)
u := c.PrepareClientURL(requestURL)

// we create a new buffer and encode everything to json to send it in the request
jsonValue, _ := json.Marshal(params)
Expand All @@ -143,12 +143,12 @@ func (c *Client) SendPostRequest(requestURL string, params interface{}) ([]byte,
if err != nil {
return nil, err
}
return c.sendRequest(req)
return c.SendRequest(req)
}

// SendPutRequest sends a correctly authenticated put request to the API server
func (c *Client) SendPutRequest(requestURL string, params interface{}) ([]byte, error) {
u := c.prepareClientURL(requestURL)
u := c.PrepareClientURL(requestURL)

// we create a new buffer and encode everything to json to send it in the request
jsonValue, _ := json.Marshal(params)
Expand All @@ -157,23 +157,23 @@ func (c *Client) SendPutRequest(requestURL string, params interface{}) ([]byte,
if err != nil {
return nil, err
}
return c.sendRequest(req)
return c.SendRequest(req)
}

// SendDeleteRequest sends a correctly authenticated delete request to the API server
func (c *Client) SendDeleteRequest(requestURL string) ([]byte, error) {
u := c.prepareClientURL(requestURL)
u := c.PrepareClientURL(requestURL)
req, err := http.NewRequest("DELETE", u.String(), nil)
if err != nil {
return nil, err
}

return c.sendRequest(req)
return c.SendRequest(req)
}

// SendDeleteRequestWithBody sends a correctly authenticated delete request to the API server
func (c *Client) SendDeleteRequestWithBody(requestURL string, params interface{}) ([]byte, error) {
u := c.prepareClientURL(requestURL)
u := c.PrepareClientURL(requestURL)

// we create a new buffer and encode everything to json to send it in the request
jsonValue, _ := json.Marshal(params)
Expand All @@ -183,7 +183,7 @@ func (c *Client) SendDeleteRequestWithBody(requestURL string, params interface{}
return nil, err
}

return c.sendRequest(req)
return c.SendRequest(req)
}

// SetUserAgent sets the user agent for the client
Expand Down
45 changes: 45 additions & 0 deletions fptcloud/mfke/mfke_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fptcloud_mfke

import (
"bytes"
"encoding/json"
"net/http"
"terraform-provider-fptcloud/commons"
)

type MfkeApiClient struct {
*commons.Client
}

func newMfkeApiClient(c *commons.Client) *MfkeApiClient {
return &MfkeApiClient{c}
}

func (m *MfkeApiClient) sendGet(requestURL string) ([]byte, error) {
u := m.Client.PrepareClientURL(requestURL)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}

return m.sendRequestWithHeader(req)
}

func (m *MfkeApiClient) sendPost(requestURL string, params interface{}) ([]byte, error) {
u := m.Client.PrepareClientURL(requestURL)

// we create a new buffer and encode everything to json to send it in the request
jsonValue, _ := json.Marshal(params)

req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
return m.sendRequestWithHeader(req)
}

func (m *MfkeApiClient) sendRequestWithHeader(request *http.Request) ([]byte, error) {
request.Header.Set("fpt-region", m.Client.Region)
request.Header.Set("infra-type", "VMW")
return m.Client.SendRequest(request)
}
Loading

0 comments on commit 7edd991

Please sign in to comment.