cdn.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 aws
  15. import (
  16. "strings"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/pkg/errors"
  20. )
  21. type AliasesType struct {
  22. Quantity int `xml:"Quantity"`
  23. Items []string `xml:"Items>CNAME"`
  24. }
  25. type Origin struct {
  26. DomainName string `xml:"DomainName"`
  27. Id string `xml:"Id"`
  28. ConnectionAttempts int `xml:"ConnectionAttempts,omitempty"`
  29. ConnectionTimeout int `xml:"ConnectionTimeout,omitempty"`
  30. OriginAccessControlId string `xml:"OriginAccessControlId,omitempty"`
  31. OriginPath string `xml:"OriginPath,omitempty"`
  32. }
  33. type OriginsType struct {
  34. Items []Origin `xml:"Items>Origin"`
  35. Quantity int `xml:"Quantity"`
  36. }
  37. type DistributionConfigType struct {
  38. CallerReference string `xml:"CallerReference"`
  39. Comment string `xml:"Comment"`
  40. Enabled bool `xml:"Enabled"`
  41. Origins OriginsType `xml:"Origins"`
  42. Aliases AliasesType `xml:"Aliases,omitempty"`
  43. ContinuousDeploymentPolicyId string `xml:"ContinuousDeploymentPolicyId,omitempty"`
  44. DefaultRootObject string `xml:"DefaultRootObject,omitempty"`
  45. HttpVersion string `xml:"HttpVersion,omitempty"`
  46. IsIPV6Enabled bool `xml:"IsIPV6Enabled,omitempty"`
  47. PriceClass string `xml:"PriceClass,omitempty"`
  48. Staging bool `xml:"Staging,omitempty"`
  49. WebACLId string `xml:"WebACLId,omitempty"`
  50. }
  51. type SCdnDomain struct {
  52. multicloud.SCDNDomainBase
  53. AwsTags
  54. client *SAwsClient
  55. Aliases AliasesType `xml:"Aliases"`
  56. ARN string `xml:"ARN"`
  57. Comment string `xml:"Comment"`
  58. DomainName string `xml:"DomainName"`
  59. Enabled bool `xml:"Enabled"`
  60. HttpVersion string `xml:"HttpVersion"`
  61. Id string `xml:"Id"`
  62. IsIPV6Enabled bool `xml:"IsIPV6Enabled"`
  63. LastModifiedTime string `xml:"LastModifiedTime"` // or time.Time if parsed
  64. Origins OriginsType `xml:"Origins"`
  65. PriceClass string `xml:"PriceClass"`
  66. Staging bool `xml:"Staging"`
  67. Status string `xml:"Status"`
  68. WebACLId string `xml:"WebACLId"`
  69. DistributionConfig DistributionConfigType `xml:"DistributionConfig"`
  70. }
  71. func (cd *SCdnDomain) GetCacheKeys() (*cloudprovider.SCDNCacheKeys, error) {
  72. return nil, cloudprovider.ErrNotSupported
  73. }
  74. func (cd *SCdnDomain) GetRangeOriginPull() (*cloudprovider.SCDNRangeOriginPull, error) {
  75. return nil, cloudprovider.ErrNotSupported
  76. }
  77. func (cd *SCdnDomain) GetCache() (*cloudprovider.SCDNCache, error) {
  78. return nil, cloudprovider.ErrNotSupported
  79. }
  80. func (cd *SCdnDomain) GetHTTPS() (*cloudprovider.SCDNHttps, error) {
  81. return nil, cloudprovider.ErrNotSupported
  82. }
  83. func (cd *SCdnDomain) GetForceRedirect() (*cloudprovider.SCDNForceRedirect, error) {
  84. return nil, cloudprovider.ErrNotSupported
  85. }
  86. func (cd *SCdnDomain) GetReferer() (*cloudprovider.SCDNReferer, error) {
  87. return nil, cloudprovider.ErrNotSupported
  88. }
  89. func (cd *SCdnDomain) GetMaxAge() (*cloudprovider.SCDNMaxAge, error) {
  90. return nil, cloudprovider.ErrNotSupported
  91. }
  92. func (cd *SCdnDomain) GetArea() string {
  93. return "global"
  94. }
  95. func (cd *SCdnDomain) GetCname() string {
  96. res := make([]string, len(cd.Aliases.Items))
  97. for idx, item := range cd.Aliases.Items {
  98. res[idx] = item
  99. }
  100. return strings.Join(res, ",")
  101. }
  102. func (cd *SCdnDomain) GetEnabled() bool {
  103. return cd.Enabled
  104. }
  105. func (cd *SCdnDomain) GetId() string {
  106. return cd.ARN
  107. }
  108. func (cd *SCdnDomain) GetGlobalId() string {
  109. return cd.ARN
  110. }
  111. func (cd *SCdnDomain) GetName() string {
  112. return cd.DomainName
  113. }
  114. func (cd *SCdnDomain) Refresh() error {
  115. domain, err := cd.client.GetCdnDomain(cd.Id)
  116. if err != nil {
  117. return errors.Wrapf(err, "GetCdnDomain")
  118. }
  119. cd.Status = domain.Status
  120. return nil
  121. }
  122. func (cd *SCdnDomain) GetOrigins() *cloudprovider.SCdnOrigins {
  123. domain, err := cd.client.GetCdnDomain(cd.Id)
  124. if err != nil {
  125. return nil
  126. }
  127. ret := cloudprovider.SCdnOrigins{}
  128. for _, origin := range domain.Origins.Items {
  129. ret = append(ret, cloudprovider.SCdnOrigin{
  130. Origin: origin.DomainName,
  131. Path: origin.OriginPath,
  132. })
  133. }
  134. return &ret
  135. }
  136. func (cd *SCdnDomain) GetServiceType() string {
  137. return "wholeSite"
  138. }
  139. func (cd *SCdnDomain) GetStatus() string {
  140. if cd.Status == "Deployed" {
  141. return "online"
  142. } else {
  143. return "offline"
  144. }
  145. }
  146. func (cd *SCdnDomain) Delete() error {
  147. return cloudprovider.ErrNotSupported
  148. }
  149. func (cd *SCdnDomain) GetProjectId() string {
  150. return ""
  151. }
  152. func (cd *SCdnDomain) GetDescription() string {
  153. return ""
  154. }
  155. func (ac *SAwsClient) GetICloudCDNDomains() ([]cloudprovider.ICloudCDNDomain, error) {
  156. domains, err := ac.GetCdnDomains()
  157. if err != nil {
  158. return nil, errors.Wrapf(err, "GetCdnDomains")
  159. }
  160. ret := make([]cloudprovider.ICloudCDNDomain, 0)
  161. for i := range domains {
  162. domains[i].client = ac
  163. ret = append(ret, &domains[i])
  164. }
  165. return ret, nil
  166. }
  167. func (ac *SAwsClient) GetICloudCDNDomainByName(name string) (cloudprovider.ICloudCDNDomain, error) {
  168. return ac.GetCDNDomainByName(name)
  169. }
  170. func (ac *SAwsClient) GetCDNDomainByName(name string) (*SCdnDomain, error) {
  171. domains, err := ac.GetCdnDomains()
  172. if err != nil {
  173. return nil, errors.Wrapf(err, "GetCdnDomain")
  174. }
  175. for i := range domains {
  176. if domains[i].DomainName == name {
  177. domains[i].client = ac
  178. return &domains[i], nil
  179. }
  180. }
  181. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", name)
  182. }
  183. func (ac *SAwsClient) GetCdnDomains() ([]SCdnDomain, error) {
  184. domains := make([]SCdnDomain, 0)
  185. marker := ""
  186. for {
  187. part, nextMarker, err := ac.DescribeUserDomains(marker, int64(100))
  188. if err != nil {
  189. return nil, errors.Wrapf(err, "DescribeUserDomains")
  190. }
  191. domains = append(domains, part...)
  192. if nextMarker == "" {
  193. break
  194. } else {
  195. marker = nextMarker
  196. }
  197. }
  198. return domains, nil
  199. }
  200. func (ac *SAwsClient) DescribeUserDomains(marker string, pageSize int64) ([]SCdnDomain, string, error) {
  201. resp, NextMarker, err := ac.cdnList(marker, pageSize)
  202. if err != nil {
  203. return nil, "", errors.Wrap(err, "DescribeUserDomains")
  204. }
  205. domains := make([]SCdnDomain, 0)
  206. for i := range resp {
  207. resp[i].client = ac
  208. domains = append(domains, resp[i])
  209. }
  210. return domains, NextMarker, nil
  211. }
  212. func (ac *SAwsClient) GetCdnDomain(domainID string) (*SCdnDomain, error) {
  213. resp, err := ac.cdnGet(domainID)
  214. if err != nil {
  215. return nil, errors.Wrapf(err, "ShowDomainDetail")
  216. }
  217. domain := &SCdnDomain{client: ac}
  218. domain.Status = resp.Status
  219. domain.Origins = resp.DistributionConfig.Origins
  220. if err != nil {
  221. return nil, errors.Wrapf(err, "resp.Unmarshal")
  222. }
  223. return domain, nil
  224. }