From 5d9d253328715ffdb7951f381483413c7eaca687 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Mon, 7 Nov 2022 02:19:24 +0800 Subject: [PATCH] fix: mcclient code cleanup 20221106 (#15310) Co-authored-by: Qiu Jian --- cmd/climc/shell/misc/pprof.go | 2 +- go.mod | 2 +- go.sum | 4 +- pkg/apigateway/handler/backendproxy.go | 2 +- pkg/apigateway/handler/proxy.go | 2 +- pkg/baremetal/cronjobs.go | 2 +- pkg/baremetal/manager.go | 2 +- pkg/cloudcommon/agent/agent.go | 2 + pkg/cloudcommon/consts/consts.go | 10 + pkg/cloudcommon/syncman/watcher/watcher.go | 5 +- pkg/cloudmon/misc/alerts.go | 2 +- pkg/cloudmon/misc/pinger.go | 2 +- pkg/cloudmon/misc/usageservice.go | 2 +- pkg/cloudmon/providerdriver/base.go | 104 +--- pkg/cloudmon/providerdriver/huawei.go | 12 +- pkg/cloudmon/providerdriver/huaweistack.go | 12 +- pkg/cloudmon/providerdriver/qcloud.go | 27 +- pkg/cloudmon/resources/resources.go | 2 +- pkg/compute/guestdrivers/base.go | 2 +- pkg/compute/hostdrivers/managedvirtual.go | 7 +- .../models/loadbalanceragents_deploy.go | 5 +- pkg/devtool/models/actions.go | 3 +- pkg/hostman/hostinfo/hostinfo.go | 17 +- pkg/hostman/hostutils/hostutils.go | 9 +- pkg/hostman/storageman/storage_agent.go | 4 +- pkg/hostman/storageman/storage_local.go | 4 +- pkg/hostman/storageman/storage_rbd.go | 4 +- pkg/mcclient/auth/auth.go | 48 +- pkg/mcclient/catalog.go | 12 +- pkg/mcclient/mcclient.go | 2 +- pkg/mcclient/modulebase/appoptions.go | 2 +- pkg/mcclient/modulebase/base.go | 16 +- pkg/mcclient/modulebase/misc.go | 4 +- pkg/mcclient/modulebase/modules.go | 6 +- pkg/mcclient/modulebase/stats.go | 2 +- pkg/mcclient/modulebase/utils.go | 4 +- pkg/mcclient/modules/cloudnet/common.go | 2 +- pkg/mcclient/modules/cloudproxy/common.go | 2 +- pkg/mcclient/modules/compute/mod_metadatas.go | 4 +- pkg/mcclient/modules/compute/mod_skus.go | 2 +- pkg/mcclient/modules/compute/mod_tasks.go | 4 +- pkg/mcclient/modules/k8s/base.go | 2 +- pkg/mcclient/modules/managers.go | 36 +- .../modules/webconsole/mod_commandlog.go | 2 +- .../modules/webconsole/mod_webconsole.go | 4 +- pkg/mcclient/session.go | 50 +- pkg/mcclient/token2.go | 14 +- pkg/mcclient/token3.go | 14 +- pkg/mcclient/tokensimple.go | 4 +- pkg/monitor/models/datasource.go | 2 +- vendor/modules.txt | 3 +- .../x/cloudmux/pkg/cloudprovider/resources.go | 5 +- .../multicloud/aliyun/provider/provider.go | 3 +- .../pkg/multicloud/aliyun/storagecache.go | 25 +- .../pkg/multicloud/apsara/storagecache.go | 13 +- .../pkg/multicloud/aws/storagecache.go | 13 +- .../pkg/multicloud/azure/storagecache.go | 13 +- .../pkg/multicloud/bingocloud/storagecache.go | 5 +- .../pkg/multicloud/cloudpods/image.go | 3 +- .../pkg/multicloud/cloudpods/storagecache.go | 7 +- .../pkg/multicloud/ctyun/storagecache.go | 5 +- .../pkg/multicloud/ecloud/storagecache.go | 5 +- .../pkg/multicloud/esxi/storagecache.go | 5 +- .../pkg/multicloud/google/storagecache.go | 9 +- .../pkg/multicloud/hcs/storagecache.go | 13 +- .../pkg/multicloud/hcso/storagecache.go | 13 +- .../pkg/multicloud/huawei/storagecache.go | 13 +- .../multicloud/incloudsphere/storagecache.go | 5 +- .../pkg/multicloud/jdcloud/storagecache.go | 5 +- .../pkg/multicloud/nutanix/storagecache.go | 5 +- .../pkg/multicloud/openstack/openstack.go | 27 +- .../pkg/multicloud/openstack/oscli/catalog.go | 36 ++ .../pkg/multicloud/openstack/oscli/input.go | 131 +++++ .../pkg/multicloud/openstack/oscli/oscli.go | 328 ++++++++++++ .../pkg/multicloud/openstack/oscli/session.go | 329 ++++++++++++ .../pkg/multicloud/openstack/oscli/token.go | 96 ++++ .../pkg/multicloud/openstack/oscli/token2.go | 382 ++++++++++++++ .../pkg/multicloud/openstack/oscli/token3.go | 473 ++++++++++++++++++ .../multicloud/openstack/oscli/tokensimple.go | 229 +++++++++ .../pkg/multicloud/openstack/project.go | 9 +- .../pkg/multicloud/openstack/storagecache.go | 9 +- .../pkg/multicloud/proxmox/storagecache.go | 5 +- .../pkg/multicloud/qcloud/storagecache.go | 9 +- .../pkg/multicloud/ucloud/storagecache.go | 9 +- .../pkg/multicloud/zstack/storagecache.go | 9 +- 85 files changed, 2323 insertions(+), 415 deletions(-) create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/catalog.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/input.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/oscli.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/session.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token2.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token3.go create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/tokensimple.go diff --git a/cmd/climc/shell/misc/pprof.go b/cmd/climc/shell/misc/pprof.go index 6857d8d03e..f89e64d49d 100644 --- a/cmd/climc/shell/misc/pprof.go +++ b/cmd/climc/shell/misc/pprof.go @@ -58,7 +58,7 @@ func init() { svcUrl string ) if len(opts.Service) > 0 { - svcUrl, err = s.GetServiceURL(opts.Service, "", "") + svcUrl, err = s.GetServiceURL(opts.Service, "") if err != nil { return errors.Wrapf(err, "get service %s url", opts.Service) } diff --git a/go.mod b/go.mod index 8e8751a990..5c4e8d67df 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,7 @@ require ( k8s.io/client-go v0.19.3 k8s.io/cluster-bootstrap v0.19.3 moul.io/http2curl/v2 v2.3.0 - yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221104111004-66eebdd3aa2f + yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221106172354-3354ac00fde8 yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 yunion.io/x/jsonutils v1.0.1-0.20220819091305-3bab322ab4fd yunion.io/x/log v1.0.0 diff --git a/go.sum b/go.sum index c8ca115b74..121ebccafc 100644 --- a/go.sum +++ b/go.sum @@ -1162,8 +1162,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q= sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= -yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221104111004-66eebdd3aa2f h1:k9lRSQE5M8W3Ua1Z5vYim2wV64No5rvVW4QrGie2Ros= -yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221104111004-66eebdd3aa2f/go.mod h1:drc/WDD65r6wI1tSejk19xcl9QwmzZlgJGXN7a9o2KQ= +yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221106172354-3354ac00fde8 h1:2QYq5STsNw36PnAgle1z3Ibg1ydLTaDuHBNRJJ+9AgU= +yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221106172354-3354ac00fde8/go.mod h1:drc/WDD65r6wI1tSejk19xcl9QwmzZlgJGXN7a9o2KQ= yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656 h1:0zlZD5uhZoIHgLVAWCz2aHaYk2ZrNsACCYD7R6EIBII= yunion.io/x/executor v0.0.0-20211018100936-39a2cd966656/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws= yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634= diff --git a/pkg/apigateway/handler/backendproxy.go b/pkg/apigateway/handler/backendproxy.go index ebd535ef08..d50f115538 100644 --- a/pkg/apigateway/handler/backendproxy.go +++ b/pkg/apigateway/handler/backendproxy.go @@ -147,7 +147,7 @@ func (h *SBackendServiceProxyHandler) fetchReverseEndpoint() *proxy.SEndpointFac if len(zone) > 0 { session.SetZone(zone) } - ep, err := session.GetServiceURL(serviceName, endpointType, "") + ep, err := session.GetServiceURL(serviceName, endpointType) if err != nil { return "", httperrors.NewBadRequestError("invalid service %s: %s", serviceName, err) } diff --git a/pkg/apigateway/handler/proxy.go b/pkg/apigateway/handler/proxy.go index 9e0c62aa4f..e5a4553f13 100644 --- a/pkg/apigateway/handler/proxy.go +++ b/pkg/apigateway/handler/proxy.go @@ -69,7 +69,7 @@ func fetchReverseEndpoint(serviceName string) *proxy.SEndpointFactory { f := func(ctx context.Context, r *http.Request) (string, error) { endpointType := "internalURL" session := auth.GetAdminSession(ctx, FetchRegion(r)) - ep, err := session.GetServiceURL(serviceName, endpointType, "") + ep, err := session.GetServiceURL(serviceName, endpointType) if err != nil { return "", err } diff --git a/pkg/baremetal/cronjobs.go b/pkg/baremetal/cronjobs.go index c6381579b1..9699b33cc2 100644 --- a/pkg/baremetal/cronjobs.go +++ b/pkg/baremetal/cronjobs.go @@ -217,7 +217,7 @@ func (job *SSendMetricsJob) Name() string { func (job *SSendMetricsJob) Do(ctx context.Context, now time.Time) error { s := auth.GetAdminSession(ctx, consts.GetRegion()) - urls, err := s.GetServiceURLs("influxdb", o.Options.SessionEndpointType, "") + urls, err := s.GetServiceURLs("influxdb", o.Options.SessionEndpointType) if err != nil { return errors.Wrap(err, "s.GetServiceURLs") } diff --git a/pkg/baremetal/manager.go b/pkg/baremetal/manager.go index ae695e41bc..3c9bdc6f1a 100644 --- a/pkg/baremetal/manager.go +++ b/pkg/baremetal/manager.go @@ -1073,7 +1073,7 @@ func (b *SBaremetalInstance) getTftpFileUrl(filename string) string { func (b *SBaremetalInstance) GetImageUrl(disableImageCache bool) string { if disableImageCache { - url, err := b.GetPublicClientSession().GetServiceURL(apis.SERVICE_TYPE_IMAGE, apiidenty.EndpointInterfacePublic, "") + url, err := b.GetPublicClientSession().GetServiceURL(apis.SERVICE_TYPE_IMAGE, apiidenty.EndpointInterfacePublic) if err != nil { log.Errorf("Get image public url: %v", err) return "" diff --git a/pkg/cloudcommon/agent/agent.go b/pkg/cloudcommon/agent/agent.go index 8d6b47e556..190e0798a3 100644 --- a/pkg/cloudcommon/agent/agent.go +++ b/pkg/cloudcommon/agent/agent.go @@ -26,6 +26,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/agent/iagent" + "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/object" "yunion.io/x/onecloud/pkg/hostman/storageman" "yunion.io/x/onecloud/pkg/mcclient" @@ -191,6 +192,7 @@ func (agent *SBaseAgent) fetchZone(session *mcclient.ClientSession) error { return err } agent.Zone = &zone + consts.SetZone(zone.Name) return nil } diff --git a/pkg/cloudcommon/consts/consts.go b/pkg/cloudcommon/consts/consts.go index d6d4c97c12..a9692d5147 100644 --- a/pkg/cloudcommon/consts/consts.go +++ b/pkg/cloudcommon/consts/consts.go @@ -25,6 +25,8 @@ var ( globalRegion = "" + globalZone = "" + globalServiceType = "" tenantCacheExpireSeconds = 900 @@ -53,6 +55,14 @@ func GetRegion() string { return globalRegion } +func SetZone(zone string) { + globalZone = zone +} + +func GetZone() string { + return globalZone +} + func SetDataResp(enable bool) { enableDataResp = enable } diff --git a/pkg/cloudcommon/syncman/watcher/watcher.go b/pkg/cloudcommon/syncman/watcher/watcher.go index 3103ad2629..9ecdf8f488 100644 --- a/pkg/cloudcommon/syncman/watcher/watcher.go +++ b/pkg/cloudcommon/syncman/watcher/watcher.go @@ -22,6 +22,7 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/onecloud/pkg/apis" + identity_api "yunion.io/x/onecloud/pkg/apis/identity" "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/syncman" "yunion.io/x/onecloud/pkg/mcclient" @@ -60,9 +61,9 @@ func (manager *SInformerSyncManager) OnServiceCatalogChange(catalog mcclient.ISe if manager.done { return } - url, _ := catalog.GetServiceURL(apis.SERVICE_TYPE_ETCD, consts.GetRegion(), "", "internal") + url, _ := mcclient.CatalogGetServiceURL(catalog, apis.SERVICE_TYPE_ETCD, consts.GetRegion(), "", identity_api.EndpointInterfaceInternal) if len(url) == 0 { - log.Debugf("[%s] OnServiceCatalogChange: no etcd url found, retry", manager.Name()) + log.Debugf("[%s] OnServiceCatalogChange: no etcd internal url found, retry", manager.Name()) return } err := manager.startWatcher() diff --git a/pkg/cloudmon/misc/alerts.go b/pkg/cloudmon/misc/alerts.go index dd8a34e684..de70f99edc 100644 --- a/pkg/cloudmon/misc/alerts.go +++ b/pkg/cloudmon/misc/alerts.go @@ -101,7 +101,7 @@ func AlertHistoryReport(ctx context.Context, userCred mcclient.TokenCredential, } metrics = append(metrics, metric) } - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") + urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType) if err != nil { return errors.Wrap(err, "GetServiceURLs") } diff --git a/pkg/cloudmon/misc/pinger.go b/pkg/cloudmon/misc/pinger.go index beb8d8650d..0375319c02 100644 --- a/pkg/cloudmon/misc/pinger.go +++ b/pkg/cloudmon/misc/pinger.go @@ -78,7 +78,7 @@ func PingProbe(ctx context.Context, userCred mcclient.TokenCredential, isStart b } metrics = append(metrics, m...) } - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") + urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType) if err != nil { return errors.Wrap(err, "GetServiceURLs") } diff --git a/pkg/cloudmon/misc/usageservice.go b/pkg/cloudmon/misc/usageservice.go index dde1da9f9b..196655e811 100644 --- a/pkg/cloudmon/misc/usageservice.go +++ b/pkg/cloudmon/misc/usageservice.go @@ -90,7 +90,7 @@ func UsegReport(ctx context.Context, userCred mcclient.TokenCredential, isStart } dataList = append(dataList, data...) //写入influDb - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") + urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType) if err != nil { return errors.Wrap(err, "GetServiceURLs") } diff --git a/pkg/cloudmon/providerdriver/base.go b/pkg/cloudmon/providerdriver/base.go index 51fb2569bb..c9845310eb 100644 --- a/pkg/cloudmon/providerdriver/base.go +++ b/pkg/cloudmon/providerdriver/base.go @@ -81,12 +81,16 @@ func (self *SBaseCollectDriver) CollectStorageMetrics(ctx context.Context, manag } metrics = append(metrics, metric) } + return self.sendMetrics(ctx, manager, "storage", len(res), metrics) +} + +func (self *SBaseCollectDriver) sendMetrics(ctx context.Context, manager api.CloudproviderDetails, resName string, resCnt int, metrics []influxdb.SMetricData) error { s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") + urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType) if err != nil { return errors.Wrap(err, "GetServiceURLs") } - log.Infof("send %d storage with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) + log.Infof("send %d %s with %d metrics for %s(%s)", resCnt, resName, len(metrics), manager.Name, manager.Id) return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) } @@ -179,13 +183,7 @@ func (self *SCollectByResourceIdDriver) CollectDBInstanceMetrics(ctx context.Con } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d rds with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "rds", len(res), metrics) } func (self *SCollectByResourceIdDriver) CollectServerMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ServerDetails, start, end time.Time) error { @@ -264,13 +262,7 @@ func (self *SCollectByResourceIdDriver) CollectServerMetrics(ctx context.Context } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d server with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "server", len(res), metrics) } func (self *SCollectByResourceIdDriver) CollectHostMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.HostDetails, start, end time.Time) error { @@ -347,13 +339,7 @@ func (self *SCollectByResourceIdDriver) CollectHostMetrics(ctx context.Context, } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d host with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "host", len(res), metrics) } func (self *SCollectByResourceIdDriver) CollectRedisMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ElasticcacheDetails, start, end time.Time) error { @@ -430,13 +416,7 @@ func (self *SCollectByResourceIdDriver) CollectRedisMetrics(ctx context.Context, } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d redis with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "redis", len(res), metrics) } func (self *SCollectByResourceIdDriver) CollectBucketMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.BucketDetails, start, end time.Time) error { @@ -512,20 +492,13 @@ func (self *SCollectByResourceIdDriver) CollectBucketMetrics(ctx context.Context } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d bucket with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "bucket", len(res), metrics) } func (self *SCollectByResourceIdDriver) CollectK8sMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.KubeClusterDetails, start, end time.Time) error { ch := make(chan struct{}, options.Options.CloudResourceCollectMetricsBatchCount) defer close(ch) metrics := []influxdb.SMetricData{} - s := auth.GetAdminSession(ctx, options.Options.Region) var wg sync.WaitGroup var mu sync.Mutex for i := range res { @@ -599,12 +572,7 @@ func (self *SCollectByResourceIdDriver) CollectK8sMetrics(ctx context.Context, m } wg.Wait() - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d k8s with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "k8s", len(res), metrics) } type SCollectByMetricTypeDriver struct { @@ -675,13 +643,7 @@ func (self *SCollectByMetricTypeDriver) CollectDBInstanceMetrics(ctx context.Con } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d rds with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "rds", len(res), metrics) } func (self *SCollectByMetricTypeDriver) CollectServerMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ServerDetails, start, end time.Time) error { @@ -759,13 +721,7 @@ func (self *SCollectByMetricTypeDriver) CollectServerMetrics(ctx context.Context } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d server with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "server", len(res), metrics) } func (self *SCollectByMetricTypeDriver) CollectHostMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.HostDetails, start, end time.Time) error { @@ -842,13 +798,7 @@ func (self *SCollectByMetricTypeDriver) CollectHostMetrics(ctx context.Context, } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d host with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "host", len(res), metrics) } func (self *SCollectByMetricTypeDriver) CollectRedisMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ElasticcacheDetails, start, end time.Time) error { @@ -924,13 +874,7 @@ func (self *SCollectByMetricTypeDriver) CollectRedisMetrics(ctx context.Context, } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d redis with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "redis", len(res), metrics) } func (self *SCollectByMetricTypeDriver) CollectBucketMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.BucketDetails, start, end time.Time) error { @@ -1006,13 +950,7 @@ func (self *SCollectByMetricTypeDriver) CollectBucketMetrics(ctx context.Context } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d bucket with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "bucket", len(res), metrics) } func (self *SCollectByMetricTypeDriver) CollectK8sMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.KubeClusterDetails, start, end time.Time) error { @@ -1080,11 +1018,5 @@ func (self *SCollectByMetricTypeDriver) CollectK8sMetrics(ctx context.Context, m } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d k8s with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "k8s", len(res), metrics) } diff --git a/pkg/cloudmon/providerdriver/huawei.go b/pkg/cloudmon/providerdriver/huawei.go index 14411448db..3f7af90ab4 100644 --- a/pkg/cloudmon/providerdriver/huawei.go +++ b/pkg/cloudmon/providerdriver/huawei.go @@ -24,10 +24,7 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" - "yunion.io/x/onecloud/pkg/apis" api "yunion.io/x/onecloud/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/cloudmon/options" - "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/util/influxdb" ) @@ -133,11 +130,6 @@ func (self *HuaweiCollect) CollectModelartsPoolMetrics(ctx context.Context, mana }(res[i]) } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d modelarts_pool with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + + return self.sendMetrics(ctx, manager, "modelarts_pool", len(res), metrics) } diff --git a/pkg/cloudmon/providerdriver/huaweistack.go b/pkg/cloudmon/providerdriver/huaweistack.go index 0315446ea9..917b1c0d02 100644 --- a/pkg/cloudmon/providerdriver/huaweistack.go +++ b/pkg/cloudmon/providerdriver/huaweistack.go @@ -24,10 +24,7 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" - "yunion.io/x/onecloud/pkg/apis" api "yunion.io/x/onecloud/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/cloudmon/options" - "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/util/influxdb" ) @@ -107,11 +104,6 @@ func (self *HuaweiStackCollect) CollectModelartsPoolMetrics(ctx context.Context, }(res[i]) } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d modelarts_pool with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + + return self.sendMetrics(ctx, manager, "modelarts_pool", len(res), metrics) } diff --git a/pkg/cloudmon/providerdriver/qcloud.go b/pkg/cloudmon/providerdriver/qcloud.go index 0004af003b..0d8df4726b 100644 --- a/pkg/cloudmon/providerdriver/qcloud.go +++ b/pkg/cloudmon/providerdriver/qcloud.go @@ -25,10 +25,7 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" - "yunion.io/x/onecloud/pkg/apis" api "yunion.io/x/onecloud/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/cloudmon/options" - "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/util/influxdb" ) @@ -166,13 +163,7 @@ func (self *QcloudCollect) CollectServerMetrics(ctx context.Context, manager api } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d server with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "server", len(res), metrics) } func (self *QcloudCollect) CollectDBInstanceMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.DBInstanceDetails, start, end time.Time) error { @@ -268,13 +259,7 @@ func (self *QcloudCollect) CollectDBInstanceMetrics(ctx context.Context, manager } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d rds with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "rds", len(res), metrics) } func (self *QcloudCollect) CollectRedisMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.ElasticcacheDetails, start, end time.Time) error { @@ -370,13 +355,7 @@ func (self *QcloudCollect) CollectRedisMetrics(ctx context.Context, manager api. } wg.Wait() - s := auth.GetAdminSession(ctx, options.Options.Region) - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") - if err != nil { - return errors.Wrap(err, "GetServiceURLs") - } - log.Infof("send %d redis with %d metrics for %s(%s)", len(res), len(metrics), manager.Name, manager.Id) - return influxdb.BatchSendMetrics(urls, options.Options.InfluxDatabase, metrics, false) + return self.sendMetrics(ctx, manager, "redis", len(res), metrics) } func (self *QcloudCollect) CollectK8sMetrics(ctx context.Context, manager api.CloudproviderDetails, provider cloudprovider.ICloudProvider, res map[string]api.KubeClusterDetails, start, end time.Time) error { diff --git a/pkg/cloudmon/resources/resources.go b/pkg/cloudmon/resources/resources.go index 6a6ed1d976..1014291ff0 100644 --- a/pkg/cloudmon/resources/resources.go +++ b/pkg/cloudmon/resources/resources.go @@ -756,7 +756,7 @@ func (self *SResources) CollectMetrics(ctx context.Context, userCred mcclient.To } metrics = append(metrics, metric) } - urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType, "") + urls, err := s.GetServiceURLs(apis.SERVICE_TYPE_INFLUXDB, options.Options.SessionEndpointType) if err != nil { return } diff --git a/pkg/compute/guestdrivers/base.go b/pkg/compute/guestdrivers/base.go index 68af4dd487..97998934d1 100644 --- a/pkg/compute/guestdrivers/base.go +++ b/pkg/compute/guestdrivers/base.go @@ -497,7 +497,7 @@ func (self *SBaseGuestDriver) RequestQgaCommand(ctx context.Context, userCred mc func (self *SBaseGuestDriver) FetchMonitorUrl(ctx context.Context, guest *models.SGuest) string { s := auth.GetAdminSessionWithPublic(ctx, consts.GetRegion()) - influxdbUrl, err := s.GetServiceURL(apis.SERVICE_TYPE_INFLUXDB, options.Options.MonitorEndpointType, "") + influxdbUrl, err := s.GetServiceURL(apis.SERVICE_TYPE_INFLUXDB, options.Options.MonitorEndpointType) if err != nil { log.Errorf("FetchMonitorUrl fail %s", err) } diff --git a/pkg/compute/hostdrivers/managedvirtual.go b/pkg/compute/hostdrivers/managedvirtual.go index dd6375e31b..0ad294280f 100644 --- a/pkg/compute/hostdrivers/managedvirtual.go +++ b/pkg/compute/hostdrivers/managedvirtual.go @@ -59,7 +59,6 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context. image.OsVersion = input.OsFullVersion } - userCred := task.GetUserCred() taskman.LocalTaskRun(task, func() (jsonutils.JSONObject, error) { lockman.LockRawObject(ctx, "cachedimages", fmt.Sprintf("%s-%s", storageCache.Id, image.ImageId)) @@ -106,7 +105,7 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context. if errors.Cause(err) != cloudprovider.ErrNotFound { return "", errors.Wrapf(err, "GetIImageById(%s)", image.ExternalId) } - return iStorageCache.UploadImage(ctx, userCred, image, callback) + return iStorageCache.UploadImage(ctx, image, callback) } if iImg.GetImageStatus() == cloudprovider.IMAGE_STATUS_ACTIVE && !input.IsForce { return image.ExternalId, nil @@ -135,7 +134,7 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context. return reader, sizeByte, err } log.Debugf("UploadImage: no external ID") - return iStorageCache.UploadImage(ctx, userCred, image, callback) + return iStorageCache.UploadImage(ctx, image, callback) }() } else { _, err = iStorageCache.GetIImageById(cachedImage.ExternalId) @@ -245,7 +244,7 @@ func (self *SManagedVirtualizationHostDriver) RequestSaveUploadImageOnHost(ctx c return nil, err } } - result, err := iStoragecache.DownloadImage(task.GetUserCred(), imageId, iImage.GetId(), options.Options.TempPath) + result, err := iStoragecache.DownloadImage(imageId, iImage.GetId(), options.Options.TempPath) if err != nil { scimg.SetStatus(task.GetUserCred(), api.CACHED_IMAGE_STATUS_CACHE_FAILED, err.Error()) return nil, err diff --git a/pkg/compute/models/loadbalanceragents_deploy.go b/pkg/compute/models/loadbalanceragents_deploy.go index fb64adb296..8fce0712cf 100644 --- a/pkg/compute/models/loadbalanceragents_deploy.go +++ b/pkg/compute/models/loadbalanceragents_deploy.go @@ -310,10 +310,9 @@ func (lbagent *SLoadbalancerAgent) PerformDeploy(ctx context.Context, userCred m if !token.HasSystemAdminPrivilege() { return nil, httperrors.NewBadRequestError("user must have system admin privileges") } - authURL, err = token.GetServiceURL( + s := cli.NewSession(ctx, options.Options.Region, "", identity_apis.EndpointInterfacePublic, token) + authURL, err = s.GetServiceURL( identity_apis.SERVICE_TYPE, - options.Options.Region, - "", identity_apis.EndpointInterfacePublic) if err != nil { return nil, httperrors.NewClientError("get %s service %s url: %v", diff --git a/pkg/devtool/models/actions.go b/pkg/devtool/models/actions.go index 0a435ad9ad..189861ad9e 100644 --- a/pkg/devtool/models/actions.go +++ b/pkg/devtool/models/actions.go @@ -60,9 +60,8 @@ func getServerAttrs(ID string, s *mcclient.ClientSession) (map[string]string, er } func getInfluxdbURL() (string, error) { - s := auth.GetAdminSessionWithPublic(nil, "") - url, err := s.GetServiceURL("influxdb", apiidentity.EndpointInterfacePublic, "") + url, err := s.GetServiceURL("influxdb", apiidentity.EndpointInterfacePublic) if err != nil { log.Errorf("get influxdb Endpoint error %s", err) diff --git a/pkg/hostman/hostinfo/hostinfo.go b/pkg/hostman/hostinfo/hostinfo.go index f3397d029d..284a3fe864 100644 --- a/pkg/hostman/hostinfo/hostinfo.go +++ b/pkg/hostman/hostinfo/hostinfo.go @@ -41,6 +41,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" identityapi "yunion.io/x/onecloud/pkg/apis/identity" napi "yunion.io/x/onecloud/pkg/apis/notify" + "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" "yunion.io/x/onecloud/pkg/cloudcommon/types" "yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver" @@ -58,6 +59,7 @@ import ( "yunion.io/x/onecloud/pkg/hostman/system_service" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + "yunion.io/x/onecloud/pkg/mcclient/auth" modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute" "yunion.io/x/onecloud/pkg/util/cgrouputils" "yunion.io/x/onecloud/pkg/util/cgrouputils/cpuset" @@ -1158,6 +1160,7 @@ func (h *SHostInfo) getZoneInfo(zoneId string) { h.onFail(fmt.Errorf("failed to found zone with id %s", zoneId)) return } + consts.SetZone(zone.Name) h.getHostInfo(h.ZoneId) } @@ -2037,9 +2040,13 @@ func (h *SHostInfo) OnCatalogChanged(catalog mcclient.KeystoneServiceCatalogV3) if len(defaultEndpointType) == 0 { defaultEndpointType = identityapi.EndpointInterfacePublic } + s := auth.AdminSession(context.Background(), options.HostOptions.Region, h.Zone, defaultEndpointType) + // replace session catalog + s.SetServiceCatalog(catalog) + if options.HostOptions.ManageNtpConfiguration { ntpd := system_service.GetService("ntpd") - urls, _ := catalog.GetServiceURLs("ntp", options.HostOptions.Region, h.Zone, defaultEndpointType) + urls, _ := s.GetServiceURLs("ntp", defaultEndpointType) if len(urls) > 0 { log.Infof("Get Ntp urls: %v", urls) } else { @@ -2073,11 +2080,11 @@ func (h *SHostInfo) OnCatalogChanged(catalog mcclient.KeystoneServiceCatalogV3) hostconsts.TELEGRAF_TAG_KEY_HOST_TYPE: hostconsts.TELEGRAF_TAG_ONECLOUD_HOST_TYPE_HOST, } conf["nics"] = h.getNicsTelegrafConf() - urls, _ := catalog.GetServiceURLs("kafka", options.HostOptions.Region, "", defaultEndpointType) + urls, _ := s.GetServiceURLs("kafka", defaultEndpointType) if len(urls) > 0 { conf["kafka"] = map[string]interface{}{"brokers": urls, "topic": "telegraf"} } - urls, _ = catalog.GetServiceURLs("influxdb", options.HostOptions.Region, "", defaultEndpointType) + urls, _ = s.GetServiceURLs("influxdb", defaultEndpointType) if len(urls) > 0 { conf["influxdb"] = map[string]interface{}{"url": urls, "database": "telegraf"} } @@ -2091,7 +2098,7 @@ func (h *SHostInfo) OnCatalogChanged(catalog mcclient.KeystoneServiceCatalogV3) } } - urls, _ = catalog.GetServiceURLs("elasticsearch", + /*urls, _ = catalog.GetServiceURLs("elasticsearch", options.HostOptions.Region, "zone", defaultEndpointType) if len(urls) > 0 { conf["elasticsearch"] = map[string]interface{}{"url": urls[0]} @@ -2100,7 +2107,7 @@ func (h *SHostInfo) OnCatalogChanged(catalog mcclient.KeystoneServiceCatalogV3) fluentbit.SetConf(conf) fluentbit.BgReload(conf) } - } + }*/ } func (h *SHostInfo) getNicsTelegrafConf() []map[string]interface{} { diff --git a/pkg/hostman/hostutils/hostutils.go b/pkg/hostman/hostutils/hostutils.go index 35d07b2d3f..9fa79f7069 100644 --- a/pkg/hostman/hostutils/hostutils.go +++ b/pkg/hostman/hostutils/hostutils.go @@ -24,6 +24,7 @@ import ( "yunion.io/x/onecloud/pkg/appctx" "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/workmanager" "yunion.io/x/onecloud/pkg/hostman/hostinfo/hostbridge" "yunion.io/x/onecloud/pkg/hostman/hostutils/kubelet" @@ -63,15 +64,15 @@ type IHost interface { } func GetComputeSession(ctx context.Context) *mcclient.ClientSession { - return auth.GetAdminSession(ctx, options.HostOptions.Region) + return auth.GetAdminSession(ctx, consts.GetRegion()) } func GetK8sSession(ctx context.Context) *mcclient.ClientSession { - return auth.GetAdminSession(ctx, options.HostOptions.Region) + return auth.GetAdminSession(ctx, consts.GetRegion()) } -func GetImageSession(ctx context.Context, zone string) *mcclient.ClientSession { - return auth.AdminSession(ctx, options.HostOptions.Region, zone, "") +func GetImageSession(ctx context.Context) *mcclient.ClientSession { + return auth.AdminSession(ctx, consts.GetRegion(), consts.GetZone(), "") } func TaskFailed(ctx context.Context, reason string) { diff --git a/pkg/hostman/storageman/storage_agent.go b/pkg/hostman/storageman/storage_agent.go index b85711aaa7..596dd6cca8 100644 --- a/pkg/hostman/storageman/storage_agent.go +++ b/pkg/hostman/storageman/storage_agent.go @@ -572,7 +572,7 @@ func (as *SAgentStorage) saveToGlance(ctx context.Context, imageId, imagePath st } params.Set("image_id", jsonutils.NewString(imageId)) - _, err = modules.Images.Upload(hostutils.GetImageSession(ctx, as.agent.GetZoneId()), + _, err = modules.Images.Upload(hostutils.GetImageSession(ctx), params, f, size) if err != nil { return errors.Wrap(err, "Images.Upload") @@ -583,7 +583,7 @@ func (as *SAgentStorage) saveToGlance(ctx context.Context, imageId, imagePath st func (as *SAgentStorage) onSaveToGlanceFailed(ctx context.Context, imageId string) { params := jsonutils.NewDict() params.Set("status", jsonutils.NewString("killed")) - _, err := modules.Images.Update(hostutils.GetImageSession(ctx, as.agent.GetZoneId()), + _, err := modules.Images.Update(hostutils.GetImageSession(ctx), imageId, params) if err != nil { log.Errorln(err) diff --git a/pkg/hostman/storageman/storage_local.go b/pkg/hostman/storageman/storage_local.go index 16e4b47f62..805b6d00da 100644 --- a/pkg/hostman/storageman/storage_local.go +++ b/pkg/hostman/storageman/storage_local.go @@ -476,7 +476,7 @@ func (s *SLocalStorage) saveToGlance(ctx context.Context, imageId, imagePath str } params.Set("image_id", jsonutils.NewString(imageId)) - _, err = image.Images.Upload(hostutils.GetImageSession(ctx, s.GetZoneId()), + _, err = image.Images.Upload(hostutils.GetImageSession(ctx), params, f, size) return err } @@ -486,7 +486,7 @@ func (s *SLocalStorage) onSaveToGlanceFailed(ctx context.Context, imageId string params.Set("status", jsonutils.NewString("killed")) params.Set("reason", jsonutils.NewString(reason)) _, err := image.Images.PerformAction( - hostutils.GetImageSession(ctx, ""), + hostutils.GetImageSession(ctx), imageId, "update-status", params, ) if err != nil { diff --git a/pkg/hostman/storageman/storage_rbd.go b/pkg/hostman/storageman/storage_rbd.go index 748c4bf0a1..570f93503a 100644 --- a/pkg/hostman/storageman/storage_rbd.go +++ b/pkg/hostman/storageman/storage_rbd.go @@ -495,7 +495,7 @@ func (s *SRbdStorage) SaveToGlance(ctx context.Context, params interface{}) (jso func (s *SRbdStorage) onSaveToGlanceFailed(ctx context.Context, imageId string) { params := jsonutils.NewDict() params.Set("status", jsonutils.NewString("killed")) - _, err := image.Images.Update(hostutils.GetImageSession(ctx, ""), + _, err := image.Images.Update(hostutils.GetImageSession(ctx), imageId, params) if err != nil { log.Errorln(err) @@ -555,7 +555,7 @@ func (s *SRbdStorage) saveToGlance(ctx context.Context, imageId, imagePath strin } params.Set("image_id", jsonutils.NewString(imageId)) - _, err = image.Images.Upload(hostutils.GetImageSession(ctx, ""), + _, err = image.Images.Upload(hostutils.GetImageSession(ctx), params, f, size) return err } diff --git a/pkg/mcclient/auth/auth.go b/pkg/mcclient/auth/auth.go index 3980727d0f..f4589eaff8 100644 --- a/pkg/mcclient/auth/auth.go +++ b/pkg/mcclient/auth/auth.go @@ -221,17 +221,11 @@ func (a *authManager) reAuth() { } func (a *authManager) GetServiceURL(service, region, zone, endpointType string) (string, error) { - if endpointType == "" && globalEndpointType != "" { - endpointType = globalEndpointType - } - return a.adminCredential.GetServiceURL(service, region, zone, endpointType) + return a.getAdminSession(context.Background(), region, zone, endpointType).GetServiceURL(service, endpointType) } func (a *authManager) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { - if endpointType == "" && globalEndpointType != "" { - endpointType = globalEndpointType - } - return a.adminCredential.GetServiceURLs(service, region, zone, endpointType) + return a.getAdminSession(context.Background(), region, zone, endpointType).GetServiceURLs(service, endpointType) } func (a *authManager) getServiceIPs(service, region, zone, endpointType string, needResolve bool) ([]string, error) { @@ -277,6 +271,21 @@ func (a *authManager) isAuthed() bool { return true } +func (a *authManager) getAdminSession(ctx context.Context, region, zone, endpointType string) *mcclient.ClientSession { + return a.getSession(ctx, manager.adminCredential, region, zone, endpointType) +} + +func (a *authManager) getSession(ctx context.Context, token mcclient.TokenCredential, region, zone, endpointType string) *mcclient.ClientSession { + cli := Client() + if cli == nil { + return nil + } + if endpointType == "" && globalEndpointType != "" { + endpointType = globalEndpointType + } + return cli.NewSession(ctx, region, zone, endpointType, token) +} + func GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject { return manager.adminCredential.GetCatalogData(serviceTypes, region) } @@ -327,24 +336,17 @@ func AdminCredential() mcclient.TokenCredential { // Deprecated func AdminSession(ctx context.Context, region, zone, endpointType string) *mcclient.ClientSession { - cli := Client() - if cli == nil { - return nil - } - if endpointType == "" && globalEndpointType != "" { - endpointType = globalEndpointType - } - return cli.NewSession(ctx, region, zone, endpointType, AdminCredential()) + return manager.getAdminSession(ctx, region, zone, endpointType) } // Deprecated func AdminSessionWithInternal(ctx context.Context, region, zone string) *mcclient.ClientSession { - return AdminSession(ctx, region, zone, "internal") + return AdminSession(ctx, region, zone, identity.EndpointInterfaceInternal) } // Deprecated func AdminSessionWithPublic(ctx context.Context, region, zone string) *mcclient.ClientSession { - return AdminSession(ctx, region, zone, "public") + return AdminSession(ctx, region, zone, identity.EndpointInterfacePublic) } type AuthCompletedCallback func() @@ -382,21 +384,17 @@ func GetAdminSessionWithInternal(ctx context.Context, region string) *mcclient.C func GetSession(ctx context.Context, token mcclient.TokenCredential, region string) *mcclient.ClientSession { if len(globalEndpointType) != 0 { - return getSessionByType(ctx, token, region, globalEndpointType) + return manager.getSession(ctx, token, region, "", globalEndpointType) } return GetSessionWithInternal(ctx, token, region) } func GetSessionWithInternal(ctx context.Context, token mcclient.TokenCredential, region string) *mcclient.ClientSession { - return getSessionByType(ctx, token, region, identity.EndpointInterfaceInternal) + return manager.getSession(ctx, token, region, "", identity.EndpointInterfaceInternal) } func GetSessionWithPublic(ctx context.Context, token mcclient.TokenCredential, region string) *mcclient.ClientSession { - return getSessionByType(ctx, token, region, identity.EndpointInterfacePublic) -} - -func getSessionByType(ctx context.Context, token mcclient.TokenCredential, region string, epType string) *mcclient.ClientSession { - return manager.client.NewSession(ctx, region, "", epType, token) + return manager.getSession(ctx, token, region, "", identity.EndpointInterfacePublic) } // use for climc test only diff --git a/pkg/mcclient/catalog.go b/pkg/mcclient/catalog.go index 524aa9cbaf..f63e10cc92 100644 --- a/pkg/mcclient/catalog.go +++ b/pkg/mcclient/catalog.go @@ -16,8 +16,8 @@ package mcclient type IServiceCatalog interface { Len() int - GetServiceURL(service, region, zone, endpointType string) (string, error) - GetServiceURLs(service, region, zone, endpointType string) ([]string, error) + getServiceURL(service, region, zone, endpointType string) (string, error) + getServiceURLs(service, region, zone, endpointType string) ([]string, error) GetInternalServices(region string) []string GetExternalServices(region string) []ExternalService GetServicesByInterface(region string, infType string) []ExternalService @@ -76,3 +76,11 @@ func (cli *Client) SetServiceCatalog(catalog IServiceCatalog) { func (this *Client) GetServiceCatalog() IServiceCatalog { return this._serviceCatalog } + +func CatalogGetServiceURL(catalog IServiceCatalog, service, region, zone, endpointType string) (string, error) { + return catalog.getServiceURL(service, region, zone, endpointType) +} + +func CatalogGetServiceURLs(catalog IServiceCatalog, service, region, zone, endpointType string) ([]string, error) { + return catalog.getServiceURLs(service, region, zone, endpointType) +} diff --git a/pkg/mcclient/mcclient.go b/pkg/mcclient/mcclient.go index 94df994e19..9fa0cfea3f 100644 --- a/pkg/mcclient/mcclient.go +++ b/pkg/mcclient/mcclient.go @@ -357,7 +357,7 @@ func (this *Client) GetCommonEtcdEndpoint(token TokenCredential, region, interfa return nil, errors.Errorf("current version %s not support get internal etcd endpoint", this.AuthVersion()) } - _, err := this.GetServiceCatalog().GetServiceURL(apis.SERVICE_TYPE_ETCD, region, "", interfaceType) + _, err := this.GetServiceCatalog().getServiceURL(apis.SERVICE_TYPE_ETCD, region, "", interfaceType) if err != nil { return nil, err } diff --git a/pkg/mcclient/modulebase/appoptions.go b/pkg/mcclient/modulebase/appoptions.go index 108c9a5e16..55621ef10e 100644 --- a/pkg/mcclient/modulebase/appoptions.go +++ b/pkg/mcclient/modulebase/appoptions.go @@ -22,7 +22,7 @@ import ( ) func GetAppOptions(s *mcclient.ClientSession, serviceType string) (jsonutils.JSONObject, error) { - man := NewBaseManager(serviceType, "", "", nil, nil, "") + man := NewBaseManager(serviceType, "", "", nil, nil) _, resp, err := man.jsonRequest(s, "GET", "/app-options", nil, nil) if err != nil { return nil, errors.Wrap(err, "rawBaseUrlRequest") diff --git a/pkg/mcclient/modulebase/base.go b/pkg/mcclient/modulebase/base.go index 67e01c1aea..eed2415780 100644 --- a/pkg/mcclient/modulebase/base.go +++ b/pkg/mcclient/modulebase/base.go @@ -33,18 +33,18 @@ type BaseManager struct { serviceType string endpointType string version string - apiVersion string + // apiVersion string columns []string adminColumns []string } -func NewBaseManager(serviceType, endpointType, version string, columns, adminColumns []string, apiVersion string) *BaseManager { +func NewBaseManager(serviceType, endpointType, version string, columns, adminColumns []string) *BaseManager { return &BaseManager{ serviceType: serviceType, endpointType: endpointType, version: version, - apiVersion: apiVersion, + // apiVersion: apiVersion, columns: columns, adminColumns: adminColumns, } @@ -62,9 +62,9 @@ func (this *BaseManager) SetVersion(v string) { this.version = v } -func (this *BaseManager) GetApiVersion() string { +/*func (this *BaseManager) GetApiVersion() string { return this.apiVersion -} +}*/ func (this *BaseManager) versionedURL(path string) string { offset := 0 @@ -85,7 +85,7 @@ func (this *BaseManager) jsonRequest(session *mcclient.ClientSession, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { hdr, resp, err := session.JSONVersionRequest(this.serviceType, this.endpointType, method, this.versionedURL(path), - header, body, this.GetApiVersion()) + header, body) if err != nil { if e, ok := err.(*httputils.JSONClientError); ok { switch e.Class { @@ -109,7 +109,7 @@ func (this *BaseManager) rawRequest(session *mcclient.ClientSession, header http.Header, body io.Reader) (*http.Response, error) { return session.RawVersionRequest(this.serviceType, this.endpointType, method, this.versionedURL(path), - header, body, this.GetApiVersion()) + header, body) } func (this *BaseManager) rawBaseUrlRequest(s *mcclient.ClientSession, @@ -123,7 +123,7 @@ func (this *BaseManager) rawBaseUrlRequest(s *mcclient.ClientSession, return s.RawBaseUrlRequest( this.serviceType, this.endpointType, method, this.versionedURL(path), - header, body, this.GetApiVersion(), baseUrlF) + header, body, baseUrlF) } type ListResult struct { diff --git a/pkg/mcclient/modulebase/misc.go b/pkg/mcclient/modulebase/misc.go index 8b3febed82..24f22b9ef5 100644 --- a/pkg/mcclient/modulebase/misc.go +++ b/pkg/mcclient/modulebase/misc.go @@ -23,7 +23,7 @@ import ( ) func GetVersion(s *mcclient.ClientSession, serviceType string) (string, error) { - man := NewBaseManager(serviceType, "", "", nil, nil, "") + man := NewBaseManager(serviceType, "", "", nil, nil) resp, err := man.rawBaseUrlRequest(s, "GET", "/version", nil, nil) if err != nil { return "", err @@ -37,7 +37,7 @@ func GetVersion(s *mcclient.ClientSession, serviceType string) (string, error) { } func ListWorkers(s *mcclient.ClientSession, serviceType string) (*ListResult, error) { - man := NewBaseManager(serviceType, "", "", nil, nil, "") + man := NewBaseManager(serviceType, "", "", nil, nil) resp, err := man.rawBaseUrlRequest(s, "GET", "/worker_stats", nil, nil) if err != nil { return nil, err diff --git a/pkg/mcclient/modulebase/modules.go b/pkg/mcclient/modulebase/modules.go index 7336077eb6..de783964bf 100644 --- a/pkg/mcclient/modulebase/modules.go +++ b/pkg/mcclient/modulebase/modules.go @@ -28,7 +28,7 @@ import ( type IBaseManager interface { Version() string - GetApiVersion() string + // GetApiVersion() string GetKeyword() string KeyString() string ServiceType() string @@ -246,7 +246,7 @@ func _getModule(session *mcclient.ClientSession, name string) (IBaseManager, err } for _, mod := range mods { - url, e := session.GetServiceURL(mod.ServiceType(), mod.EndpointType(), mod.GetApiVersion()) + url, e := session.GetServiceURL(mod.ServiceType(), mod.EndpointType()) if e != nil { return nil, errors.Wrap(e, "session.GetServiceURL") } @@ -293,7 +293,7 @@ func GetJointModule2(session *mcclient.ClientSession, mod1 Manager, mod2 Manager return nil, fmt.Errorf("No such joint module: %s", key) } for _, mod := range mods { - url, e := session.GetServiceVersionURL(mod.ServiceType(), mod.EndpointType(), mod.GetApiVersion()) + url, e := session.GetServiceVersionURL(mod.ServiceType(), mod.EndpointType()) if e != nil { return nil, e } diff --git a/pkg/mcclient/modulebase/stats.go b/pkg/mcclient/modulebase/stats.go index ffdea6326d..4c15c20127 100644 --- a/pkg/mcclient/modulebase/stats.go +++ b/pkg/mcclient/modulebase/stats.go @@ -25,7 +25,7 @@ import ( // path: stats, db_stats, worker_stats func GetStats(s *mcclient.ClientSession, path string, serviceType string) (jsonutils.JSONObject, error) { - man := NewBaseManager(serviceType, "", "", nil, nil, "") + man := NewBaseManager(serviceType, "", "", nil, nil) resp, err := man.rawBaseUrlRequest(s, "GET", "/"+path, nil, nil) if err != nil { return nil, errors.Wrap(err, "man.rawBaseUrlRequest") diff --git a/pkg/mcclient/modulebase/utils.go b/pkg/mcclient/modulebase/utils.go index 30921d4301..97e25a2992 100644 --- a/pkg/mcclient/modulebase/utils.go +++ b/pkg/mcclient/modulebase/utils.go @@ -57,7 +57,7 @@ func RawRequest(manager ResourceManager, session *mcclient.ClientSession, header http.Header, body io.Reader) (*http.Response, error) { return session.RawVersionRequest(manager.serviceType, manager.endpointType, method, manager.versionedURL(path), - header, body, manager.GetApiVersion()) + header, body) } func JsonRequest(manager ResourceManager, session *mcclient.ClientSession, @@ -65,5 +65,5 @@ func JsonRequest(manager ResourceManager, session *mcclient.ClientSession, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { return session.JSONVersionRequest(manager.serviceType, manager.endpointType, method, manager.versionedURL(path), - header, body, manager.GetApiVersion()) + header, body) } diff --git a/pkg/mcclient/modules/cloudnet/common.go b/pkg/mcclient/modules/cloudnet/common.go index 002bad7734..239ef6804a 100644 --- a/pkg/mcclient/modules/cloudnet/common.go +++ b/pkg/mcclient/modules/cloudnet/common.go @@ -20,7 +20,7 @@ import ( func NewCloudnetManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager("cloudnet", "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager("cloudnet", "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } diff --git a/pkg/mcclient/modules/cloudproxy/common.go b/pkg/mcclient/modules/cloudproxy/common.go index dee1ad4627..004953504e 100644 --- a/pkg/mcclient/modules/cloudproxy/common.go +++ b/pkg/mcclient/modules/cloudproxy/common.go @@ -20,7 +20,7 @@ import ( func NewCloudProxyManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager("cloudproxy", "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager("cloudproxy", "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } diff --git a/pkg/mcclient/modules/compute/mod_metadatas.go b/pkg/mcclient/modules/compute/mod_metadatas.go index 8c31cced96..60e93e99fe 100644 --- a/pkg/mcclient/modules/compute/mod_metadatas.go +++ b/pkg/mcclient/modules/compute/mod_metadatas.go @@ -92,13 +92,13 @@ func (this *MetadataManager) getModule(session *mcclient.ClientSession, params j } } - _, err := session.GetServiceURL(service, "", "") + _, err := session.GetServiceURL(service, "") if err != nil { return nil, httperrors.NewNotFoundError("service %s not found error: %v", service, err) } return &modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(service, "", "", []string{}, []string{}, ""), + BaseManager: *modulebase.NewBaseManager(service, "", "", []string{}, []string{}), Keyword: "metadata", KeywordPlural: "metadatas", }, nil } diff --git a/pkg/mcclient/modules/compute/mod_skus.go b/pkg/mcclient/modules/compute/mod_skus.go index 84c625b0ad..1bcb248169 100644 --- a/pkg/mcclient/modules/compute/mod_skus.go +++ b/pkg/mcclient/modules/compute/mod_skus.go @@ -92,7 +92,7 @@ func (self *SkusManager) GetSkus(s *mcclient.ClientSession, providerId, regionId } func (self *OfflineCloudmetaManager) GetSkuSourcesMeta(s *mcclient.ClientSession, client *http.Client) (jsonutils.JSONObject, error) { - baseUrl, err := s.GetServiceVersionURL(self.ServiceType(), self.EndpointType(), self.GetApiVersion()) + baseUrl, err := s.GetServiceVersionURL(self.ServiceType(), self.EndpointType()) if err != nil { return nil, err } diff --git a/pkg/mcclient/modules/compute/mod_tasks.go b/pkg/mcclient/modules/compute/mod_tasks.go index b05bc24391..41c5b6b492 100644 --- a/pkg/mcclient/modules/compute/mod_tasks.go +++ b/pkg/mcclient/modules/compute/mod_tasks.go @@ -86,13 +86,13 @@ func (self *TasksManager) getManager(session *mcclient.ClientSession, params jso version = "v1" } - _, err := session.GetServiceURL(serviceType, "", "") + _, err := session.GetServiceURL(serviceType, "") if err != nil { return nil, httperrors.NewNotFoundError("service %s not found error: %v", serviceType, err) } return &modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(serviceType, "", version, []string{}, []string{}, ""), + BaseManager: *modulebase.NewBaseManager(serviceType, "", version, []string{}, []string{}), Keyword: "task", KeywordPlural: "tasks", }, nil } diff --git a/pkg/mcclient/modules/k8s/base.go b/pkg/mcclient/modules/k8s/base.go index 8a78985152..426500799c 100644 --- a/pkg/mcclient/modules/k8s/base.go +++ b/pkg/mcclient/modules/k8s/base.go @@ -28,7 +28,7 @@ type ResourceManager struct { func NewResourceManager(keyword, keywordPlural string, columns, adminColumns *Columns) *ResourceManager { man := &modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager("k8s", "", "", columns.Array(), adminColumns.Array(), ""), + BaseManager: *modulebase.NewBaseManager("k8s", "", "", columns.Array(), adminColumns.Array()), Keyword: keyword, KeywordPlural: keywordPlural, } diff --git a/pkg/mcclient/modules/managers.go b/pkg/mcclient/modules/managers.go index 8452381f70..586d72c561 100644 --- a/pkg/mcclient/modules/managers.go +++ b/pkg/mcclient/modules/managers.go @@ -27,25 +27,25 @@ import ( func NewResourceManager(serviceType string, keyword, keywordPlural string, columns, adminColumns []string, apiVersion string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(serviceType, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(serviceType, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewComputeManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_REGION, "", "", columns, adminColumns, "v2"), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_REGION, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewActionManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_LOG, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_LOG, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewMonitorV2Manager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_MONITOR, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_MONITOR, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } @@ -66,13 +66,13 @@ func NewCloudwatcherManager(keyword, keywordPlural string, columns, adminColumns func NewNotifyManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_NOTIFY, "", "v1", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_NOTIFY, "", "v1", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewNotifyv2Manager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_NOTIFY, "", "v2", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_NOTIFY, "", "v2", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural, } @@ -103,25 +103,25 @@ func NewJointServiceTreeManager(keyword, keywordPlural string, columns, adminCol func NewIdentityManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_KEYSTONE, "", "v2.0", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_KEYSTONE, "", "v2.0", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewIdentityV3Manager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_KEYSTONE, "", "v3", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_KEYSTONE, "", "v3", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewImageManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_IMAGE, "", "v1", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_IMAGE, "", "v1", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewVNCProxyManager() modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_VNCPROXY, "", "", nil, nil, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_VNCPROXY, "", "", nil, nil), Keyword: "vncproxy", KeywordPlural: "vncproxy", } @@ -129,48 +129,48 @@ func NewVNCProxyManager() modulebase.ResourceManager { func NewSchedulerManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_SCHEDULER, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_SCHEDULER, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewYunionConfManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_YUNIONCONF, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_YUNIONCONF, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewOfflineCloudmetaManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_OFFLINE_CLOUDMETA, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_OFFLINE_CLOUDMETA, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewAnsibleManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_ANSIBLE, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_ANSIBLE, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewDevtoolManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_DEVTOOL, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_DEVTOOL, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewCloudeventManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_CLOUDEVENT, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_CLOUDEVENT, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewCloudIdManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_CLOUDID, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_CLOUDID, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } func NewScheduledtaskManager(keyword, keywordPlural string, columns, adminColumns []string) modulebase.ResourceManager { return modulebase.ResourceManager{ - BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_SCHEDULEDTASK, "", "", columns, adminColumns, ""), + BaseManager: *modulebase.NewBaseManager(apis.SERVICE_TYPE_SCHEDULEDTASK, "", "", columns, adminColumns), Keyword: keyword, KeywordPlural: keywordPlural} } diff --git a/pkg/mcclient/modules/webconsole/mod_commandlog.go b/pkg/mcclient/modules/webconsole/mod_commandlog.go index 02e11c1780..125ab5c476 100644 --- a/pkg/mcclient/modules/webconsole/mod_commandlog.go +++ b/pkg/mcclient/modules/webconsole/mod_commandlog.go @@ -38,7 +38,7 @@ func NewCommandLogManager() *CommandLogManager { BaseManager: *modulebase.NewBaseManager("webconsole", "", "webconsole", []string{ "id", "ops_time", "obj_id", "obj_type", "obj_name", "user", "user_id", "tenant", "tenant_id", "owner_tenant_id", "action", "notes", "session_id", "accessed_at", "type", "login_user", "start_time", "ps1", "command", - }, nil, ""), + }, nil), Keyword: "commandlog", KeywordPlural: "commandlogs", }, } diff --git a/pkg/mcclient/modules/webconsole/mod_webconsole.go b/pkg/mcclient/modules/webconsole/mod_webconsole.go index 13cb622461..817e5abe7f 100644 --- a/pkg/mcclient/modules/webconsole/mod_webconsole.go +++ b/pkg/mcclient/modules/webconsole/mod_webconsole.go @@ -43,7 +43,7 @@ type WebConsoleManager struct { } func NewWebConsoleManager() modulebase.ResourceManager { - return modulebase.ResourceManager{BaseManager: *modulebase.NewBaseManager("webconsole", "", "", nil, nil, ""), + return modulebase.ResourceManager{BaseManager: *modulebase.NewBaseManager("webconsole", "", "", nil, nil), Keyword: "webconsole", KeywordPlural: "webconsole"} } @@ -110,7 +110,7 @@ func (m WebConsoleManager) DoCloudShell(s *mcclient.ClientSession, _ jsonutils.J req.Container = "climc" req.Command = "/bin/bash" endpointType := "internal" - authUrl, _ := s.GetServiceURL("identity", endpointType, "") + authUrl, _ := s.GetServiceURL("identity", endpointType) if err != nil { return nil, httperrors.NewNotFoundError("auth_url not found") } diff --git a/pkg/mcclient/session.go b/pkg/mcclient/session.go index cd32e06e36..b369dca79e 100644 --- a/pkg/mcclient/session.go +++ b/pkg/mcclient/session.go @@ -58,6 +58,8 @@ type ClientSession struct { notifyChannel chan string customizeServiceUrl map[string]string + + catalog IServiceCatalog } func populateHeader(self *http.Header, update http.Header) { @@ -119,42 +121,47 @@ func getApiVersionByServiceType(serviceType string) string { return "" } -func (this *ClientSession) getServiceName(service, apiVersion string) string { - if len(apiVersion) == 0 { - apiVersion = getApiVersionByServiceType(service) - } +func (this *ClientSession) getServiceName(service string) string { + apiVersion := getApiVersionByServiceType(service) if len(apiVersion) > 0 && apiVersion != DEFAULT_API_VERSION { service = fmt.Sprintf("%s_%s", service, apiVersion) } return service } -func (this *ClientSession) GetServiceURL(service, endpointType, apiVersion string) (string, error) { - return this.GetServiceVersionURL(service, endpointType, apiVersion) +func (this *ClientSession) GetServiceURL(service, endpointType string) (string, error) { + return this.GetServiceVersionURL(service, endpointType) +} + +func (this *ClientSession) SetServiceCatalog(catalog IServiceCatalog) { + this.catalog = catalog } func (this *ClientSession) GetServiceCatalog() IServiceCatalog { + if this.catalog != nil { + return this.catalog + } return this.client.GetServiceCatalog() } -func (this *ClientSession) GetServiceVersionURL(service, endpointType, apiVersion string) (string, error) { - urls, err := this.GetServiceVersionURLs(service, endpointType, apiVersion) +func (this *ClientSession) GetServiceVersionURL(service, endpointType string) (string, error) { + urls, err := this.GetServiceVersionURLs(service, endpointType) if err != nil { return "", errors.Wrap(err, "GetServiceVersionURLs") } return urls[rand.Intn(len(urls))], nil } -func (this *ClientSession) GetServiceURLs(service, endpointType, apiVersion string) ([]string, error) { - return this.GetServiceVersionURLs(service, endpointType, apiVersion) +func (this *ClientSession) GetServiceURLs(service, endpointType string) ([]string, error) { + return this.GetServiceVersionURLs(service, endpointType) } -func (this *ClientSession) GetServiceVersionURLs(service, endpointType, apiVersion string) ([]string, error) { +func (this *ClientSession) GetServiceVersionURLs(service, endpointType string) ([]string, error) { if len(this.endpointType) > 0 { // session specific endpoint type should override the input endpointType, which is supplied by manager endpointType = this.endpointType } - service = this.getServiceName(service, apiVersion) + service = this.getServiceName(service) if endpointType == api.EndpointInterfaceApigateway { return this.getApigatewayServiceURLs(service, this.region, this.zone, endpointType) } else { @@ -204,7 +211,7 @@ func (this *ClientSession) getServiceVersionURLs(service, region, zone, endpoint if gotypes.IsNil(catalog) { return []string{this.client.authUrl}, nil } - urls, err := catalog.GetServiceURLs(service, region, zone, endpointType) + urls, err := catalog.getServiceURLs(service, region, zone, endpointType) // HACK! in case of fail to get kestone url or schema of keystone changed, always trust authUrl if service == api.SERVICE_TYPE && (err != nil || len(urls) == 0 || (len(this.client.authUrl) != 0 && this.client.authUrl[:5] != urls[0][:5])) { var msg string @@ -224,14 +231,14 @@ func (this *ClientSession) getServiceVersionURLs(service, region, zone, endpoint return urls, err } -func (this *ClientSession) getBaseUrl(service, endpointType, apiVersion string) (string, error) { +func (this *ClientSession) getBaseUrl(service, endpointType string) (string, error) { if len(service) > 0 { if strings.HasPrefix(service, "http://") || strings.HasPrefix(service, "https://") { return service, nil } else if url, ok := this.customizeServiceUrl[service]; ok { return url, nil } else { - return this.GetServiceVersionURL(service, endpointType, apiVersion) + return this.GetServiceVersionURL(service, endpointType) } } else { return "", fmt.Errorf("Empty service type or baseURL") @@ -242,10 +249,9 @@ func (this *ClientSession) RawBaseUrlRequest( service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body io.Reader, - apiVersion string, baseurlFactory func(string) string, ) (*http.Response, error) { - baseurl, err := this.getBaseUrl(service, endpointType, apiVersion) + baseurl, err := this.getBaseUrl(service, endpointType) if err != nil { return nil, err } @@ -270,21 +276,19 @@ func (this *ClientSession) RawBaseUrlRequest( func (this *ClientSession) RawVersionRequest( service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body io.Reader, - apiVersion string, ) (*http.Response, error) { - return this.RawBaseUrlRequest(service, endpointType, method, url, headers, body, apiVersion, nil) + return this.RawBaseUrlRequest(service, endpointType, method, url, headers, body, nil) } func (this *ClientSession) RawRequest(service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body io.Reader) (*http.Response, error) { - return this.RawVersionRequest(service, endpointType, method, url, headers, body, "") + return this.RawVersionRequest(service, endpointType, method, url, headers, body) } func (this *ClientSession) JSONVersionRequest( service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body jsonutils.JSONObject, - apiVersion string, ) (http.Header, jsonutils.JSONObject, error) { - baseUrl, err := this.getBaseUrl(service, endpointType, apiVersion) + baseUrl, err := this.getBaseUrl(service, endpointType) if err != nil { return headers, nil, err } @@ -304,7 +308,7 @@ func (this *ClientSession) JSONVersionRequest( } func (this *ClientSession) JSONRequest(service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { - return this.JSONVersionRequest(service, endpointType, method, url, headers, body, "") + return this.JSONVersionRequest(service, endpointType, method, url, headers, body) } func (this *ClientSession) ParseJSONResponse(reqBody string, resp *http.Response, err error) (http.Header, jsonutils.JSONObject, error) { diff --git a/pkg/mcclient/token2.go b/pkg/mcclient/token2.go index b91e24d6de..3d1370ca6a 100644 --- a/pkg/mcclient/token2.go +++ b/pkg/mcclient/token2.go @@ -227,12 +227,12 @@ func (this *TokenCredentialV2) Len() int { return this.ServiceCatalog.Len() } -func (this *TokenCredentialV2) GetServiceURL(service, region, zone, endpointType string) (string, error) { - return this.ServiceCatalog.GetServiceURL(service, region, zone, endpointType) +func (this *TokenCredentialV2) getServiceURL(service, region, zone, endpointType string) (string, error) { + return this.ServiceCatalog.getServiceURL(service, region, zone, endpointType) } -func (this *TokenCredentialV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { - return this.ServiceCatalog.GetServiceURLs(service, region, zone, endpointType) +func (this *TokenCredentialV2) getServiceURLs(service, region, zone, endpointType string) ([]string, error) { + return this.ServiceCatalog.getServiceURLs(service, region, zone, endpointType) } func (this *TokenCredentialV2) GetInternalServices(region string) []string { @@ -335,7 +335,7 @@ func (catalog KeystoneServiceCatalogV2) Len() int { return len(catalog) } -func (catalog KeystoneServiceCatalogV2) GetServiceURL(service, region, zone, endpointType string) (string, error) { +func (catalog KeystoneServiceCatalogV2) getServiceURL(service, region, zone, endpointType string) (string, error) { ep, err := catalog.getServiceEndpoint(service, region, zone) if err != nil { return "", err @@ -343,8 +343,8 @@ func (catalog KeystoneServiceCatalogV2) GetServiceURL(service, region, zone, end return ep.getURL(endpointType), nil } -func (catalog KeystoneServiceCatalogV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { - url, err := catalog.GetServiceURL(service, region, zone, endpointType) +func (catalog KeystoneServiceCatalogV2) getServiceURLs(service, region, zone, endpointType string) ([]string, error) { + url, err := catalog.getServiceURL(service, region, zone, endpointType) if err != nil { return nil, err } diff --git a/pkg/mcclient/token3.go b/pkg/mcclient/token3.go index d181ed1de3..49caad9b43 100644 --- a/pkg/mcclient/token3.go +++ b/pkg/mcclient/token3.go @@ -259,12 +259,12 @@ func (this *TokenCredentialV3) Len() int { return this.Token.Catalog.Len() } -func (this *TokenCredentialV3) GetServiceURL(service, region, zone, endpointType string) (string, error) { - return this.Token.Catalog.GetServiceURL(service, region, zone, endpointType) +func (this *TokenCredentialV3) getServiceURL(service, region, zone, endpointType string) (string, error) { + return this.Token.Catalog.getServiceURL(service, region, zone, endpointType) } -func (this *TokenCredentialV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { - return this.Token.Catalog.GetServiceURLs(service, region, zone, endpointType) +func (this *TokenCredentialV3) getServiceURLs(service, region, zone, endpointType string) ([]string, error) { + return this.Token.Catalog.getServiceURLs(service, region, zone, endpointType) } func (this *TokenCredentialV3) GetInternalServices(region string) []string { @@ -390,15 +390,15 @@ func (catalog KeystoneServiceCatalogV3) Len() int { return len(catalog) } -func (catalog KeystoneServiceCatalogV3) GetServiceURL(service, region, zone, endpointType string) (string, error) { - urls, err := catalog.GetServiceURLs(service, region, zone, endpointType) +func (catalog KeystoneServiceCatalogV3) getServiceURL(service, region, zone, endpointType string) (string, error) { + urls, err := catalog.getServiceURLs(service, region, zone, endpointType) if err != nil { return "", err } return urls[rand.Intn(len(urls))], nil } -func (catalog KeystoneServiceCatalogV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { +func (catalog KeystoneServiceCatalogV3) getServiceURLs(service, region, zone, endpointType string) ([]string, error) { if endpointType == "" { endpointType = "internalURL" } diff --git a/pkg/mcclient/tokensimple.go b/pkg/mcclient/tokensimple.go index b6d2f8b779..4ef6f59f5a 100644 --- a/pkg/mcclient/tokensimple.go +++ b/pkg/mcclient/tokensimple.go @@ -154,11 +154,11 @@ func (self *SSimpleToken) Len() int { return 0 } -func (self *SSimpleToken) GetServiceURL(service, region, zone, endpointType string) (string, error) { +func (self *SSimpleToken) getServiceURL(service, region, zone, endpointType string) (string, error) { return "", fmt.Errorf("Not available") } -func (self *SSimpleToken) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { +func (self *SSimpleToken) getServiceURLs(service, region, zone, endpointType string) ([]string, error) { return nil, fmt.Errorf("Not available") } diff --git a/pkg/monitor/models/datasource.go b/pkg/monitor/models/datasource.go index 281e8660f2..65da5dc65d 100644 --- a/pkg/monitor/models/datasource.go +++ b/pkg/monitor/models/datasource.go @@ -106,7 +106,7 @@ func (man *SDataSourceManager) initDefaultDataSource(ctx context.Context) error log.Errorf("get empty public session for region %s", region) return } - url, err := s.GetServiceURL("influxdb", epType, "") + url, err := s.GetServiceURL("influxdb", epType) if err != nil { log.Errorf("get influxdb public url: %v", err) return diff --git a/vendor/modules.txt b/vendor/modules.txt index 08e07103c3..7f6babb23d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1428,7 +1428,7 @@ sigs.k8s.io/structured-merge-diff/v4/value # sigs.k8s.io/yaml v1.2.0 ## explicit; go 1.12 sigs.k8s.io/yaml -# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221104111004-66eebdd3aa2f +# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20221106172354-3354ac00fde8 ## explicit; go 1.18 yunion.io/x/cloudmux/pkg/apis yunion.io/x/cloudmux/pkg/apis/billing @@ -1496,6 +1496,7 @@ yunion.io/x/cloudmux/pkg/multicloud/objectstore/provider yunion.io/x/cloudmux/pkg/multicloud/objectstore/xsky yunion.io/x/cloudmux/pkg/multicloud/objectstore/xsky/provider yunion.io/x/cloudmux/pkg/multicloud/openstack +yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli yunion.io/x/cloudmux/pkg/multicloud/openstack/provider yunion.io/x/cloudmux/pkg/multicloud/proxmox yunion.io/x/cloudmux/pkg/multicloud/proxmox/provider diff --git a/vendor/yunion.io/x/cloudmux/pkg/cloudprovider/resources.go b/vendor/yunion.io/x/cloudmux/pkg/cloudprovider/resources.go index 78e5c5b4a9..8fdf7191e1 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/cloudprovider/resources.go +++ b/vendor/yunion.io/x/cloudmux/pkg/cloudprovider/resources.go @@ -22,7 +22,6 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/pkg/tristate" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/billing" "yunion.io/x/onecloud/pkg/util/rbacutils" "yunion.io/x/onecloud/pkg/util/samlutils" @@ -250,9 +249,9 @@ type ICloudStoragecache interface { CreateIImage(snapshotId, imageName, osType, imageDesc string) (ICloudImage, error) - DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) + DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) - UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *SImageCreateOption, callback func(float32)) (string, error) + UploadImage(ctx context.Context, image *SImageCreateOption, callback func(float32)) (string, error) } type ICloudStorage interface { diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/provider/provider.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/provider/provider.go index 8c2a134072..58868ab2dd 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/provider/provider.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/provider/provider.go @@ -24,9 +24,10 @@ import ( api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" + "yunion.io/x/cloudmux/pkg/multicloud/aliyun" + "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" - "yunion.io/x/cloudmux/pkg/multicloud/aliyun" ) type SAliyunProviderFactory struct { diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/storagecache.go index 8416ff129a..a044956726 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/aliyun/storagecache.go @@ -30,7 +30,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -101,7 +100,7 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { if len(image.ExternalId) > 0 { status, err := self.region.GetImageStatus(image.ExternalId) if err != nil { @@ -115,10 +114,10 @@ func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.To } } } - return self.uploadImage(ctx, userCred, image, callback) + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { reader, sizeByte, err := image.GetReader(image.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") @@ -268,8 +267,8 @@ func (self *SRegion) createIImage(snapshoutId, imageName, imageDesc string) (str } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId, path) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId, path) } // 定义进度条监听器。 @@ -295,7 +294,7 @@ func (listener *OssProgressListener) ProgressChanged(event *oss.ProgressEvent) { } } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { err := self.region.GetClient().EnableImageExport() if err != nil { log.Errorf("fail to enable export privileges: %s", err) @@ -310,19 +309,19 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag defer os.Remove(tmpImageFile.Name()) bucketName := strings.ToLower(fmt.Sprintf("imgcache-%s", self.region.GetId())) if bucket, err := self.region.checkBucket(bucketName); err != nil { - return nil, err + return nil, errors.Wrap(err, "region.checkBucket") } else if _, err := self.region.GetImage(extId); err != nil { - return nil, err + return nil, errors.Wrap(err, "region.GetImage") } else if task, err := self.region.ExportImage(extId, bucket); err != nil { - return nil, err + return nil, errors.Wrap(err, "region.ExportImage") } else if err := self.region.waitTaskStatus(ExportImageTask, task.TaskId, TaskStatusFinished, 15*time.Second, 3600*time.Second); err != nil { - return nil, err + return nil, errors.Wrap(err, "region.waitTaskStatus") } else if imageList, err := bucket.ListObjects(oss.Prefix(fmt.Sprintf("%sexport", strings.Replace(extId, "-", "", -1)))); err != nil { - return nil, err + return nil, errors.Wrap(err, "bucket.ListObjects") } else if len(imageList.Objects) != 1 { return nil, fmt.Errorf("exported image not find") } else if err := bucket.DownloadFile(imageList.Objects[0].Key, tmpImageFile.Name(), 12*1024*1024, oss.Routines(3), oss.Progress(&OssProgressListener{})); err != nil { - return nil, err + return nil, errors.Wrap(err, "bucket.DownloadFile") } else { return nil, cloudprovider.ErrNotSupported } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/apsara/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/apsara/storagecache.go index 32e4d55615..9b24097b4e 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/apsara/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/apsara/storagecache.go @@ -28,7 +28,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -123,7 +122,7 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { if len(image.ExternalId) > 0 { status, err := self.region.GetImageStatus(image.ExternalId) @@ -140,10 +139,10 @@ func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.To } } - return self.uploadImage(ctx, userCred, image, callback) + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { reader, sizeByte, err := image.GetReader(image.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") @@ -272,8 +271,8 @@ func (self *SRegion) createIImage(snapshoutId, imageName, imageDesc string) (str } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId, path) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId, path) } // 定义进度条监听器。 @@ -299,7 +298,7 @@ func (listener *OssProgressListener) ProgressChanged(event *oss.ProgressEvent) { } } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/aws/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/aws/storagecache.go index 44657e9268..c11b5932d9 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/aws/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/aws/storagecache.go @@ -30,7 +30,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -120,16 +119,16 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc return iimage[0], nil } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId) } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return self.uploadImage(ctx, userCred, image, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { err := self.region.initVmimport() if err != nil { return "", errors.Wrap(err, "initVmimport") @@ -222,7 +221,7 @@ func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.To return task.ImageId, nil } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string) (jsonutils.JSONObject, error) { // aws 导出镜像限制比较多。https://docs.aws.amazon.com/zh_cn/vm-import/latest/userguide/vmexport.html bucketName := GetBucketName(self.region.GetId(), imageId) if err := self.region.checkBucket(bucketName); err != nil { diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/storagecache.go index 48c020595a..ddb43a4b83 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/storagecache.go @@ -29,7 +29,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -101,12 +100,12 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { err := os.MkdirAll(image.TmpPath, os.ModePerm) if err != nil { log.Warningf("failed to create tmp path %s error: %v", image.TmpPath, err) } - return self.uploadImage(ctx, userCred, image, image.TmpPath, callback) + return self.uploadImage(ctx, image, image.TmpPath, callback) } func (self *SStoragecache) checkStorageAccount() (*SStorageAccount, error) { @@ -139,7 +138,7 @@ func (self *SStoragecache) checkStorageAccount() (*SStorageAccount, error) { return storageaccount, nil } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, tmpPath string, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, tmpPath string, callback func(progress float32)) (string, error) { reader, sizeBytes, err := image.GetReader(image.ImageId, string(qemuimg.VHD)) if err != nil { return "", errors.Wrapf(err, "GetReader") @@ -194,11 +193,11 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId, path) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId, path) } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { // TODO: need to fix scenarios where image is a public image // XXX Qiu Jian if image, err := self.region.getPrivateImage(extId); err != nil { diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/bingocloud/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/bingocloud/storagecache.go index e0d0339612..5ad11cbe65 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/bingocloud/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/bingocloud/storagecache.go @@ -21,7 +21,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -62,10 +61,10 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { return "", cloudprovider.ErrNotImplemented } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/image.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/image.go index 6c29d612be..c6a34944ff 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/image.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/image.go @@ -22,7 +22,6 @@ import ( "yunion.io/x/pkg/utils" api "yunion.io/x/onecloud/pkg/apis/image" - "yunion.io/x/onecloud/pkg/mcclient" modules "yunion.io/x/onecloud/pkg/mcclient/modules/image" "yunion.io/x/onecloud/pkg/util/imagetools" "yunion.io/x/onecloud/pkg/util/qemuimg" @@ -189,7 +188,7 @@ func (self *SRegion) GetImage(id string) (*SImage, error) { return image, resp.Unmarshal(image) } -func (self *SRegion) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SRegion) UploadImage(ctx context.Context, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { reader, sizeByte, err := opts.GetReader(opts.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", err diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/storagecache.go index d485ffbcf7..be312db683 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/cloudpods/storagecache.go @@ -22,7 +22,6 @@ import ( "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" @@ -74,12 +73,12 @@ func (self *SStoragecache) CreateIImage(snapshotId, name, osType, desc string) ( return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - id, err := self.region.UploadImage(ctx, userCred, opts, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, opts *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + id, err := self.region.UploadImage(ctx, opts, callback) if err != nil { return "", errors.Wrapf(err, "UploadImage") } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/ctyun/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/ctyun/storagecache.go index 94ffe6c5dd..e08e3a6a4c 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/ctyun/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/ctyun/storagecache.go @@ -23,7 +23,6 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -37,11 +36,11 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, update func(progress float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, update func(progress float32)) (string, error) { return "", cloudprovider.ErrNotSupported } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/ecloud/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/ecloud/storagecache.go index d0b2db5a19..3a96bd748e 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/ecloud/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/ecloud/storagecache.go @@ -22,7 +22,6 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -36,11 +35,11 @@ func (sc *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc s return nil, cloudprovider.ErrNotSupported } -func (sc *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (sc *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotSupported } -func (sc *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (sc *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { return "", cloudprovider.ErrNotSupported } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/esxi/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/esxi/storagecache.go index d750975aa7..4eb9927528 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/esxi/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/esxi/storagecache.go @@ -25,7 +25,6 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -283,10 +282,10 @@ func (self *SDatastoreImageCache) CreateIImage(snapshotId, imageName, osType, im return nil, cloudprovider.ErrNotImplemented } -func (self *SDatastoreImageCache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SDatastoreImageCache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SDatastoreImageCache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SDatastoreImageCache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { return "", cloudprovider.ErrNotImplemented } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/google/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/google/storagecache.go index 04db6c2a93..3a73b19c6b 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/google/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/google/storagecache.go @@ -27,7 +27,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -92,8 +91,8 @@ func (cache *SStoragecache) GetPath() string { return "" } -func (cache *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return cache.uploadImage(ctx, userCred, image, callback) +func (cache *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return cache.uploadImage(ctx, image, callback) } func (region *SRegion) checkAndCreateBucket(bucketName string) (*SBucket, error) { @@ -111,7 +110,7 @@ func (region *SRegion) checkAndCreateBucket(bucketName string) (*SBucket, error) return bucket, nil } -func (cache *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (cache *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { reader, sizeBytes, err := image.GetReader(image.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") @@ -174,7 +173,7 @@ func (cache *SStoragecache) CreateIImage(snapshoutId, imageName, osType, imageDe return nil, cloudprovider.ErrNotImplemented } -func (cache *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (cache *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcs/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcs/storagecache.go index 0c6bde073f..9ff9f0ea75 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcs/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcs/storagecache.go @@ -25,7 +25,6 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" api "yunion.io/x/cloudmux/pkg/apis/compute" @@ -110,19 +109,19 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId) } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return self.uploadImage(ctx, userCred, image, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { bucketName := GetBucketName(self.region.GetId(), image.ImageId) exist, _ := self.region.IBucketExist(bucketName) diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcso/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcso/storagecache.go index f90bbecebc..f9adc164e8 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcso/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/hcso/storagecache.go @@ -29,7 +29,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" "yunion.io/x/cloudmux/pkg/multicloud/huawei" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -139,19 +138,19 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId) } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return self.uploadImage(ctx, userCred, image, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { bucketName := GetBucketName(self.region.GetId(), image.ImageId) exist, _ := self.region.IBucketExist(bucketName) diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/huawei/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/huawei/storagecache.go index 2e71534f91..bfa67efb4e 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/huawei/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/huawei/storagecache.go @@ -28,7 +28,6 @@ import ( api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -116,19 +115,19 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc } } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { - return self.downloadImage(userCred, imageId, extId) +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { + return self.downloadImage(imageId, extId) } -func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imageId string, extId string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) downloadImage(imageId string, extId string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return self.uploadImage(ctx, userCred, image, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { bucketName := GetBucketName(self.region.GetId(), image.ImageId) exist, _ := self.region.IBucketExist(bucketName) diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/incloudsphere/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/incloudsphere/storagecache.go index e395032c15..7a3d8ae628 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/incloudsphere/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/incloudsphere/storagecache.go @@ -24,7 +24,6 @@ import ( "yunion.io/x/pkg/utils" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -98,11 +97,11 @@ func (self *SStoragecache) CreateIImage(snpId, imageName, osType, imageDesc stri return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { return "", cloudprovider.ErrNotSupported } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/jdcloud/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/jdcloud/storagecache.go index d86f763463..525783fb4e 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/jdcloud/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/jdcloud/storagecache.go @@ -21,7 +21,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -98,10 +97,10 @@ func (sc *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc s return nil, cloudprovider.ErrNotSupported } -func (sc *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (sc *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotSupported } -func (sc *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (sc *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { return "", cloudprovider.ErrNotSupported } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/nutanix/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/nutanix/storagecache.go index 9c3fe2fbe6..2ad8058408 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/nutanix/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/nutanix/storagecache.go @@ -22,7 +22,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -87,11 +86,11 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, opts *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, opts *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { reader, size, err := opts.GetReader(opts.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/openstack.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/openstack.go index a5b599d8b1..b57c3a896e 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/openstack.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/openstack.go @@ -27,13 +27,14 @@ import ( "yunion.io/x/pkg/errors" "yunion.io/x/pkg/utils" - api "yunion.io/x/cloudmux/pkg/apis/compute" - "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/httperrors" - "yunion.io/x/onecloud/pkg/mcclient" - "yunion.io/x/cloudmux/pkg/multicloud" "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/onecloud/pkg/util/version" + + api "yunion.io/x/cloudmux/pkg/apis/compute" + "yunion.io/x/cloudmux/pkg/cloudprovider" + "yunion.io/x/cloudmux/pkg/multicloud" + "yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli" ) const ( @@ -101,7 +102,7 @@ func (cfg *OpenstackClientConfig) Debug(debug bool) *OpenstackClientConfig { type SOpenStackClient struct { *OpenstackClientConfig - tokenCredential mcclient.TokenCredential + tokenCredential oscli.TokenCredential iregions []cloudprovider.ICloudRegion defaultRegionName string @@ -124,7 +125,7 @@ func (cli *SOpenStackClient) getDefaultRegionName() string { return cli.defaultRegionName } -func (cli *SOpenStackClient) getProjectToken(projectId, projectName string) (mcclient.TokenCredential, error) { +func (cli *SOpenStackClient) getProjectToken(projectId, projectName string) (oscli.TokenCredential, error) { client := cli.getDefaultClient() tokenCredential, err := client.Authenticate(cli.username, cli.password, cli.domainName, projectName, cli.projectDomain) if err != nil { @@ -210,7 +211,7 @@ func (v *sApiVersions) GetMaxVersion() string { return maxVersion } -func (cli *SOpenStackClient) getApiVerion(token mcclient.TokenCredential, url string, debug bool) (string, error) { +func (cli *SOpenStackClient) getApiVerion(token oscli.TokenCredential, url string, debug bool) (string, error) { client := httputils.NewJsonClient(cli.getDefaultClient().HttpClient()) req := httputils.NewJsonRequest(httputils.THttpMethod("GET"), strings.TrimSuffix(url, token.GetTenantId()), nil) header := http.Header{} @@ -236,7 +237,7 @@ func (cli *SOpenStackClient) GetMaxVersion(region, service string) (string, erro return cli.getApiVerion(cli.tokenCredential, serviceUrl, cli.debug) } -func (cli *SOpenStackClient) jsonReuest(token mcclient.TokenCredential, service, region, endpointType string, method httputils.THttpMethod, resource string, query url.Values, body interface{}, debug bool) (jsonutils.JSONObject, error) { +func (cli *SOpenStackClient) jsonReuest(token oscli.TokenCredential, service, region, endpointType string, method httputils.THttpMethod, resource string, query url.Values, body interface{}, debug bool) (jsonutils.JSONObject, error) { serviceUrl, err := token.GetServiceURL(service, region, "", endpointType) if err != nil { return nil, errors.Wrapf(err, "GetServiceURL(%s, %s, %s)", service, region, endpointType) @@ -412,7 +413,7 @@ func (cli *SOpenStackClient) checkEndpointType() error { return errors.Errorf("failed to find right endpoint type for compute service") } -func (cli *SOpenStackClient) getDefaultSession(regionName string) *mcclient.ClientSession { +func (cli *SOpenStackClient) getDefaultSession(regionName string) *oscli.ClientSession { if len(regionName) == 0 { regionName = cli.getDefaultRegionName() } @@ -420,8 +421,8 @@ func (cli *SOpenStackClient) getDefaultSession(regionName string) *mcclient.Clie return client.NewSession(context.Background(), regionName, "", cli.endpointType, cli.tokenCredential) } -func (cli *SOpenStackClient) getDefaultClient() *mcclient.Client { - client := mcclient.NewClient(cli.authURL, 5, cli.debug, false, "", "") +func (cli *SOpenStackClient) getDefaultClient() *oscli.Client { + client := oscli.NewClient(cli.authURL, 5, cli.debug, false, "", "") client.SetHttpTransportProxyFunc(cli.cpcfg.ProxyFunc) _client := client.GetClient() ts, _ := _client.Transport.(*http.Transport) @@ -442,7 +443,7 @@ func (cli *SOpenStackClient) getDefaultClient() *mcclient.Client { return client } -func (cli *SOpenStackClient) getDefaultToken() (mcclient.TokenCredential, error) { +func (cli *SOpenStackClient) getDefaultToken() (oscli.TokenCredential, error) { client := cli.getDefaultClient() token, err := client.Authenticate(cli.username, cli.password, cli.domainName, cli.project, cli.projectDomain) if err != nil { @@ -456,7 +457,7 @@ func (cli *SOpenStackClient) getDefaultToken() (mcclient.TokenCredential, error) return token, nil } -func (cli *SOpenStackClient) getProjectTokenCredential(projectId string) (mcclient.TokenCredential, error) { +func (cli *SOpenStackClient) getProjectTokenCredential(projectId string) (oscli.TokenCredential, error) { project, err := cli.GetProject(projectId) if err != nil { return nil, errors.Wrapf(err, "GetProject(%s)", projectId) diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/catalog.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/catalog.go new file mode 100644 index 0000000000..0c0d1396d3 --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/catalog.go @@ -0,0 +1,36 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +type IServiceCatalog interface { + Len() int + GetServiceURL(service, region, zone, endpointType string) (string, error) + GetServiceURLs(service, region, zone, endpointType string) ([]string, error) + GetInternalServices(region string) []string + GetExternalServices(region string) []ExternalService + GetServicesByInterface(region string, infType string) []ExternalService +} + +type IServiceCatalogChangeListener interface { + OnServiceCatalogChange(catalog IServiceCatalog) +} + +func (cli *Client) SetServiceCatalog(catalog IServiceCatalog) { + cli._serviceCatalog = catalog +} + +func (this *Client) GetServiceCatalog() IServiceCatalog { + return this._serviceCatalog +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/input.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/input.go new file mode 100644 index 0000000000..dd0359ffe6 --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/input.go @@ -0,0 +1,131 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +type SAuthenticationInputV2 struct { + // keystone v2 认证接口认证信息 + // required:true + Auth struct { + // 如果使用用户名/密码认证,则需要设置passwordCredentials + PasswordCredentials struct { + // 用户名 + Username string `json:"username,omitempty"` + // 用户密码 + Password string `json:"password,omitempty"` + } `json:"passwordCredentials,omitempty"` + // 指定认证用户的所属项目名称,该字段和tenantId二选一,或者不设置。 + // 如果不提供tenantName和tenantId,则用户认证成功后,获得一个unscoped token + // 此时,如果用户需要访问具体项目的资源,还是需要用unscoped token进行认证,获得指定项目的token + // required:false + TenantName string `json:"tenantName,omitempty"` + // 指定认证用户的所属项目ID,该字段和tenantName二选一,或者不设置。 + // required:false + TenantId string `json:"tenantId,omitempty"` + // 如果使用token认证,则需要设置token.Id + Token struct { + // token的字符串 + Id string `json:"id,omitempty"` + } `json:"token,omitempty"` + } `json:"auth,omitempty"` +} + +type SAuthenticationIdentity struct { + // ID of identity provider, optional + // required:false + Id string `json:"id,omitempty"` + // 认证方式列表,支持认证方式如下: + // + // | method | 说明 | + // |----------|--------------------------------------------------------------------| + // | password | 用户名密码认证 | + // | token | token认证,已经通过其他方式获得token之后,可以用旧的token认证获得新的token | + // | aksk | Access Key/Secret key认证 | + // | cas | 通过SSO统一认证平台CAS认证 | + // | saml | 作为SAML 2.0 SP通过IDP认证 | + // | oidc | 作为OpenID Connect/OAuth2 Client认证 | + // | oauth2 | OAuth2认证 | + // | verify | 手机短信或邮箱认证 | + // + Methods []string `json:"methods,omitempty"` + // 当认证方式为password时,通过该字段提供密码认证信息 + Password struct { + User struct { + // 用户ID + Id string `json:"id,omitempty"` + // 用户名称 + Name string `json:"name,omitempty"` + // 密码 + Password string `json:"password,omitempty"` + // 用户所属域的信息 + Domain struct { + // 域ID + Id string `json:"id,omitempty"` + // 域名称 + Name string `json:"name,omitempty"` + } + } `json:"user,omitempty"` + } `json:"password,omitempty"` + // 当认证方式为token时,通过该字段提供token认证信息 + Token struct { + // token + Id string `json:"id,omitempty"` + } `json:"token,omitempty"` +} + +type SAuthenticationInputV3 struct { + // keystone v3 认证接口认证信息 + // required:true + Auth struct { + // 认证信息 + // required:true + Identity SAuthenticationIdentity `json:"identity,omitempty"` + // 指定认证范围, 该字段可选。如果未指定scope,则用户认证成功后获得一个unscoped token, + // 当用户需要访问指定项目的资源时,需要通过该unscope token进行认证,获得该项目scope的token + // 目前只支持Project scope的token + // required:false + Scope struct { + // 指定token的scope为指定的项目 + // required:false + Project struct { + // 指定项目的ID,由于ID全局唯一,因此指定ID后不需要指定项目所在的域(Domain),ID和Name只需要指定其中一个 + // required:false + Id string `json:"id,omitempty"` + // 指定项目的Name,指定Name时,需要指定项目所在的域(domain) + // required:false + Name string `json:"name,omitempty"` + // 指定项目所在的域(domain) + // required:false + Domain struct { + // 指定项目所在域的ID,ID和Name只需要指定其中一个 + // required:false + Id string `json:"id,omitempty"` + // 指定项目所在域的Name + // required:false + Name string `json:"name,omitempty"` + } `json:"domain,omitempty"` + } `json:"project,omitempty"` + // 指定token的scope为指定的域 + // required:false + Domain struct { + // 指定domain的ID,ID和Name只需要指定其中一个 + // required:false + Id string `json:"id,omitempty"` + // 指定Domain的Name + // required:false + Name string `json:"name,omitempty"` + } `json:"domain,omitempty"` + } `json:"scope,omitempty"` + } `json:"auth,omitempty"` +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/oscli.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/oscli.go new file mode 100644 index 0000000000..fb89e22a5a --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/oscli.go @@ -0,0 +1,328 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +import ( + "context" + "crypto/tls" + "fmt" + "io" + "net/http" + "strings" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/gotypes" + + api "yunion.io/x/onecloud/pkg/apis/identity" + "yunion.io/x/onecloud/pkg/util/httputils" + "yunion.io/x/onecloud/pkg/util/seclib2" +) + +type Client struct { + authUrl string + timeout int + debug bool + + httpconn *http.Client + _serviceCatalog IServiceCatalog +} + +func NewClient(authUrl string, timeout int, debug bool, insecure bool, certFile, keyFile string) *Client { + var tlsConf *tls.Config + + if len(certFile) > 0 && len(keyFile) > 0 { + var err error + tlsConf, err = seclib2.InitTLSConfig(certFile, keyFile) + if err != nil { + log.Errorf("load TLS failed %s", err) + } + } + + if tlsConf == nil || gotypes.IsNil(tlsConf) { + tlsConf = &tls.Config{} + } + tlsConf.InsecureSkipVerify = insecure + + tr := httputils.GetTransport(insecure) + tr.TLSClientConfig = tlsConf + tr.IdleConnTimeout = 5 * time.Second + tr.TLSHandshakeTimeout = 10 * time.Second + tr.ResponseHeaderTimeout = 0 + + client := Client{authUrl: authUrl, + timeout: timeout, + debug: debug, + httpconn: &http.Client{ + Transport: tr, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, // 不自动处理重定向请求 + }, + } + + return &client +} + +func (this *Client) HttpClient() *http.Client { + return this.httpconn +} + +func (this *Client) SetHttpTransportProxyFunc(proxyFunc httputils.TransportProxyFunc) { + httputils.SetClientProxyFunc(this.httpconn, proxyFunc) +} + +func (this *Client) GetClient() *http.Client { + return this.httpconn +} + +func (this *Client) SetTransport(ts http.RoundTripper) { + this.httpconn.Transport = ts +} + +func (this *Client) SetDebug(debug bool) { + this.debug = debug +} + +func (this *Client) GetDebug() bool { + return this.debug +} + +func (this *Client) AuthVersion() string { + pos := strings.LastIndexByte(this.authUrl, '/') + if pos > 0 { + return this.authUrl[pos+1:] + } else { + return "" + } +} + +func (this *Client) NewAuthTokenCredential() TokenCredential { + if this.AuthVersion() == "v3" { + return &TokenCredentialV3{} + } + return &TokenCredentialV2{} +} + +func getDefaultHeader(header http.Header, token string) http.Header { + if len(token) > 0 { + if header == nil { + header = http.Header{} + } + if len(header.Get(AUTH_TOKEN)) == 0 { + header.Add(AUTH_TOKEN, token) + } + } + return header +} + +func joinUrl(baseUrl, path string) string { + base, version := SplitVersionedURL(baseUrl) + if len(version) > 0 { + if strings.HasPrefix(path, fmt.Sprintf("/%s/", version)) { + baseUrl = base + } + } + return fmt.Sprintf("%s%s", baseUrl, path) +} + +func (this *Client) rawRequest(ctx context.Context, endpoint string, token string, method httputils.THttpMethod, url string, header http.Header, body io.Reader) (*http.Response, error) { + return httputils.Request(this.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, this.debug) +} + +func (this *Client) jsonRequest(ctx context.Context, endpoint string, token string, method httputils.THttpMethod, url string, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { + return httputils.JSONRequest(this.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, this.debug) +} + +func (this *Client) _authV3(domainName, uname, passwd, projectId, projectName, projectDomain, token string) (TokenCredential, error) { + input := SAuthenticationInputV3{} + if len(uname) > 0 && len(passwd) > 0 { // Password authentication + input.Auth.Identity.Methods = []string{api.AUTH_METHOD_PASSWORD} + input.Auth.Identity.Password.User.Name = uname + input.Auth.Identity.Password.User.Password = passwd + if len(domainName) > 0 { + input.Auth.Identity.Password.User.Domain.Name = domainName + } + // else { + // input.Auth.Identity.Password.User.Domain.Name = api.DEFAULT_DOMAIN_ID + //} + } else if len(token) > 0 { + input.Auth.Identity.Methods = []string{api.AUTH_METHOD_TOKEN} + input.Auth.Identity.Token.Id = token + } + if len(projectId) > 0 { + input.Auth.Scope.Project.Id = projectId + } + if len(projectName) > 0 { + input.Auth.Scope.Project.Name = projectName + if len(projectDomain) > 0 { + input.Auth.Scope.Project.Domain.Name = projectDomain + } + // else { + // input.Auth.Scope.Project.Domain.Id = api.DEFAULT_DOMAIN_ID + // } + } + return this._authV3Input(input) +} + +func (this *Client) _authV3Input(input SAuthenticationInputV3) (TokenCredential, error) { + hdr, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/auth/tokens", nil, jsonutils.Marshal(&input)) + if err != nil { + return nil, err + } + + tokenId := hdr.Get("X-Subject-Token") + if len(tokenId) == 0 { + return nil, fmt.Errorf("No X-Subject-Token in header") + } + ret, err := this.unmarshalV3Token(rbody, tokenId) + return ret, err +} + +func (this *Client) _authV2(uname, passwd, tenantId, tenantName, token string) (TokenCredential, error) { + input := SAuthenticationInputV2{} + input.Auth.PasswordCredentials.Username = uname + input.Auth.PasswordCredentials.Password = passwd + if len(tenantName) > 0 { + input.Auth.TenantName = tenantName + } + if len(tenantId) > 0 { + input.Auth.TenantId = tenantId + } + if len(token) > 0 { + input.Auth.Token.Id = token + } + _, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/tokens", nil, jsonutils.Marshal(&input)) + if err != nil { + return nil, err + } + return this.unmarshalV2Token(rbody) +} + +func (this *Client) Authenticate(uname, passwd, domainName, tenantName, tenantDomain string) (TokenCredential, error) { + if this.AuthVersion() == "v3" { + return this._authV3(domainName, uname, passwd, "", tenantName, tenantDomain, "") + } + return this._authV2(uname, passwd, "", tenantName, "") +} + +func (this *Client) unmarshalV3Token(rbody jsonutils.JSONObject, tokenId string) (cred TokenCredential, err error) { + cred = &TokenCredentialV3{Id: tokenId} + err = rbody.Unmarshal(cred) + if err != nil { + err = fmt.Errorf("Invalid response when unmarshal V3 Token: %v", err) + } + cata := cred.GetServiceCatalog() + if cata == nil || cata.Len() == 0 { + log.Warningf("No service catalog avaiable") + } else { + this.SetServiceCatalog(cata) + } + return +} + +func (this *Client) unmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err error) { + access, err := rbody.Get("access") + if err == nil { + cred = &TokenCredentialV2{} + err = access.Unmarshal(cred) + if err != nil { + err = fmt.Errorf("Invalid response when unmarshal V2 Token: %s", err) + } + cata := cred.GetServiceCatalog() + if cata == nil || cata.Len() == 0 { + log.Warningf("No srvice catalog avaiable") + } else { + this.SetServiceCatalog(cata) + } + return + } + err = fmt.Errorf("Invalid response: no access object") + return +} + +func (this *Client) verifyV3(adminToken, token string) (TokenCredential, error) { + header := http.Header{} + header.Add(api.AUTH_TOKEN_HEADER, adminToken) + header.Add(api.AUTH_SUBJECT_TOKEN_HEADER, token) + _, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "GET", "/auth/tokens", header, nil) + if err != nil { + return nil, err + } + return this.unmarshalV3Token(rbody, token) +} + +func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error) { + header := http.Header{} + header.Add(api.AUTH_TOKEN_HEADER, adminToken) + verifyUrl := fmt.Sprintf("/tokens/%s", token) + _, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "GET", verifyUrl, header, nil) + if err != nil { + return nil, err + } + return this.unmarshalV2Token(rbody) +} + +func (this *Client) Verify(adminToken, token string) (cred TokenCredential, err error) { + if this.AuthVersion() == "v3" { + return this.verifyV3(adminToken, token) + } + return this.verifyV2(adminToken, token) +} + +func (this *Client) SetTenant(tenantId, tenantName, tenantDomain string, token TokenCredential) (TokenCredential, error) { + return this.SetProject(tenantId, tenantName, tenantDomain, token) +} + +func (this *Client) AuthenticateToken(token string, projName, projDomain string) (TokenCredential, error) { + if this.AuthVersion() == "v3" { + return this._authV3("", "", "", "", projName, projDomain, token) + } else { + return this._authV2("", "", "", projName, token) + } +} + +func (this *Client) SetProject(tenantId, tenantName, tenantDomain string, token TokenCredential) (TokenCredential, error) { + if this.AuthVersion() == "v3" { + return this._authV3("", "", "", tenantId, tenantName, tenantDomain, token.GetTokenString()) + } else { + return this._authV2("", "", "", tenantName, token.GetTokenString()) + } +} + +func (this *Client) NewSession(ctx context.Context, region, zone, endpointType string, token TokenCredential) *ClientSession { + cata := token.GetServiceCatalog() + if this.GetServiceCatalog() == nil { + if cata == nil || cata.Len() == 0 { + log.Warningf("Missing service catalog in token") + } else { + this.SetServiceCatalog(cata) + } + } + if ctx == nil { + ctx = context.Background() + } + return &ClientSession{ + ctx: ctx, + client: this, + region: region, + zone: zone, + endpointType: endpointType, + token: token, + Header: http.Header{}, + customizeServiceUrl: map[string]string{}, + } +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/session.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/session.go new file mode 100644 index 0000000000..62c0674fb5 --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/session.go @@ -0,0 +1,329 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +import ( + "context" + "fmt" + "io" + "math/rand" + "net/http" + "regexp" + "strings" + + "yunion.io/x/jsonutils" + "yunion.io/x/log" + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/gotypes" + + api "yunion.io/x/onecloud/pkg/apis/identity" + "yunion.io/x/onecloud/pkg/i18n" + "yunion.io/x/onecloud/pkg/util/httputils" +) + +const ( + AUTH_TOKEN = "X-Auth-Token" + REGION_VERSION = "X-Region-Version" + + DEFAULT_API_VERSION = "v1" + V2_API_VERSION = "v2" +) + +type ClientSession struct { + ctx context.Context + + client *Client + region string + zone string + endpointType string + token TokenCredential + Header http.Header /// headers for this session + notifyChannel chan string + + customizeServiceUrl map[string]string + + catalog IServiceCatalog +} + +func populateHeader(self *http.Header, update http.Header) { + for k, v := range update { + for _, vv := range v { + // self.Add(k, vv) + self.Set(k, vv) + } + } +} + +func GetTokenHeaders(userCred TokenCredential) http.Header { + headers := http.Header{} + headers.Set(AUTH_TOKEN, userCred.GetTokenString()) + headers.Set(REGION_VERSION, V2_API_VERSION) + return headers +} + +func SplitVersionedURL(url string) (string, string) { + endidx := len(url) - 1 + for ; endidx >= 0 && url[endidx] == '/'; endidx-- { + } + lastslash := strings.LastIndexByte(url[0:endidx+1], '/') + if lastslash >= 0 { + if strings.EqualFold(url[lastslash+1:endidx+1], "latest") { + return url[0:lastslash], "" + } + match, err := regexp.MatchString(`^v\d+\.?\d*`, url[lastslash+1:endidx+1]) + if err == nil && match { + return url[0:lastslash], url[lastslash+1 : endidx+1] + } + } + return url[0 : endidx+1], "" +} + +func (this *ClientSession) GetEndpointType() string { + return this.endpointType +} + +func (this *ClientSession) GetClient() *Client { + return this.client +} + +func (this *ClientSession) SetZone(zone string) { + this.zone = zone +} + +func (this *ClientSession) GetServiceURL(service, endpointType string) (string, error) { + return this.GetServiceVersionURL(service, endpointType) +} + +func (this *ClientSession) SetServiceCatalog(catalog IServiceCatalog) { + this.catalog = catalog +} + +func (this *ClientSession) GetServiceCatalog() IServiceCatalog { + if this.catalog != nil { + return this.catalog + } + return this.client.GetServiceCatalog() +} + +func (this *ClientSession) GetServiceVersionURL(service, endpointType string) (string, error) { + urls, err := this.GetServiceVersionURLs(service, endpointType) + if err != nil { + return "", errors.Wrap(err, "GetServiceVersionURLs") + } + return urls[rand.Intn(len(urls))], nil +} + +func (this *ClientSession) GetServiceURLs(service, endpointType string) ([]string, error) { + return this.GetServiceVersionURLs(service, endpointType) +} + +func (this *ClientSession) GetServiceVersionURLs(service, endpointType string) ([]string, error) { + if len(this.endpointType) > 0 { + // session specific endpoint type should override the input endpointType, which is supplied by manager + endpointType = this.endpointType + } + return this.getServiceVersionURLs(service, this.region, this.zone, endpointType) +} + +func (this *ClientSession) getServiceVersionURLs(service, region, zone, endpointType string) ([]string, error) { + catalog := this.GetServiceCatalog() + if gotypes.IsNil(catalog) { + return []string{this.client.authUrl}, nil + } + urls, err := catalog.GetServiceURLs(service, region, zone, endpointType) + // HACK! in case of fail to get kestone url or schema of keystone changed, always trust authUrl + if service == api.SERVICE_TYPE && (err != nil || len(urls) == 0 || (len(this.client.authUrl) != 0 && this.client.authUrl[:5] != urls[0][:5])) { + var msg string + if err != nil { + msg = fmt.Sprintf("fail to retrieve keystone urls: %s", err) + } else if len(urls) == 0 { + msg = fmt.Sprintf("empty keystone url") + } else { + msg = fmt.Sprintf("Schema of keystone authUrl and endpoint mismatch: %s!=%s", this.client.authUrl, urls) + } + log.Warningln(msg) + return []string{this.client.authUrl}, nil + } + if err != nil { + return nil, errors.Wrap(err, "catalog.GetServiceURLs") + } + return urls, err +} + +func (this *ClientSession) getBaseUrl(service, endpointType string) (string, error) { + if len(service) > 0 { + if strings.HasPrefix(service, "http://") || strings.HasPrefix(service, "https://") { + return service, nil + } else if url, ok := this.customizeServiceUrl[service]; ok { + return url, nil + } else { + return this.GetServiceVersionURL(service, endpointType) + } + } else { + return "", fmt.Errorf("Empty service type or baseURL") + } +} + +func (this *ClientSession) RawBaseUrlRequest( + service, endpointType string, + method httputils.THttpMethod, url string, + headers http.Header, body io.Reader, + baseurlFactory func(string) string, +) (*http.Response, error) { + baseurl, err := this.getBaseUrl(service, endpointType) + if err != nil { + return nil, err + } + if baseurlFactory != nil { + baseurl = baseurlFactory(baseurl) + } + tmpHeader := http.Header{} + if headers != nil { + populateHeader(&tmpHeader, headers) + } + populateHeader(&tmpHeader, this.Header) + i18n.SetHTTPLangHeader(this.ctx, tmpHeader) + ctx := this.ctx + if this.ctx == nil { + ctx = context.Background() + } + return this.client.rawRequest(ctx, baseurl, + this.token.GetTokenString(), + method, url, tmpHeader, body) +} + +func (this *ClientSession) RawVersionRequest( + service, endpointType string, method httputils.THttpMethod, url string, + headers http.Header, body io.Reader, +) (*http.Response, error) { + return this.RawBaseUrlRequest(service, endpointType, method, url, headers, body, nil) +} + +func (this *ClientSession) RawRequest(service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body io.Reader) (*http.Response, error) { + return this.RawVersionRequest(service, endpointType, method, url, headers, body) +} + +func (this *ClientSession) JSONVersionRequest( + service, endpointType string, method httputils.THttpMethod, url string, + headers http.Header, body jsonutils.JSONObject, +) (http.Header, jsonutils.JSONObject, error) { + baseUrl, err := this.getBaseUrl(service, endpointType) + if err != nil { + return headers, nil, err + } + tmpHeader := http.Header{} + if headers != nil { + populateHeader(&tmpHeader, headers) + } + populateHeader(&tmpHeader, this.Header) + i18n.SetHTTPLangHeader(this.ctx, tmpHeader) + ctx := this.ctx + if this.ctx == nil { + ctx = context.Background() + } + return this.client.jsonRequest(ctx, baseUrl, + this.token.GetTokenString(), + method, url, tmpHeader, body) +} + +func (this *ClientSession) JSONRequest(service, endpointType string, method httputils.THttpMethod, url string, headers http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { + return this.JSONVersionRequest(service, endpointType, method, url, headers, body) +} + +func (this *ClientSession) ParseJSONResponse(reqBody string, resp *http.Response, err error) (http.Header, jsonutils.JSONObject, error) { + return httputils.ParseJSONResponse(reqBody, resp, err, this.client.debug) +} + +func (this *ClientSession) HasSystemAdminPrivilege() bool { + return this.token.HasSystemAdminPrivilege() +} + +func (this *ClientSession) GetRegion() string { + return this.region +} + +func (this *ClientSession) GetUserId() string { + return this.token.GetUserId() +} + +func (this *ClientSession) GetTenantId() string { + return this.token.GetTenantId() +} + +func (this *ClientSession) GetTenantName() string { + return this.token.GetTenantName() +} + +func (this *ClientSession) GetProjectId() string { + return this.GetTenantId() +} + +func (this *ClientSession) GetProjectName() string { + return this.GetTenantName() +} + +func (this *ClientSession) GetProjectDomain() string { + return this.token.GetProjectDomain() +} + +func (this *ClientSession) GetProjectDomainId() string { + return this.token.GetProjectDomainId() +} + +func (this *ClientSession) GetDomainId() string { + return this.token.GetDomainId() +} + +func (this *ClientSession) GetDomainName() string { + return this.token.GetDomainName() +} + +func (this *ClientSession) SetServiceUrl(service, url string) { + this.customizeServiceUrl[service] = url +} + +func (this *ClientSession) ToJson() jsonutils.JSONObject { + params := jsonutils.NewDict() + simpleToken := SimplifyToken(this.token) + tokenJson := jsonutils.Marshal(simpleToken) + params.Update(tokenJson) + // params.Add(jsonutils.NewString(this.GetApiVersion()), "api_version") + if len(this.endpointType) > 0 { + params.Add(jsonutils.NewString(this.endpointType), "endpoint_type") + } + if len(this.region) > 0 { + params.Add(jsonutils.NewString(this.region), "region") + } + if len(this.zone) > 0 { + params.Add(jsonutils.NewString(this.zone), "zone") + } + if tokenV3, ok := this.token.(*TokenCredentialV3); ok { + params.Add(jsonutils.NewStringArray(tokenV3.Token.Policies.Project), "project_policies") + params.Add(jsonutils.NewStringArray(tokenV3.Token.Policies.Domain), "domain_policies") + params.Add(jsonutils.NewStringArray(tokenV3.Token.Policies.System), "system_policies") + } + return params +} + +func (cs *ClientSession) GetToken() TokenCredential { + return cs.token +} + +func (cs *ClientSession) GetContext() context.Context { + if cs.ctx == nil { + return context.Background() + } + return cs.ctx +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token.go new file mode 100644 index 0000000000..e4a535cde5 --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token.go @@ -0,0 +1,96 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +import ( + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/gotypes" + + "yunion.io/x/onecloud/pkg/util/rbacutils" +) + +type ExternalService struct { + Name string + Url string + + Service string +} + +type Endpoint struct { + Id string + RegionId string + ServiceId string + ServiceName string + Url string + Interface string +} + +func OwnerIdString(owner IIdentityProvider, scope rbacutils.TRbacScope) string { + switch scope { + case rbacutils.ScopeDomain: + return owner.GetProjectDomainId() + case rbacutils.ScopeProject: + return owner.GetProjectId() + case rbacutils.ScopeUser: + return owner.GetUserId() + default: + return "" + } +} + +type IIdentityProvider interface { + GetProjectId() string + GetUserId() string + GetTenantId() string + GetProjectDomainId() string + + GetTenantName() string + GetProjectName() string + GetProjectDomain() string + + GetUserName() string + GetDomainId() string + GetDomainName() string +} + +type TokenCredential interface { + gotypes.ISerializable + + IServiceCatalog + + IIdentityProvider + + GetTokenString() string + GetRoles() []string + GetRoleIds() []string + GetExpires() time.Time + IsValid() bool + ValidDuration() time.Duration + // IsAdmin() bool + HasSystemAdminPrivilege() bool + + IsAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult + + GetRegions() []string + + GetServiceCatalog() IServiceCatalog + GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject + + GetEndpoints(region string, endpointType string) []Endpoint + + ToJson() jsonutils.JSONObject +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token2.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token2.go new file mode 100644 index 0000000000..2d588c490f --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token2.go @@ -0,0 +1,382 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +import ( + "fmt" + "math/rand" + "strings" + "time" + + "yunion.io/x/jsonutils" + + api "yunion.io/x/onecloud/pkg/apis/identity" + "yunion.io/x/onecloud/pkg/util/rbacutils" +) + +type KeystoneEndpointV2 struct { + // 接口ID + Id string `json:"id"` + // 内部URL + InternalURL string `json:"internal_url"` + // 外部URL + PublicURL string `json:"public_url"` + // 管理URL + AdminURL string `json:"admin_url"` + // 区域ID + Region string `json:"region"` +} + +type KeystoneServiceV2 struct { + // 服务名称 + Name string `json:"name"` + // 服务类型 + Type string `json:"type"` + // 服务接口地址列表 + Endpoints []KeystoneEndpointV2 `json:"endpoints"` +} + +type KeystoneRoleV2 struct { + // 角色名称 + Name string `json:"name"` + // 角色ID + Id string `json:"id"` +} + +type KeystoneUserV2 struct { + // 用户ID + Id string `json:"id"` + // 用户名 + Name string `json:"name"` + // 用户username + Username string `json:"username"` + // 用户角色列表 + Roles []KeystoneRoleV2 `json:"roles"` +} + +type KeystoneTenantV2 struct { + // 项目ID + Id string `json:"id"` + // 项目名称 + Name string `json:"name"` + // 是否启用 + Enabled bool `json:"enabled"` + // 描述 + Description string `json:"description"` + // 项目归属域信息 + Domain struct { + // 域ID + Id string `json:"id"` + // 域名称 + Name string `json:"name"` + } `json:"domain"` +} + +type KeystoneTokenV2 struct { + // token + Id string `json:"id"` + // 过期时间(UTC) + Expires time.Time `json:"expires"` + // token有效的项目信息 + Tenant KeystoneTenantV2 `json:"tenant"` +} + +type KeystoneMetadataV2 struct { + // 是否为管理员 + IsAdmin int `json:"is_admin"` + // 角色 + Roles []string `json:"roles"` +} + +type KeystoneServiceCatalogV2 []KeystoneServiceV2 + +// Keystone token信息V2 +type TokenCredentialV2 struct { + // token信息 + Token KeystoneTokenV2 `json:"token"` + // 服务目录 + ServiceCatalog KeystoneServiceCatalogV2 `json:"service_catalog"` + // 认证用户信息 + User KeystoneUserV2 `json:"user"` + // 用户所属项目列表 + Tenants []KeystoneTenantV2 `json:"tenants"` + // 认证元数据 + Metadata KeystoneMetadataV2 `json:"metadata"` +} + +func (token *TokenCredentialV2) GetTokenString() string { + return token.Token.Id +} + +func (token *TokenCredentialV2) GetDomainId() string { + return api.DEFAULT_DOMAIN_ID +} + +func (token *TokenCredentialV2) GetDomainName() string { + return api.DEFAULT_DOMAIN_NAME +} + +func (token *TokenCredentialV2) GetTenantId() string { + return token.Token.Tenant.Id +} + +func (token *TokenCredentialV2) GetTenantName() string { + return token.Token.Tenant.Name +} + +func (token *TokenCredentialV2) GetProjectId() string { + return token.GetTenantId() +} + +func (token *TokenCredentialV2) GetProjectName() string { + return token.GetTenantName() +} + +func (token *TokenCredentialV2) GetProjectDomainId() string { + return api.DEFAULT_DOMAIN_ID +} + +func (token *TokenCredentialV2) GetProjectDomain() string { + return api.DEFAULT_DOMAIN_NAME +} + +func (token *TokenCredentialV2) GetUserName() string { + return token.User.Username +} + +func (token *TokenCredentialV2) GetUserId() string { + return token.User.Id +} + +func (token *TokenCredentialV2) GetRoles() []string { + roles := make([]string, 0) + for i := 0; i < len(token.User.Roles); i++ { + roles = append(roles, token.User.Roles[i].Name) + } + return roles +} + +func (token *TokenCredentialV2) GetRoleIds() []string { + roles := make([]string, 0) + for i := 0; i < len(token.User.Roles); i++ { + roles = append(roles, token.User.Roles[i].Id) + } + return roles +} + +func (this *TokenCredentialV2) GetExpires() time.Time { + return this.Token.Expires +} + +func (this *TokenCredentialV2) IsValid() bool { + return this.ValidDuration() > 0 +} + +func (this *TokenCredentialV2) ValidDuration() time.Duration { + return time.Until(this.Token.Expires) +} + +func (this *TokenCredentialV2) IsAdmin() bool { + for i := 0; i < len(this.User.Roles); i++ { + if this.User.Roles[i].Name == "admin" { + return true + } + } + return false +} + +func (this *TokenCredentialV2) GetRegions() []string { + return this.ServiceCatalog.getRegions() +} + +func (this *TokenCredentialV2) HasSystemAdminPrivilege() bool { + return this.IsAdmin() && this.GetTenantName() == "system" +} + +func (this *TokenCredentialV2) IsAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult { + if this.isAllow(scope, service, resource, action, extra...) { + return rbacutils.PolicyAllow + } else { + return rbacutils.PolicyDeny + } +} + +func (this *TokenCredentialV2) isAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) bool { + if scope == rbacutils.ScopeSystem || scope == rbacutils.ScopeDomain { + return this.HasSystemAdminPrivilege() + } else { + return true + } +} + +func (this *TokenCredentialV2) Len() int { + return this.ServiceCatalog.Len() +} + +func (this *TokenCredentialV2) GetServiceURL(service, region, zone, endpointType string) (string, error) { + return this.ServiceCatalog.GetServiceURL(service, region, zone, endpointType) +} + +func (this *TokenCredentialV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { + return this.ServiceCatalog.GetServiceURLs(service, region, zone, endpointType) +} + +func (this *TokenCredentialV2) GetInternalServices(region string) []string { + return nil +} + +func (this *TokenCredentialV2) GetExternalServices(region string) []ExternalService { + return nil +} + +func (this *TokenCredentialV2) GetServicesByInterface(region string, infType string) []ExternalService { + return nil +} + +func (this *TokenCredentialV2) GetEndpoints(region string, endpointType string) []Endpoint { + return nil +} + +func (this *TokenCredentialV2) GetServiceCatalog() IServiceCatalog { + return this.ServiceCatalog +} + +func stringArrayContains(arr []string, needle string) bool { + for i := 0; i < len(arr); i++ { + if arr[i] == needle { + return true + } + } + return false +} + +func (catalog KeystoneServiceCatalogV2) getRegions() []string { + regions := make([]string, 0) + for i := 0; i < len(catalog); i++ { + for j := 0; j < len(catalog[i].Endpoints); j++ { + r := catalog[i].Endpoints[j].Region + slash := strings.IndexByte(r, '/') + if slash > 0 { + r = r[:slash] + } + if !stringArrayContains(regions, r) { + regions = append(regions, r) + } + } + } + return regions +} + +func (catalog KeystoneServiceCatalogV2) getServiceEndpoint(service, region, zone string) (KeystoneEndpointV2, error) { + var selected KeystoneEndpointV2 + var findService bool + for i := 0; i < len(catalog); i++ { + if service == catalog[i].Type { + findService = true + if len(catalog[i].Endpoints) == 0 { + continue + } + if len(region) == 0 { + selected = catalog[i].Endpoints[0] + } else { + regionEps := make([]KeystoneEndpointV2, 0) + zoneEps := make([]KeystoneEndpointV2, 0) + if len(zone) > 0 { + zone = fmt.Sprintf("%s/%s", region, zone) + } + for j := 0; j < len(catalog[i].Endpoints); j++ { + if catalog[i].Endpoints[j].Region == region { + regionEps = append(regionEps, catalog[i].Endpoints[j]) + } else if len(zone) > 0 && catalog[i].Endpoints[j].Region == zone { + zoneEps = append(zoneEps, catalog[i].Endpoints[j]) + } + } + if len(zone) > 0 && len(zoneEps) > 0 { + selected = zoneEps[rand.Intn(len(zoneEps))] + } else if len(regionEps) > 0 { + selected = regionEps[rand.Intn(len(regionEps))] + } else { + return selected, fmt.Errorf("No such region %s", region) + } + } + return selected, nil + } + } + if findService { + return selected, fmt.Errorf("No default region") + } else { + return selected, fmt.Errorf("No such service %s", service) + } +} + +func (catalog KeystoneServiceCatalogV2) Len() int { + return len(catalog) +} + +func (catalog KeystoneServiceCatalogV2) GetServiceURL(service, region, zone, endpointType string) (string, error) { + ep, err := catalog.getServiceEndpoint(service, region, zone) + if err != nil { + return "", err + } + return ep.getURL(endpointType), nil +} + +func (catalog KeystoneServiceCatalogV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { + url, err := catalog.GetServiceURL(service, region, zone, endpointType) + if err != nil { + return nil, err + } + return []string{url}, nil +} + +func (catalog KeystoneServiceCatalogV2) GetInternalServices(region string) []string { + return nil +} + +func (catalog KeystoneServiceCatalogV2) GetExternalServices(region string) []ExternalService { + return nil +} + +func (catalog KeystoneServiceCatalogV2) GetServicesByInterface(region string, infType string) []ExternalService { + return nil +} + +func (ep KeystoneEndpointV2) getURL(epType string) string { + switch epType { + case "publicURL": + return ep.PublicURL + case "adminURL": + return ep.AdminURL + default: + return ep.InternalURL + } +} + +func (self *TokenCredentialV2) GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject { + return jsonutils.Marshal(self.GetServiceCatalog()) +} + +func (self *TokenCredentialV2) String() string { + token := SimplifyToken(self) + return token.String() +} + +func (self *TokenCredentialV2) IsZero() bool { + return len(self.GetUserId()) == 0 && len(self.GetProjectId()) == 0 +} + +func (self *TokenCredentialV2) ToJson() jsonutils.JSONObject { + return SimplifyToken(self).ToJson() +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token3.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token3.go new file mode 100644 index 0000000000..a234968c40 --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/token3.go @@ -0,0 +1,473 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +import ( + "fmt" + "math/rand" + "strings" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/utils" + + api "yunion.io/x/onecloud/pkg/apis/identity" + "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/util/rbacutils" +) + +const REGION_ZONE_SEP = '-' + +type KeystoneEndpointV3 struct { + // endpoint ID + // example: 75f4e36100184a5a8a3e36cb0f12aa87 + Id string `json:"id"` + // endpoint接口类型,目前定义了一下集中类型 + // + // | interface | 说明 | + // |-----------|--------------------------------------------------------| + // | internal | 内部接口,访问服务时默认用inernal类型的接口 | + // | public | 外部接口 | + // | admin | 管理类型接口,deprecated | + // | console | web控制台接口,指定显示在web控制台的外部服务的接口地址 | + // + Interface string `json:"interface"` + // 区域名称 + Region string `json:"region"` + // 区域ID + RegionId string `json:"region_id"` + // 接口URL + Url string `json:"url"` + // 接口名称 + Name string `json:"name"` +} + +type KeystoneServiceV3 struct { + // service ID + Id string `json:"id,omitempty"` + // service Name + Name string `json:"name,omitempty"` + // service Type,例如identity, compute等 + Type string `json:"type,omitempty"` + // service的访问endpoints + Endpoints []KeystoneEndpointV3 `json:"endpoints,omitempty"` +} + +type KeystoneDomainV3 api.SIdentityObject + +type KeystoneRoleV3 api.SIdentityObject + +type KeystoneProjectV3 struct { + // 项目ID + Id string + // 项目名称 + Name string + // 项目归属域 + Domain KeystoneDomainV3 +} + +type KeystoneUserV3 struct { + // 用户ID + Id string + // 用户名称 + Name string + // 用户归属域 + Domain KeystoneDomainV3 + // 用户密码过期时间 + PasswordExpiresAt time.Time + + // 用户的显式名称,通常为中文名 + Displayname string + // 用户Email + Email string + // 用户手机号 + Mobile string +} + +type KeystoneServiceCatalogV3 []KeystoneServiceV3 + +type KeystonePolicy struct { + // 项目范围的权限 + Project []string + // 域范围的权限 + Domain []string + // 系统范围的权限 + System []string +} + +type KeystoneTokenV3 struct { + // AutdiIds, 没有什么用 + // swagger:ignore + AuditIds []string `json:"audit_ids"` + // token过期时间 + ExpiresAt time.Time `json:"expires_at"` + // 是否为域的token + IsDomain bool `json:"is_domain,allowfalse"` + // token颁发时间 + IssuedAt time.Time `json:"issued_at"` + // 获取token的认证方式 + Methods []string `json:"methods"` + // token的关联项目,如果用户认证时scope为项目,则为改指定项目的信息 + Project KeystoneProjectV3 `json:"project"` + // token的关联用户在关联项目的权限信息,只有项目scope的token才有这个属性 + Policies KeystonePolicy `json:"policies"` + // token的关联用户在关联项目的角色列表,只有项目scope的token才有这个属性 + Roles []KeystoneRoleV3 `json:"roles"` + // token的关联用户信息 + User KeystoneUserV3 `json:"user"` + // 服务目录 + Catalog KeystoneServiceCatalogV3 `json:"catalog"` +} + +type TokenCredentialV3 struct { + // keystone V3 token + Token KeystoneTokenV3 `json:"token"` + + // swagger:ignore + Id string `json:"id"` +} + +func (token *TokenCredentialV3) GetTokenString() string { + return token.Id +} + +func (token *TokenCredentialV3) GetDomainId() string { + return token.Token.User.Domain.Id +} + +func (token *TokenCredentialV3) GetDomainName() string { + return token.Token.User.Domain.Name +} + +func (token *TokenCredentialV3) GetProjectId() string { + return token.Token.Project.Id +} + +func (token *TokenCredentialV3) GetProjectName() string { + return token.Token.Project.Name +} + +func (token *TokenCredentialV3) GetTenantId() string { + return token.Token.Project.Id +} + +func (token *TokenCredentialV3) GetTenantName() string { + return token.Token.Project.Name +} + +func (token *TokenCredentialV3) GetProjectDomainId() string { + return token.Token.Project.Domain.Id +} + +func (token *TokenCredentialV3) GetProjectDomain() string { + return token.Token.Project.Domain.Name +} + +func (token *TokenCredentialV3) GetUserName() string { + return token.Token.User.Name +} + +func (token *TokenCredentialV3) GetUserId() string { + return token.Token.User.Id +} + +func (token *TokenCredentialV3) GetRoles() []string { + roles := make([]string, 0) + for i := 0; i < len(token.Token.Roles); i++ { + roles = append(roles, token.Token.Roles[i].Name) + } + return roles +} + +func (token *TokenCredentialV3) GetRoleIds() []string { + roles := make([]string, 0) + for i := 0; i < len(token.Token.Roles); i++ { + roles = append(roles, token.Token.Roles[i].Id) + } + return roles +} + +func (this *TokenCredentialV3) GetExpires() time.Time { + return this.Token.ExpiresAt +} + +func (this *TokenCredentialV3) IsValid() bool { + return len(this.Id) > 0 && this.ValidDuration() > 0 +} + +func (this *TokenCredentialV3) ValidDuration() time.Duration { + return time.Until(this.Token.ExpiresAt) +} + +func (this *TokenCredentialV3) IsAdmin() bool { + for i := 0; i < len(this.Token.Roles); i++ { + if this.Token.Roles[i].Name == "admin" { + return true + } + } + return false +} + +func (this *TokenCredentialV3) HasSystemAdminPrivilege() bool { + return this.IsAdmin() && this.GetTenantName() == "system" +} + +func (this *TokenCredentialV3) IsAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult { + if this.isAllow(scope, service, resource, action, extra...) { + return rbacutils.PolicyAllow + } else { + return rbacutils.PolicyDeny + } +} + +func (this *TokenCredentialV3) isAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) bool { + if scope == rbacutils.ScopeSystem || scope == rbacutils.ScopeDomain { + return this.HasSystemAdminPrivilege() + } else { + return true + } +} + +func (this *TokenCredentialV3) GetRegions() []string { + return this.Token.Catalog.getRegions() +} + +func (this *TokenCredentialV3) Len() int { + return this.Token.Catalog.Len() +} + +func (this *TokenCredentialV3) GetServiceURL(service, region, zone, endpointType string) (string, error) { + return this.Token.Catalog.GetServiceURL(service, region, zone, endpointType) +} + +func (this *TokenCredentialV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { + return this.Token.Catalog.GetServiceURLs(service, region, zone, endpointType) +} + +func (this *TokenCredentialV3) GetInternalServices(region string) []string { + return this.Token.Catalog.GetInternalServices(region) +} + +func (this *TokenCredentialV3) GetExternalServices(region string) []ExternalService { + return this.Token.Catalog.GetExternalServices(region) +} + +func (this *TokenCredentialV3) GetServicesByInterface(region string, infType string) []ExternalService { + return this.Token.Catalog.GetServicesByInterface(region, infType) +} + +func (this *TokenCredentialV3) GetEndpoints(region string, endpointType string) []Endpoint { + return this.Token.Catalog.getEndpoints(region, endpointType) +} + +func (this *TokenCredentialV3) GetServiceCatalog() IServiceCatalog { + return this.Token.Catalog +} + +func (catalog KeystoneServiceCatalogV3) GetInternalServices(region string) []string { + services := make([]string, 0) + for i := 0; i < len(catalog); i++ { + exit := false + for j := 0; j < len(catalog[i].Endpoints); j++ { + if catalog[i].Endpoints[j].RegionId == region && + catalog[i].Endpoints[j].Interface == "internal" { + exit = true + break + } + } + if exit { + services = append(services, catalog[i].Type) + } + } + return services +} + +func (catalog KeystoneServiceCatalogV3) GetExternalServices(region string) []ExternalService { + return catalog.GetServicesByInterface(region, "console") +} + +func (catalog KeystoneServiceCatalogV3) GetServicesByInterface(region string, infType string) []ExternalService { + services := make([]ExternalService, 0) + for i := 0; i < len(catalog); i++ { + for j := 0; j < len(catalog[i].Endpoints); j++ { + if catalog[i].Endpoints[j].RegionId == region && + catalog[i].Endpoints[j].Interface == infType && + len(catalog[i].Endpoints[j].Name) > 0 { + srv := ExternalService{ + Name: catalog[i].Endpoints[j].Name, + Url: catalog[i].Endpoints[j].Url, + Service: catalog[i].Type, + } + services = append(services, srv) + } + } + } + return services +} + +func (catalog KeystoneServiceCatalogV3) getRegions() []string { + regions := make([]string, 0) + for i := 0; i < len(catalog); i++ { + for j := 0; j < len(catalog[i].Endpoints); j++ { + if len(catalog[i].Endpoints[j].RegionId) > 0 && !stringArrayContains(regions, catalog[i].Endpoints[j].RegionId) { + regions = append(regions, catalog[i].Endpoints[j].RegionId) + } + } + } + return regions +} + +func (catalog KeystoneServiceCatalogV3) getEndpoints(region string, endpointType string) []Endpoint { + endpoints := make([]Endpoint, 0) + for i := 0; i < len(catalog); i++ { + for j := 0; j < len(catalog[i].Endpoints); j++ { + endpoint := catalog[i].Endpoints[j] + if (endpoint.RegionId == region || strings.HasPrefix(endpoint.RegionId, region+"-")) && endpoint.Interface == endpointType { + endpoints = append(endpoints, Endpoint{ + endpoint.Id, + endpoint.RegionId, + catalog[i].Id, + catalog[i].Name, + endpoint.Url, + endpoint.Interface, + }) + } + } + } + + return endpoints +} + +func RegionID(region, zone string) string { + if len(region) > 0 && len(zone) > 0 { + return fmt.Sprintf("%s%c%s", region, REGION_ZONE_SEP, zone) + } else { + return region + } +} + +func Id2RegionZone(id string) (string, string) { + pos := strings.IndexByte(id, REGION_ZONE_SEP) + if pos > 0 { + return id[:pos], id[pos+1:] + } else { + return id, "" + } +} + +func (catalog KeystoneServiceCatalogV3) Len() int { + return len(catalog) +} + +func (catalog KeystoneServiceCatalogV3) GetServiceURL(service, region, zone, endpointType string) (string, error) { + urls, err := catalog.GetServiceURLs(service, region, zone, endpointType) + if err != nil { + return "", err + } + return urls[rand.Intn(len(urls))], nil +} + +func (catalog KeystoneServiceCatalogV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { + if endpointType == "" { + endpointType = "internalURL" + } + for i := 0; i < len(catalog); i++ { + if service == catalog[i].Type { + if len(catalog[i].Endpoints) == 0 { + continue + } + var selected []string + regeps := make(map[string][]string) + regionzone := "" + if len(zone) > 0 { + regionzone = RegionID(region, zone) + } + for j := 0; j < len(catalog[i].Endpoints); j++ { + ep := catalog[i].Endpoints[j] + if strings.HasPrefix(endpointType, ep.Interface) && + (ep.RegionId == region || + ep.RegionId == regionzone || + len(region) == 0) { + _, ok := regeps[ep.RegionId] + if !ok { + regeps[ep.RegionId] = make([]string, 0) + } + regeps[ep.RegionId] = append(regeps[ep.RegionId], ep.Url) + } + } + if len(region) == 0 { + if len(regeps) >= 1 { + for _, v := range regeps { + selected = v + break + } + } else { + return nil, fmt.Errorf("No default region for region(%s) zone(%s)", region, zone) + } + } else { + _, ok := regeps[regionzone] + if ok { + selected = regeps[regionzone] + } else { + selected, ok = regeps[region] + if !ok { + return nil, fmt.Errorf("No valid %s endpoints for %s in region %s", endpointType, service, RegionID(region, zone)) + } + } + } + return selected, nil + } + } + return nil, errors.Wrapf(httperrors.ErrNotFound, "No such service %s", service) +} + +func (self *TokenCredentialV3) GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject { + catalog := self.Token.Catalog + ret := make([]map[string]interface{}, 0) + for i := 0; i < len(catalog); i++ { + if !utils.IsInStringArray(catalog[i].Type, serviceTypes) { + continue + } + neps := make([]KeystoneEndpointV3, 0) + for j := 0; j < len(catalog[i].Endpoints); j++ { + if catalog[i].Endpoints[j].Region != region { + continue + } + neps = append(neps, catalog[i].Endpoints[j]) + } + if len(neps) > 0 { + data := map[string]interface{}{ + "type": catalog[i].Type, + "endpoints": neps, + } + ret = append(ret, data) + } + } + return jsonutils.Marshal(ret) +} + +func (self *TokenCredentialV3) String() string { + token := SimplifyToken(self) + return token.String() +} + +func (self *TokenCredentialV3) IsZero() bool { + return len(self.GetUserId()) == 0 && len(self.GetProjectId()) == 0 +} + +func (self *TokenCredentialV3) ToJson() jsonutils.JSONObject { + return SimplifyToken(self).ToJson() +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/tokensimple.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/tokensimple.go new file mode 100644 index 0000000000..7419bdde04 --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli/tokensimple.go @@ -0,0 +1,229 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package oscli + +import ( + "fmt" + "reflect" + "strings" + "time" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/gotypes" + + api "yunion.io/x/onecloud/pkg/apis/identity" + "yunion.io/x/onecloud/pkg/util/rbacutils" +) + +type SSimpleToken struct { + Token string + Domain string + DomainId string + User string + UserId string + Project string `json:"tenant"` + ProjectId string `json:"tenant_id"` + + ProjectDomain string + ProjectDomainId string + + Roles string + RoleIds string + Expires time.Time +} + +func (self *SSimpleToken) GetTokenString() string { + return self.Token +} + +func (self *SSimpleToken) GetDomainId() string { + return self.DomainId +} + +func (self *SSimpleToken) GetDomainName() string { + return self.Domain +} + +func (self *SSimpleToken) GetTenantId() string { + return self.ProjectId +} + +func (self *SSimpleToken) GetTenantName() string { + return self.Project +} + +func (self *SSimpleToken) GetProjectId() string { + return self.ProjectId +} + +func (self *SSimpleToken) GetProjectName() string { + return self.Project +} + +func (self *SSimpleToken) GetProjectDomainId() string { + if len(self.ProjectDomainId) == 0 { + return api.DEFAULT_DOMAIN_ID + } + return self.ProjectDomainId +} + +func (self *SSimpleToken) GetProjectDomain() string { + if len(self.ProjectDomain) == 0 { + return api.DEFAULT_DOMAIN_NAME + } + return self.ProjectDomain +} + +func (self *SSimpleToken) GetUserId() string { + return self.UserId +} + +func (self *SSimpleToken) GetUserName() string { + return self.User +} + +func (self *SSimpleToken) GetRoles() []string { + return strings.Split(self.Roles, ",") +} + +func (self *SSimpleToken) GetRoleIds() []string { + return strings.Split(self.RoleIds, ",") +} + +func (self *SSimpleToken) GetExpires() time.Time { + return self.Expires +} + +func (self *SSimpleToken) IsValid() bool { + return self.ValidDuration() > 0 +} + +func (self *SSimpleToken) ValidDuration() time.Duration { + return time.Until(self.Expires) +} + +func (self *SSimpleToken) IsAdmin() bool { + roles := self.GetRoles() + for i := 0; i < len(roles); i++ { + if roles[i] == "admin" { + return true + } + } + return false +} + +func (self *SSimpleToken) HasSystemAdminPrivilege() bool { + return self.IsAdmin() && self.Project == "system" +} + +func (this *SSimpleToken) IsAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) rbacutils.SPolicyResult { + if this.isAllow(scope, service, resource, action, extra...) { + return rbacutils.PolicyAllow + } else { + return rbacutils.PolicyDeny + } +} + +func (this *SSimpleToken) isAllow(scope rbacutils.TRbacScope, service string, resource string, action string, extra ...string) bool { + if scope == rbacutils.ScopeSystem || scope == rbacutils.ScopeDomain { + return this.HasSystemAdminPrivilege() + } else { + return true + } +} + +func (self *SSimpleToken) GetRegions() []string { + return nil +} + +func (self *SSimpleToken) Len() int { + return 0 +} + +func (self *SSimpleToken) GetServiceURL(service, region, zone, endpointType string) (string, error) { + return "", fmt.Errorf("Not available") +} + +func (self *SSimpleToken) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) { + return nil, fmt.Errorf("Not available") +} + +func (this *SSimpleToken) GetServicesByInterface(region string, infType string) []ExternalService { + return nil +} + +func (self *SSimpleToken) GetInternalServices(region string) []string { + return nil +} + +func (self *SSimpleToken) GetExternalServices(region string) []ExternalService { + return nil +} + +func (this *SSimpleToken) GetEndpoints(region string, endpointType string) []Endpoint { + return nil +} + +func (this *SSimpleToken) GetServiceCatalog() IServiceCatalog { + return nil +} + +func SimplifyToken(token TokenCredential) TokenCredential { + simToken, ok := token.(*SSimpleToken) + if ok { + return simToken + } + return &SSimpleToken{Token: token.GetTokenString(), + Domain: token.GetDomainName(), + DomainId: token.GetDomainId(), + User: token.GetUserName(), + UserId: token.GetUserId(), + Project: token.GetProjectName(), + ProjectId: token.GetProjectId(), + + ProjectDomain: token.GetProjectDomain(), + ProjectDomainId: token.GetProjectDomainId(), + + Roles: strings.Join(token.GetRoles(), ","), + RoleIds: strings.Join(token.GetRoleIds(), ","), + Expires: token.GetExpires(), + } +} + +func (self *SSimpleToken) GetCatalogData(serviceTypes []string, region string) jsonutils.JSONObject { + return nil +} + +func (self *SSimpleToken) String() string { + return self.ToJson().String() +} + +func (self *SSimpleToken) IsZero() bool { + return len(self.UserId) == 0 && len(self.ProjectId) == 0 +} + +func (self *SSimpleToken) ToJson() jsonutils.JSONObject { + return jsonutils.Marshal(self) +} + +var TokenCredentialType reflect.Type + +func init() { + TokenCredentialType = reflect.TypeOf((*TokenCredential)(nil)).Elem() + + gotypes.RegisterSerializable(TokenCredentialType, func() gotypes.ISerializable { + return &SSimpleToken{} + }) +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/project.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/project.go index 842ac072b4..c50ff3f81b 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/project.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/project.go @@ -21,10 +21,11 @@ import ( "yunion.io/x/log" "yunion.io/x/pkg/errors" - api "yunion.io/x/cloudmux/pkg/apis/compute" - "yunion.io/x/onecloud/pkg/mcclient" - "yunion.io/x/cloudmux/pkg/multicloud" "yunion.io/x/onecloud/pkg/util/httputils" + + api "yunion.io/x/cloudmux/pkg/apis/compute" + "yunion.io/x/cloudmux/pkg/multicloud" + "yunion.io/x/cloudmux/pkg/multicloud/openstack/oscli" ) type SProject struct { @@ -61,7 +62,7 @@ func (p *SProject) GetStatus() string { return api.EXTERNAL_PROJECT_STATUS_AVAILABLE } -func (p *SProject) getToken() (mcclient.TokenCredential, error) { +func (p *SProject) getToken() (oscli.TokenCredential, error) { return p.client.getProjectToken(p.Id, p.Name) } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/storagecache.go index db7e5b1dbb..b684099452 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/openstack/storagecache.go @@ -25,7 +25,6 @@ import ( api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -89,11 +88,11 @@ func (cache *SStoragecache) GetPath() string { return "" } -func (cache *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return cache.uploadImage(ctx, userCred, image, callback) +func (cache *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return cache.uploadImage(ctx, image, callback) } -func (cache *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (cache *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { reader, size, err := image.GetReader(image.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") @@ -148,6 +147,6 @@ func (cache *SStoragecache) CreateIImage(snapshoutId, imageName, osType, imageDe return nil, cloudprovider.ErrNotImplemented } -func (cache *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (cache *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/proxmox/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/proxmox/storagecache.go index 3ac36744e9..829af484a9 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/proxmox/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/proxmox/storagecache.go @@ -20,7 +20,6 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/cloudmux/pkg/cloudprovider" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/cloudmux/pkg/multicloud" ) @@ -86,10 +85,10 @@ func (self *SStoragecache) CreateIImage(snpId, imageName, osType, imageDesc stri return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(float32)) (string, error) { return "", cloudprovider.ErrNotSupported } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/qcloud/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/qcloud/storagecache.go index 455e339d0f..4016727181 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/qcloud/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/qcloud/storagecache.go @@ -27,7 +27,6 @@ import ( api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -78,7 +77,7 @@ func (self *SStoragecache) CreateIImage(snapshoutId, imageName, osType, imageDes return nil, nil } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { //return self.downloadImage(userCred, imageId, extId, path) return nil, nil } @@ -123,8 +122,8 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return self.uploadImage(ctx, userCred, image, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return self.uploadImage(ctx, image, callback) } func (self *SRegion) getCosUrl(bucket, object string) string { @@ -132,7 +131,7 @@ func (self *SRegion) getCosUrl(bucket, object string) string { return fmt.Sprintf("http://%s-%s.cos.%s.myqcloud.com/%s", bucket, self.client.appId, self.Region, object) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { reader, sizeBytes, err := image.GetReader(image.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/ucloud/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/ucloud/storagecache.go index a4e68413ff..107ef9bdaf 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/ucloud/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/ucloud/storagecache.go @@ -28,7 +28,6 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -97,16 +96,16 @@ func (self *SStoragecache) CreateIImage(snapshotId, imageName, osType, imageDesc return nil, cloudprovider.ErrNotSupported } -func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (self *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented } // https://docs.ucloud.cn/api/uhost-api/import_custom_image -func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return self.uploadImage(ctx, userCred, image, callback) +func (self *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return self.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { if len(image.OsVersion) == 0 { return "", fmt.Errorf("uploadImage os version is empty") } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/zstack/storagecache.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/zstack/storagecache.go index 14065cd4d4..4953ba79c6 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/zstack/storagecache.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/zstack/storagecache.go @@ -26,7 +26,6 @@ import ( api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/util/qemuimg" ) @@ -97,11 +96,11 @@ func (scache *SStoragecache) GetPath() string { return "" } -func (scache *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { - return scache.uploadImage(ctx, userCred, image, callback) +func (scache *SStoragecache) UploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { + return scache.uploadImage(ctx, image, callback) } -func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, image *cloudprovider.SImageCreateOption, callback func(progress float32)) (string, error) { reader, size, err := image.GetReader(image.ImageId, string(qemuimg.QCOW2)) if err != nil { return "", errors.Wrapf(err, "GetReader") @@ -127,6 +126,6 @@ func (scache *SStoragecache) CreateIImage(snapshoutId, imageName, osType, imageD return nil, cloudprovider.ErrNotImplemented } -func (scache *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) { +func (scache *SStoragecache) DownloadImage(imageId string, extId string, path string) (jsonutils.JSONObject, error) { return nil, cloudprovider.ErrNotImplemented }