yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/dnspod_record.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 qcloud
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  )
    28  
    29  type SRecordCreateRet struct {
    30  	ID     string `json:"id"`
    31  	Name   string `json:"name"`
    32  	Status string `json:"status"`
    33  	Weight int    `json:"weight"`
    34  }
    35  
    36  type SRecordCountInfo struct {
    37  	RecordTotal string `json:"record_total"`
    38  	RecordsNum  string `json:"records_num"`
    39  	SubDomains  string `json:"sub_domains"`
    40  }
    41  
    42  type SDnsRecord struct {
    43  	domain *SDomian
    44  
    45  	Line          string `json:"Line"`
    46  	LineId        string `json:"LineId"`
    47  	MX            int64  `json:"MX"`
    48  	MonitorStatus string `json:"MonitorStatus"`
    49  	Name          string `json:"Name"`
    50  	RecordId      int    `json:"RecordId"`
    51  	Remark        string `json:"Remark"`
    52  	Status        string `json:"Status"`
    53  	TTL           int64  `json:"TTL"`
    54  	Type          string `json:"Type"`
    55  	UpdatedOn     string `json:"UpdatedOn"`
    56  	Value         string `json:"Value"`
    57  }
    58  
    59  func (self *SQcloudClient) GetDnsRecords(domain string, offset int, limit int) ([]SDnsRecord, int, error) {
    60  	params := map[string]string{}
    61  	params["Domain"] = domain
    62  	params["Offset"] = strconv.Itoa(offset)
    63  	if limit > 0 {
    64  		params["Limit"] = strconv.Itoa(limit)
    65  	}
    66  	resp, err := self.dnsRequest("DescribeRecordList", params)
    67  	if err != nil {
    68  		return nil, 0, errors.Wrapf(err, "DescribeRecordList")
    69  	}
    70  	ret := []SDnsRecord{}
    71  	err = resp.Unmarshal(&ret, "RecordList")
    72  	if err != nil {
    73  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal RecordList")
    74  	}
    75  	total, err := resp.Float("RecordCountInfo", "TotalCount")
    76  	return ret, int(total), err
    77  }
    78  
    79  func GetRecordLineLineType(policyinfo cloudprovider.TDnsPolicyValue) string {
    80  	switch policyinfo {
    81  	case cloudprovider.DnsPolicyValueMainland:
    82  		return "境内"
    83  	case cloudprovider.DnsPolicyValueOversea:
    84  		return "境外"
    85  	case cloudprovider.DnsPolicyValueTelecom:
    86  		return "电信"
    87  	case cloudprovider.DnsPolicyValueUnicom:
    88  		return "联通"
    89  	case cloudprovider.DnsPolicyValueChinaMobile:
    90  		return "移动"
    91  	case cloudprovider.DnsPolicyValueCernet:
    92  		return "教育网"
    93  
    94  	case cloudprovider.DnsPolicyValueBaidu:
    95  		return "百度"
    96  	case cloudprovider.DnsPolicyValueGoogle:
    97  		return "谷歌"
    98  	case cloudprovider.DnsPolicyValueYoudao:
    99  		return "有道"
   100  	case cloudprovider.DnsPolicyValueBing:
   101  		return "必应"
   102  	case cloudprovider.DnsPolicyValueSousou:
   103  		return "搜搜"
   104  	case cloudprovider.DnsPolicyValueSougou:
   105  		return "搜狗"
   106  	case cloudprovider.DnsPolicyValueQihu360:
   107  		return "奇虎"
   108  	default:
   109  		return "默认"
   110  	}
   111  }
   112  
   113  // https://cloud.tencent.com/document/api/1427/56180
   114  func (self *SQcloudClient) CreateDnsRecord(opts *cloudprovider.DnsRecordSet, domainName string) (string, error) {
   115  	params := map[string]string{}
   116  	recordline := GetRecordLineLineType(opts.PolicyValue)
   117  	if opts.Ttl < 600 {
   118  		opts.Ttl = 600
   119  	}
   120  	if opts.Ttl > 604800 {
   121  		opts.Ttl = 604800
   122  	}
   123  	if len(opts.DnsName) < 1 {
   124  		opts.DnsName = "@"
   125  	}
   126  	params["Domain"] = domainName
   127  	params["SubDomain"] = opts.DnsName
   128  	params["RecordType"] = string(opts.DnsType)
   129  	params["TTL"] = strconv.FormatInt(opts.Ttl, 10)
   130  	params["Value"] = opts.DnsValue
   131  	params["RecordLine"] = recordline
   132  	if opts.DnsType == cloudprovider.DnsTypeMX {
   133  		params["MX"] = strconv.FormatInt(opts.MxPriority, 10)
   134  	}
   135  	if !opts.Enabled {
   136  		params["Status"] = "DISABLE"
   137  	}
   138  	resp, err := self.dnsRequest("CreateRecord", params)
   139  	if err != nil {
   140  		return "", errors.Wrapf(err, "CreateRecord")
   141  	}
   142  	return resp.GetString("RecordId")
   143  }
   144  
   145  // https://cloud.tencent.com/document/api/1427/56157
   146  func (self *SQcloudClient) ModifyDnsRecord(opts *cloudprovider.DnsRecordSet, domainName string) error {
   147  	params := map[string]string{}
   148  	recordline := GetRecordLineLineType(opts.PolicyValue)
   149  	if opts.Ttl < 600 {
   150  		opts.Ttl = 600
   151  	}
   152  	if opts.Ttl > 604800 {
   153  		opts.Ttl = 604800
   154  	}
   155  	subDomain := strings.TrimSuffix(opts.DnsName, "."+domainName)
   156  	if len(subDomain) < 1 {
   157  		subDomain = "@"
   158  	}
   159  	params["Domain"] = domainName
   160  	params["RecordId"] = opts.ExternalId
   161  	params["SubDomain"] = subDomain
   162  	params["RecordType"] = string(opts.DnsType)
   163  	params["TTL"] = strconv.FormatInt(opts.Ttl, 10)
   164  	params["Value"] = opts.DnsValue
   165  	params["RecordLine"] = recordline
   166  	if opts.DnsType == cloudprovider.DnsTypeMX {
   167  		params["MX"] = strconv.FormatInt(opts.MxPriority, 10)
   168  	}
   169  	if !opts.Enabled {
   170  		params["Status"] = "DISABLE"
   171  	}
   172  	_, err := self.dnsRequest("ModifyRecord", params)
   173  	if err != nil {
   174  		return errors.Wrapf(err, "ModifyRecord")
   175  	}
   176  	return nil
   177  }
   178  
   179  // https://cloud.tencent.com/document/product/302/8519
   180  func (client *SQcloudClient) ModifyRecordStatus(status, recordId, domain string) error {
   181  	params := map[string]string{}
   182  	params["Domain"] = domain
   183  	params["RecordId"] = recordId
   184  	params["Status"] = status // “disable” 和 “enable”
   185  	_, err := client.dnsRequest("ModifyRecordStatus", params)
   186  	if err != nil {
   187  		return errors.Wrapf(err, "ModifyRecordStatus")
   188  	}
   189  	return nil
   190  }
   191  
   192  // https://cloud.tencent.com/document/api/1427/56176
   193  func (client *SQcloudClient) DeleteDnsRecord(recordId string, domainName string) error {
   194  	params := map[string]string{}
   195  	params["Domain"] = domainName
   196  	params["RecordId"] = recordId
   197  	_, err := client.dnsRequest("DeleteRecord", params)
   198  	if err != nil {
   199  		return errors.Wrapf(err, "DeleteRecord")
   200  	}
   201  	return nil
   202  }
   203  
   204  func (self *SDnsRecord) GetGlobalId() string {
   205  	return fmt.Sprintf("%d", self.RecordId)
   206  }
   207  
   208  func (self *SDnsRecord) GetDnsName() string {
   209  	return self.Name
   210  }
   211  
   212  func (self *SDnsRecord) GetStatus() string {
   213  	if self.Status != "SPAM" {
   214  		return api.DNS_RECORDSET_STATUS_AVAILABLE
   215  	}
   216  	return api.DNS_ZONE_STATUS_UNKNOWN
   217  }
   218  
   219  func (self *SDnsRecord) GetEnabled() bool {
   220  	return self.Status == "ENABLE"
   221  }
   222  
   223  func (self *SDnsRecord) GetDnsType() cloudprovider.TDnsType {
   224  	return cloudprovider.TDnsType(self.Type)
   225  }
   226  
   227  func (self *SDnsRecord) GetDnsValue() string {
   228  	if self.GetDnsType() == cloudprovider.DnsTypeMX || self.GetDnsType() == cloudprovider.DnsTypeCNAME || self.GetDnsType() == cloudprovider.DnsTypeSRV {
   229  		return self.Value[:len(self.Value)-1]
   230  	}
   231  	return self.Value
   232  }
   233  
   234  func (self *SDnsRecord) GetTTL() int64 {
   235  	return int64(self.TTL)
   236  }
   237  
   238  func (self *SDnsRecord) GetMxPriority() int64 {
   239  	if self.GetDnsType() == cloudprovider.DnsTypeMX {
   240  		return self.MX
   241  	}
   242  	return 0
   243  }
   244  
   245  func (self *SDnsRecord) GetPolicyType() cloudprovider.TDnsPolicyType {
   246  	switch self.Line {
   247  	case "境内", "境外":
   248  		return cloudprovider.DnsPolicyTypeByGeoLocation
   249  	case "电信", "联通", "移动", "教育网":
   250  		return cloudprovider.DnsPolicyTypeByCarrier
   251  	case "百度", "谷歌", "有道", "必应", "搜搜", "搜狗", "奇虎":
   252  		return cloudprovider.DnsPolicyTypeBySearchEngine
   253  	default:
   254  		return cloudprovider.DnsPolicyTypeSimple
   255  	}
   256  }
   257  
   258  func (self *SDnsRecord) GetPolicyOptions() *jsonutils.JSONDict {
   259  	return nil
   260  }
   261  
   262  func (self *SDnsRecord) GetPolicyValue() cloudprovider.TDnsPolicyValue {
   263  	switch self.Line {
   264  	case "境内":
   265  		return cloudprovider.DnsPolicyValueMainland
   266  	case "境外":
   267  		return cloudprovider.DnsPolicyValueOversea
   268  
   269  	case "电信":
   270  		return cloudprovider.DnsPolicyValueTelecom
   271  	case "联通":
   272  		return cloudprovider.DnsPolicyValueUnicom
   273  	case "移动":
   274  		return cloudprovider.DnsPolicyValueChinaMobile
   275  	case "教育网":
   276  		return cloudprovider.DnsPolicyValueCernet
   277  
   278  	case "百度":
   279  		return cloudprovider.DnsPolicyValueBaidu
   280  	case "谷歌":
   281  		return cloudprovider.DnsPolicyValueGoogle
   282  	case "有道":
   283  		return cloudprovider.DnsPolicyValueYoudao
   284  	case "必应":
   285  		return cloudprovider.DnsPolicyValueBing
   286  	case "搜搜":
   287  		return cloudprovider.DnsPolicyValueSousou
   288  	case "搜狗":
   289  		return cloudprovider.DnsPolicyValueSougou
   290  	case "奇虎":
   291  		return cloudprovider.DnsPolicyValueQihu360
   292  	default:
   293  		return cloudprovider.DnsPolicyValueEmpty
   294  	}
   295  }