yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/bingocloud/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  	"fmt"
    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/bingocloud"
    29  )
    30  
    31  type SBingoCloudProviderFactory struct {
    32  	cloudprovider.SPrivateCloudBaseProviderFactory
    33  }
    34  
    35  func (self *SBingoCloudProviderFactory) GetId() string {
    36  	return bingocloud.CLOUD_PROVIDER_BINGO_CLOUD
    37  }
    38  
    39  func (self *SBingoCloudProviderFactory) GetName() string {
    40  	return bingocloud.CLOUD_PROVIDER_BINGO_CLOUD
    41  }
    42  
    43  func (self *SBingoCloudProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error {
    44  	return fmt.Errorf("Changing %s bandwidth is not supported", bingocloud.CLOUD_PROVIDER_BINGO_CLOUD)
    45  }
    46  
    47  func (self *SBingoCloudProviderFactory) 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  	if len(input.Endpoint) == 0 {
    56  		return output, errors.Wrap(httperrors.ErrMissingParameter, "endpoint")
    57  	}
    58  	output.AccessUrl = input.Endpoint
    59  	output.Account = input.AccessKeyId
    60  	output.Secret = input.AccessKeySecret
    61  	return output, nil
    62  }
    63  
    64  func (self *SBingoCloudProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    65  	output := cloudprovider.SCloudaccount{}
    66  	if len(input.AccessKeyId) == 0 {
    67  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    68  	}
    69  	if len(input.AccessKeySecret) == 0 {
    70  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    71  	}
    72  	output = cloudprovider.SCloudaccount{
    73  		Account: input.AccessKeyId,
    74  		Secret:  input.AccessKeySecret,
    75  	}
    76  	return output, nil
    77  }
    78  
    79  func (self *SBingoCloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
    80  	client, err := bingocloud.NewBingoCloudClient(
    81  		bingocloud.NewBingoCloudClientConfig(
    82  			cfg.URL, cfg.Account, cfg.Secret,
    83  		).CloudproviderConfig(cfg),
    84  	)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	return &SBingoCloudProvider{
    89  		SBaseProvider: cloudprovider.NewBaseProvider(self),
    90  		client:        client,
    91  	}, nil
    92  }
    93  
    94  func (self *SBingoCloudProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
    95  	return map[string]string{
    96  		"BINGO_CLOUD_ENDPOINT":   info.Url,
    97  		"BINGO_CLOUD_ACCESS_KEY": info.Account,
    98  		"BINGO_CLOUD_SECRET_KEY": info.Secret,
    99  	}, nil
   100  }
   101  
   102  func init() {
   103  	factory := SBingoCloudProviderFactory{}
   104  	cloudprovider.RegisterFactory(&factory)
   105  }
   106  
   107  type SBingoCloudProvider struct {
   108  	cloudprovider.SBaseProvider
   109  	client *bingocloud.SBingoCloudClient
   110  }
   111  
   112  func (self *SBingoCloudProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   113  	return jsonutils.NewDict(), nil
   114  }
   115  
   116  func (self *SBingoCloudProvider) GetVersion() string {
   117  	return "2009-08-15"
   118  }
   119  
   120  func (self *SBingoCloudProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   121  	return self.client.GetSubAccounts()
   122  }
   123  
   124  func (self *SBingoCloudProvider) GetAccountId() string {
   125  	return self.client.GetAccountId()
   126  }
   127  
   128  func (self *SBingoCloudProvider) GetIRegions() []cloudprovider.ICloudRegion {
   129  	return self.client.GetIRegions()
   130  }
   131  
   132  func (self *SBingoCloudProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
   133  	return self.client.GetIRegionById(id)
   134  }
   135  
   136  func (self *SBingoCloudProvider) GetBalance() (float64, string, error) {
   137  	return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported
   138  }
   139  
   140  func (self *SBingoCloudProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   141  	return []cloudprovider.ICloudProject{}, nil
   142  }
   143  
   144  func (self *SBingoCloudProvider) GetStorageClasses(regionId string) []string {
   145  	return nil
   146  }
   147  
   148  func (self *SBingoCloudProvider) GetBucketCannedAcls(regionId string) []string {
   149  	return nil
   150  }
   151  
   152  func (self *SBingoCloudProvider) GetObjectCannedAcls(regionId string) []string {
   153  	return nil
   154  }
   155  
   156  func (self *SBingoCloudProvider) GetCapabilities() []string {
   157  	return self.client.GetCapabilities()
   158  }
   159  
   160  func (self *SBingoCloudProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   161  	return self.client.GetMetrics(opts)
   162  }