yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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/ucloud"
    29  )
    30  
    31  // tag:finished
    32  type SUcloudProviderFactory struct {
    33  	cloudprovider.SPublicCloudBaseProviderFactory
    34  }
    35  
    36  func (self *SUcloudProviderFactory) GetId() string {
    37  	return ucloud.CLOUD_PROVIDER_UCLOUD
    38  }
    39  
    40  func (self *SUcloudProviderFactory) GetName() string {
    41  	return ucloud.CLOUD_PROVIDER_UCLOUD_CN
    42  }
    43  
    44  func (self *SUcloudProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    45  	output := cloudprovider.SCloudaccount{}
    46  	if len(input.AccessKeyId) == 0 {
    47  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    48  	}
    49  	if len(input.AccessKeySecret) == 0 {
    50  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    51  	}
    52  	output.Account = input.AccessKeyId
    53  	output.Secret = input.AccessKeySecret
    54  	return output, nil
    55  }
    56  
    57  func (self *SUcloudProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    58  	output := cloudprovider.SCloudaccount{}
    59  	if len(input.AccessKeyId) == 0 {
    60  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    61  	}
    62  	if len(input.AccessKeySecret) == 0 {
    63  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    64  	}
    65  	output = cloudprovider.SCloudaccount{
    66  		Account: input.AccessKeyId,
    67  		Secret:  input.AccessKeySecret,
    68  	}
    69  	return output, nil
    70  }
    71  
    72  func parseAccount(account string) (accessKey string, projectId string) {
    73  	segs := strings.Split(account, "::")
    74  	if len(segs) == 2 {
    75  		accessKey = segs[0]
    76  		projectId = segs[1]
    77  	} else {
    78  		accessKey = account
    79  		projectId = ""
    80  	}
    81  
    82  	return
    83  }
    84  
    85  func (self *SUcloudProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
    86  	accessKey, projectId := parseAccount(cfg.Account)
    87  	client, err := ucloud.NewUcloudClient(
    88  		ucloud.NewUcloudClientConfig(
    89  			accessKey, cfg.Secret,
    90  		).ProjectId(projectId).CloudproviderConfig(cfg),
    91  	)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return &SUcloudProvider{
    96  		SBaseProvider: cloudprovider.NewBaseProvider(self),
    97  		client:        client,
    98  	}, nil
    99  }
   100  
   101  func (self *SUcloudProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
   102  	accessKey, projectId := parseAccount(info.Account)
   103  	return map[string]string{
   104  		"UCLOUD_ACCESS_KEY": accessKey,
   105  		"UCLOUD_SECRET":     info.Secret,
   106  		"UCLOUD_REGION":     ucloud.UCLOUD_DEFAULT_REGION,
   107  		"UCLOUD_PROJECT":    projectId,
   108  	}, nil
   109  }
   110  
   111  func init() {
   112  	factory := SUcloudProviderFactory{}
   113  	cloudprovider.RegisterFactory(&factory)
   114  }
   115  
   116  type SUcloudProvider struct {
   117  	cloudprovider.SBaseProvider
   118  	client *ucloud.SUcloudClient
   119  }
   120  
   121  func (self *SUcloudProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   122  	projects, err := self.client.FetchProjects()
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	iprojects := make([]cloudprovider.ICloudProject, len(projects))
   128  	for i := range projects {
   129  		iprojects[i] = &projects[i]
   130  	}
   131  
   132  	return iprojects, nil
   133  }
   134  
   135  func (self *SUcloudProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   136  	regions := self.client.GetIRegions()
   137  	info := jsonutils.NewDict()
   138  	info.Add(jsonutils.NewInt(int64(len(regions))), "region_count")
   139  	info.Add(jsonutils.NewString(ucloud.UCLOUD_API_VERSION), "api_version")
   140  	return info, nil
   141  }
   142  
   143  func (self *SUcloudProvider) GetVersion() string {
   144  	return ucloud.UCLOUD_API_VERSION
   145  }
   146  
   147  func (self *SUcloudProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   148  	return self.client.GetSubAccounts()
   149  }
   150  
   151  func (self *SUcloudProvider) GetAccountId() string {
   152  	return self.client.GetAccountId()
   153  }
   154  
   155  func (self *SUcloudProvider) GetIRegions() []cloudprovider.ICloudRegion {
   156  	return self.client.GetIRegions()
   157  }
   158  
   159  func (self *SUcloudProvider) GetIRegionById(extId string) (cloudprovider.ICloudRegion, error) {
   160  	return self.client.GetIRegionById(extId)
   161  }
   162  
   163  func (self *SUcloudProvider) GetBalance() (float64, string, error) {
   164  	return 0.0, api.CLOUD_PROVIDER_HEALTH_NORMAL, cloudprovider.ErrNotSupported
   165  }
   166  
   167  func (self *SUcloudProvider) GetOnPremiseIRegion() (cloudprovider.ICloudRegion, error) {
   168  	return nil, cloudprovider.ErrNotImplemented
   169  }
   170  
   171  func (self *SUcloudProvider) GetStorageClasses(regionId string) []string {
   172  	return []string{
   173  		"STANDARD", "IA", "ARCHIVE",
   174  	}
   175  }
   176  
   177  func (self *SUcloudProvider) GetBucketCannedAcls(regionId string) []string {
   178  	return []string{
   179  		string(cloudprovider.ACLPrivate),
   180  		string(cloudprovider.ACLPublicRead),
   181  	}
   182  }
   183  
   184  func (self *SUcloudProvider) GetObjectCannedAcls(regionId string) []string {
   185  	return []string{
   186  		string(cloudprovider.ACLPrivate),
   187  		string(cloudprovider.ACLPublicRead),
   188  	}
   189  }
   190  
   191  func (self *SUcloudProvider) GetCapabilities() []string {
   192  	return self.client.GetCapabilities()
   193  }