yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/dnspod_domain.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  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type SDomian struct {
    31  	multicloud.SResourceBase
    32  	QcloudTags
    33  	client *SQcloudClient
    34  
    35  	CNAMESpeedup     string   `json:"CNAMESpeedup"`
    36  	CreatedOn        string   `json:"CreatedOn"`
    37  	DNSStatus        string   `json:"DNSStatus"`
    38  	DomainId         int      `json:"DomainId"`
    39  	EffectiveDNS     []string `json:"EffectiveDNS"`
    40  	Grade            string   `json:"Grade"`
    41  	GradeLevel       int64    `json:"GradeLevel"`
    42  	GradeTitle       string   `json:"GradeTitle"`
    43  	GroupId          int64    `json:"GroupId"`
    44  	IsVip            string   `json:"IsVip"`
    45  	Name             string   `json:"Name"`
    46  	Owner            string   `json:"Owner"`
    47  	Punycode         string   `json:"Punycode"`
    48  	RecordCount      int64    `json:"RecordCount"`
    49  	Remark           string   `json:"Remark"`
    50  	SearchEnginePush string   `json:"SearchEnginePush"`
    51  	Status           string   `json:"Status"`
    52  	TTL              int64    `json:"TTL"`
    53  	UpdatedOn        string   `json:"UpdatedOn"`
    54  	VipAutoRenew     string   `json:"VipAutoRenew"`
    55  	VipEndAt         string   `json:"VipEndAt"`
    56  	VipStartAt       string   `json:"VipStartAt"`
    57  }
    58  
    59  func (self *SQcloudClient) GetDomains(key string, offset int, limit int) ([]SDomian, int, error) {
    60  	params := map[string]string{}
    61  	params["Offset"] = strconv.Itoa(offset)
    62  	if limit > 0 {
    63  		params["Limit"] = strconv.Itoa(limit)
    64  	}
    65  	if len(key) > 0 {
    66  		params["Keyword"] = key
    67  	}
    68  	resp, err := self.dnsRequest("DescribeDomainList", params)
    69  	if err != nil {
    70  		return nil, 0, errors.Wrapf(err, "DescribeDomainList")
    71  	}
    72  	domains := []SDomian{}
    73  	err = resp.Unmarshal(&domains, "DomainList")
    74  	if err != nil {
    75  		return nil, 0, errors.Wrapf(err, "resp.Unmarshal DomainList")
    76  	}
    77  	total, err := resp.Float("DomainCountInfo", "DomainTotal")
    78  	return domains, int(total), err
    79  }
    80  
    81  func (self *SQcloudClient) GetDomain(domain string) (*SDomian, error) {
    82  	domains, _, err := self.GetDomains(domain, 0, 2)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	for i := range domains {
    87  		if domains[i].Name == domain {
    88  			domains[i].client = self
    89  			return &domains[i], nil
    90  		}
    91  	}
    92  	return nil, errors.Wrapf(cloudprovider.ErrNotFound, domain)
    93  }
    94  
    95  func (self *SQcloudClient) GetICloudDnsZones() ([]cloudprovider.ICloudDnsZone, error) {
    96  	result := []cloudprovider.ICloudDnsZone{}
    97  	domains := []SDomian{}
    98  	for {
    99  		part, total, err := self.GetDomains("", len(domains), 1000)
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  		domains = append(domains, part...)
   104  		if len(domains) >= total {
   105  			break
   106  		}
   107  	}
   108  	for i := 0; i < len(domains); i++ {
   109  		domains[i].client = self
   110  		result = append(result, &domains[i])
   111  	}
   112  	return result, nil
   113  }
   114  
   115  func (self *SQcloudClient) CreateDomian(domianName string) (*SDomian, error) {
   116  	params := map[string]string{}
   117  	params["Domain"] = domianName
   118  	_, err := self.dnsRequest("CreateDomain", params)
   119  	if err != nil {
   120  		return nil, errors.Wrapf(err, "CreateDomain")
   121  	}
   122  	return self.GetDomain(domianName)
   123  }
   124  
   125  func (self *SQcloudClient) CreateICloudDnsZone(opts *cloudprovider.SDnsZoneCreateOptions) (cloudprovider.ICloudDnsZone, error) {
   126  	domain, err := self.CreateDomian(opts.Name)
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	return domain, nil
   131  }
   132  
   133  func (client *SQcloudClient) DeleteDomian(domianName string) error {
   134  	params := map[string]string{}
   135  	params["Domain"] = domianName
   136  	_, err := client.dnsRequest("DeleteDomain", params)
   137  	if err != nil {
   138  		return errors.Wrapf(err, "DeleteDomain")
   139  	}
   140  	return nil
   141  }
   142  
   143  func (self *SDomian) GetId() string {
   144  	return fmt.Sprintf("%d", self.DomainId)
   145  }
   146  
   147  func (self *SDomian) GetName() string {
   148  	if len(self.Punycode) > 0 {
   149  		return self.Punycode
   150  	}
   151  	return self.Name
   152  }
   153  
   154  func (self *SDomian) GetGlobalId() string {
   155  	return self.Name
   156  }
   157  
   158  func (self *SDomian) GetStatus() string {
   159  	switch self.Status {
   160  	case "ENABLE":
   161  		return api.DNS_ZONE_STATUS_AVAILABLE
   162  	case "PAUSE":
   163  		return api.DNS_ZONE_STATUS_AVAILABLE
   164  	default:
   165  		return api.DNS_ZONE_STATUS_UNKNOWN
   166  	}
   167  }
   168  
   169  func (self *SDomian) GetEnabled() bool {
   170  	if self.Status == "ENABLE" {
   171  		return true
   172  	}
   173  	return false
   174  }
   175  
   176  func (self *SDomian) Refresh() error {
   177  	domain, err := self.client.GetDomain(self.Name)
   178  	if err != nil {
   179  		return err
   180  	}
   181  	return jsonutils.Update(self, domain)
   182  }
   183  
   184  func (self *SDomian) Delete() error {
   185  	return self.client.DeleteDomian(self.Name)
   186  }
   187  
   188  func (self *SDomian) GetZoneType() cloudprovider.TDnsZoneType {
   189  	return cloudprovider.PublicZone
   190  }
   191  
   192  func (self *SDomian) GetOptions() *jsonutils.JSONDict {
   193  	return nil
   194  }
   195  
   196  func (self *SDomian) GetICloudVpcIds() ([]string, error) {
   197  	return nil, nil
   198  }
   199  
   200  func (self *SDomian) AddVpc(vpc *cloudprovider.SPrivateZoneVpc) error {
   201  	return cloudprovider.ErrNotSupported
   202  }
   203  
   204  func (self *SDomian) RemoveVpc(vpc *cloudprovider.SPrivateZoneVpc) error {
   205  	return cloudprovider.ErrNotSupported
   206  }
   207  
   208  func (self *SDomian) GetIDnsRecordSets() ([]cloudprovider.ICloudDnsRecordSet, error) {
   209  	records := []SDnsRecord{}
   210  	for {
   211  		part, total, err := self.client.GetDnsRecords(self.Name, len(records), 1000)
   212  		if err != nil {
   213  			return nil, err
   214  		}
   215  		records = append(records, part...)
   216  		if len(records) >= total {
   217  			break
   218  		}
   219  	}
   220  	result := []cloudprovider.ICloudDnsRecordSet{}
   221  	for i := 0; i < len(records); i++ {
   222  		records[i].domain = self
   223  		result = append(result, &records[i])
   224  	}
   225  	return result, nil
   226  }
   227  
   228  func (self *SDomian) AddDnsRecordSet(opts *cloudprovider.DnsRecordSet) error {
   229  	values := strings.Split(opts.DnsValue, "*")
   230  	for i := 0; i < len(values); i++ {
   231  		opts.DnsValue = values[i]
   232  		recordId, err := self.client.CreateDnsRecord(opts, self.Name)
   233  		if err != nil {
   234  			return errors.Wrapf(err, "CreateDnsRecord")
   235  		}
   236  		opts.ExternalId = recordId
   237  	}
   238  	return nil
   239  }
   240  
   241  func (self *SDomian) UpdateDnsRecordSet(opts *cloudprovider.DnsRecordSet) error {
   242  	values := strings.Split(opts.DnsValue, "*")
   243  	for i := 0; i < len(values); i++ {
   244  		opts.DnsValue = values[i]
   245  		err := self.client.ModifyDnsRecord(opts, self.Name)
   246  		if err != nil {
   247  			return errors.Wrapf(err, "ModifyDnsRecord")
   248  		}
   249  	}
   250  	return nil
   251  }
   252  
   253  func (self *SDomian) RemoveDnsRecordSet(opts *cloudprovider.DnsRecordSet) error {
   254  	return self.client.DeleteDnsRecord(opts.ExternalId, self.GetName())
   255  }
   256  
   257  func (self *SDomian) SyncDnsRecordSets(common, add, del, update []cloudprovider.DnsRecordSet) error {
   258  	for i := 0; i < len(del); i++ {
   259  		err := self.RemoveDnsRecordSet(&del[i])
   260  		if err != nil {
   261  			return errors.Wrapf(err, "RemoveDnsRecordSet(%s)", del[i].ExternalId)
   262  		}
   263  	}
   264  
   265  	for i := 0; i < len(add); i++ {
   266  		err := self.AddDnsRecordSet(&add[i])
   267  		if err != nil {
   268  			return errors.Wrapf(err, "AddDnsRecordSet(%s)", add[i].Id)
   269  		}
   270  	}
   271  
   272  	for i := 0; i < len(update); i++ {
   273  		err := self.UpdateDnsRecordSet(&update[i])
   274  		if err != nil {
   275  			return errors.Wrapf(err, "UpdateDnsRecordSet(%s)", update[i].ExternalId)
   276  		}
   277  	}
   278  	return nil
   279  }
   280  
   281  func (self *SDomian) GetDnsProductType() cloudprovider.TDnsProductType {
   282  	switch self.GradeTitle {
   283  	case "企业旗舰版":
   284  		return cloudprovider.DnsProductEnterpriseUltimate
   285  	case "企业标准版":
   286  		return cloudprovider.DnsProductEnterpriseStandard
   287  	case "企业基础版":
   288  		return cloudprovider.DnsProductEnterpriseBasic
   289  	case "个人专业版":
   290  		return cloudprovider.DnsProductPersonalProfessional
   291  	case "免费版":
   292  		return cloudprovider.DnsProductFree
   293  	default:
   294  		return cloudprovider.DnsProductFree
   295  	}
   296  }