cdn.go 8.3 KB

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