yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/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/apsara"
    29  )
    30  
    31  type SApsaraProviderFactory struct {
    32  	cloudprovider.SPrivateCloudBaseProviderFactory
    33  }
    34  
    35  func (self *SApsaraProviderFactory) GetId() string {
    36  	return apsara.CLOUD_PROVIDER_APSARA
    37  }
    38  
    39  func (self *SApsaraProviderFactory) GetName() string {
    40  	return apsara.CLOUD_PROVIDER_APSARA_CN
    41  }
    42  
    43  func (self *SApsaraProviderFactory) IsMultiTenant() bool {
    44  	return true
    45  }
    46  
    47  func (self *SApsaraProviderFactory) IsNeedForceAutoCreateProject() bool {
    48  	return true
    49  }
    50  
    51  func (self *SApsaraProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    52  	output := cloudprovider.SCloudaccount{}
    53  	if len(input.AccessKeyId) == 0 {
    54  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    55  	}
    56  	if len(input.AccessKeySecret) == 0 {
    57  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    58  	}
    59  	output.Account = input.AccessKeyId
    60  	if input.OrganizationId > 0 {
    61  		output.Account = fmt.Sprintf("%s/%d", input.AccessKeyId, input.OrganizationId)
    62  	}
    63  	output.Secret = input.AccessKeySecret
    64  	if len(input.Endpoint) == 0 {
    65  		return output, httperrors.NewMissingParameterError("endpoint")
    66  	}
    67  	output.AccessUrl = input.Endpoint
    68  	if len(input.DefaultRegion) == 0 {
    69  		return output, httperrors.NewMissingParameterError("default_region")
    70  	}
    71  	return output, nil
    72  }
    73  
    74  func (self *SApsaraProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    75  	output := cloudprovider.SCloudaccount{}
    76  	if len(input.AccessKeyId) == 0 {
    77  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_id")
    78  	}
    79  	if len(input.AccessKeySecret) == 0 {
    80  		return output, errors.Wrap(httperrors.ErrMissingParameter, "access_key_secret")
    81  	}
    82  	account := input.AccessKeyId
    83  	if input.OrganizationId > 0 {
    84  		account = fmt.Sprintf("%s/%d", input.AccessKeyId, input.OrganizationId)
    85  	}
    86  	output = cloudprovider.SCloudaccount{
    87  		Account: account,
    88  		Secret:  input.AccessKeySecret,
    89  	}
    90  	return output, nil
    91  }
    92  
    93  func (self *SApsaraProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
    94  	client, err := apsara.NewApsaraClient(
    95  		apsara.NewApsaraClientConfig(
    96  			cfg.Account,
    97  			cfg.Secret,
    98  			cfg.URL,
    99  		).CloudproviderConfig(cfg),
   100  	)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	return &SApsaraProvider{
   105  		SBaseProvider: cloudprovider.NewBaseProvider(self),
   106  		client:        client,
   107  	}, nil
   108  }
   109  
   110  func (self *SApsaraProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
   111  	region := ""
   112  	if info.Options != nil {
   113  		region, _ = info.Options.GetString("default_region")
   114  	}
   115  	return map[string]string{
   116  		"APSARA_ACCESS_KEY": info.Account,
   117  		"APSARA_SECRET":     info.Secret,
   118  		"APSARA_ENDPOINT":   info.Url,
   119  		"APSARA_REGION":     region,
   120  	}, nil
   121  }
   122  
   123  func init() {
   124  	factory := SApsaraProviderFactory{}
   125  	cloudprovider.RegisterFactory(&factory)
   126  }
   127  
   128  type SApsaraProvider struct {
   129  	cloudprovider.SBaseProvider
   130  	client *apsara.SApsaraClient
   131  }
   132  
   133  func (self *SApsaraProvider) WithClient(client *apsara.SApsaraClient) *SApsaraProvider {
   134  	self.client = client
   135  	return self
   136  }
   137  
   138  func (self *SApsaraProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   139  	regions := self.client.GetIRegions()
   140  	info := jsonutils.NewDict()
   141  	info.Add(jsonutils.NewInt(int64(len(regions))), "region_count")
   142  	info.Add(jsonutils.NewString(apsara.APSARA_API_VERSION), "api_version")
   143  	return info, nil
   144  }
   145  
   146  func (self *SApsaraProvider) GetVersion() string {
   147  	return apsara.APSARA_API_VERSION
   148  }
   149  
   150  func (self *SApsaraProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   151  	return self.client.GetSubAccounts()
   152  }
   153  
   154  func (self *SApsaraProvider) GetAccountId() string {
   155  	return self.client.GetAccountId()
   156  }
   157  
   158  func (self *SApsaraProvider) GetIRegions() []cloudprovider.ICloudRegion {
   159  	return self.client.GetIRegions()
   160  }
   161  
   162  func (self *SApsaraProvider) GetIRegionById(extId string) (cloudprovider.ICloudRegion, error) {
   163  	return self.client.GetIRegionById(extId)
   164  }
   165  
   166  func (self *SApsaraProvider) GetBalance() (float64, string, error) {
   167  	return 0, api.CLOUD_PROVIDER_HEALTH_NORMAL, nil
   168  }
   169  
   170  func (self *SApsaraProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   171  	return self.client.GetIProjects()
   172  }
   173  
   174  func (self *SApsaraProvider) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
   175  	return self.client.CreateIProject(name)
   176  }
   177  
   178  func (self *SApsaraProvider) GetStorageClasses(regionId string) []string {
   179  	return []string{
   180  		"Standard", "IA", "Archive",
   181  	}
   182  }
   183  
   184  func (self *SApsaraProvider) GetBucketCannedAcls(regionId string) []string {
   185  	return []string{
   186  		string(cloudprovider.ACLPrivate),
   187  		string(cloudprovider.ACLPublicRead),
   188  		string(cloudprovider.ACLPublicReadWrite),
   189  	}
   190  }
   191  
   192  func (self *SApsaraProvider) GetObjectCannedAcls(regionId string) []string {
   193  	return []string{
   194  		string(cloudprovider.ACLPrivate),
   195  		string(cloudprovider.ACLPublicRead),
   196  		string(cloudprovider.ACLPublicReadWrite),
   197  	}
   198  }
   199  
   200  func (self *SApsaraProvider) GetCapabilities() []string {
   201  	return self.client.GetCapabilities()
   202  }
   203  
   204  func (self *SApsaraProvider) GetIamLoginUrl() string {
   205  	return self.client.GetIamLoginUrl()
   206  }
   207  
   208  func (self *SApsaraProvider) CreateIClouduser(conf *cloudprovider.SClouduserCreateConfig) (cloudprovider.IClouduser, error) {
   209  	return self.client.CreateIClouduser(conf)
   210  }
   211  
   212  func (self *SApsaraProvider) GetICloudusers() ([]cloudprovider.IClouduser, error) {
   213  	return self.client.GetICloudusers()
   214  }
   215  
   216  func (self *SApsaraProvider) GetICloudgroups() ([]cloudprovider.ICloudgroup, error) {
   217  	return self.client.GetICloudgroups()
   218  }
   219  
   220  func (self *SApsaraProvider) GetICloudgroupByName(name string) (cloudprovider.ICloudgroup, error) {
   221  	return self.client.GetICloudgroupByName(name)
   222  }
   223  
   224  func (self *SApsaraProvider) GetIClouduserByName(name string) (cloudprovider.IClouduser, error) {
   225  	return self.client.GetIClouduserByName(name)
   226  }
   227  
   228  func (self *SApsaraProvider) CreateICloudgroup(name, desc string) (cloudprovider.ICloudgroup, error) {
   229  	return self.client.CreateICloudgroup(name, desc)
   230  }
   231  
   232  func (self *SApsaraProvider) GetISystemCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
   233  	return self.client.GetISystemCloudpolicies()
   234  }
   235  
   236  func (self *SApsaraProvider) GetICustomCloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
   237  	return self.client.GetICustomCloudpolicies()
   238  }
   239  
   240  func (self *SApsaraProvider) CreateICloudpolicy(opts *cloudprovider.SCloudpolicyCreateOptions) (cloudprovider.ICloudpolicy, error) {
   241  	return self.client.CreateICloudpolicy(opts)
   242  }
   243  
   244  func (self *SApsaraProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   245  	return self.client.GetMetrics(opts)
   246  }