feat: new deployment provider: lecdn
This commit is contained in:
49
internal/pkg/sdk3rd/lecdn/v3/client/api.go
Normal file
49
internal/pkg/sdk3rd/lecdn/v3/client/api.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) ensureAccessTokenExists() error {
|
||||
c.accessTokenMtx.Lock()
|
||||
defer c.accessTokenMtx.Unlock()
|
||||
if c.accessToken != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
req := &loginRequest{
|
||||
Username: c.username,
|
||||
Password: c.password,
|
||||
}
|
||||
res, err := c.sendRequest(http.MethodPost, "/login", req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp := &loginResponse{}
|
||||
if err := json.Unmarshal(res.Body(), &resp); err != nil {
|
||||
return fmt.Errorf("lecdn api error: failed to unmarshal response: %w", err)
|
||||
} else if resp.GetCode() != 200 {
|
||||
return fmt.Errorf("lecdn get token failed: code='%d', message='%s'", resp.GetCode(), resp.GetMessage())
|
||||
}
|
||||
|
||||
c.accessToken = resp.Data.Token
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateCertificate(certId int64, req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
|
||||
if certId == 0 {
|
||||
return nil, fmt.Errorf("lecdn api error: invalid parameter: CertId")
|
||||
}
|
||||
|
||||
if err := c.ensureAccessTokenExists(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &UpdateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/certificate/%d", certId), req, resp)
|
||||
return resp, err
|
||||
}
|
||||
98
internal/pkg/sdk3rd/lecdn/v3/client/client.go
Normal file
98
internal/pkg/sdk3rd/lecdn/v3/client/client.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
apiHost string
|
||||
username string
|
||||
password string
|
||||
|
||||
accessToken string
|
||||
accessTokenMtx sync.Mutex
|
||||
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(apiHost, username, password string) *Client {
|
||||
client := resty.New()
|
||||
|
||||
return &Client{
|
||||
apiHost: strings.TrimRight(apiHost, "/"),
|
||||
username: username,
|
||||
password: password,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) WithTLSConfig(config *tls.Config) *Client {
|
||||
c.client.SetTLSClientConfig(config)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
|
||||
req := c.client.R().SetBasicAuth(c.username, c.password)
|
||||
if strings.EqualFold(method, http.MethodGet) {
|
||||
qs := make(map[string]string)
|
||||
if params != nil {
|
||||
temp := make(map[string]any)
|
||||
jsonb, _ := json.Marshal(params)
|
||||
json.Unmarshal(jsonb, &temp)
|
||||
for k, v := range temp {
|
||||
if v != nil {
|
||||
qs[k] = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
req = req.
|
||||
SetHeader("Authorization", "Bearer "+c.accessToken).
|
||||
SetQueryParams(qs)
|
||||
} else {
|
||||
req = req.
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("Authorization", "Bearer "+c.accessToken).
|
||||
SetBody(params)
|
||||
}
|
||||
|
||||
resp, err := req.Execute(method, c.apiHost+"/prod-api"+path)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return resp, fmt.Errorf("lecdn api error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result BaseResponse) error {
|
||||
resp, err := c.sendRequest(method, path, params)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
json.Unmarshal(resp.Body(), &result)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("lecdn api error: failed to unmarshal response: %w", err)
|
||||
} else if errcode := result.GetCode(); errcode != 200 {
|
||||
return fmt.Errorf("lecdn api error: code='%d', message='%s'", errcode, result.GetMessage())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
46
internal/pkg/sdk3rd/lecdn/v3/client/models.go
Normal file
46
internal/pkg/sdk3rd/lecdn/v3/client/models.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package client
|
||||
|
||||
type BaseResponse interface {
|
||||
GetCode() int32
|
||||
GetMessage() string
|
||||
}
|
||||
|
||||
type baseResponse struct {
|
||||
Code int32 `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetCode() int32 {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetMessage() string {
|
||||
return r.Message
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type loginResponse struct {
|
||||
baseResponse
|
||||
Data *struct {
|
||||
UserId int64 `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Token string `json:"token"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type UpdateCertificateRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
SSLPEM string `json:"ssl_pem"`
|
||||
SSLKey string `json:"ssl_key"`
|
||||
AutoRenewal bool `json:"auto_renewal"`
|
||||
}
|
||||
|
||||
type UpdateCertificateResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
49
internal/pkg/sdk3rd/lecdn/v3/master/api.go
Normal file
49
internal/pkg/sdk3rd/lecdn/v3/master/api.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package master
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) ensureAccessTokenExists() error {
|
||||
c.accessTokenMtx.Lock()
|
||||
defer c.accessTokenMtx.Unlock()
|
||||
if c.accessToken != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
req := &loginRequest{
|
||||
Username: c.username,
|
||||
Password: c.password,
|
||||
}
|
||||
res, err := c.sendRequest(http.MethodPost, "/auth/login", req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp := &loginResponse{}
|
||||
if err := json.Unmarshal(res.Body(), &resp); err != nil {
|
||||
return fmt.Errorf("lecdn api error: failed to unmarshal response: %w", err)
|
||||
} else if resp.GetCode() != 200 {
|
||||
return fmt.Errorf("lecdn get token failed: code='%d', message='%s'", resp.GetCode(), resp.GetMessage())
|
||||
}
|
||||
|
||||
c.accessToken = resp.Data.Token
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateCertificate(certId int64, req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
|
||||
if certId == 0 {
|
||||
return nil, fmt.Errorf("lecdn api error: invalid parameter: CertId")
|
||||
}
|
||||
|
||||
if err := c.ensureAccessTokenExists(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &UpdateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/certificate/%d", certId), req, resp)
|
||||
return resp, err
|
||||
}
|
||||
98
internal/pkg/sdk3rd/lecdn/v3/master/client.go
Normal file
98
internal/pkg/sdk3rd/lecdn/v3/master/client.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package master
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
apiHost string
|
||||
username string
|
||||
password string
|
||||
|
||||
accessToken string
|
||||
accessTokenMtx sync.Mutex
|
||||
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(apiHost, username, password string) *Client {
|
||||
client := resty.New()
|
||||
|
||||
return &Client{
|
||||
apiHost: strings.TrimRight(apiHost, "/"),
|
||||
username: username,
|
||||
password: password,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) WithTLSConfig(config *tls.Config) *Client {
|
||||
c.client.SetTLSClientConfig(config)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
|
||||
req := c.client.R().SetBasicAuth(c.username, c.password)
|
||||
if strings.EqualFold(method, http.MethodGet) {
|
||||
qs := make(map[string]string)
|
||||
if params != nil {
|
||||
temp := make(map[string]any)
|
||||
jsonb, _ := json.Marshal(params)
|
||||
json.Unmarshal(jsonb, &temp)
|
||||
for k, v := range temp {
|
||||
if v != nil {
|
||||
qs[k] = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
req = req.
|
||||
SetHeader("Authorization", "Bearer "+c.accessToken).
|
||||
SetQueryParams(qs)
|
||||
} else {
|
||||
req = req.
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("Authorization", "Bearer "+c.accessToken).
|
||||
SetBody(params)
|
||||
}
|
||||
|
||||
resp, err := req.Execute(method, c.apiHost+"/prod-api"+path)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return resp, fmt.Errorf("lecdn api error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result BaseResponse) error {
|
||||
resp, err := c.sendRequest(method, path, params)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
json.Unmarshal(resp.Body(), &result)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("lecdn api error: failed to unmarshal response: %w", err)
|
||||
} else if errcode := result.GetCode(); errcode != 200 {
|
||||
return fmt.Errorf("lecdn api error: code='%d', message='%s'", errcode, result.GetMessage())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
47
internal/pkg/sdk3rd/lecdn/v3/master/models.go
Normal file
47
internal/pkg/sdk3rd/lecdn/v3/master/models.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package master
|
||||
|
||||
type BaseResponse interface {
|
||||
GetCode() int32
|
||||
GetMessage() string
|
||||
}
|
||||
|
||||
type baseResponse struct {
|
||||
Code int32 `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetCode() int32 {
|
||||
return r.Code
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetMessage() string {
|
||||
return r.Message
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type loginResponse struct {
|
||||
baseResponse
|
||||
Data *struct {
|
||||
UserId int64 `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Token string `json:"token"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
type UpdateCertificateRequest struct {
|
||||
ClientId int64 `json:"client_id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
SSLPEM string `json:"ssl_pem"`
|
||||
SSLKey string `json:"ssl_key"`
|
||||
AutoRenewal bool `json:"auto_renewal"`
|
||||
}
|
||||
|
||||
type UpdateCertificateResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
Reference in New Issue
Block a user