yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/objectstore/xsky/xsky.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 xsky
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  	"yunion.io/x/s3cli"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud/objectstore"
    29  	"yunion.io/x/onecloud/pkg/util/httputils"
    30  )
    31  
    32  type SXskyClient struct {
    33  	*objectstore.SObjectStoreClient
    34  
    35  	adminApi *SXskyAdminApi
    36  
    37  	adminUser   *sUser
    38  	initAccount string
    39  }
    40  
    41  func parseAccount(account string) (user string, accessKey string) {
    42  	accountInfo := strings.Split(account, "/")
    43  	user = accountInfo[0]
    44  	if len(accountInfo) > 1 {
    45  		accessKey = strings.Join(accountInfo[1:], "/")
    46  	}
    47  	return
    48  }
    49  
    50  func NewXskyClient(cfg *objectstore.ObjectStoreClientConfig) (*SXskyClient, error) {
    51  	usrname, accessKey := parseAccount(cfg.GetAccessKey())
    52  	adminApi := newXskyAdminApi(
    53  		usrname,
    54  		cfg.GetAccessSecret(),
    55  		cfg.GetEndpoint(),
    56  		cfg.GetDebug(),
    57  	)
    58  	httputils.SetClientProxyFunc(adminApi.httpClient(), cfg.GetCloudproviderConfig().ProxyFunc)
    59  
    60  	gwEp, err := adminApi.getS3GatewayEndpoint(context.Background())
    61  	if err != nil {
    62  		return nil, errors.Wrap(err, "adminApi.getS3GatewayIP")
    63  	}
    64  
    65  	var usr *sUser
    66  	var key *sKey
    67  	if len(accessKey) > 0 {
    68  		usr, key, err = adminApi.findUserByAccessKey(context.Background(), accessKey)
    69  		if err != nil {
    70  			return nil, errors.Wrap(err, "adminApi.findUserByAccessKey")
    71  		}
    72  	} else {
    73  		usr, key, err = adminApi.findFirstUserWithAccessKey(context.Background())
    74  		if err != nil {
    75  			return nil, errors.Wrap(err, "adminApi.findFirstUserWithAccessKey")
    76  		}
    77  	}
    78  
    79  	s3store, err := objectstore.NewObjectStoreClientAndFetch(
    80  		objectstore.NewObjectStoreClientConfig(
    81  			gwEp, accessKey, key.SecretKey,
    82  		).Debug(cfg.GetDebug()).CloudproviderConfig(cfg.GetCloudproviderConfig()),
    83  		false,
    84  	)
    85  	if err != nil {
    86  		return nil, errors.Wrap(err, "NewObjectStoreClient")
    87  	}
    88  
    89  	client := SXskyClient{
    90  		SObjectStoreClient: s3store,
    91  		adminApi:           adminApi,
    92  		adminUser:          usr,
    93  	}
    94  
    95  	if len(accessKey) > 0 {
    96  		client.initAccount = cfg.GetAccessKey()
    97  	}
    98  
    99  	client.SetVirtualObject(&client)
   100  
   101  	err = client.FetchBuckets()
   102  	if err != nil {
   103  		return nil, errors.Wrap(err, "fetchBuckets")
   104  	}
   105  
   106  	return &client, nil
   107  }
   108  
   109  func (cli *SXskyClient) GetSubAccounts() ([]cloudprovider.SSubAccount, error) {
   110  	if len(cli.initAccount) > 0 {
   111  		return []cloudprovider.SSubAccount{
   112  			{
   113  				Account:      cli.initAccount,
   114  				Name:         cli.adminUser.Name,
   115  				HealthStatus: api.CLOUD_PROVIDER_HEALTH_NORMAL,
   116  			},
   117  		}, nil
   118  	}
   119  	usrs, err := cli.adminApi.getUsers(context.Background())
   120  	if err != nil {
   121  		return nil, errors.Wrap(err, "api.getUsers")
   122  	}
   123  	subAccounts := make([]cloudprovider.SSubAccount, 0)
   124  	for i := range usrs {
   125  		ak := usrs[i].getMinKey()
   126  		if len(ak) > 0 {
   127  			subAccount := cloudprovider.SSubAccount{
   128  				Account:      fmt.Sprintf("%s/%s", cli.adminApi.username, ak),
   129  				Name:         usrs[i].Name,
   130  				HealthStatus: api.CLOUD_PROVIDER_HEALTH_NORMAL,
   131  			}
   132  			subAccounts = append(subAccounts, subAccount)
   133  		}
   134  	}
   135  	return subAccounts, nil
   136  }
   137  
   138  func (cli *SXskyClient) GetAccountId() string {
   139  	return cli.adminApi.username
   140  }
   141  
   142  func (cli *SXskyClient) GetVersion() string {
   143  	return ""
   144  }
   145  
   146  func (cli *SXskyClient) About() jsonutils.JSONObject {
   147  	about := jsonutils.NewDict()
   148  	if cli.adminUser != nil {
   149  		about.Add(jsonutils.Marshal(cli.adminUser), "admin_user")
   150  	}
   151  	return about
   152  }
   153  
   154  func (cli *SXskyClient) GetProvider() string {
   155  	return api.CLOUD_PROVIDER_XSKY
   156  }
   157  
   158  func (cli *SXskyClient) NewBucket(bucket s3cli.BucketInfo) cloudprovider.ICloudBucket {
   159  	if cli.SObjectStoreClient == nil {
   160  		return nil
   161  	}
   162  	generalBucket := cli.SObjectStoreClient.NewBucket(bucket)
   163  	return &SXskyBucket{
   164  		SBucket: generalBucket.(*objectstore.SBucket),
   165  		client:  cli,
   166  	}
   167  }