yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancerservercertificate.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 apsara
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/cloudmux/pkg/multicloud"
    23  )
    24  
    25  type SubjectAlternativeNames struct {
    26  	SubjectAlternativeName []string
    27  }
    28  
    29  type SLoadbalancerServerCertificate struct {
    30  	multicloud.SResourceBase
    31  	ApsaraTags
    32  	region *SRegion
    33  
    34  	ServerCertificateId     string                  //	服务器证书ID。
    35  	ServerCertificateName   string                  //	服务器证书名称。
    36  	Fingerprint             string                  //	服务器证书的指纹。
    37  	CreateTime              string                  //	服务器证书上传的时间。
    38  	CreateTimeStamp         uint64                  // 服务器证书上传的时间戳。
    39  	IsAliCloudCertificate   int                     // 是否是阿里云证书。0代表不是阿里云证书。
    40  	AliCloudCertificateName string                  //	阿里云证书名称。
    41  	AliCloudCertificateId   string                  //	阿里云证书ID。
    42  	ExpireTime              time.Time               //	过期时间。
    43  	ExpireTimeStamp         uint64                  //	过期时间戳。
    44  	CommonName              string                  //	域名,对应证书的CommonName字段。
    45  	SubjectAlternativeNames SubjectAlternativeNames // 数组格式,返回证书的备用域名列表,对应证书的Subject Alternative Name字段,详情请参见SubjectAlternativeNames。
    46  	RegionId                string                  //	负载均衡实例的地域。
    47  	DepartmentInfo
    48  }
    49  
    50  func (certificate *SLoadbalancerServerCertificate) GetPublickKey() string {
    51  	return ""
    52  }
    53  
    54  func (certificate *SLoadbalancerServerCertificate) GetPrivateKey() string {
    55  	return ""
    56  }
    57  
    58  func (certificate *SLoadbalancerServerCertificate) GetName() string {
    59  	return certificate.ServerCertificateName
    60  }
    61  
    62  func (certificate *SLoadbalancerServerCertificate) GetId() string {
    63  	return certificate.ServerCertificateId
    64  }
    65  
    66  func (certificate *SLoadbalancerServerCertificate) GetGlobalId() string {
    67  	return certificate.GetId()
    68  }
    69  
    70  func (certificate *SLoadbalancerServerCertificate) GetStatus() string {
    71  	return ""
    72  }
    73  
    74  func (certificate *SLoadbalancerServerCertificate) IsEmulated() bool {
    75  	return false
    76  }
    77  
    78  func (certificate *SLoadbalancerServerCertificate) GetCommonName() string {
    79  	return certificate.CommonName
    80  }
    81  
    82  func (certificate *SLoadbalancerServerCertificate) GetSubjectAlternativeNames() string {
    83  	return strings.Join(certificate.SubjectAlternativeNames.SubjectAlternativeName, ",")
    84  }
    85  
    86  func (certificate *SLoadbalancerServerCertificate) GetFingerprint() string {
    87  	return fmt.Sprintf("sha1:%s", strings.Replace(certificate.Fingerprint, ":", "", -1))
    88  }
    89  
    90  func (certificate *SLoadbalancerServerCertificate) GetExpireTime() time.Time {
    91  	return certificate.ExpireTime
    92  }
    93  
    94  func (certificate *SLoadbalancerServerCertificate) Refresh() error {
    95  	return nil
    96  }
    97  
    98  func (region *SRegion) UpdateServerCertificateName(certId, name string) error {
    99  	params := map[string]string{}
   100  	params["RegionId"] = region.RegionId
   101  	params["ServerCertificateId"] = certId
   102  	params["ServerCertificateName"] = name
   103  	_, err := region.lbRequest("SetServerCertificateName", params)
   104  	return err
   105  }
   106  
   107  func (certificate *SLoadbalancerServerCertificate) Sync(name string, privateKey string, publicKey string) error {
   108  	if certificate.ServerCertificateName != name {
   109  		return certificate.region.UpdateServerCertificateName(certificate.ServerCertificateId, name)
   110  	}
   111  	return nil
   112  }
   113  
   114  func (certificate *SLoadbalancerServerCertificate) Delete() error {
   115  	return certificate.region.DeleteServerCertificate(certificate.ServerCertificateId)
   116  }
   117  
   118  func (region *SRegion) DeleteServerCertificate(certId string) error {
   119  	params := map[string]string{}
   120  	params["RegionId"] = region.RegionId
   121  	params["ServerCertificateId"] = certId
   122  	_, err := region.lbRequest("DeleteServerCertificate", params)
   123  	return err
   124  }
   125  
   126  func (region *SRegion) GetLoadbalancerServerCertificates() ([]SLoadbalancerServerCertificate, error) {
   127  	params := map[string]string{}
   128  	params["RegionId"] = region.RegionId
   129  	body, err := region.lbRequest("DescribeServerCertificates", params)
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  	certificates := []SLoadbalancerServerCertificate{}
   134  	return certificates, body.Unmarshal(&certificates, "ServerCertificates", "ServerCertificate")
   135  }