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