| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package aws
- import (
- "strings"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- "yunion.io/x/pkg/errors"
- )
- type AliasesType struct {
- Quantity int `xml:"Quantity"`
- Items []string `xml:"Items>CNAME"`
- }
- type Origin struct {
- DomainName string `xml:"DomainName"`
- Id string `xml:"Id"`
- ConnectionAttempts int `xml:"ConnectionAttempts,omitempty"`
- ConnectionTimeout int `xml:"ConnectionTimeout,omitempty"`
- OriginAccessControlId string `xml:"OriginAccessControlId,omitempty"`
- OriginPath string `xml:"OriginPath,omitempty"`
- }
- type OriginsType struct {
- Items []Origin `xml:"Items>Origin"`
- Quantity int `xml:"Quantity"`
- }
- type DistributionConfigType struct {
- CallerReference string `xml:"CallerReference"`
- Comment string `xml:"Comment"`
- Enabled bool `xml:"Enabled"`
- Origins OriginsType `xml:"Origins"`
- Aliases AliasesType `xml:"Aliases,omitempty"`
- ContinuousDeploymentPolicyId string `xml:"ContinuousDeploymentPolicyId,omitempty"`
- DefaultRootObject string `xml:"DefaultRootObject,omitempty"`
- HttpVersion string `xml:"HttpVersion,omitempty"`
- IsIPV6Enabled bool `xml:"IsIPV6Enabled,omitempty"`
- PriceClass string `xml:"PriceClass,omitempty"`
- Staging bool `xml:"Staging,omitempty"`
- WebACLId string `xml:"WebACLId,omitempty"`
- }
- type SCdnDomain struct {
- multicloud.SCDNDomainBase
- AwsTags
- client *SAwsClient
- Aliases AliasesType `xml:"Aliases"`
- ARN string `xml:"ARN"`
- Comment string `xml:"Comment"`
- DomainName string `xml:"DomainName"`
- Enabled bool `xml:"Enabled"`
- HttpVersion string `xml:"HttpVersion"`
- Id string `xml:"Id"`
- IsIPV6Enabled bool `xml:"IsIPV6Enabled"`
- LastModifiedTime string `xml:"LastModifiedTime"` // or time.Time if parsed
- Origins OriginsType `xml:"Origins"`
- PriceClass string `xml:"PriceClass"`
- Staging bool `xml:"Staging"`
- Status string `xml:"Status"`
- WebACLId string `xml:"WebACLId"`
- DistributionConfig DistributionConfigType `xml:"DistributionConfig"`
- }
- func (cd *SCdnDomain) GetCacheKeys() (*cloudprovider.SCDNCacheKeys, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetRangeOriginPull() (*cloudprovider.SCDNRangeOriginPull, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetCache() (*cloudprovider.SCDNCache, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetHTTPS() (*cloudprovider.SCDNHttps, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetForceRedirect() (*cloudprovider.SCDNForceRedirect, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetReferer() (*cloudprovider.SCDNReferer, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetMaxAge() (*cloudprovider.SCDNMaxAge, error) {
- return nil, cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetArea() string {
- return "global"
- }
- func (cd *SCdnDomain) GetCname() string {
- res := make([]string, len(cd.Aliases.Items))
- for idx, item := range cd.Aliases.Items {
- res[idx] = item
- }
- return strings.Join(res, ",")
- }
- func (cd *SCdnDomain) GetEnabled() bool {
- return cd.Enabled
- }
- func (cd *SCdnDomain) GetId() string {
- return cd.ARN
- }
- func (cd *SCdnDomain) GetGlobalId() string {
- return cd.ARN
- }
- func (cd *SCdnDomain) GetName() string {
- return cd.DomainName
- }
- func (cd *SCdnDomain) Refresh() error {
- domain, err := cd.client.GetCdnDomain(cd.Id)
- if err != nil {
- return errors.Wrapf(err, "GetCdnDomain")
- }
- cd.Status = domain.Status
- return nil
- }
- func (cd *SCdnDomain) GetOrigins() *cloudprovider.SCdnOrigins {
- domain, err := cd.client.GetCdnDomain(cd.Id)
- if err != nil {
- return nil
- }
- ret := cloudprovider.SCdnOrigins{}
- for _, origin := range domain.Origins.Items {
- ret = append(ret, cloudprovider.SCdnOrigin{
- Origin: origin.DomainName,
- Path: origin.OriginPath,
- })
- }
- return &ret
- }
- func (cd *SCdnDomain) GetServiceType() string {
- return "wholeSite"
- }
- func (cd *SCdnDomain) GetStatus() string {
- if cd.Status == "Deployed" {
- return "online"
- } else {
- return "offline"
- }
- }
- func (cd *SCdnDomain) Delete() error {
- return cloudprovider.ErrNotSupported
- }
- func (cd *SCdnDomain) GetProjectId() string {
- return ""
- }
- func (cd *SCdnDomain) GetDescription() string {
- return ""
- }
- func (ac *SAwsClient) GetICloudCDNDomains() ([]cloudprovider.ICloudCDNDomain, error) {
- domains, err := ac.GetCdnDomains()
- if err != nil {
- return nil, errors.Wrapf(err, "GetCdnDomains")
- }
- ret := make([]cloudprovider.ICloudCDNDomain, 0)
- for i := range domains {
- domains[i].client = ac
- ret = append(ret, &domains[i])
- }
- return ret, nil
- }
- func (ac *SAwsClient) GetICloudCDNDomainByName(name string) (cloudprovider.ICloudCDNDomain, error) {
- return ac.GetCDNDomainByName(name)
- }
- func (ac *SAwsClient) GetCDNDomainByName(name string) (*SCdnDomain, error) {
- domains, err := ac.GetCdnDomains()
- if err != nil {
- return nil, errors.Wrapf(err, "GetCdnDomain")
- }
- for i := range domains {
- if domains[i].DomainName == name {
- domains[i].client = ac
- return &domains[i], nil
- }
- }
- return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", name)
- }
- func (ac *SAwsClient) GetCdnDomains() ([]SCdnDomain, error) {
- domains := make([]SCdnDomain, 0)
- marker := ""
- for {
- part, nextMarker, err := ac.DescribeUserDomains(marker, int64(100))
- if err != nil {
- return nil, errors.Wrapf(err, "DescribeUserDomains")
- }
- domains = append(domains, part...)
- if nextMarker == "" {
- break
- } else {
- marker = nextMarker
- }
- }
- return domains, nil
- }
- func (ac *SAwsClient) DescribeUserDomains(marker string, pageSize int64) ([]SCdnDomain, string, error) {
- resp, NextMarker, err := ac.cdnList(marker, pageSize)
- if err != nil {
- return nil, "", errors.Wrap(err, "DescribeUserDomains")
- }
- domains := make([]SCdnDomain, 0)
- for i := range resp {
- resp[i].client = ac
- domains = append(domains, resp[i])
- }
- return domains, NextMarker, nil
- }
- func (ac *SAwsClient) GetCdnDomain(domainID string) (*SCdnDomain, error) {
- resp, err := ac.cdnGet(domainID)
- if err != nil {
- return nil, errors.Wrapf(err, "ShowDomainDetail")
- }
- domain := &SCdnDomain{client: ac}
- domain.Status = resp.Status
- domain.Origins = resp.DistributionConfig.Origins
- if err != nil {
- return nil, errors.Wrapf(err, "resp.Unmarshal")
- }
- return domain, nil
- }
|