yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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/hcs"
    29  )
    30  
    31  type SHcsProviderFactory struct {
    32  	cloudprovider.SPrivateCloudBaseProviderFactory
    33  }
    34  
    35  func (self *SHcsProviderFactory) GetId() string {
    36  	return hcs.CLOUD_PROVIDER_HCS
    37  }
    38  
    39  func (self *SHcsProviderFactory) GetName() string {
    40  	return hcs.CLOUD_PROVIDER_HCS_CN
    41  }
    42  
    43  func (self *SHcsProviderFactory) IsCloudeventRegional() bool {
    44  	return true
    45  }
    46  
    47  func (self *SHcsProviderFactory) GetMaxCloudEventSyncDays() int {
    48  	return 7
    49  }
    50  
    51  func (self *SHcsProviderFactory) GetMaxCloudEventKeepDays() int {
    52  	return 7
    53  }
    54  
    55  func (factory *SHcsProviderFactory) IsSupportModifyRouteTable() bool {
    56  	return true
    57  }
    58  
    59  func (factory *SHcsProviderFactory) IsSupportSAMLAuth() bool {
    60  	return false
    61  }
    62  
    63  func (self *SHcsProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    64  	output := cloudprovider.SCloudaccount{}
    65  	if len(input.AccessKeyId) == 0 {
    66  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    67  	}
    68  	if len(input.AccessKeySecret) == 0 {
    69  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    70  	}
    71  	if len(input.AuthUrl) == 0 {
    72  		return output, errors.Wrap(httperrors.ErrMissingParameter, "auth_url")
    73  	}
    74  	output.Account = input.AccessKeyId
    75  	output.Secret = input.AccessKeySecret
    76  	output.AccessUrl = input.AuthUrl
    77  	return output, nil
    78  }
    79  
    80  func (self *SHcsProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    81  	output := cloudprovider.SCloudaccount{}
    82  	if len(input.AccessKeyId) == 0 {
    83  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    84  	}
    85  	if len(input.AccessKeySecret) == 0 {
    86  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    87  	}
    88  	output = cloudprovider.SCloudaccount{
    89  		Account: input.AccessKeyId,
    90  		Secret:  input.AccessKeySecret,
    91  	}
    92  	return output, nil
    93  }
    94  
    95  func parseAccount(account string) (accessKey string, projectId string) {
    96  	segs := strings.Split(account, "/")
    97  	if len(segs) == 2 {
    98  		accessKey = segs[0]
    99  		projectId = segs[1]
   100  	} else {
   101  		accessKey = account
   102  		projectId = ""
   103  	}
   104  	return
   105  }
   106  
   107  func (self *SHcsProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
   108  	accessKey, projectId := parseAccount(cfg.Account)
   109  	client, err := hcs.NewHcsClient(
   110  		hcs.NewHcsConfig(
   111  			accessKey, cfg.Secret, projectId, cfg.URL,
   112  		).CloudproviderConfig(cfg),
   113  	)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	return &SHcsProvider{
   118  		SBaseProvider: cloudprovider.NewBaseProvider(self),
   119  		client:        client,
   120  	}, nil
   121  }
   122  
   123  func (self *SHcsProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
   124  	accessKey, projectId := parseAccount(info.Account)
   125  	return map[string]string{
   126  		"HCS_AUTH_URL":   info.Url,
   127  		"HCS_ACCESS_KEY": accessKey,
   128  		"HCS_SECRET":     info.Secret,
   129  		"HCS_PROJECT_ID": projectId,
   130  	}, nil
   131  }
   132  
   133  func init() {
   134  	factory := SHcsProviderFactory{}
   135  	cloudprovider.RegisterFactory(&factory)
   136  }
   137  
   138  type SHcsProvider struct {
   139  	cloudprovider.SBaseProvider
   140  	client *hcs.SHcsClient
   141  }
   142  
   143  func (self *SHcsProvider) GetVersion() string {
   144  	return ""
   145  }
   146  
   147  func (self *SHcsProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   148  	regions := self.client.GetIRegions()
   149  	info := jsonutils.NewDict()
   150  	info.Add(jsonutils.NewInt(int64(len(regions))), "region_count")
   151  	info.Add(jsonutils.NewString(hcs.HCS_API_VERSION), "api_version")
   152  	return info, nil
   153  }
   154  
   155  func (self *SHcsProvider) GetIRegions() []cloudprovider.ICloudRegion {
   156  	return self.client.GetIRegions()
   157  }
   158  
   159  func (self *SHcsProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
   160  	region, err := self.client.GetRegion(id)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  	return region, nil
   165  }
   166  
   167  func (self *SHcsProvider) GetBalance() (float64, string, error) {
   168  	return 0, api.CLOUD_PROVIDER_HEALTH_NORMAL, nil
   169  }
   170  
   171  func (self *SHcsProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   172  	return self.client.GetSubAccounts()
   173  }
   174  
   175  func (self *SHcsProvider) GetAccountId() string {
   176  	return self.client.GetAccountId()
   177  }
   178  
   179  func (self *SHcsProvider) GetCloudRegionExternalIdPrefix() string {
   180  	return self.client.GetCloudRegionExternalIdPrefix()
   181  }
   182  
   183  func (self *SHcsProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   184  	return []cloudprovider.ICloudProject{}, nil
   185  }
   186  
   187  func (self *SHcsProvider) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
   188  	return nil, cloudprovider.ErrNotSupported
   189  }
   190  
   191  func (self *SHcsProvider) GetStorageClasses(regionId string) []string {
   192  	return []string{
   193  		"STANDARD", "WARM", "COLD",
   194  	}
   195  }
   196  
   197  func (self *SHcsProvider) GetBucketCannedAcls(regionId string) []string {
   198  	return []string{
   199  		string(cloudprovider.ACLPrivate),
   200  		string(cloudprovider.ACLAuthRead),
   201  		string(cloudprovider.ACLPublicRead),
   202  		string(cloudprovider.ACLPublicReadWrite),
   203  	}
   204  }
   205  
   206  func (self *SHcsProvider) GetObjectCannedAcls(regionId string) []string {
   207  	return []string{
   208  		string(cloudprovider.ACLPrivate),
   209  		string(cloudprovider.ACLAuthRead),
   210  		string(cloudprovider.ACLPublicRead),
   211  		string(cloudprovider.ACLPublicReadWrite),
   212  	}
   213  }
   214  
   215  func (self *SHcsProvider) GetCapabilities() []string {
   216  	return self.client.GetCapabilities()
   217  }