yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/private_zone_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 SPvtzRecords struct {
    28  	PageNumber int          `json:"PageNumber"`
    29  	Records    sPvtzRecords `json:"Records"`
    30  	PageSize   int          `json:"PageSize"`
    31  	// RequestID  string       `json:"RequestId"`
    32  	TotalItems int `json:"TotalItems"`
    33  	TotalPages int `json:"TotalPages"`
    34  }
    35  
    36  type SPvtzRecord struct {
    37  	szone    *SPrivateZone
    38  	Status   string `json:"Status"`
    39  	Value    string `json:"Value"`
    40  	Rr       string `json:"Rr"`
    41  	RecordID int64  `json:"RecordId"`
    42  	TTL      int64  `json:"Ttl"`
    43  	Type     string `json:"Type"`
    44  	Priority int64  `json:"Priority"`
    45  }
    46  
    47  type sPvtzRecords struct {
    48  	Record []SPvtzRecord `json:"Record"`
    49  }
    50  
    51  // https://help.aliyun.com/document_detail/66252.html?spm=a2c4g.11186623.6.595.3f3f243fU1AjOV
    52  func (client *SAliyunClient) DescribeZoneRecords(ZoneId string, pageNumber int, pageSize int) (SPvtzRecords, error) {
    53  	sRecords := SPvtzRecords{}
    54  	params := map[string]string{}
    55  	params["Action"] = "DescribeZoneRecords"
    56  	params["ZoneId"] = ZoneId
    57  	params["PageNumber"] = strconv.Itoa(pageNumber)
    58  	params["PageSize"] = strconv.Itoa(pageSize)
    59  	resp, err := client.pvtzRequest("DescribeZoneRecords", params)
    60  	if err != nil {
    61  		return sRecords, errors.Wrap(err, "DescribeZoneRecords")
    62  	}
    63  	err = resp.Unmarshal(&sRecords)
    64  	if err != nil {
    65  		return sRecords, errors.Wrap(err, "resp.Unmarshal")
    66  	}
    67  	return sRecords, nil
    68  }
    69  
    70  func (client *SAliyunClient) GetAllZoneRecords(ZoneId string) ([]SPvtzRecord, error) {
    71  	count := 0
    72  	pageNumber := 0
    73  	srecords := []SPvtzRecord{}
    74  
    75  	for {
    76  		pageNumber++
    77  		records, err := client.DescribeZoneRecords(ZoneId, pageNumber, 20)
    78  		if err != nil {
    79  			return nil, errors.Wrapf(err, "client.DescribeZones(%d, 20)", count)
    80  		}
    81  		count += len(records.Records.Record)
    82  
    83  		srecords = append(srecords, records.Records.Record...)
    84  		if count >= records.TotalItems {
    85  			break
    86  		}
    87  	}
    88  	return srecords, nil
    89  }
    90  
    91  func (client *SAliyunClient) AddZoneRecord(ZoneId string, opts cloudprovider.DnsRecordSet) (string, error) {
    92  	params := map[string]string{}
    93  	params["Action"] = "AddZoneRecord"
    94  	params["Rr"] = opts.DnsName
    95  	params["Type"] = string(opts.DnsType)
    96  	params["Value"] = opts.DnsValue
    97  	params["ZoneId"] = ZoneId
    98  	params["Ttl"] = strconv.FormatInt(opts.Ttl, 10)
    99  	if opts.DnsType == cloudprovider.DnsTypeMX {
   100  		params["Priority"] = strconv.FormatInt(opts.MxPriority, 10)
   101  	}
   102  	ret, err := client.pvtzRequest("AddZoneRecord", params)
   103  	if err != nil {
   104  		return "", errors.Wrap(err, "AddZoneRecord")
   105  	}
   106  	recordId := ""
   107  	return recordId, ret.Unmarshal(&recordId, "RecordId")
   108  }
   109  
   110  // https://help.aliyun.com/document_detail/66251.html?spm=a2c4g.11186623.6.596.15d55563zpsSWE
   111  // status ENABLE: 启用解析 DISABLE: 暂停解析
   112  func (client *SAliyunClient) SetZoneRecordStatus(recordId string, status string) error {
   113  	params := map[string]string{}
   114  	params["Action"] = "SetZoneRecordStatus"
   115  	params["RecordId"] = recordId
   116  	params["Status"] = status
   117  	_, err := client.pvtzRequest("SetZoneRecordStatus", params)
   118  	if err != nil {
   119  		return errors.Wrap(err, "SetZoneRecordStatus")
   120  	}
   121  	return nil
   122  }
   123  
   124  func (client *SAliyunClient) UpdateZoneRecord(opts cloudprovider.DnsRecordSet) error {
   125  	params := map[string]string{}
   126  	params["Action"] = "UpdateZoneRecord"
   127  	params["RecordId"] = opts.ExternalId
   128  	params["Rr"] = opts.DnsName
   129  	params["Type"] = string(opts.DnsType)
   130  	params["Value"] = opts.DnsValue
   131  	params["Ttl"] = strconv.FormatInt(opts.Ttl, 10)
   132  	if opts.DnsType == cloudprovider.DnsTypeMX {
   133  		params["Priority"] = strconv.FormatInt(opts.MxPriority, 10)
   134  	}
   135  	_, err := client.pvtzRequest("UpdateZoneRecord", params)
   136  	if err != nil {
   137  		return errors.Wrap(err, "UpdateZoneRecord")
   138  	}
   139  	return nil
   140  }
   141  
   142  func (client *SAliyunClient) DeleteZoneRecord(RecordId string) error {
   143  	params := map[string]string{}
   144  	params["Action"] = "DeleteZoneRecord"
   145  	params["RecordId"] = RecordId
   146  	_, err := client.pvtzRequest("DeleteZoneRecord", params)
   147  	if err != nil {
   148  		return errors.Wrap(err, "DeleteZoneRecord")
   149  	}
   150  	return nil
   151  }
   152  
   153  func (self *SPvtzRecord) GetGlobalId() string {
   154  	return strconv.FormatInt(self.RecordID, 10)
   155  }
   156  
   157  func (self *SPvtzRecord) GetDnsName() string {
   158  	return self.Rr
   159  }
   160  
   161  func (self *SPvtzRecord) GetStatus() string {
   162  	return api.DNS_RECORDSET_STATUS_AVAILABLE
   163  }
   164  
   165  func (self *SPvtzRecord) GetEnabled() bool {
   166  	return self.Status == "ENABLE"
   167  }
   168  
   169  func (self *SPvtzRecord) GetDnsType() cloudprovider.TDnsType {
   170  	return cloudprovider.TDnsType(self.Type)
   171  }
   172  
   173  func (self *SPvtzRecord) GetDnsValue() string {
   174  	return self.Value
   175  }
   176  
   177  func (self *SPvtzRecord) GetTTL() int64 {
   178  	return self.TTL
   179  }
   180  
   181  func (self *SPvtzRecord) GetMxPriority() int64 {
   182  	if self.GetDnsType() == cloudprovider.DnsTypeMX {
   183  		return self.Priority
   184  	}
   185  	return 0
   186  }
   187  
   188  func (self *SPvtzRecord) GetPolicyType() cloudprovider.TDnsPolicyType {
   189  	return cloudprovider.DnsPolicyTypeSimple
   190  }
   191  
   192  func (self *SPvtzRecord) GetPolicyValue() cloudprovider.TDnsPolicyValue {
   193  	return ""
   194  }
   195  
   196  func (self *SPvtzRecord) GetPolicyOptions() *jsonutils.JSONDict {
   197  	return nil
   198  }