yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/loadbalancer_cert.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package hcs
    16  
    17  import (
    18  	"crypto/sha1"
    19  	"fmt"
    20  	"net/url"
    21  	"strings"
    22  	"time"
    23  
    24  	"yunion.io/x/jsonutils"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type SElbCert struct {
    32  	multicloud.SResourceBase
    33  	HcsTags
    34  	region *SRegion
    35  
    36  	Certificate  string    `json:"certificate"`
    37  	CreateTime   time.Time `json:"create_time"`
    38  	ExpireTime   time.Time `json:"expire_time"`
    39  	Description  string    `json:"description"`
    40  	Domain       string    `json:"domain"`
    41  	ID           string    `json:"id"`
    42  	AdminStateUp bool      `json:"admin_state_up"`
    43  	TenantID     string    `json:"tenant_id"`
    44  	Name         string    `json:"name"`
    45  	PrivateKey   string    `json:"private_key"`
    46  	Type         string    `json:"type"`
    47  	UpdateTime   time.Time `json:"update_time"`
    48  }
    49  
    50  func (self *SElbCert) GetPublickKey() string {
    51  	return self.Certificate
    52  }
    53  
    54  func (self *SElbCert) GetPrivateKey() string {
    55  	return self.PrivateKey
    56  }
    57  
    58  func (self *SElbCert) GetId() string {
    59  	return self.ID
    60  }
    61  
    62  func (self *SElbCert) GetName() string {
    63  	return self.Name
    64  }
    65  
    66  func (self *SElbCert) GetGlobalId() string {
    67  	return self.GetId()
    68  }
    69  
    70  func (self *SElbCert) GetStatus() string {
    71  	return api.LB_STATUS_ENABLED
    72  }
    73  
    74  func (self *SElbCert) Refresh() error {
    75  	cert, err := self.region.GetLoadBalancerCertificate(self.GetId())
    76  	if err != nil {
    77  		return err
    78  	}
    79  	return jsonutils.Update(self, cert)
    80  }
    81  
    82  func (self *SElbCert) IsEmulated() bool {
    83  	return false
    84  }
    85  
    86  func (self *SElbCert) GetProjectId() string {
    87  	return ""
    88  }
    89  
    90  func (self *SElbCert) Sync(name, privateKey, publickKey string) error {
    91  	params := map[string]interface{}{
    92  		"name":        name,
    93  		"private_key": privateKey,
    94  		"certificate": publickKey,
    95  	}
    96  	return self.region.lbUpdate("lbaas/certificates/"+self.GetId(), params)
    97  }
    98  
    99  func (self *SElbCert) Delete() error {
   100  	return self.region.lbDelete("lbaas/certificates/" + self.GetId())
   101  }
   102  
   103  func (self *SElbCert) GetCommonName() string {
   104  	return self.Domain
   105  }
   106  
   107  func (self *SElbCert) GetSubjectAlternativeNames() string {
   108  	return self.Domain
   109  }
   110  
   111  func (self *SElbCert) GetFingerprint() string {
   112  	_fp := sha1.Sum([]byte(self.Certificate))
   113  	fp := fmt.Sprintf("sha1:% x", _fp)
   114  	return strings.Replace(fp, " ", ":", -1)
   115  }
   116  
   117  func (self *SElbCert) GetExpireTime() time.Time {
   118  	return self.ExpireTime
   119  }
   120  
   121  func (self *SRegion) GetLoadBalancerCertificate(id string) (*SElbCert, error) {
   122  	ret := &SElbCert{region: self}
   123  	err := self.lbGet("lbaas/certificates/"+id, ret)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	return ret, nil
   128  }
   129  
   130  // https://support.huaweicloud.com/api-elb/elb_qy_zs_0001.html
   131  func (self *SRegion) CreateLoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (*SElbCert, error) {
   132  	params := map[string]interface{}{
   133  		"name":        cert.Name,
   134  		"private_key": cert.PrivateKey,
   135  		"certificate": cert.Certificate,
   136  	}
   137  	ret := &SElbCert{region: self}
   138  	err := self.lbCreate("lbaas/certificates", params, ret)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	return ret, nil
   143  }
   144  
   145  func (self *SRegion) GetLoadBalancerCertificates() ([]SElbCert, error) {
   146  	ret := []SElbCert{}
   147  	err := self.lbList("lbaas/certificates", url.Values{}, &ret)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	return ret, nil
   152  }
   153  
   154  func (self *SRegion) GetILoadBalancerCertificates() ([]cloudprovider.ICloudLoadbalancerCertificate, error) {
   155  	ret, err := self.GetLoadBalancerCertificates()
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  
   160  	iret := make([]cloudprovider.ICloudLoadbalancerCertificate, len(ret))
   161  	for i := range ret {
   162  		ret[i].region = self
   163  		iret[i] = &ret[i]
   164  	}
   165  	return iret, nil
   166  }
   167  
   168  func (self *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (cloudprovider.ICloudLoadbalancerCertificate, error) {
   169  	ret, err := self.CreateLoadBalancerCertificate(cert)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	return ret, nil
   174  }
   175  
   176  func (self *SRegion) GetILoadBalancerCertificateById(certId string) (cloudprovider.ICloudLoadbalancerCertificate, error) {
   177  	cert, err := self.GetLoadBalancerCertificate(certId)
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  	return cert, nil
   182  }