yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/remotefile/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  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/onecloud/pkg/httperrors"
    26  	"yunion.io/x/onecloud/pkg/mcclient"
    27  	"yunion.io/x/cloudmux/pkg/multicloud/remotefile"
    28  )
    29  
    30  type SRemoteFileProviderFactory struct {
    31  	cloudprovider.SPrivateCloudBaseProviderFactory
    32  }
    33  
    34  func (self *SRemoteFileProviderFactory) GetId() string {
    35  	return remotefile.CLOUD_PROVIDER_REMOTEFILE
    36  }
    37  
    38  func (self *SRemoteFileProviderFactory) GetName() string {
    39  	return remotefile.CLOUD_PROVIDER_REMOTEFILE
    40  }
    41  
    42  func (self *SRemoteFileProviderFactory) ValidateCreateCloudaccountData(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential) (cloudprovider.SCloudaccount, error) {
    43  	output := cloudprovider.SCloudaccount{}
    44  	if len(input.AuthUrl) == 0 {
    45  		return output, errors.Wrap(httperrors.ErrMissingParameter, "auth_url")
    46  	}
    47  	if len(input.Username) == 0 {
    48  		input.Username = "root"
    49  	}
    50  	if len(input.Password) == 0 {
    51  		input.Password = "password"
    52  	}
    53  	output.Account = input.Username
    54  	output.Secret = input.Password
    55  	output.AccessUrl = input.AuthUrl
    56  	return output, nil
    57  }
    58  
    59  func (self *SRemoteFileProviderFactory) ValidateUpdateCloudaccountCredential(ctx context.Context, userCred mcclient.TokenCredential, input cloudprovider.SCloudaccountCredential, cloudaccount string) (cloudprovider.SCloudaccount, error) {
    60  	output := cloudprovider.SCloudaccount{}
    61  	return output, cloudprovider.ErrNotSupported
    62  }
    63  
    64  func (self *SRemoteFileProviderFactory) GetProvider(cfg cloudprovider.ProviderConfig) (cloudprovider.ICloudProvider, error) {
    65  	client, err := remotefile.NewRemoteFileClient(
    66  		remotefile.NewRemoteFileClientConfig(
    67  			cfg.URL,
    68  			cfg.Account,
    69  			cfg.Secret,
    70  		).CloudproviderConfig(cfg),
    71  	)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	return &SRemoteFileProvider{
    76  		SBaseProvider: cloudprovider.NewBaseProvider(self),
    77  		client:        client,
    78  	}, nil
    79  }
    80  
    81  func (self *SRemoteFileProviderFactory) GetClientRC(info cloudprovider.SProviderInfo) (map[string]string, error) {
    82  	return map[string]string{
    83  		"REMOTEFILE_AUTH_URL": info.Url,
    84  	}, nil
    85  }
    86  
    87  func init() {
    88  	factory := SRemoteFileProviderFactory{}
    89  	cloudprovider.RegisterFactory(&factory)
    90  }
    91  
    92  type SRemoteFileProvider struct {
    93  	cloudprovider.SBaseProvider
    94  	client *remotefile.SRemoteFileClient
    95  }
    96  
    97  func (self *SRemoteFileProvider) GetVersion() string {
    98  	return ""
    99  }
   100  
   101  func (self *SRemoteFileProvider) GetSysInfo() (jsonutils.JSONObject, error) {
   102  	return jsonutils.NewDict(), nil
   103  }
   104  
   105  func (self *SRemoteFileProvider) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   106  	return self.client.GetSubAccounts()
   107  }
   108  
   109  func (self *SRemoteFileProvider) GetAccountId() string {
   110  	return ""
   111  }
   112  
   113  func (self *SRemoteFileProvider) GetIRegions() []cloudprovider.ICloudRegion {
   114  	return self.client.GetIRegions()
   115  }
   116  
   117  func (self *SRemoteFileProvider) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
   118  	return self.client.GetIRegionById(id)
   119  }
   120  
   121  func (self *SRemoteFileProvider) GetBalance() (float64, string, error) {
   122  	return 0.0, api.CLOUD_PROVIDER_HEALTH_UNKNOWN, cloudprovider.ErrNotSupported
   123  }
   124  
   125  func (self *SRemoteFileProvider) GetCloudRegionExternalIdPrefix() string {
   126  	return self.client.GetCloudRegionExternalIdPrefix()
   127  }
   128  
   129  func (self *SRemoteFileProvider) GetIProjects() ([]cloudprovider.ICloudProject, error) {
   130  	return self.client.GetIProjects()
   131  }
   132  
   133  func (self *SRemoteFileProvider) CreateIProject(name string) (cloudprovider.ICloudProject, error) {
   134  	return nil, cloudprovider.ErrNotSupported
   135  }
   136  
   137  func (self *SRemoteFileProvider) GetStorageClasses(regionId string) []string {
   138  	return nil
   139  }
   140  
   141  func (self *SRemoteFileProvider) GetBucketCannedAcls(regionId string) []string {
   142  	return nil
   143  }
   144  
   145  func (self *SRemoteFileProvider) GetObjectCannedAcls(regionId string) []string {
   146  	return nil
   147  }
   148  
   149  func (self *SRemoteFileProvider) GetCapabilities() []string {
   150  	return self.client.GetCapabilities()
   151  }
   152  
   153  func (self *SRemoteFileProvider) GetMetrics(opts *cloudprovider.MetricListOptions) ([]cloudprovider.MetricValues, error) {
   154  	return self.client.GetMetrics(opts)
   155  }