yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/dns_domain_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 aliyun
    16  
    17  import (
    18  	"strconv"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SDomainRecords struct {
    28  	//RequestID     string         `json:"RequestId"`
    29  	TotalCount    int            `json:"TotalCount"`
    30  	PageNumber    int            `json:"PageNumber"`
    31  	PageSize      int            `json:"PageSize"`
    32  	DomainRecords sDomainRecords `json:"DomainRecords"`
    33  }
    34  
    35  // https://help.aliyun.com/document_detail/29777.html?spm=a2c4g.11186623.6.666.aa4832307YdopF
    36  type SDomainRecord struct {
    37  	DomainId   string `json:"DomainId"`
    38  	GroupId    string `json:"GroupId"`
    39  	GroupName  string `json:"GroupName"`
    40  	PunyCode   string `json:"PunyCode"`
    41  	RR         string `json:"RR"`
    42  	Status     string `json:"Status"`
    43  	Value      string `json:"Value"`
    44  	RecordID   string `json:"RecordId"`
    45  	Type       string `json:"Type"`
    46  	RequestID  string `json:"RequestId"`
    47  	DomainName string `json:"DomainName"`
    48  	Locked     bool   `json:"Locked"`
    49  	Line       string `json:"Line"`
    50  	TTL        int64  `json:"TTL"`
    51  	Priority   int64  `json:"Priority"`
    52  	Remark     string
    53  }
    54  
    55  type sDomainRecords struct {
    56  	Record []SDomainRecord `json:"Record"`
    57  }
    58  
    59  func (client *SAliyunClient) DescribeDomainRecords(domainName string, pageNumber int, pageSize int) (SDomainRecords, error) {
    60  	srecords := SDomainRecords{}
    61  	params := map[string]string{}
    62  	params["Action"] = "DescribeDomainRecords"
    63  	params["DomainName"] = domainName
    64  	params["PageNumber"] = strconv.Itoa(pageNumber)
    65  	params["PageSize"] = strconv.Itoa(pageSize)
    66  	resp, err := client.alidnsRequest("DescribeDomainRecords", params)
    67  	if err != nil {
    68  		return srecords, errors.Wrap(err, "DescribeDomainRecords")
    69  	}
    70  	err = resp.Unmarshal(&srecords)
    71  	if err != nil {
    72  		return srecords, errors.Wrap(err, "resp.Unmarshal")
    73  	}
    74  	return srecords, nil
    75  }
    76  
    77  func (client *SAliyunClient) GetAllDomainRecords(domainName string) ([]SDomainRecord, error) {
    78  	srecords := []SDomainRecord{}
    79  	pageNumber := 0
    80  	for {
    81  		pageNumber++
    82  		records, err := client.DescribeDomainRecords(domainName, pageNumber, 2)
    83  		if err != nil {
    84  			return nil, errors.Wrapf(err, "client.DescribeDomainRecords(%d, 20)", len(srecords))
    85  		}
    86  		srecords = append(srecords, records.DomainRecords.Record...)
    87  		if len(srecords) >= records.TotalCount {
    88  			break
    89  		}
    90  	}
    91  	return srecords, nil
    92  }
    93  
    94  func (client *SAliyunClient) DescribeDomainRecordInfo(recordId string) (*SDomainRecord, error) {
    95  	srecord := SDomainRecord{}
    96  	params := map[string]string{}
    97  	params["Action"] = "DescribeDomainRecordInfo"
    98  	params["RecordId"] = recordId
    99  
   100  	resp, err := client.alidnsRequest("DescribeDomainRecordInfo", params)
   101  	if err != nil {
   102  		return nil, errors.Wrap(err, "DescribeDomainRecordInfo")
   103  	}
   104  	err = resp.Unmarshal(&srecord)
   105  	if err != nil {
   106  		return nil, errors.Wrap(err, "resp.Unmarshal")
   107  	}
   108  	return &srecord, nil
   109  }
   110  
   111  func GetRecordLineLineType(policyinfo cloudprovider.TDnsPolicyValue) string {
   112  	switch policyinfo {
   113  	case cloudprovider.DnsPolicyValueOversea:
   114  		return "oversea"
   115  	case cloudprovider.DnsPolicyValueTelecom:
   116  		return "telecom"
   117  	case cloudprovider.DnsPolicyValueUnicom:
   118  		return "unicom"
   119  	case cloudprovider.DnsPolicyValueChinaMobile:
   120  		return "mobile"
   121  	case cloudprovider.DnsPolicyValueCernet:
   122  		return "edu"
   123  	case cloudprovider.DnsPolicyValueDrPeng:
   124  		return "drpeng"
   125  	case cloudprovider.DnsPolicyValueBtvn:
   126  		return "btvn"
   127  
   128  	case cloudprovider.DnsPolicyValueBaidu:
   129  		return "baidu"
   130  	case cloudprovider.DnsPolicyValueGoogle:
   131  		return "google"
   132  	case cloudprovider.DnsPolicyValueYoudao:
   133  		return "youdao"
   134  	case cloudprovider.DnsPolicyValueBing:
   135  		return "biying"
   136  	default:
   137  		return "default"
   138  	}
   139  }
   140  
   141  func (client *SAliyunClient) AddDomainRecord(domainName string, opts cloudprovider.DnsRecordSet) (string, error) {
   142  	line := GetRecordLineLineType(opts.PolicyValue)
   143  	params := map[string]string{}
   144  	params["Action"] = "AddDomainRecord"
   145  	params["RR"] = opts.DnsName
   146  	params["Type"] = string(opts.DnsType)
   147  	params["Value"] = opts.DnsValue
   148  	params["DomainName"] = domainName
   149  	params["TTL"] = strconv.FormatInt(opts.Ttl, 10)
   150  	params["Line"] = line
   151  	if opts.DnsType == cloudprovider.DnsTypeMX {
   152  		params["Priority"] = strconv.FormatInt(opts.MxPriority, 10)
   153  	}
   154  	ret, err := client.alidnsRequest("AddDomainRecord", params)
   155  	if err != nil {
   156  		return "", errors.Wrap(err, "AddDomainRecord")
   157  	}
   158  	recordId := ""
   159  	return recordId, ret.Unmarshal(&recordId, "RecordId")
   160  }
   161  
   162  // line
   163  func (client *SAliyunClient) UpdateDomainRecord(opts cloudprovider.DnsRecordSet) error {
   164  	line := GetRecordLineLineType(opts.PolicyValue)
   165  	params := map[string]string{}
   166  	params["Action"] = "UpdateDomainRecord"
   167  	params["RR"] = opts.DnsName
   168  	params["RecordId"] = opts.ExternalId
   169  	params["Type"] = string(opts.DnsType)
   170  	params["Value"] = opts.DnsValue
   171  	params["TTL"] = strconv.FormatInt(opts.Ttl, 10)
   172  	params["Line"] = line
   173  	if opts.DnsType == cloudprovider.DnsTypeMX {
   174  		params["Priority"] = strconv.FormatInt(opts.MxPriority, 10)
   175  	}
   176  	_, err := client.alidnsRequest("UpdateDomainRecord", params)
   177  	if err != nil {
   178  		return errors.Wrap(err, "UpdateDomainRecord")
   179  	}
   180  	return nil
   181  }
   182  
   183  func (client *SAliyunClient) UpdateDomainRecordRemark(recordId string, remark string) error {
   184  	params := map[string]string{}
   185  	params["RecordId"] = recordId
   186  	params["Remark"] = remark
   187  
   188  	_, err := client.alidnsRequest("UpdateDomainRecordRemark", params)
   189  	if err != nil {
   190  		return errors.Wrap(err, "UpdateDomainRecordRemark")
   191  	}
   192  	return nil
   193  }
   194  
   195  // Enable: 启用解析 Disable: 暂停解析
   196  func (client *SAliyunClient) SetDomainRecordStatus(recordId, status string) error {
   197  	params := map[string]string{}
   198  	params["Action"] = "SetDomainRecordStatus"
   199  	params["RecordId"] = recordId
   200  	params["Status"] = status
   201  	_, err := client.alidnsRequest("SetDomainRecordStatus", params)
   202  	if err != nil {
   203  		return errors.Wrap(err, "SetDomainRecordStatus")
   204  	}
   205  	return nil
   206  }
   207  
   208  func (client *SAliyunClient) DeleteDomainRecord(recordId string) error {
   209  	params := map[string]string{}
   210  	params["Action"] = "DeleteDomainRecord"
   211  	params["RecordId"] = recordId
   212  	_, err := client.alidnsRequest("DeleteDomainRecord", params)
   213  	if err != nil {
   214  		return errors.Wrap(err, "DeleteDomainRecord")
   215  	}
   216  	return nil
   217  }
   218  
   219  func (self *SDomainRecord) GetGlobalId() string {
   220  	return self.RecordID
   221  }
   222  
   223  func (self *SDomainRecord) GetDnsName() string {
   224  	return self.RR
   225  }
   226  
   227  func (self *SDomainRecord) GetStatus() string {
   228  	return api.DNS_RECORDSET_STATUS_AVAILABLE
   229  }
   230  
   231  func (self *SDomainRecord) GetEnabled() bool {
   232  	return self.Status == "ENABLE"
   233  }
   234  
   235  func (self *SDomainRecord) GetDnsType() cloudprovider.TDnsType {
   236  	return cloudprovider.TDnsType(self.Type)
   237  }
   238  
   239  func (self *SDomainRecord) GetDnsValue() string {
   240  	return self.Value
   241  }
   242  
   243  func (self *SDomainRecord) GetTTL() int64 {
   244  	return self.TTL
   245  }
   246  
   247  func (self *SDomainRecord) GetMxPriority() int64 {
   248  	if self.GetDnsType() == cloudprovider.DnsTypeMX {
   249  		return self.Priority
   250  	}
   251  	return 0
   252  }
   253  
   254  func (self *SDomainRecord) GetPolicyType() cloudprovider.TDnsPolicyType {
   255  	switch self.Line {
   256  	case "telecom", "unicom", "mobile", "edu", "drpeng", "btvn":
   257  		return cloudprovider.DnsPolicyTypeByCarrier
   258  	case "google", "baidu", "biying", "youdao":
   259  		return cloudprovider.DnsPolicyTypeBySearchEngine
   260  	case "oversea":
   261  		return cloudprovider.DnsPolicyTypeByGeoLocation
   262  	default:
   263  		return cloudprovider.DnsPolicyTypeSimple
   264  	}
   265  }
   266  
   267  func (self *SDomainRecord) GetPolicyValue() cloudprovider.TDnsPolicyValue {
   268  	switch self.Line {
   269  	case "telecom":
   270  		return cloudprovider.DnsPolicyValueTelecom
   271  	case "unicom":
   272  		return cloudprovider.DnsPolicyValueUnicom
   273  	case "mobile":
   274  		return cloudprovider.DnsPolicyValueChinaMobile
   275  	case "oversea":
   276  		return cloudprovider.DnsPolicyValueOversea
   277  	case "edu":
   278  		return cloudprovider.DnsPolicyValueCernet
   279  
   280  	case "drpeng":
   281  		return cloudprovider.DnsPolicyValueDrPeng
   282  	case "btvn":
   283  		return cloudprovider.DnsPolicyValueBtvn
   284  	case "google":
   285  		return cloudprovider.DnsPolicyValueGoogle
   286  	case "baidu":
   287  		return cloudprovider.DnsPolicyValueBaidu
   288  	case "biying":
   289  		return cloudprovider.DnsPolicyValueBing
   290  	case "youdao":
   291  		return cloudprovider.DnsPolicyValueYoudao
   292  
   293  	}
   294  	return ""
   295  }
   296  
   297  func (self *SDomainRecord) GetPolicyOptions() *jsonutils.JSONDict {
   298  	return nil
   299  }
   300  
   301  func (self *SDomainRecord) match(update cloudprovider.DnsRecordSet) bool {
   302  	if update.DnsName != self.GetDnsName() {
   303  		return false
   304  	}
   305  	if update.DnsType != self.GetDnsType() {
   306  		return false
   307  	}
   308  	if update.DnsValue != self.GetDnsValue() {
   309  		return false
   310  	}
   311  	if update.PolicyType != self.GetPolicyType() {
   312  		return false
   313  	}
   314  	if update.PolicyValue != self.GetPolicyValue() {
   315  		return false
   316  	}
   317  	if update.Ttl != self.GetTTL() {
   318  		return false
   319  	}
   320  	return true
   321  }