cdn.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 qcloud
  15. import (
  16. "fmt"
  17. "strconv"
  18. "yunion.io/x/jsonutils"
  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 SCdnOrigin struct {
  25. Origins []string `json:"Origins"`
  26. OriginType string `json:"OriginType"`
  27. ServerName string `json:"ServerName"`
  28. CosPrivateAccess string `json:"CosPrivateAccess"`
  29. OriginPullProtocol string `json:"OriginPullProtocol"`
  30. BackupOrigins []string `json:"BackupOrigins"`
  31. BackupOriginType string `json:"BackupOriginType"`
  32. BackupServerName string `json:"BackupServerName"`
  33. }
  34. type SCdnDomain struct {
  35. multicloud.SCDNDomainBase
  36. QcloudTags
  37. client *SQcloudClient
  38. Area string `json:"Area"`
  39. Cname string `json:"Cname"`
  40. CreateTime string `json:"CreateTime"`
  41. Disable string `json:"Disable"`
  42. Domain string `json:"Domain"`
  43. Origin SCdnOrigin `json:"Origin"`
  44. ProjectId string `json:"ProjectId"`
  45. Readonly string `json:"Readonly"`
  46. ResourceID string `json:"ResourceId"`
  47. ServiceType string `json:"ServiceType"`
  48. Status string `json:"Status"`
  49. UpdateTime string `json:"UpdateTime"`
  50. config *SCdnConfig
  51. }
  52. func (self *SCdnDomain) GetConfig() (*SCdnConfig, error) {
  53. var err error = nil
  54. if self.config == nil {
  55. self.config, err = self.client.GetCdnConfig(self.ResourceID)
  56. }
  57. return self.config, err
  58. }
  59. type SCacheKey struct {
  60. FullUrlCache string
  61. IgnoreCase string
  62. KeyRules []struct {
  63. RulePaths []string
  64. RuleType string
  65. FullUrlCache string
  66. IgnoreCase string
  67. QueryString struct {
  68. Switch string
  69. Action string
  70. Value string
  71. }
  72. RuleTag string
  73. }
  74. }
  75. type SCache struct {
  76. RuleCache []CdnCache
  77. }
  78. type CdnCache struct {
  79. CdnCacheCacheConfig CdnCacheCacheConfig `json:"CacheConfig"`
  80. RulePaths []string `json:"RulePaths"`
  81. RuleType string `json:"RuleType"`
  82. }
  83. type CdnCacheCacheConfig struct {
  84. CdnCacheCacheConfigCache CdnCacheCacheConfigCache `json:"Cache"`
  85. CdnCacheCacheConfigFollowOrigin CdnCacheCacheConfigFollowOrigin `json:"FollowOrigin"`
  86. CdnCacheCacheConfigNoCache CdnCacheCacheConfigNoCache `json:"NoCache"`
  87. }
  88. type CdnCacheCacheConfigFollowOrigin struct {
  89. CdnCacheCacheConfigFollowOriginHeuristicCache CdnCacheCacheConfigFollowOriginHeuristicCache `json:"HeuristicCache"`
  90. Switch string `json:"Switch"`
  91. }
  92. type CdnCacheCacheConfigFollowOriginHeuristicCache struct {
  93. CdnCacheCacheConfigFollowOriginHeuristicCacheCacheConfig CdnCacheCacheConfigFollowOriginHeuristicCacheCacheConfig `json:"CacheConfig"`
  94. Switch string `json:"Switch"`
  95. }
  96. type CdnCacheCacheConfigFollowOriginHeuristicCacheCacheConfig struct {
  97. HeuristicCacheTime int `json:"HeuristicCacheTime"`
  98. HeuristicCacheTimeSwitch string `json:"HeuristicCacheTimeSwitch"`
  99. }
  100. type CdnCacheCacheConfigNoCache struct {
  101. Revalidate string `json:"Revalidate"`
  102. Switch string `json:"Switch"`
  103. }
  104. type CdnCacheCacheConfigCache struct {
  105. CacheTime int `json:"CacheTime"`
  106. CompareMaxAge string `json:"CompareMaxAge"`
  107. IgnoreCacheControl string `json:"IgnoreCacheControl"`
  108. IgnoreSetCookie string `json:"IgnoreSetCookie"`
  109. Switch string `json:"Switch"`
  110. }
  111. type SRangeOriginPull struct {
  112. Switch string
  113. RangeRules []struct {
  114. Switch string
  115. RuleType string
  116. RulePaths []string
  117. }
  118. Cache *SCache
  119. }
  120. type SCdnHttps struct {
  121. Switch string
  122. Http2 string
  123. }
  124. type SForceRedirect struct {
  125. Switch string
  126. RedirectType string
  127. }
  128. type SCdnReferer struct {
  129. Switch string
  130. RefererRules []struct {
  131. RuleType string
  132. RulePaths []string
  133. RefererType string
  134. Referers []string
  135. AllowEmpty bool
  136. }
  137. }
  138. type SMaxAge struct {
  139. Switch string
  140. MaxAgeRules []struct {
  141. MaxAgeType string
  142. MaxAgeContents []string
  143. MaxAgeTime int
  144. FollowOrigin string
  145. }
  146. }
  147. type SCdnConfig struct {
  148. CacheKey *SCacheKey
  149. RangeOriginPull *SRangeOriginPull
  150. Cache *SCache
  151. Https *SCdnHttps
  152. ForceRedirect *SForceRedirect
  153. Referer *SCdnReferer
  154. MaxAge *SMaxAge
  155. }
  156. func (self *SCdnDomain) GetName() string {
  157. return self.Domain
  158. }
  159. func (self *SCdnDomain) GetGlobalId() string {
  160. return self.Domain
  161. }
  162. func (self *SCdnDomain) GetId() string {
  163. return self.Domain
  164. }
  165. func (self *SCdnDomain) GetStatus() string {
  166. return self.Status
  167. }
  168. func (self *SCdnDomain) GetEnabled() bool {
  169. return self.Disable == "normal"
  170. }
  171. func (self *SCdnDomain) GetCname() string {
  172. return self.Cname
  173. }
  174. func (self *SCdnDomain) GetOrigins() *cloudprovider.SCdnOrigins {
  175. ret := cloudprovider.SCdnOrigins{}
  176. if self.Origin.OriginType == "cos" {
  177. self.Origin.OriginType = api.CDN_DOMAIN_ORIGIN_TYPE_BUCKET
  178. }
  179. for _, org := range self.Origin.Origins {
  180. ret = append(ret, cloudprovider.SCdnOrigin{
  181. Type: self.Origin.OriginType,
  182. ServerName: self.Origin.ServerName,
  183. Protocol: self.Origin.OriginPullProtocol,
  184. Origin: org,
  185. })
  186. }
  187. for _, org := range self.Origin.BackupOrigins {
  188. ret = append(ret, cloudprovider.SCdnOrigin{
  189. Type: self.Origin.BackupOriginType,
  190. ServerName: self.Origin.BackupServerName,
  191. Origin: org,
  192. })
  193. }
  194. return &ret
  195. }
  196. func (self *SCdnDomain) GetArea() string {
  197. return self.Area
  198. }
  199. func (self *SCdnDomain) GetServiceType() string {
  200. return self.ServiceType
  201. }
  202. func (self *SQcloudClient) GetCdnDomain(domain string) (*SCdnDomain, error) {
  203. domains, _, err := self.DescribeCdnDomains([]string{domain}, nil, "", 0, 100)
  204. if err != nil {
  205. return nil, errors.Wrapf(err, "DescribeCdnDomains")
  206. }
  207. for i := range domains {
  208. if domains[i].Domain == domain {
  209. domains[i].client = self
  210. return &domains[i], nil
  211. }
  212. }
  213. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", domain)
  214. }
  215. func (self *SCdnDomain) Refresh() error {
  216. domain, err := self.client.GetCdnDomain(self.Domain)
  217. if err != nil {
  218. return err
  219. }
  220. return jsonutils.Update(self, domain)
  221. }
  222. func (self *SCdnDomain) Delete() error {
  223. err := self.client.StopCdnDomain(self.Domain)
  224. if err != nil {
  225. return errors.Wrapf(err, "StopCdnDomain")
  226. }
  227. return self.client.DeleteCdnDomain(self.Domain)
  228. }
  229. func (self *SCdnDomain) SetTags(tags map[string]string, replace bool) error {
  230. region, err := self.client.getDefaultRegion()
  231. if err != nil {
  232. return errors.Wrapf(err, "getDefaultRegion")
  233. }
  234. return region.SetResourceTags("cdn", "domain", []string{self.Domain}, tags, replace)
  235. }
  236. func (self *SQcloudClient) StopCdnDomain(domain string) error {
  237. params := map[string]string{
  238. "Domain": domain,
  239. }
  240. _, err := self.cdnRequest("StopCdnDomain", params)
  241. return errors.Wrapf(err, "StopCdnDomain")
  242. }
  243. func (self *SQcloudClient) StartCdnDomain(domain string) error {
  244. params := map[string]string{
  245. "Domain": domain,
  246. }
  247. _, err := self.cdnRequest("StartCdnDomain", params)
  248. return errors.Wrapf(err, "StartCdnDomain")
  249. }
  250. func (self *SQcloudClient) DeleteCdnDomain(domain string) error {
  251. params := map[string]string{
  252. "Domain": domain,
  253. }
  254. _, err := self.cdnRequest("DeleteCdnDomain", params)
  255. return errors.Wrapf(err, "DeleteCdnDomain")
  256. }
  257. type SDomains struct {
  258. RequestID string `json:"RequestId"`
  259. Domains []SCdnDomain `json:"Domains"`
  260. TotalNumber int `json:"TotalNumber"`
  261. }
  262. func (self *SQcloudClient) GetICloudCDNDomains() ([]cloudprovider.ICloudCDNDomain, error) {
  263. cdns, err := self.DescribeAllCdnDomains(nil, nil, "")
  264. if err != nil {
  265. return nil, err
  266. }
  267. ret := []cloudprovider.ICloudCDNDomain{}
  268. for i := range cdns {
  269. cdns[i].client = self
  270. ret = append(ret, &cdns[i])
  271. }
  272. return ret, nil
  273. }
  274. func (self *SQcloudClient) GetICloudCDNDomainByName(name string) (cloudprovider.ICloudCDNDomain, error) {
  275. domains, _, err := self.DescribeCdnDomains([]string{name}, nil, "", 0, 1)
  276. if err != nil {
  277. return nil, errors.Wrapf(err, "DescribeCdnDomains")
  278. }
  279. for i := range domains {
  280. if domains[i].Domain == name {
  281. domains[i].client = self
  282. return &domains[i], nil
  283. }
  284. }
  285. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", name)
  286. }
  287. func (client *SQcloudClient) AddCdnDomain(domain string, originType string, origins []string, cosPrivateAccess string) error {
  288. params := map[string]string{}
  289. params["Domain"] = domain
  290. params["ServiceType"] = "web"
  291. for i := range origins {
  292. params[fmt.Sprintf("Origin.Origins.%d", i)] = origins[i]
  293. }
  294. params["Origin.OriginType"] = originType
  295. params["Origin.CosPrivateAccess"] = cosPrivateAccess
  296. _, err := client.cdnRequest("AddCdnDomain", params)
  297. if err != nil {
  298. return errors.Wrapf(err, `AddCdnDomain %s`, params)
  299. }
  300. return nil
  301. }
  302. func (client *SQcloudClient) DescribeCdnDomains(domains, origins []string, domainType string, offset int, limit int) ([]SCdnDomain, int, error) {
  303. params := map[string]string{}
  304. params["Offset"] = strconv.Itoa(offset)
  305. params["Limit"] = strconv.Itoa(limit)
  306. filterIndex := 0
  307. if len(domains) > 0 {
  308. params[fmt.Sprintf("Filters.%d.Name", filterIndex)] = "domain"
  309. for i := range domains {
  310. params[fmt.Sprintf("Filters.%d.Value.%d", filterIndex, i)] = domains[i]
  311. }
  312. filterIndex++
  313. }
  314. if len(origins) > 0 {
  315. params[fmt.Sprintf("Filters.%d.Name", filterIndex)] = "origin"
  316. for i := range origins {
  317. params[fmt.Sprintf("Filters.%d.Value.%d", filterIndex, i)] = origins[i]
  318. }
  319. filterIndex++
  320. }
  321. if len(domainType) > 0 {
  322. params[fmt.Sprintf("Filters.%d.Name", filterIndex)] = "domainType"
  323. params[fmt.Sprintf("Filters.%d.Value.0", filterIndex)] = domainType
  324. filterIndex++
  325. }
  326. resp, err := client.cdnRequest("DescribeDomainsConfig", params)
  327. if err != nil {
  328. return nil, 0, errors.Wrapf(err, "DescribeDomainsConfig %s", params)
  329. }
  330. cdnDomains := []SCdnDomain{}
  331. err = resp.Unmarshal(&cdnDomains, "Domains")
  332. if err != nil {
  333. return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
  334. }
  335. totalcount, _ := resp.Float("TotalNumber")
  336. return cdnDomains, int(totalcount), nil
  337. }
  338. func (client *SQcloudClient) DescribeAllCdnDomains(domains, origins []string, domainType string) ([]SCdnDomain, error) {
  339. cdnDomains := make([]SCdnDomain, 0)
  340. for {
  341. part, total, err := client.DescribeCdnDomains(domains, origins, domainType, len(cdnDomains), 50)
  342. if err != nil {
  343. return nil, errors.Wrap(err, "DescribeCdnDomains")
  344. }
  345. cdnDomains = append(cdnDomains, part...)
  346. if len(cdnDomains) >= total {
  347. break
  348. }
  349. }
  350. return cdnDomains, nil
  351. }
  352. func (self *SQcloudClient) CreateCDNDomain(opts *cloudprovider.CdnCreateOptions) (*SCdnDomain, error) {
  353. params := map[string]string{
  354. "Domain": opts.Domain,
  355. "ServiceType": opts.ServiceType,
  356. }
  357. if len(opts.Area) > 0 {
  358. params["Area"] = opts.Area
  359. }
  360. originTypes := map[string][]string{}
  361. for _, origin := range opts.Origins {
  362. _, ok := originTypes[origin.Type]
  363. if !ok {
  364. originTypes[origin.Type] = []string{}
  365. }
  366. originTypes[origin.Type] = append(originTypes[origin.Type], origin.Origin)
  367. }
  368. for _, origin := range opts.Origins {
  369. params["Origin.OriginType"] = origin.Type
  370. if origin.Type == api.CDN_DOMAIN_ORIGIN_TYPE_BUCKET {
  371. params["Origin.OriginType"] = "cos"
  372. if len(origin.ServerName) > 0 {
  373. params["Origin.ServerName"] = origin.ServerName
  374. } else {
  375. params["Origin.ServerName"] = origin.Origin
  376. }
  377. }
  378. if len(origin.Protocol) > 0 {
  379. params["Origin.OriginPullProtocol"] = origin.Protocol
  380. }
  381. origins, ok := originTypes[origin.Type]
  382. if ok {
  383. for i, origin := range origins {
  384. params[fmt.Sprintf("Origin.Origins.%d", i)] = origin
  385. }
  386. }
  387. }
  388. _, err := self.cdnRequest("AddCdnDomain", params)
  389. if err != nil {
  390. return nil, errors.Wrapf(err, "AddCdnDomain")
  391. }
  392. return self.GetCdnDomain(opts.Domain)
  393. }
  394. func (self *SQcloudClient) GetCdnConfig(resourceId string) (*SCdnConfig, error) {
  395. params := map[string]string{
  396. "Filters.0.Name": "resourceId",
  397. "Filters.0.Value.0": resourceId,
  398. "Limit": "1",
  399. }
  400. resp, err := self.cdnRequest("DescribeDomainsConfig", params)
  401. if err != nil {
  402. return nil, errors.Wrapf(err, "DescribeDomainsConfig")
  403. }
  404. result := struct {
  405. Domains []SCdnConfig
  406. TotalNumber int
  407. }{}
  408. err = resp.Unmarshal(&result)
  409. if err != nil {
  410. return nil, errors.Wrapf(err, "resp.Unmarshal")
  411. }
  412. for i := range result.Domains {
  413. return &result.Domains[i], nil
  414. }
  415. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", resourceId)
  416. }
  417. func (self *SCdnDomain) GetCacheKeys() (*cloudprovider.SCDNCacheKeys, error) {
  418. config, err := self.GetConfig()
  419. if err != nil {
  420. return nil, err
  421. }
  422. if config.CacheKey == nil {
  423. return nil, nil
  424. }
  425. enabled, ignoreCase := false, false
  426. ret := &cloudprovider.SCDNCacheKeys{
  427. KeyRules: []cloudprovider.CacheKeyRule{},
  428. }
  429. if config.CacheKey.FullUrlCache == "on" {
  430. enabled = true
  431. }
  432. if config.CacheKey.IgnoreCase == "on" {
  433. ignoreCase = true
  434. }
  435. ret.Enabled, ret.IgnoreCase = &enabled, &ignoreCase
  436. for _, r := range config.CacheKey.KeyRules {
  437. rule := cloudprovider.CacheKeyRule{
  438. RulePaths: r.RulePaths,
  439. RuleType: r.RuleType,
  440. FullUrlCache: r.FullUrlCache == "on",
  441. IgnoreCase: r.IgnoreCase == "on",
  442. RuleTag: r.RuleTag,
  443. QueryString: cloudprovider.CacheKeyRuleQueryString{
  444. Enabled: r.QueryString.Switch == "on",
  445. Action: r.QueryString.Action,
  446. Value: r.QueryString.Value,
  447. },
  448. }
  449. ret.KeyRules = append(ret.KeyRules, rule)
  450. }
  451. return ret, nil
  452. }
  453. func (self *SCdnDomain) GetRangeOriginPull() (*cloudprovider.SCDNRangeOriginPull, error) {
  454. config, err := self.GetConfig()
  455. if err != nil {
  456. return nil, err
  457. }
  458. if config.RangeOriginPull == nil {
  459. return nil, nil
  460. }
  461. ret := &cloudprovider.SCDNRangeOriginPull{RangeOriginPullRules: []cloudprovider.SRangeOriginPullRule{}}
  462. enabled := false
  463. if config.RangeOriginPull.Switch == "on" {
  464. enabled = true
  465. }
  466. ret.Enabled = &enabled
  467. for _, obj := range config.RangeOriginPull.RangeRules {
  468. rule := cloudprovider.SRangeOriginPullRule{
  469. Enabled: false,
  470. RuleType: obj.RuleType,
  471. RulePaths: obj.RulePaths,
  472. }
  473. if obj.Switch == "on" {
  474. rule.Enabled = true
  475. }
  476. ret.RangeOriginPullRules = append(ret.RangeOriginPullRules, rule)
  477. }
  478. return ret, nil
  479. }
  480. func (self *SCdnDomain) GetCache() (*cloudprovider.SCDNCache, error) {
  481. config, err := self.GetConfig()
  482. if err != nil {
  483. return nil, err
  484. }
  485. if config.Cache == nil {
  486. return nil, nil
  487. }
  488. ret := &cloudprovider.SCDNCache{
  489. RuleCache: []cloudprovider.SCacheRuleCache{},
  490. }
  491. for i, r := range config.Cache.RuleCache {
  492. rule := cloudprovider.SCacheRuleCache{
  493. Priority: i + 1,
  494. RulePaths: r.RulePaths,
  495. RuleType: r.RuleType,
  496. CacheConfig: &cloudprovider.RuleCacheConfig{},
  497. }
  498. if r.CdnCacheCacheConfig.CdnCacheCacheConfigCache.Switch == "on" {
  499. rule.CacheConfig.Cache = &struct {
  500. Enabled bool
  501. CacheTime int
  502. CompareMaxAge bool
  503. IgnoreCacheControl bool
  504. IgnoreSetCookie bool
  505. }{
  506. Enabled: true,
  507. CacheTime: r.CdnCacheCacheConfig.CdnCacheCacheConfigCache.CacheTime,
  508. CompareMaxAge: r.CdnCacheCacheConfig.CdnCacheCacheConfigCache.CompareMaxAge == "on",
  509. IgnoreCacheControl: r.CdnCacheCacheConfig.CdnCacheCacheConfigCache.IgnoreCacheControl == "on",
  510. IgnoreSetCookie: r.CdnCacheCacheConfig.CdnCacheCacheConfigCache.IgnoreSetCookie == "on",
  511. }
  512. } else if r.CdnCacheCacheConfig.CdnCacheCacheConfigNoCache.Switch == "on" {
  513. rule.CacheConfig.NoCache = &struct {
  514. Enabled bool
  515. Revalidate bool
  516. }{
  517. Enabled: true,
  518. Revalidate: r.CdnCacheCacheConfig.CdnCacheCacheConfigNoCache.Revalidate == "on",
  519. }
  520. } else if r.CdnCacheCacheConfig.CdnCacheCacheConfigFollowOrigin.Switch == "on" {
  521. follow := r.CdnCacheCacheConfig.CdnCacheCacheConfigFollowOrigin
  522. rule.CacheConfig.FollowOrigin = &struct {
  523. Enabled bool
  524. HeuristicCache struct {
  525. Enabled bool
  526. CacheConfig struct {
  527. HeuristicCacheTimeSwitch bool
  528. HeuristicCacheTime int
  529. }
  530. }
  531. }{
  532. Enabled: true,
  533. }
  534. rule.CacheConfig.FollowOrigin.HeuristicCache.Enabled = follow.Switch == "on"
  535. rule.CacheConfig.FollowOrigin.HeuristicCache.CacheConfig.HeuristicCacheTimeSwitch = follow.CdnCacheCacheConfigFollowOriginHeuristicCache.Switch == "on"
  536. rule.CacheConfig.FollowOrigin.HeuristicCache.CacheConfig.HeuristicCacheTime = follow.CdnCacheCacheConfigFollowOriginHeuristicCache.CdnCacheCacheConfigFollowOriginHeuristicCacheCacheConfig.HeuristicCacheTime
  537. }
  538. ret.RuleCache = append(ret.RuleCache, rule)
  539. }
  540. return ret, nil
  541. }
  542. func (self *SCdnDomain) GetHTTPS() (*cloudprovider.SCDNHttps, error) {
  543. config, err := self.GetConfig()
  544. if err != nil {
  545. return nil, err
  546. }
  547. if config.Https == nil {
  548. return nil, nil
  549. }
  550. ret := &cloudprovider.SCDNHttps{}
  551. enabled, enableHttp2 := false, false
  552. if config.Https.Switch == "on" {
  553. enabled = true
  554. }
  555. if config.Https.Http2 == "on" {
  556. enableHttp2 = true
  557. }
  558. ret.Enabled, ret.Http2 = &enabled, &enableHttp2
  559. return ret, nil
  560. }
  561. func (self *SCdnDomain) GetForceRedirect() (*cloudprovider.SCDNForceRedirect, error) {
  562. config, err := self.GetConfig()
  563. if err != nil {
  564. return nil, err
  565. }
  566. if config.ForceRedirect == nil {
  567. return nil, nil
  568. }
  569. ret := &cloudprovider.SCDNForceRedirect{
  570. RedirectType: config.ForceRedirect.RedirectType,
  571. }
  572. enabled := false
  573. if config.ForceRedirect.Switch == "on" {
  574. enabled = true
  575. }
  576. ret.Enabled = &enabled
  577. return ret, nil
  578. }
  579. func (self *SCdnDomain) GetReferer() (*cloudprovider.SCDNReferer, error) {
  580. config, err := self.GetConfig()
  581. if err != nil {
  582. return nil, err
  583. }
  584. if config.Referer == nil {
  585. return nil, nil
  586. }
  587. ret := &cloudprovider.SCDNReferer{
  588. RefererRules: []cloudprovider.RefererRule{},
  589. }
  590. enabled := false
  591. if config.Referer.Switch == "on" {
  592. enabled = true
  593. }
  594. ret.Enabled = &enabled
  595. for _, r := range config.Referer.RefererRules {
  596. rule := cloudprovider.RefererRule{
  597. RuleType: r.RuleType,
  598. RulePaths: r.RulePaths,
  599. RefererType: r.RuleType,
  600. Referers: r.Referers,
  601. AllowEmpty: &r.AllowEmpty,
  602. }
  603. ret.RefererRules = append(ret.RefererRules, rule)
  604. }
  605. return ret, nil
  606. }
  607. func (self *SCdnDomain) GetMaxAge() (*cloudprovider.SCDNMaxAge, error) {
  608. config, err := self.GetConfig()
  609. if err != nil {
  610. return nil, err
  611. }
  612. if config.MaxAge == nil {
  613. return nil, nil
  614. }
  615. ret := &cloudprovider.SCDNMaxAge{}
  616. enabled := false
  617. if config.MaxAge.Switch == "on" {
  618. enabled = true
  619. }
  620. ret.Enabled = &enabled
  621. for _, r := range config.MaxAge.MaxAgeRules {
  622. rule := cloudprovider.SMaxAgeRule{
  623. MaxAgeType: r.MaxAgeType,
  624. MaxAgeContents: r.MaxAgeContents,
  625. MaxAgeTime: r.MaxAgeTime,
  626. FollowOrigin: false,
  627. }
  628. if r.FollowOrigin == "on" {
  629. rule.FollowOrigin = true
  630. }
  631. ret.MaxAgeRules = append(ret.MaxAgeRules, rule)
  632. }
  633. return ret, nil
  634. }
  635. func (self *SCdnDomain) GetProjectId() string {
  636. return self.ProjectId
  637. }