yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/cdn.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  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SCdnDomainNames struct {
    30  	DomainNames []string `json:"domainNames"`
    31  }
    32  
    33  type SDomainInfo struct {
    34  	DomainCname string    `json:"DomainCname"`
    35  	Status      string    `json:"Status"`
    36  	CreateTime  time.Time `json:"CreateTime"`
    37  	UpdateTime  time.Time `json:"UpdateTime"`
    38  	DomainName  string    `json:"DomainName"`
    39  }
    40  
    41  type SCdnDomainInfos struct {
    42  	DomainInfo []SDomainInfo `json:"domainInfo"`
    43  }
    44  
    45  type SCdnDomainsData struct {
    46  	Source      string          `json:"Source"`
    47  	Domains     SCdnDomainNames `json:"Domains"`
    48  	DomainInfos SCdnDomainInfos `json:"DomainInfos"`
    49  }
    50  
    51  type SCdnDomainsList struct {
    52  	DomainsData []SCdnDomainsData `json:"DomainsData"`
    53  }
    54  
    55  type SCdnSource struct {
    56  	Port     int    `json:"Port"`
    57  	Weight   string `json:"Weight"`
    58  	Type     string `json:"Type"`
    59  	Content  string `json:"Content"`
    60  	Priority string `json:"Priority"`
    61  }
    62  
    63  type SCdnSources struct {
    64  	Source []SCdnSource `json:"Source"`
    65  }
    66  
    67  type SCdnDomain struct {
    68  	multicloud.SResourceBase
    69  
    70  	client *SAliyunClient
    71  
    72  	Cname           string     `json:"Cname"`
    73  	Description     string     `json:"Description"`
    74  	CdnType         string     `json:"CdnType"`
    75  	ResourceGroupID string     `json:"ResourceGroupId"`
    76  	DomainStatus    string     `json:"DomainStatus"`
    77  	SslProtocol     string     `json:"SslProtocol"`
    78  	DomainName      string     `json:"DomainName"`
    79  	Coverage        string     `json:"Coverage"`
    80  	Sources         SCdnSource `json:"Sources"`
    81  	GmtModified     string     `json:"GmtModified"`
    82  	Sandbox         string     `json:"Sandbox"`
    83  	GmtCreated      time.Time  `json:"GmtCreated"`
    84  	SourceModels    struct {
    85  		SourceModel []struct {
    86  			Content  string
    87  			Type     string
    88  			Port     int
    89  			Enabled  string
    90  			Priority int
    91  			Weight   string
    92  		}
    93  	}
    94  }
    95  
    96  func (self *SCdnDomain) GetArea() string {
    97  	switch self.Coverage {
    98  	case "domestic":
    99  		return api.CDN_DOMAIN_AREA_MAINLAND
   100  	case "global":
   101  		return api.CDN_DOMAIN_AREA_GLOBAL
   102  	case "overseas":
   103  		return api.CDN_DOMAIN_AREA_OVERSEAS
   104  	default:
   105  		return self.Coverage
   106  	}
   107  }
   108  
   109  func (self *SCdnDomain) GetCname() string {
   110  	return self.Cname
   111  }
   112  
   113  func (self *SCdnDomain) GetEnabled() bool {
   114  	return self.DomainStatus != "offline"
   115  }
   116  
   117  func (self *SCdnDomain) GetId() string {
   118  	return self.DomainName
   119  }
   120  
   121  func (self *SCdnDomain) GetGlobalId() string {
   122  	return self.DomainName
   123  }
   124  
   125  func (self *SCdnDomain) GetName() string {
   126  	return self.DomainName
   127  }
   128  
   129  func (self *SCdnDomain) Refresh() error {
   130  	domain, err := self.client.GetCdnDomain(self.DomainName)
   131  	if err != nil {
   132  		return errors.Wrapf(err, "GetCdnDomain")
   133  	}
   134  	self.DomainStatus = domain.DomainStatus
   135  	return nil
   136  }
   137  
   138  func (self *SCdnDomain) GetTags() (map[string]string, error) {
   139  	_, tags, err := self.client.GetRegion("").ListSysAndUserTags(ALIYUN_SERVICE_CDN, "DOMAIN", self.DomainName)
   140  	if err != nil {
   141  		return nil, errors.Wrapf(err, "ListTags")
   142  	}
   143  	tagMaps := map[string]string{}
   144  	for k, v := range tags {
   145  		tagMaps[strings.ToLower(k)] = v
   146  	}
   147  	return tagMaps, nil
   148  }
   149  
   150  func (self *SCdnDomain) GetSysTags() map[string]string {
   151  	tags, _, err := self.client.GetRegion("").ListSysAndUserTags(ALIYUN_SERVICE_CDN, "DOMAIN", self.DomainName)
   152  	if err != nil {
   153  		return nil
   154  	}
   155  	tagMaps := map[string]string{}
   156  	for k, v := range tags {
   157  		tagMaps[strings.ToLower(k)] = v
   158  	}
   159  	return tagMaps
   160  }
   161  
   162  func (self *SCdnDomain) SetTags(tags map[string]string, replace bool) error {
   163  	return self.client.GetRegion("").SetResourceTags(ALIYUN_SERVICE_CDN, "DOMAIN", self.DomainName, tags, replace)
   164  }
   165  
   166  func (self *SCdnDomain) GetOrigins() *cloudprovider.SCdnOrigins {
   167  	domain, err := self.client.GetCdnDomain(self.DomainName)
   168  	if err != nil {
   169  		return nil
   170  	}
   171  	ret := cloudprovider.SCdnOrigins{}
   172  	for _, origin := range domain.SourceModels.SourceModel {
   173  		ret = append(ret, cloudprovider.SCdnOrigin{
   174  			Type:     origin.Type,
   175  			Origin:   origin.Content,
   176  			Port:     origin.Port,
   177  			Enabled:  origin.Enabled,
   178  			Priority: origin.Priority,
   179  		})
   180  	}
   181  	return &ret
   182  }
   183  
   184  func (self *SCdnDomain) GetServiceType() string {
   185  	return self.CdnType
   186  }
   187  
   188  func (self *SCdnDomain) GetStatus() string {
   189  	switch self.DomainStatus {
   190  	case "online", "offline":
   191  		return self.DomainStatus
   192  	case "configuring", "checking":
   193  		return api.CDN_DOMAIN_STATUS_PROCESSING
   194  	case "configure_failed":
   195  		return self.DomainStatus
   196  	case "check_failed":
   197  		return api.CDN_DOMAIN_STATUS_REJECTED
   198  	}
   199  	return self.DomainStatus
   200  }
   201  
   202  func (self *SCdnDomain) Delete() error {
   203  	params := map[string]string{
   204  		"DomainName": self.DomainName,
   205  	}
   206  	_, err := self.client.cdnRequest("DeleteCdnDomain", params)
   207  	return errors.Wrapf(err, "DeleteCdnDomain")
   208  }
   209  
   210  func (self *SAliyunClient) GetICloudCDNDomains() ([]cloudprovider.ICloudCDNDomain, error) {
   211  	domains, err := self.GetCdnDomains()
   212  	if err != nil {
   213  		return nil, errors.Wrapf(err, "GetCdnDomains")
   214  	}
   215  	ret := []cloudprovider.ICloudCDNDomain{}
   216  	for i := range domains {
   217  		domains[i].client = self
   218  		ret = append(ret, &domains[i])
   219  	}
   220  	return ret, nil
   221  }
   222  
   223  func (self *SAliyunClient) GetICloudCDNDomainByName(name string) (cloudprovider.ICloudCDNDomain, error) {
   224  	return self.GetCDNDomainByName(name)
   225  }
   226  
   227  func (self *SAliyunClient) GetCDNDomainByName(name string) (*SCdnDomain, error) {
   228  	domains, total, err := self.DescribeUserDomains(name, 1, 1)
   229  	if err != nil {
   230  		return nil, errors.Wrapf(err, "GetCdnDomain")
   231  	}
   232  	if total == 1 {
   233  		domains[0].client = self
   234  		return &domains[0], nil
   235  	}
   236  	if total == 0 {
   237  		return nil, errors.Wrapf(cloudprovider.ErrNotFound, name)
   238  	}
   239  	return nil, errors.Wrapf(cloudprovider.ErrDuplicateId, name)
   240  }
   241  
   242  func (client *SAliyunClient) DescribeDomainsBySource(origin string) (SCdnDomainsList, error) {
   243  	sproducts := SCdnDomainsList{}
   244  	params := map[string]string{}
   245  	params["Action"] = "DescribeDomainsBySource"
   246  	params["Sources"] = origin
   247  	resp, err := client.cdnRequest("DescribeDomainsBySource", params)
   248  	if err != nil {
   249  		return sproducts, errors.Wrap(err, "DescribeDomainsBySource")
   250  	}
   251  	err = resp.Unmarshal(&sproducts, "DomainsList")
   252  	if err != nil {
   253  		return sproducts, errors.Wrap(err, "resp.Unmarshal")
   254  	}
   255  	return sproducts, nil
   256  }
   257  
   258  func (self *SAliyunClient) GetCdnDomains() ([]SCdnDomain, error) {
   259  	domains := []SCdnDomain{}
   260  	for {
   261  		part, total, err := self.DescribeUserDomains("", 50, len(domains)/50+1)
   262  		if err != nil {
   263  			return nil, errors.Wrapf(err, "DescribeUserDomains")
   264  		}
   265  		domains = append(domains, part...)
   266  		if len(domains) >= total || len(part) == 0 {
   267  			break
   268  		}
   269  	}
   270  	return domains, nil
   271  }
   272  
   273  func (client *SAliyunClient) DescribeUserDomains(domain string, pageSize, pageNumber int) ([]SCdnDomain, int, error) {
   274  	if pageSize < 1 || pageSize > 50 {
   275  		pageSize = 50
   276  	}
   277  	if pageNumber < 1 {
   278  		pageNumber = 1
   279  	}
   280  	params := map[string]string{
   281  		"PageSize":   fmt.Sprintf("%d", pageSize),
   282  		"PageNumber": fmt.Sprintf("%d", pageNumber),
   283  	}
   284  	if len(domain) > 0 {
   285  		params["DomainName"] = domain
   286  		params["DomainSearchType"] = "full_match"
   287  	}
   288  	resp, err := client.cdnRequest("DescribeUserDomains", params)
   289  	if err != nil {
   290  		return nil, 0, errors.Wrap(err, "DescribeUserDomains")
   291  	}
   292  	domains := []SCdnDomain{}
   293  	err = resp.Unmarshal(&domains, "Domains", "PageData")
   294  	if err != nil {
   295  		return nil, 0, errors.Wrap(err, "resp.Unmarshal")
   296  	}
   297  	totalCount, _ := resp.Int("TotalCount")
   298  	return domains, int(totalCount), nil
   299  }
   300  
   301  func (self *SAliyunClient) GetCdnDomain(domainName string) (*SCdnDomain, error) {
   302  	params := map[string]string{
   303  		"DomainName": domainName,
   304  	}
   305  	resp, err := self.cdnRequest("DescribeCdnDomainDetail", params)
   306  	if err != nil {
   307  		return nil, errors.Wrapf(err, "DescribeCdnDomainDetail")
   308  	}
   309  	domain := &SCdnDomain{client: self}
   310  	err = resp.Unmarshal(domain, "GetDomainDetailModel")
   311  	if err != nil {
   312  		return nil, errors.Wrapf(err, "resp.Unmarshal")
   313  	}
   314  	return domain, nil
   315  }