Skip to content

Commit

Permalink
fix(huawei): ssl certificate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito committed Dec 19, 2023
1 parent 9257091 commit d76c3d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 53 deletions.
20 changes: 4 additions & 16 deletions pkg/multicloud/huawei/huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,21 +631,9 @@ func (self *SHuaweiClient) QueryAccountBalance() (*SBalance, error) {
}

func (self *SHuaweiClient) GetISSLCertificates() ([]cloudprovider.ICloudSSLCertificate, error) {
ret := make([]SSSLCertificate, 0)
offset := 0

for {
part, total, err := self.GetSSLCertificates(50, offset)
if err != nil {
return nil, errors.Wrapf(err, "GetSSLCertificates")
}

ret = append(ret, part...)
if len(ret) >= total {
break
}

offset += 50
ret, err := self.GetSSLCertificates()
if err != nil {
return nil, errors.Wrapf(err, "GetSSLCertificates")
}

result := make([]cloudprovider.ICloudSSLCertificate, 0)
Expand Down Expand Up @@ -858,7 +846,7 @@ func (self *SHuaweiClient) getUrl(service, regionId, resource string, method htt
case SERVICE_NAT:
url = fmt.Sprintf("https://nat.%s.myhuaweicloud.com/v2/%s/%s", regionId, self.projectId, resource)
case SERVICE_SCM:
url = fmt.Sprintf("https://scm.%s.myhuaweicloud.com/v3/%s", HUAWEI_DEFAULT_REGION, resource)
url = fmt.Sprintf("https://scm.cn-north-4.myhuaweicloud.com/v3/%s", resource)
case SERVICE_CDN:
url = fmt.Sprintf("https://cdn.myhuaweicloud.com/v1.0/%s", resource)
case SERVICE_GAUSSDB, SERVICE_GAUSSDB_NOSQL:
Expand Down
26 changes: 21 additions & 5 deletions pkg/multicloud/huawei/shell/sslcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,35 @@ import (

func init() {
type SSlCertificateListOptions struct {
Page int
Size int
}
shellutils.R(
&SSlCertificateListOptions{},
"sslcertificate-list",
"ssl-certificate-list",
"List ssl certificates",
func(cli *huawei.SRegion, args *SSlCertificateListOptions) error {
certs, _, err := cli.GetClient().GetSSLCertificates(args.Size, args.Page)
certs, err := cli.GetClient().GetSSLCertificates()
if err != nil {
return err
}
printList(certs, 0, 0, 0, nil)
return nil
})
}

type SSlCertificateIdOptions struct {
ID string
}

shellutils.R(
&SSlCertificateIdOptions{},
"ssl-certificate-show",
"List ssl certificates",
func(cli *huawei.SRegion, args *SSlCertificateIdOptions) error {
cert, err := cli.GetClient().GetSSLCertificate(args.ID)
if err != nil {
return err
}
printObject(cert)
return nil
})

}
58 changes: 26 additions & 32 deletions pkg/multicloud/huawei/sslcertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,48 +137,42 @@ func (s *SSSLCertificate) GetKey() string {

func (s *SSSLCertificate) GetDetails() (*SSSLCertificate, error) {
if !s.detailsInitd {
cert, err := s.client.GetISSLCertificate(s.GetId())
cert, err := s.client.GetSSLCertificate(s.GetId())
if err != nil {
return nil, err
}
s.detailsInitd = true
_cert, ok := cert.(*SSSLCertificate)
if !ok {
return nil, errors.Wrapf(err, "cert.(*SSSLCertificate)")
}
s.Certificate = _cert.Certificate
s.PrivateKey = _cert.PrivateKey
s.Certificate = cert.Certificate
s.PrivateKey = cert.PrivateKey
}
return s, nil
}

func (r *SHuaweiClient) GetSSLCertificates(size, offset int) ([]SSSLCertificate, int, error) {
if size < 1 || size > 50 {
size = 50
}
if offset < 0 {
offset = 0
}

params := url.Values{
"limit": []string{fmt.Sprintf("%d", size)},
"offset": []string{fmt.Sprintf("%d", offset)},
"sort_key": []string{"certExpiredTime"},
"sort_dir": []string{"DESC"},
}
resp, err := r.list(SERVICE_SCM, "", "scm/certificates", params)
if err != nil {
return nil, 0, errors.Wrapf(err, "CertificateList")
}

func (r *SHuaweiClient) GetSSLCertificates() ([]SSSLCertificate, error) {
params := url.Values{}
params.Set("sort_key", "certExpiredTime")
params.Set("sort_dir", "DESC")
ret := make([]SSSLCertificate, 0)
err = resp.Unmarshal(&ret, "certificates")
if err != nil {
return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
for {
resp, err := r.list(SERVICE_SCM, "", "scm/certificates", params)
if err != nil {
return nil, errors.Wrapf(err, "list certificates")
}
part := struct {
Certificates []SSSLCertificate
TotalCount int
}{}
err = resp.Unmarshal(&part)
if err != nil {
return nil, err
}
ret = append(ret, part.Certificates...)
if len(ret) >= part.TotalCount || len(part.Certificates) == 0 {
break
}
params.Set("offset", fmt.Sprintf("%d", len(ret)))
}

totalCount, _ := resp.Int("total_count")
return ret, int(totalCount), nil
return ret, nil
}

func (r *SHuaweiClient) GetSSLCertificate(certId string) (*SSSLCertificate, error) {
Expand Down

0 comments on commit d76c3d8

Please sign in to comment.