yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/objectstore/ceph/ceph.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 ceph
    16  
    17  import (
    18  	"context"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  	"yunion.io/x/s3cli"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/onecloud/pkg/httperrors"
    28  	"yunion.io/x/cloudmux/pkg/multicloud/objectstore"
    29  	"yunion.io/x/onecloud/pkg/util/httputils"
    30  )
    31  
    32  type SCephRadosClient struct {
    33  	*objectstore.SObjectStoreClient
    34  
    35  	adminApi *SCephAdminApi
    36  
    37  	userQuota   *SQuota
    38  	bucketQuota *SQuota
    39  	userInfo    *SUserInfo
    40  }
    41  
    42  func NewCephRados(cfg *objectstore.ObjectStoreClientConfig) (*SCephRadosClient, error) {
    43  	s3store, err := objectstore.NewObjectStoreClientAndFetch(cfg, false)
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "NewObjectStoreClient")
    46  	}
    47  	adminApi := newCephAdminApi(
    48  		cfg.GetAccessKey(),
    49  		cfg.GetAccessSecret(),
    50  		cfg.GetEndpoint(),
    51  		cfg.GetDebug(),
    52  		"",
    53  	)
    54  	httputils.SetClientProxyFunc(adminApi.httpClient(), cfg.GetCloudproviderConfig().ProxyFunc)
    55  
    56  	client := SCephRadosClient{
    57  		SObjectStoreClient: s3store,
    58  		adminApi:           adminApi,
    59  	}
    60  
    61  	client.SetVirtualObject(&client)
    62  
    63  	err = client.FetchBuckets()
    64  	if err != nil {
    65  		return nil, errors.Wrap(err, "fetchBuckets")
    66  	}
    67  
    68  	userQuota, bucketQuota, err := adminApi.GetUserQuota(context.Background(), s3store.GetAccountId())
    69  	if err != nil {
    70  		if errors.Cause(err) != httperrors.ErrForbidden {
    71  			return nil, errors.Wrap(err, "adminApi.GetUserQuota")
    72  		} else {
    73  			// skip the error
    74  			log.Errorf("adminApi.GetUserQuota fail: %s", err)
    75  		}
    76  	}
    77  	userInfo, err := adminApi.GetUserInfo(context.Background(), s3store.GetAccountId())
    78  	if err != nil {
    79  		if errors.Cause(err) != httperrors.ErrForbidden {
    80  			return nil, errors.Wrap(err, "adminApi.GetUserInfo")
    81  		} else {
    82  			// skip the error
    83  			log.Errorf("adminApi.GetUserInfo fail: %s", err)
    84  		}
    85  	}
    86  	if cfg.GetDebug() {
    87  		log.Debugf("%#v %#v %#v", userQuota, bucketQuota, userInfo)
    88  	}
    89  	client.userQuota = userQuota
    90  	client.bucketQuota = bucketQuota
    91  	client.userInfo = userInfo
    92  
    93  	return &client, nil
    94  }
    95  
    96  func (cli *SCephRadosClient) GetVersion() string {
    97  	return ""
    98  }
    99  
   100  func (cli *SCephRadosClient) About() jsonutils.JSONObject {
   101  	about := jsonutils.NewDict()
   102  	if cli.userQuota != nil {
   103  		about.Add(jsonutils.Marshal(cli.userQuota), "user_quota")
   104  	}
   105  	if cli.bucketQuota != nil {
   106  		about.Add(jsonutils.Marshal(cli.bucketQuota), "bucket_quota")
   107  	}
   108  	if cli.userInfo != nil {
   109  		about.Add(jsonutils.Marshal(cli.userInfo), "user_info")
   110  	}
   111  	return about
   112  }
   113  
   114  func (cli *SCephRadosClient) GetProvider() string {
   115  	return api.CLOUD_PROVIDER_CEPH
   116  }
   117  
   118  func (cli *SCephRadosClient) NewBucket(bucket s3cli.BucketInfo) cloudprovider.ICloudBucket {
   119  	generalBucket := cli.SObjectStoreClient.NewBucket(bucket)
   120  	return &SCephRadosBucket{
   121  		SBucket: generalBucket.(*objectstore.SBucket),
   122  	}
   123  }