feat: new deployment provider: wangsu certificate management
This commit is contained in:
@@ -174,10 +174,10 @@ func (c *Client) sendReq(method string, path string, data map[string]interface{}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
r, err := io.ReadAll(resp.Body)
|
||||
bytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ func (c *Client) ensureAccessTokenExists() error {
|
||||
}
|
||||
|
||||
req := &loginRequest{
|
||||
Email: c.username,
|
||||
Username: c.username,
|
||||
Password: c.password,
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ func (r *baseResponse) GetMessage() string {
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
Email string `json:"email"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package cdn
|
||||
package cdnpro
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -10,14 +10,14 @@ import (
|
||||
|
||||
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
|
||||
resp := &CreateCertificateResponse{}
|
||||
r, err := c.client.SendRequestWithResult(http.MethodPost, "/cdn/certificates", req, resp, func(r *resty.Request) {
|
||||
rres, err := c.client.SendRequestWithResult(http.MethodPost, "/cdn/certificates", req, resp, func(r *resty.Request) {
|
||||
r.SetHeader("x-cnc-timestamp", fmt.Sprintf("%d", req.Timestamp))
|
||||
})
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
resp.CertificateUrl = r.Header().Get("Location")
|
||||
resp.CertificateUrl = rres.Header().Get("Location")
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ func (c *Client) UpdateCertificate(certificateId string, req *UpdateCertificateR
|
||||
}
|
||||
|
||||
resp := &UpdateCertificateResponse{}
|
||||
r, err := c.client.SendRequestWithResult(http.MethodPatch, fmt.Sprintf("/cdn/certificates/%s", url.PathEscape(certificateId)), req, resp, func(r *resty.Request) {
|
||||
rres, err := c.client.SendRequestWithResult(http.MethodPatch, fmt.Sprintf("/cdn/certificates/%s", url.PathEscape(certificateId)), req, resp, func(r *resty.Request) {
|
||||
r.SetHeader("x-cnc-timestamp", fmt.Sprintf("%d", req.Timestamp))
|
||||
})
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
resp.CertificateUrl = r.Header().Get("Location")
|
||||
resp.CertificateUrl = rres.Header().Get("Location")
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -50,12 +50,12 @@ func (c *Client) GetHostnameDetail(hostname string) (*GetHostnameDetailResponse,
|
||||
|
||||
func (c *Client) CreateDeploymentTask(req *CreateDeploymentTaskRequest) (*CreateDeploymentTaskResponse, error) {
|
||||
resp := &CreateDeploymentTaskResponse{}
|
||||
r, err := c.client.SendRequestWithResult(http.MethodPost, "/cdn/deploymentTasks", req, resp)
|
||||
rres, err := c.client.SendRequestWithResult(http.MethodPost, "/cdn/deploymentTasks", req, resp)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
resp.DeploymentTaskUrl = r.Header().Get("Location")
|
||||
resp.DeploymentTaskUrl = rres.Header().Get("Location")
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package cdn
|
||||
package cdnpro
|
||||
|
||||
import (
|
||||
"time"
|
||||
@@ -1,11 +1,11 @@
|
||||
package cdn
|
||||
package cdnpro
|
||||
|
||||
import (
|
||||
"github.com/usual2970/certimate/internal/pkg/sdk3rd/wangsu/openapi"
|
||||
)
|
||||
|
||||
type baseResponse struct {
|
||||
RequestId *string `json:"-"`
|
||||
RequestId *string `json:"requestId,omitempty"`
|
||||
Code *string `json:"code,omitempty"`
|
||||
Message *string `json:"message,omitempty"`
|
||||
}
|
||||
42
internal/pkg/sdk3rd/wangsu/certificate/api.go
Normal file
42
internal/pkg/sdk3rd/wangsu/certificate/api.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c *Client) ListCertificates() (*ListCertificatesResponse, error) {
|
||||
resp := &ListCertificatesResponse{}
|
||||
_, err := c.client.SendRequestWithResult(http.MethodGet, "/api/certificate", nil, resp)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
|
||||
resp := &CreateCertificateResponse{}
|
||||
rres, err := c.client.SendRequestWithResult(http.MethodPost, "/api/certificate", req, resp)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
resp.CertificateUrl = rres.Header().Get("Location")
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) UpdateCertificate(certificateId string, req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
|
||||
if certificateId == "" {
|
||||
return nil, fmt.Errorf("wangsu api error: invalid parameter: certificateId")
|
||||
}
|
||||
|
||||
resp := &UpdateCertificateResponse{}
|
||||
_, err := c.client.SendRequestWithResult(http.MethodPut, fmt.Sprintf("/api/certificate/%s", url.PathEscape(certificateId)), req, resp)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp, err
|
||||
}
|
||||
20
internal/pkg/sdk3rd/wangsu/certificate/client.go
Normal file
20
internal/pkg/sdk3rd/wangsu/certificate/client.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/usual2970/certimate/internal/pkg/sdk3rd/wangsu/openapi"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
client *openapi.Client
|
||||
}
|
||||
|
||||
func NewClient(accessKey, secretKey string) *Client {
|
||||
return &Client{client: openapi.NewClient(accessKey, secretKey)}
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
c.client.WithTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
52
internal/pkg/sdk3rd/wangsu/certificate/models.go
Normal file
52
internal/pkg/sdk3rd/wangsu/certificate/models.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"github.com/usual2970/certimate/internal/pkg/sdk3rd/wangsu/openapi"
|
||||
)
|
||||
|
||||
type baseResponse struct {
|
||||
RequestId *string `json:"requestId,omitempty"`
|
||||
Code *string `json:"code,omitempty"`
|
||||
Message *string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
var _ openapi.Result = (*baseResponse)(nil)
|
||||
|
||||
func (r *baseResponse) SetRequestId(requestId string) {
|
||||
r.RequestId = &requestId
|
||||
}
|
||||
|
||||
type CreateCertificateRequest struct {
|
||||
Name *string `json:"name,omitempty" required:"true"`
|
||||
Certificate *string `json:"certificate,omitempty" required:"true"`
|
||||
PrivateKey *string `json:"privateKey,omitempty"`
|
||||
Comment *string `json:"comment,omitempty" `
|
||||
}
|
||||
|
||||
type CreateCertificateResponse struct {
|
||||
baseResponse
|
||||
CertificateUrl string `json:"location,omitempty"`
|
||||
}
|
||||
|
||||
type UpdateCertificateRequest struct {
|
||||
Name *string `json:"name,omitempty" required:"true"`
|
||||
Certificate *string `json:"certificate,omitempty"`
|
||||
PrivateKey *string `json:"privateKey,omitempty"`
|
||||
Comment *string `json:"comment,omitempty" `
|
||||
}
|
||||
|
||||
type UpdateCertificateResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
|
||||
type ListCertificatesResponse struct {
|
||||
baseResponse
|
||||
Certificates []*struct {
|
||||
CertificateId string `json:"certificate-id"`
|
||||
Name string `json:"name"`
|
||||
Comment string `json:"comment"`
|
||||
ValidityFrom string `json:"certificate-validity-from"`
|
||||
ValidityTo string `json:"certificate-validity-to"`
|
||||
Serial string `json:"certificate-serial"`
|
||||
} `json:"ssl-certificates,omitempty"`
|
||||
}
|
||||
@@ -154,8 +154,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}, con
|
||||
req = req.SetBody(params)
|
||||
}
|
||||
|
||||
for _, fn := range configureReq {
|
||||
fn(req)
|
||||
if configureReq != nil {
|
||||
for _, fn := range configureReq {
|
||||
fn(req)
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := req.Send()
|
||||
|
||||
Reference in New Issue
Block a user