yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/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  
    20  	"yunion.io/x/jsonutils"
    21  
    22  	"yunion.io/x/onecloud/pkg/httperrors"
    23  	"yunion.io/x/onecloud/pkg/mcclient"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud/cloudpods"
    28  )
    29  
    30  type SCloudpodsProviderFactory struct {
    31  	cloudprovider.SPrivateCloudBaseProviderFactory
    32  }
    33  
    34  func (self *SCloudpodsProviderFactory) GetId() string {
    35  	return cloudpods.CLOUD_PROVIDER_CLOUDPODS
    36  }
    37  
    38  func (self *SCloudpodsProviderFactory) GetName() string {
    39  	return cloudpods.CLOUD_PROVIDER_CLOUDPODS
    40  }
    41  
    42  func (self *SCloudpodsProviderFactory) IsNeedForceAutoCreateProject() bool {
    43  	return true
    44  }
    45  
    46  func (self *SCloudpodsProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    47  	ret := cloudprovider.SCloudaccount{}
    48  	if len(input.AuthUrl) == 0 {
    49  		return ret, httperrors.NewMissingParameterError("auth_url")
    50  	}
    51  	ret.AccessUrl = input.AuthUrl
    52  	if len(input.AccessKeyId) == 0 {
    53  		return ret, httperrors.NewMissingParameterError("access_key_id")
    54  	}
    55  	ret.Account = input.AccessKeyId
    56  	if len(input.AccessKeySecret) == 0 {
    57  		return ret, httperrors.NewMissingParameterError("access_key_secret")
    58  	}
    59  	ret.Secret = input.AccessKeySecret
    60  	return ret, nil
    61  }
    62  
    63  func (self *SCloudpodsProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    64  	ret := cloudprovider.SCloudaccount{}
    65  	if len(input.AccessKeyId) == 0 {
    66  		return ret, httperrors.NewMissingParameterError("access_key_id")
    67  	}
    68  	ret.Account = input.AccessKeyId
    69  	if len(input.AccessKeySecret) == 0 {
    70  		return ret, httperrors.NewMissingParameterError("access_key_secret")
    71  	}
    72  	ret.Secret = input.AccessKeySecret
    73  	return ret, nil
    74  }
    75  
    76  func (self *SCloudpodsProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
    77  	client, err := cloudpods.NewCloudpodsClient(
    78  		cloudpods.NewCloudpodsClientConfig(
    79  			cfg.URL,
    80  			cfg.Account,
    81  			cfg.Secret,
    82  		).Debug(cfg.Debug).CloudproviderConfig(cfg),
    83  	)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return &SCloudpodsProvider{
    88  		SBaseProvider: cloudprovider.NewBaseProvider(self),
    89  		client:        client,
    90  	}, nil
    91  }
    92  
    93  func (self *SCloudpodsProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
    94  	return map[string]string{
    95  		"CLOUDPODS_AUTH_URL":      info.Url,
    96  		"CLOUDPODS_ACCESS_KEY":    info.Account,
    97  		"CLOUDPODS_ACCESS_SECRET": info.Secret,
    98  	}, nil
    99  }
   100  
   101  func init() {
   102  	factory := SCloudpodsProviderFactory{}
   103  	cloudprovider.RegisterFactory(&factory)
   104  }
   105  
   106  type SCloudpodsProvider struct {
   107  	cloudprovider.SBaseProvider
   108  	client *cloudpods.SCloudpodsClient
   109  }
   110  
   111  func (self *SCloudpodsProvider) GetAccountId() string {
   112  	return self.client.GetAccountId()
   113  }
   114  
   115  func (self *SCloudpodsProvider) GetBalance() (float64, string, error) {
   116  	return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported
   117  }
   118  
   119  func (self *SCloudpodsProvider) GetBucketCannedAcls(regionId string) []string {
   120  	return nil
   121  }
   122  
   123  func (self *SCloudpodsProvider) GetCapabilities() []string {
   124  	return self.client.GetCapabilities()
   125  }
   126  
   127  func (self *SCloudpodsProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   128  	return self.client.GetIProjects()
   129  }
   130  
   131  func (self *SCloudpodsProvider) GetIRegionById(extId string) (cloudprovider.ICloudRegion, error) {
   132  	return self.client.GetIRegionById(extId)
   133  }
   134  
   135  func (self *SCloudpodsProvider) GetIRegions() []cloudprovider.ICloudRegion {
   136  	return self.client.GetIRegions()
   137  }
   138  
   139  func (self *SCloudpodsProvider) GetObjectCannedAcls(regionId string) []string {
   140  	return nil
   141  }
   142  
   143  func (self *SCloudpodsProvider) GetStorageClasses(regionId string) []string {
   144  	return nil
   145  }
   146  
   147  func (self *SCloudpodsProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   148  	return self.client.GetSubAccounts()
   149  }
   150  
   151  func (self *SCloudpodsProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   152  	return jsonutils.NewDict(), nil
   153  }
   154  
   155  func (self *SCloudpodsProvider) GetVersion() string {
   156  	return self.client.GetVersion()
   157  }
   158  
   159  func (provider *SCloudpodsProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   160  	return provider.client.GetMetrics(opts)
   161  }