yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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/huawei"
    29  )
    30  
    31  type SHuaweiProviderFactory struct {
    32  	cloudprovider.SPublicCloudBaseProviderFactory
    33  }
    34  
    35  func (self *SHuaweiProviderFactory) GetId() string {
    36  	return huawei.CLOUD_PROVIDER_HUAWEI
    37  }
    38  
    39  func (self *SHuaweiProviderFactory) GetName() string {
    40  	return huawei.CLOUD_PROVIDER_HUAWEI_CN
    41  }
    42  
    43  func (self *SHuaweiProviderFactory) IsCloudeventRegional() bool {
    44  	return true
    45  }
    46  
    47  func (self *SHuaweiProviderFactory) GetMaxCloudEventSyncDays() int {
    48  	return 7
    49  }
    50  
    51  func (self *SHuaweiProviderFactory) GetMaxCloudEventKeepDays() int {
    52  	return 7
    53  }
    54  
    55  func (self *SHuaweiProviderFactory) IsSupportCloudIdService() bool {
    56  	return true
    57  }
    58  
    59  func (self *SHuaweiProviderFactory) IsSupportClouduserPolicy() bool {
    60  	return false
    61  }
    62  
    63  func (self *SHuaweiProviderFactory) IsSupportCreateCloudgroup() bool {
    64  	return true
    65  }
    66  
    67  func (factory *SHuaweiProviderFactory) IsSupportCrossCloudEnvVpcPeering() bool {
    68  	return false
    69  }
    70  
    71  func (factory *SHuaweiProviderFactory) IsSupportCrossRegionVpcPeering() bool {
    72  	return false
    73  }
    74  
    75  func (factory *SHuaweiProviderFactory) IsSupportVpcPeeringVpcCidrOverlap() bool {
    76  	return true
    77  }
    78  
    79  func (factory *SHuaweiProviderFactory) IsSupportModifyRouteTable() bool {
    80  	return true
    81  }
    82  
    83  func (factory *SHuaweiProviderFactory) IsSupportSAMLAuth() bool {
    84  	return true
    85  }
    86  
    87  func (self *SHuaweiProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    88  	output := cloudprovider.SCloudaccount{}
    89  	if len(input.AccessKeyId) == 0 {
    90  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    91  	}
    92  	if len(input.AccessKeySecret) == 0 {
    93  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    94  	}
    95  	if len(input.Environment) == 0 {
    96  		return output, errors.Wrap(httperrors.ErrMissingParameter, "environment")
    97  	}
    98  
    99  	output.Account = input.AccessKeyId
   100  	output.Secret = input.AccessKeySecret
   101  	output.AccessUrl = input.Environment
   102  
   103  	return output, nil
   104  }
   105  
   106  func (self *SHuaweiProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
   107  	output := cloudprovider.SCloudaccount{}
   108  	if len(input.AccessKeyId) == 0 {
   109  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
   110  	}
   111  	if len(input.AccessKeySecret) == 0 {
   112  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
   113  	}
   114  	output = cloudprovider.SCloudaccount{
   115  		Account: input.AccessKeyId,
   116  		Secret:  input.AccessKeySecret,
   117  	}
   118  	return output, nil
   119  }
   120  
   121  func parseAccount(account string) (accessKey string, projectId string) {
   122  	segs := strings.Split(account, "/")
   123  	if len(segs) == 2 {
   124  		accessKey = segs[0]
   125  		projectId = segs[1]
   126  	} else {
   127  		accessKey = account
   128  		projectId = ""
   129  	}
   130  
   131  	return
   132  }
   133  
   134  func (self *SHuaweiProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
   135  	accessKey, projectId := parseAccount(cfg.Account)
   136  	client, err := huawei.NewHuaweiClient(
   137  		huawei.NewHuaweiClientConfig(
   138  			cfg.URL, accessKey, cfg.Secret, projectId,
   139  		).CloudproviderConfig(cfg),
   140  	)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	return &SHuaweiProvider{
   145  		SBaseProvider: cloudprovider.NewBaseProvider(self),
   146  		client:        client,
   147  	}, nil
   148  }
   149  
   150  func (self *SHuaweiProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
   151  	accessKey, projectId := parseAccount(info.Account)
   152  	region := huawei.HUAWEI_DEFAULT_REGION
   153  	data := strings.Split(info.Name, "-")
   154  	if len(data) >= 3 {
   155  		region = strings.Join(data[len(data)-3:], "-")
   156  	}
   157  	return map[string]string{
   158  		"HUAWEI_CLOUD_ENV":  info.Url,
   159  		"HUAWEI_ACCESS_KEY": accessKey,
   160  		"HUAWEI_SECRET":     info.Secret,
   161  		"HUAWEI_REGION":     region,
   162  		"HUAWEI_PROJECT":    projectId,
   163  	}, nil
   164  }
   165  
   166  func init() {
   167  	factory := SHuaweiProviderFactory{}
   168  	cloudprovider.RegisterFactory(&factory)
   169  }
   170  
   171  type SHuaweiProvider struct {
   172  	cloudprovider.SBaseProvider
   173  	client *huawei.SHuaweiClient
   174  }
   175  
   176  func (self *SHuaweiProvider) GetVersion() string {
   177  	return self.client.GetVersion()
   178  }
   179  
   180  func (self *SHuaweiProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   181  	regions := self.client.GetIRegions()
   182  	info := jsonutils.NewDict()
   183  	info.Add(jsonutils.NewInt(int64(len(regions))), "region_count")
   184  	info.Add(jsonutils.NewString(huawei.HUAWEI_API_VERSION), "api_version")
   185  	return info, nil
   186  }
   187  
   188  func (self *SHuaweiProvider) GetIRegions() []cloudprovider.ICloudRegion {
   189  	return self.client.GetIRegions()
   190  }
   191  
   192  func (self *SHuaweiProvider) GetIRegionById(extId string) (cloudprovider.ICloudRegion, error) {
   193  	return self.client.GetIRegionById(extId)
   194  }
   195  
   196  func (self *SHuaweiProvider) GetBalance() (float64, string, error) {
   197  	balance, err := self.client.QueryAccountBalance()
   198  	if err != nil {
   199  		return 0.0, api.CLOUD_PROVIDER_HEALTH_UNKNOWN, err
   200  	}
   201  	status := api.CLOUD_PROVIDER_HEALTH_NORMAL
   202  	if balance.AvailableAmount < 0.0 && balance.CreditAmount < 0.0 {
   203  		status = api.CLOUD_PROVIDER_HEALTH_ARREARS
   204  	}
   205  	return balance.AvailableAmount, status, nil
   206  }
   207  
   208  func (self *SHuaweiProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   209  	return self.client.GetSubAccounts()
   210  }
   211  
   212  func (self *SHuaweiProvider) GetAccountId() string {
   213  	return self.client.GetAccountId()
   214  }
   215  
   216  func (self *SHuaweiProvider) GetIamLoginUrl() string {
   217  	return self.client.GetIamLoginUrl()
   218  }
   219  
   220  func (self *SHuaweiProvider) GetCloudRegionExternalIdPrefix() string {
   221  	return self.client.GetCloudRegionExternalIdPrefix()
   222  }
   223  
   224  func (self *SHuaweiProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   225  	return self.client.GetIProjects()
   226  }
   227  
   228  func (self *SHuaweiProvider) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
   229  	return self.client.CreateIProject(name)
   230  }
   231  
   232  func (self *SHuaweiProvider) GetStorageClasses(regionId string) []string {
   233  	return []string{
   234  		"STANDARD", "WARM", "COLD",
   235  	}
   236  }
   237  
   238  func (self *SHuaweiProvider) GetBucketCannedAcls(regionId string) []string {
   239  	return []string{
   240  		string(cloudprovider.ACLPrivate),
   241  		string(cloudprovider.ACLAuthRead),
   242  		string(cloudprovider.ACLPublicRead),
   243  		string(cloudprovider.ACLPublicReadWrite),
   244  	}
   245  }
   246  
   247  func (self *SHuaweiProvider) GetObjectCannedAcls(regionId string) []string {
   248  	return []string{
   249  		string(cloudprovider.ACLPrivate),
   250  		string(cloudprovider.ACLAuthRead),
   251  		string(cloudprovider.ACLPublicRead),
   252  		string(cloudprovider.ACLPublicReadWrite),
   253  	}
   254  }
   255  
   256  func (self *SHuaweiProvider) GetCapabilities() []string {
   257  	return self.client.GetCapabilities()
   258  }
   259  
   260  func (self *SHuaweiProvider) CreateIClouduser(conf *cloudprovider.SClouduserCreateConfig) (cloudprovider.IClouduser, error) {
   261  	return self.client.CreateIClouduser(conf)
   262  }
   263  
   264  func (self *SHuaweiProvider) GetICloudusers() ([]cloudprovider.IClouduser, error) {
   265  	return self.client.GetICloudusers()
   266  }
   267  
   268  func (self *SHuaweiProvider) GetICloudgroups() ([]cloudprovider.ICloudgroup, error) {
   269  	return self.client.GetICloudgroups()
   270  }
   271  
   272  func (self *SHuaweiProvider) GetICloudgroupByName(name string) (cloudprovider.ICloudgroup, error) {
   273  	return self.client.GetICloudgroupByName(name)
   274  }
   275  
   276  func (self *SHuaweiProvider) CreateICloudgroup(name, desc string) (cloudprovider.ICloudgroup, error) {
   277  	return self.client.CreateICloudgroup(name, desc)
   278  }
   279  
   280  func (self *SHuaweiProvider) GetISystemCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
   281  	return self.client.GetISystemCloudpolicies()
   282  }
   283  
   284  func (self *SHuaweiProvider) GetICustomCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
   285  	return []cloudprovider.ICloudpolicy{}, nil
   286  }
   287  
   288  func (self *SHuaweiProvider) GetIClouduserByName(name string) (cloudprovider.IClouduser, error) {
   289  	return self.client.GetIClouduserByName(name)
   290  }
   291  
   292  func (self *SHuaweiProvider) GetSamlEntityId() string {
   293  	return cloudprovider.SAML_ENTITY_ID_HUAWEI_CLOUD
   294  }
   295  
   296  func (self *SHuaweiProvider) GetICloudSAMLProviders() ([]cloudprovider.ICloudSAMLProvider, error) {
   297  	return self.client.GetICloudSAMLProviders()
   298  }
   299  
   300  func (self *SHuaweiProvider) CreateICloudSAMLProvider(opts *cloudprovider.SAMLProviderCreateOptions) (cloudprovider.ICloudSAMLProvider, error) {
   301  	sp, err := self.client.CreateSAMLProvider(opts)
   302  	if err != nil {
   303  		return nil, errors.Wrapf(err, "CreateSAMLProvider")
   304  	}
   305  	return sp, nil
   306  }
   307  
   308  func (self *SHuaweiProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   309  	metrics, err := self.client.GetMetrics(opts)
   310  	if err != nil {
   311  		return nil, errors.Wrapf(err, "GetMetrics")
   312  	}
   313  	return metrics, nil
   314  }