yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"crypto/sha1"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	"yunion.io/x/jsonutils"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    28  )
    29  
    30  type SElbCert struct {
    31  	multicloud.SResourceBase
    32  	huawei.HuaweiTags
    33  	region *SRegion
    34  
    35  	Certificate  string    `json:"certificate"`
    36  	CreateTime   time.Time `json:"create_time"`
    37  	ExpireTime   time.Time `json:"expire_time"`
    38  	Description  string    `json:"description"`
    39  	Domain       string    `json:"domain"`
    40  	ID           string    `json:"id"`
    41  	AdminStateUp bool      `json:"admin_state_up"`
    42  	TenantID     string    `json:"tenant_id"`
    43  	Name         string    `json:"name"`
    44  	PrivateKey   string    `json:"private_key"`
    45  	Type         string    `json:"type"`
    46  	UpdateTime   time.Time `json:"update_time"`
    47  }
    48  
    49  func (self *SElbCert) GetPublickKey() string {
    50  	return self.Certificate
    51  }
    52  
    53  func (self *SElbCert) GetPrivateKey() string {
    54  	return self.PrivateKey
    55  }
    56  
    57  func (self *SElbCert) GetId() string {
    58  	return self.ID
    59  }
    60  
    61  func (self *SElbCert) GetName() string {
    62  	return self.Name
    63  }
    64  
    65  func (self *SElbCert) GetGlobalId() string {
    66  	return self.GetId()
    67  }
    68  
    69  func (self *SElbCert) GetStatus() string {
    70  	return api.LB_STATUS_ENABLED
    71  }
    72  
    73  func (self *SElbCert) Refresh() error {
    74  	cert, err := self.region.GetLoadBalancerCertificateById(self.GetId())
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	cert.region = self.region
    80  	err = jsonutils.Update(self, cert)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  func (self *SElbCert) IsEmulated() bool {
    89  	return false
    90  }
    91  
    92  func (self *SElbCert) GetProjectId() string {
    93  	return ""
    94  }
    95  
    96  func (self *SElbCert) Sync(name, privateKey, publickKey string) error {
    97  	params := jsonutils.NewDict()
    98  	params.Set("name", jsonutils.NewString(name))
    99  	params.Set("private_key", jsonutils.NewString(privateKey))
   100  	params.Set("certificate", jsonutils.NewString(publickKey))
   101  	return DoUpdate(self.region.ecsClient.ElbCertificates.Update, self.GetId(), params, nil)
   102  }
   103  
   104  func (self *SElbCert) Delete() error {
   105  	return DoDelete(self.region.ecsClient.ElbCertificates.Delete, self.GetId(), nil, nil)
   106  }
   107  
   108  func (self *SElbCert) GetCommonName() string {
   109  	return self.Domain
   110  }
   111  
   112  func (self *SElbCert) GetSubjectAlternativeNames() string {
   113  	return self.Domain
   114  }
   115  
   116  func (self *SElbCert) GetFingerprint() string {
   117  	_fp := sha1.Sum([]byte(self.Certificate))
   118  	fp := fmt.Sprintf("sha1:% x", _fp)
   119  	return strings.Replace(fp, " ", ":", -1)
   120  }
   121  
   122  func (self *SElbCert) GetExpireTime() time.Time {
   123  	return self.ExpireTime
   124  }