refactor(provider): remove dependency of github.com/holubovskyi/apisix-client-go

This commit is contained in:
Fu Diwei
2026-01-08 18:04:46 +08:00
parent 60fd563bf1
commit d72b8f1e0e
8 changed files with 163 additions and 25 deletions
-1
View File
@@ -45,7 +45,6 @@ require (
github.com/go-resty/resty/v2 v2.17.1
github.com/go-viper/mapstructure/v2 v2.4.0
github.com/google/go-querystring v1.1.0
github.com/holubovskyi/apisix-client-go v1.4.1
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.181
github.com/jdcloud-api/jdcloud-sdk-go v1.64.0
github.com/kong/go-kong v0.71.0
-2
View File
@@ -539,8 +539,6 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.1/go.mod h1:4gW7WsVCke5TE7EPeYliwHlRUyBtfCwuFwuMg2DmyNY=
github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE=
github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk=
github.com/holubovskyi/apisix-client-go v1.4.1 h1:Z0Vqfubo0nleuChfLcsWLr/ZQ1J3+g4UMttfAx/8E9Y=
github.com/holubovskyi/apisix-client-go v1.4.1/go.mod h1:MYo1juyaHbX6RWe9S3DoqWqL0Ko/2pr3ZpDQVNKVXqI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.181 h1:t0vgvyLg/WRUlXbhLqoSCdhdl1EHynroGJTmZ8SIThU=
github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.181/go.mod h1:M+yna96Fx9o5GbIUnF3OvVvQGjgfVSyeJbV9Yb1z/wI=
+10 -20
View File
@@ -7,12 +7,11 @@ import (
"fmt"
"log/slog"
apisix "github.com/holubovskyi/apisix-client-go"
"github.com/samber/lo"
"github.com/certimate-go/certimate/pkg/core/deployer"
apisixsdk "github.com/certimate-go/certimate/pkg/sdk3rd/apisix"
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
xhttp "github.com/certimate-go/certimate/pkg/utils/http"
)
type DeployerConfig struct {
@@ -32,7 +31,7 @@ type DeployerConfig struct {
type Deployer struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *apisix.ApiClient
sdkClient *apisixsdk.Client
}
var _ deployer.Provider = (*Deployer)(nil)
@@ -90,40 +89,31 @@ func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM
// 更新 SSL 证书
// REF: https://apisix.apache.org/zh/docs/apisix/admin-api/#ssl
updateSSLCertificateReq := &apisix.SSLCertificate{
sslUpdateReq := &apisixsdk.SslUpdateRequest{
ID: lo.ToPtr(d.config.CertificateId),
Certificate: lo.ToPtr(certPEM),
PrivateKey: lo.ToPtr(privkeyPEM),
SNIs: lo.ToPtr(certX509.DNSNames),
Type: lo.ToPtr("server"),
Status: lo.ToPtr(int64(1)),
Status: lo.ToPtr(int32(1)),
}
updateSSLCertificateResp, err := d.sdkClient.UpdateSslCertificate(d.config.CertificateId, *updateSSLCertificateReq)
d.logger.Debug("sdk request 'apisix.UpdateSslCertificate'", slog.Any("request", updateSSLCertificateReq), slog.Any("response", updateSSLCertificateResp))
sslUpdateResp, err := d.sdkClient.SslUpdate(d.config.CertificateId, sslUpdateReq)
d.logger.Debug("sdk request 'apisix.SslUpdate'", slog.Any("request", sslUpdateReq), slog.Any("response", sslUpdateResp))
if err != nil {
return fmt.Errorf("failed to execute sdk request 'apisix.UpdateSslCertificate': %w", err)
return fmt.Errorf("failed to execute sdk request 'apisix.SslUpdate': %w", err)
}
return nil
}
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*apisix.ApiClient, error) {
client, err := apisix.NewClient(&serverUrl, &apiKey)
func createSDKClient(serverUrl, apiKey string, skipTlsVerify bool) (*apisixsdk.Client, error) {
client, err := apisixsdk.NewClient(serverUrl, apiKey)
if err != nil {
return nil, err
}
if skipTlsVerify {
transport := xhttp.NewDefaultTransport()
transport.DisableKeepAlives = true
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
if client.HTTPClient.Transport == nil {
client.HTTPClient.Transport = transport
} else {
transport := client.HTTPClient.Transport.(*apisix.AddHeadersRoundtripper)
transport.Nested = transport
client.HTTPClient.Transport = transport
}
client.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
}
return client, nil
+33
View File
@@ -0,0 +1,33 @@
package apisix
import (
"context"
"fmt"
"net/http"
"net/url"
)
type SslUpdateRequest = SslCertificate
type SslUpdateResponse = SslCertificate
func (c *Client) SslUpdate(sslId string, req *SslUpdateRequest) (*SslUpdateResponse, error) {
return c.SslUpdateWithContext(context.Background(), sslId, req)
}
func (c *Client) SslUpdateWithContext(ctx context.Context, sslId string, req *SslUpdateRequest) (*SslUpdateResponse, error) {
httpreq, err := c.newRequest(http.MethodPut, fmt.Sprintf("/ssls/%s", url.PathEscape(sslId)))
if err != nil {
return nil, err
} else {
httpreq.SetBody(req)
httpreq.SetContext(ctx)
}
result := &SslUpdateResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}
+105
View File
@@ -0,0 +1,105 @@
package apisix
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/certimate-go/certimate/internal/app"
)
type Client struct {
client *resty.Client
}
func NewClient(serverUrl, apiKey string) (*Client, error) {
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiKey == "" {
return nil, fmt.Errorf("sdkerr: unset apiKey")
}
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/apisix/admin").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", app.AppUserAgent).
SetHeader("X-Api-Key", apiKey)
return &Client{
client: client,
}, nil
}
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
return c
}
func (c *Client) newRequest(method string, path string) (*resty.Request, error) {
if method == "" {
return nil, fmt.Errorf("sdkerr: unset method")
}
if path == "" {
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req.Method = method
req.URL = path
return req, nil
}
func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
if req == nil {
return nil, fmt.Errorf("sdkerr: nil request")
}
// WARN:
// PLEASE DO NOT USE `req.SetResult` or `req.SetError` HERE! USE `doRequestWithResult` INSTEAD.
resp, err := req.Send()
if err != nil {
return resp, fmt.Errorf("sdkerr: failed to send request: %w", err)
} else if resp.IsError() {
return resp, fmt.Errorf("sdkerr: unexpected status code: %d (resp: %s)", resp.StatusCode(), resp.String())
}
return resp, nil
}
func (c *Client) doRequestWithResult(req *resty.Request, res interface{}) (*resty.Response, error) {
if req == nil {
return nil, fmt.Errorf("sdkerr: nil request")
}
resp, err := c.doRequest(req)
if err != nil {
if resp != nil {
json.Unmarshal(resp.Body(), &res)
}
return resp, err
}
if len(resp.Body()) != 0 {
if err := json.Unmarshal(resp.Body(), &res); err != nil {
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w (resp: %s)", err, resp.String())
}
}
return resp, nil
}
+13
View File
@@ -0,0 +1,13 @@
package apisix
type SslCertificate struct {
ID *string `json:"id,omitempty"`
Status *int32 `json:"status,omitempty"`
Certificate *string `json:"cert,omitempty"`
PrivateKey *string `json:"key,omitempty"`
SNIs *[]string `json:"snis,omitempty"`
Type *string `json:"type,omitempty"`
ValidityStart *int64 `json:"validity_start,omitempty"`
ValidityEnd *int64 `json:"validity_end,omitempty"`
Labels *map[string]string `json:"labels,omitempty"`
}
+1 -1
View File
@@ -144,7 +144,7 @@ func (c *Client) DoRequest(req *resty.Request) (*resty.Response, error) {
return resp, nil
}
func (c *Client) DoRequestWithResult(req *resty.Request, res any) (*resty.Response, error) {
func (c *Client) DoRequestWithResult(req *resty.Request, res interface{}) (*resty.Response, error) {
if req == nil {
return nil, fmt.Errorf("sdkerr: nil request")
}
+1 -1
View File
@@ -170,7 +170,7 @@ func (c *Client) DoRequest(req *resty.Request) (*resty.Response, error) {
return resp, nil
}
func (c *Client) DoRequestWithResult(req *resty.Request, res any) (*resty.Response, error) {
func (c *Client) DoRequestWithResult(req *resty.Request, res interface{}) (*resty.Response, error) {
if req == nil {
return nil, fmt.Errorf("sdkerr: nil request")
}