yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 huawei
    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  	HuaweiTags
    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  	_, err := self.region.lbUpdate("elb/certificates/"+self.GetId(), params)
    97  	return err
    98  }
    99  
   100  func (self *SElbCert) Delete() error {
   101  	_, err := self.region.lbDelete("elb/certificates/" + self.GetId())
   102  	return err
   103  }
   104  
   105  func (self *SElbCert) GetCommonName() string {
   106  	return self.Domain
   107  }
   108  
   109  func (self *SElbCert) GetSubjectAlternativeNames() string {
   110  	return self.Domain
   111  }
   112  
   113  func (self *SElbCert) GetFingerprint() string {
   114  	_fp := sha1.Sum([]byte(self.Certificate))
   115  	fp := fmt.Sprintf("sha1:% x", _fp)
   116  	return strings.Replace(fp, " ", ":", -1)
   117  }
   118  
   119  func (self *SElbCert) GetExpireTime() time.Time {
   120  	return self.ExpireTime
   121  }
   122  
   123  func (self *SRegion) GetLoadBalancerCertificate(id string) (*SElbCert, error) {
   124  	resp, err := self.lbGet("elb/certificates/" + id)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  	ret := &SElbCert{region: self}
   129  	return ret, resp.Unmarshal(ret)
   130  }
   131  
   132  // https://support.huaweicloud.com/api-elb/elb_qy_zs_0001.html
   133  func (self *SRegion) CreateLoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (*SElbCert, error) {
   134  	params := map[string]interface{}{
   135  		"name":        cert.Name,
   136  		"private_key": cert.PrivateKey,
   137  		"certificate": cert.Certificate,
   138  	}
   139  	resp, err := self.lbCreate("elb/certificates", params)
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  	ret := &SElbCert{region: self}
   144  	return ret, resp.Unmarshal(ret)
   145  }
   146  
   147  func (self *SRegion) GetLoadBalancerCertificates() ([]SElbCert, error) {
   148  	resp, err := self.lbList("elb/certificates", url.Values{})
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  	ret := []SElbCert{}
   153  	return ret, resp.Unmarshal(&ret, "certificates")
   154  }