yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ecloud/provider/provider.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 provider
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    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/onecloud/pkg/httperrors"
    27  	"yunion.io/x/onecloud/pkg/mcclient"
    28  	"yunion.io/x/cloudmux/pkg/multicloud/ecloud"
    29  )
    30  
    31  type SEcloudProviderFactory struct {
    32  	cloudprovider.SPublicCloudBaseProviderFactory
    33  }
    34  
    35  func (f *SEcloudProviderFactory) GetId() string {
    36  	return ecloud.CLOUD_PROVIDER_ECLOUD
    37  }
    38  
    39  func (f *SEcloudProviderFactory) GetName() string {
    40  	return ecloud.CLOUD_PROVIDER_ECLOUD_CN
    41  }
    42  
    43  func (f *SEcloudProviderFactory) IsSupportPrepaidResources() bool {
    44  	return true
    45  }
    46  
    47  func (f *SEcloudProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    48  	output := cloudprovider.SCloudaccount{}
    49  	if len(input.AccessKeyId) == 0 {
    50  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    51  	}
    52  	if len(input.AccessKeySecret) == 0 {
    53  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    54  	}
    55  	output.Account = input.AccessKeyId
    56  	output.Secret = input.AccessKeySecret
    57  	return output, nil
    58  }
    59  
    60  func (f *SEcloudProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    61  	output := cloudprovider.SCloudaccount{}
    62  	if len(input.AccessKeyId) == 0 {
    63  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    64  	}
    65  	if len(input.AccessKeySecret) == 0 {
    66  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    67  	}
    68  	output = cloudprovider.SCloudaccount{
    69  		Account: input.AccessKeyId,
    70  		Secret:  input.AccessKeySecret,
    71  	}
    72  	return output, nil
    73  }
    74  
    75  func (f *SEcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
    76  	segs := strings.Split(cfg.Account, "/")
    77  	account := cfg.Account
    78  	if len(segs) == 2 {
    79  		account = segs[0]
    80  	}
    81  
    82  	client, err := ecloud.NewEcloudClient(
    83  		ecloud.NewEcloudClientConfig(
    84  			ecloud.NewRamRoleSigner(account, cfg.Secret),
    85  		).SetCloudproviderConfig(cfg),
    86  	)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	err = client.TryConnect()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	return &SEcloudProvider{
    95  		SBaseProvider: cloudprovider.NewBaseProvider(f),
    96  		client:        client,
    97  	}, nil
    98  }
    99  
   100  func (f *SEcloudProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
   101  	return map[string]string{
   102  		"ECLOUD_ACCESS_URL": info.Url,
   103  		"ECLOUD_ACCESS_KEY": info.Account,
   104  		"ECLOUD_SECRET":     info.Secret,
   105  		"ECLOUD_REGION":     ecloud.ECLOUD_DEFAULT_REGION,
   106  	}, nil
   107  }
   108  
   109  func init() {
   110  	factory := SEcloudProviderFactory{}
   111  	cloudprovider.RegisterFactory(&factory)
   112  }
   113  
   114  type SEcloudProvider struct {
   115  	cloudprovider.SBaseProvider
   116  	client *ecloud.SEcloudClient
   117  }
   118  
   119  func (p *SEcloudProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   120  	return p.client.GetSubAccounts()
   121  }
   122  
   123  func (p *SEcloudProvider) GetAccountId() string {
   124  	return p.client.GetAccountId()
   125  }
   126  
   127  func (p *SEcloudProvider) GetIRegions() []cloudprovider.ICloudRegion {
   128  	return p.client.GetIRegions()
   129  }
   130  
   131  func (p *SEcloudProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   132  	iregions := p.client.GetIRegions()
   133  	info := jsonutils.NewDict()
   134  	info.Add(jsonutils.NewInt(int64(len(iregions))), "region_count")
   135  	info.Add(jsonutils.NewString(ecloud.CLOUD_API_VERSION), "api_version")
   136  	return info, nil
   137  }
   138  
   139  func (p *SEcloudProvider) GetVersion() string {
   140  	return ecloud.CLOUD_API_VERSION
   141  }
   142  
   143  func (p *SEcloudProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
   144  	iregion, err := p.client.GetIRegionById(id)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	if iregion == nil {
   149  		return nil, cloudprovider.ErrNotFound
   150  	}
   151  	return iregion, nil
   152  }
   153  
   154  func (p *SEcloudProvider) GetBalance() (float64, string, error) {
   155  	return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported
   156  }
   157  
   158  func (p *SEcloudProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   159  	return nil, cloudprovider.ErrNotImplemented
   160  }
   161  
   162  func (p *SEcloudProvider) GetStorageClasses(regionId string) []string {
   163  	// TODO
   164  	return nil
   165  }
   166  
   167  func (p *SEcloudProvider) GetBucketCannedAcls(regionId string) []string {
   168  	return nil
   169  }
   170  
   171  func (p *SEcloudProvider) GetObjectCannedAcls(regionId string) []string {
   172  	return nil
   173  }
   174  
   175  func (p *SEcloudProvider) GetCloudRegionExternalIdPrefix() string {
   176  	return p.client.GetCloudRegionExternalIdPrefix()
   177  }
   178  
   179  func (p *SEcloudProvider) GetCapabilities() []string {
   180  	return p.client.GetCapabilities()
   181  }
   182  
   183  func (self *SEcloudProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   184  	return self.client.GetMetrics(opts)
   185  }