From 2b45e2624352d9dd40a56640d30175e49cea0275 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Fri, 10 Apr 2020 00:04:23 +0800 Subject: [PATCH 1/3] fix: 1. allow share proxysetting 2. claim proxsetting as domain resources --- cmd/climc/shell/proxysettings.go | 20 ++++++++++++++ pkg/apis/cloudcommon/proxy/proxy.go | 8 ++++++ pkg/apis/compute/cloudaccount.go | 3 +-- pkg/cloudcommon/db/proxy/proxysetting.go | 14 ++++++++++ pkg/compute/models/cloudaccounts.go | 33 ++++++++++++++++++------ pkg/mcclient/options/cloudaccounts.go | 4 ++- pkg/mcclient/options/proxysettings.go | 10 +++++++ 7 files changed, 81 insertions(+), 11 deletions(-) diff --git a/cmd/climc/shell/proxysettings.go b/cmd/climc/shell/proxysettings.go index dbee6aefc3..88c6cf25f5 100644 --- a/cmd/climc/shell/proxysettings.go +++ b/cmd/climc/shell/proxysettings.go @@ -15,6 +15,8 @@ package shell import ( + "yunion.io/x/jsonutils" + "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/mcclient/options" @@ -78,4 +80,22 @@ func init() { printObject(proxysetting) return nil }) + R(&options.ProxySettingPublicOptions{}, "proxysetting-public", "Make proxysetting public", func(s *mcclient.ClientSession, opts *options.ProxySettingPublicOptions) error { + params := jsonutils.Marshal(opts) + result, err := modules.ProxySettings.PerformAction(s, opts.ID, "public", params) + if err != nil { + return err + } + printObject(result) + return nil + }) + R(&options.ProxySettingPrivateOptions{}, "proxysetting-private", "Make proxysetting private", func(s *mcclient.ClientSession, opts *options.ProxySettingPrivateOptions) error { + params := jsonutils.Marshal(opts) + result, err := modules.ProxySettings.PerformAction(s, opts.ID, "private", params) + if err != nil { + return err + } + printObject(result) + return nil + }) } diff --git a/pkg/apis/cloudcommon/proxy/proxy.go b/pkg/apis/cloudcommon/proxy/proxy.go index 2419361316..c29875d7a4 100644 --- a/pkg/apis/cloudcommon/proxy/proxy.go +++ b/pkg/apis/cloudcommon/proxy/proxy.go @@ -40,6 +40,14 @@ func (ps *SProxySetting) IsZero() bool { return false } +type ProxySettingResourceInput struct { + // 代理配置 + ProxySetting string `json:"proxy_setting"` + // Deprecated + // swagger:ignore + ProxySettingId string `json:"proxy_setting_id" deprecated-by:"proxy_setting"` +} + type ProxySettingTestInput struct { HttpProxy string HttpsProxy string diff --git a/pkg/apis/compute/cloudaccount.go b/pkg/apis/compute/cloudaccount.go index 05579df1e3..096b00be67 100644 --- a/pkg/apis/compute/cloudaccount.go +++ b/pkg/apis/compute/cloudaccount.go @@ -80,8 +80,7 @@ type CloudaccountCreateInput struct { // 额外信息,例如账单的access key Options *jsonutils.JSONObject `json:"options"` - // 代理配置 - ProxySettingId string `json:"proxy_setting_id"` + proxyapi.ProxySettingResourceInput cloudprovider.SCloudaccount cloudprovider.SCloudaccountCredential diff --git a/pkg/cloudcommon/db/proxy/proxysetting.go b/pkg/cloudcommon/db/proxy/proxysetting.go index 2dc2195741..632ee4cfda 100644 --- a/pkg/cloudcommon/db/proxy/proxysetting.go +++ b/pkg/cloudcommon/db/proxy/proxysetting.go @@ -12,6 +12,7 @@ import ( "golang.org/x/net/http/httpproxy" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -203,3 +204,16 @@ var referrersMen []db.IModelManager func RegisterReferrer(man db.IModelManager) { referrersMen = append(referrersMen, man) } + +func ValidateProxySettingResourceInput(userCred mcclient.TokenCredential, input proxyapi.ProxySettingResourceInput) (*SProxySetting, proxyapi.ProxySettingResourceInput, error) { + m, err := ProxySettingManager.FetchByIdOrName(userCred, input.ProxySetting) + if err != nil { + if errors.Cause(err) == sql.ErrNoRows { + return nil, input, errors.Wrapf(httperrors.ErrResourceNotFound, "%s %s", ProxySettingManager.Keyword(), input.ProxySetting) + } else { + return nil, input, errors.Wrapf(err, "ProxySettingManager.FetchByIdOrName") + } + } + input.ProxySetting = m.GetId() + return m.(*SProxySetting), input, nil +} diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index 70344ed4b4..44c6b44af8 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -318,11 +318,30 @@ func (self *SCloudaccount) ValidateUpdateData(ctx context.Context, userCred mccl "proxy_setting", proxy.ProxySettingManager.Keyword(), userCred, - ).Optional(true) + ) + v.Optional(true) if err := v.Validate(data); err != nil { return nil, err } + proxySetting := v.Model.(*proxy.SProxySetting) + + if proxySetting != nil { + // updated proxy setting, so do the check + proxyFunc := proxySetting.HttpTransportProxyFunc() + secret, _ := self.getPassword() + _, err := cloudprovider.IsValidCloudAccount(cloudprovider.ProviderConfig{ + Vendor: self.Provider, + URL: self.AccessUrl, + Account: self.Account, + Secret: secret, + ProxyFunc: proxyFunc, + }) + if err != nil { + return nil, httperrors.NewInputParameterError("invalid proxy setting %s", err) + } + } + return self.SEnabledStatusStandaloneResourceBase.ValidateUpdateData(ctx, userCred, query, data) } @@ -382,16 +401,14 @@ func (manager *SCloudaccountManager) ValidateCreateData(ctx context.Context, use var proxyFunc httputils.TransportProxyFunc { - if input.ProxySettingId == "" { - input.ProxySettingId = proxyapi.ProxySettingId_DIRECT + if input.ProxySetting == "" { + input.ProxySetting = proxyapi.ProxySettingId_DIRECT } - m, err := proxy.ProxySettingManager.FetchByIdOrName(userCred, input.ProxySettingId) + var proxySetting *proxy.SProxySetting + proxySetting, input.ProxySettingResourceInput, err = proxy.ValidateProxySettingResourceInput(userCred, input.ProxySettingResourceInput) if err != nil { - return input, httperrors.NewInputParameterError("fetch proxysetting %s: %s", - input.ProxySettingId, err) + return input, httperrors.NewInputParameterError("fetch proxysetting %s: %s", input.ProxySetting, err) } - proxySetting := m.(*proxy.SProxySetting) - input.ProxySettingId = proxySetting.Id proxyFunc = proxySetting.HttpTransportProxyFunc() } accountId, err := cloudprovider.IsValidCloudAccount(cloudprovider.ProviderConfig{ diff --git a/pkg/mcclient/options/cloudaccounts.go b/pkg/mcclient/options/cloudaccounts.go index ed2c7ba57d..32106a2a53 100644 --- a/pkg/mcclient/options/cloudaccounts.go +++ b/pkg/mcclient/options/cloudaccounts.go @@ -83,6 +83,8 @@ type SCloudAccountCreateBaseOptions struct { SyncIntervalSeconds int `help:"Interval to synchronize if auto sync is enable" metavar:"SECONDS"` ProjectDomain string `help:"domain for this account"` + + ProxySetting string `help:"proxy setting id or name" json:"proxy_setting"` } type SVMwareCloudAccountCreateOptions struct { @@ -229,7 +231,7 @@ type SCloudAccountUpdateBaseOptions struct { SyncIntervalSeconds int `help:"auto synchornize interval in seconds"` AutoCreateProject *bool `help:"automatically create local project for new remote project"` - ProxySetting string `help:"proxy setting name or id"` + ProxySetting string `help:"proxy setting name or id" json:"proxy_setting"` Desc string `help:"Description" json:"description" token:"desc"` } diff --git a/pkg/mcclient/options/proxysettings.go b/pkg/mcclient/options/proxysettings.go index 22a98ac920..aff1019ada 100644 --- a/pkg/mcclient/options/proxysettings.go +++ b/pkg/mcclient/options/proxysettings.go @@ -46,3 +46,13 @@ type ProxySettingTestOptions struct { type ProxySettingListOptions struct { BaseListOptions } + +type ProxySettingPublicOptions struct { + ProxySettingGetOptions + Scope string `json:"scope" help:"share scope" choices:"domain|system"` + SharedDomain []string `json:"share"` +} + +type ProxySettingPrivateOptions struct { + ProxySettingGetOptions +} From cae9564f298819e9e48038191bdfa7df5be79cbf Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Mon, 13 Apr 2020 00:17:15 +0800 Subject: [PATCH 2/3] update vendor --- go.mod | 6 +-- go.sum | 10 +++-- vendor/modules.txt | 6 +-- vendor/yunion.io/x/jsonutils/consts.go | 19 ++++++++ vendor/yunion.io/x/jsonutils/dict.go | 43 +++++++++++++++++++ vendor/yunion.io/x/jsonutils/marshal.go | 39 +++++++++++++++++ vendor/yunion.io/x/jsonutils/unmarshal.go | 2 +- vendor/yunion.io/x/log/log.go | 15 +++++-- vendor/yunion.io/x/pkg/errors/errors.go | 4 ++ .../x/pkg/util/reflectutils/jsonfield.go | 30 +++++++++++-- 10 files changed, 156 insertions(+), 18 deletions(-) create mode 100644 vendor/yunion.io/x/jsonutils/consts.go diff --git a/go.mod b/go.mod index e7454c0f5b..bfe09c904f 100644 --- a/go.mod +++ b/go.mod @@ -129,9 +129,9 @@ require ( k8s.io/cluster-bootstrap v0.17.3 k8s.io/kubernetes v1.16.0 yunion.io/x/executor v0.0.0-20200227030256-a18417815e74 - yunion.io/x/jsonutils v0.0.0-20200303051356-aa609aba0cda - yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d - yunion.io/x/pkg v0.0.0-20200304112442-9dae9351325e + yunion.io/x/jsonutils v0.0.0-20200330063846-589d9924bb8b + yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3 + yunion.io/x/pkg v0.0.0-20200403115157-880d716ed624 yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e yunion.io/x/sqlchemy v0.0.0-20200312002602-1177cd8fbc57 yunion.io/x/structarg v0.0.0-20190809075558-115bed041de3 diff --git a/go.sum b/go.sum index e7881bac7e..c15caa27ee 100644 --- a/go.sum +++ b/go.sum @@ -1015,16 +1015,18 @@ vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc/go.mod h1:so/NYdZXCz+E3ZpW0uAoCj yunion.io/x/executor v0.0.0-20200227030256-a18417815e74 h1:A15C6VdVRWvmQ9pAJHrUs9yan5qKlYH7uaRxHg1kRbk= yunion.io/x/executor v0.0.0-20200227030256-a18417815e74/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws= yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634= -yunion.io/x/jsonutils v0.0.0-20200303051356-aa609aba0cda h1:wSwQj3MDGchGYM2RAo1riYhrTz4apH+5XyBnchuDi84= -yunion.io/x/jsonutils v0.0.0-20200303051356-aa609aba0cda/go.mod h1:T7kxQJR13+t7z0TuT+Wzd7MTxBOk2H9c0pO1ONQSv90= +yunion.io/x/jsonutils v0.0.0-20200330063846-589d9924bb8b h1:mt0TOKRk76yeH0whJfmKsceXBuudXLjvoj8NKGTqpEU= +yunion.io/x/jsonutils v0.0.0-20200330063846-589d9924bb8b/go.mod h1:T7kxQJR13+t7z0TuT+Wzd7MTxBOk2H9c0pO1ONQSv90= yunion.io/x/log v0.0.0-20190514041436-04ce53b17c6b/go.mod h1:+gauLs73omeJAPlsXcevLsJLKixV+sR/E7WSYTSx1fE= yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d h1:59zrDL7Ft+hDukguJRmLr/Gdu/9V75x+yX99ovZwfaA= yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U= +yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3 h1:5Wc5hkB8PtMudmHuzCyok960RuOa9I55imIGrigSdjs= +yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3/go.mod h1:LC6f/4FozL0iaAbnFt2eDX9jlsyo3WiOUPm03d7+U4U= yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20200302034534-fdf44d54b070/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= -yunion.io/x/pkg v0.0.0-20200304112442-9dae9351325e h1:rBfX77+VEBVpe6Xxy2gDa4WB7qbtndWvXcrVKzleF84= -yunion.io/x/pkg v0.0.0-20200304112442-9dae9351325e/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= +yunion.io/x/pkg v0.0.0-20200403115157-880d716ed624 h1:yPayJlTJHOP0vKY6ovliZyTwJY9RvdWEF+QDqnxt/iw= +yunion.io/x/pkg v0.0.0-20200403115157-880d716ed624/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo= yunion.io/x/sqlchemy v0.0.0-20200312002602-1177cd8fbc57 h1:KtQAuLJ00RSUVqkiRmJ1DiDABiw0U3xxXnzD3lGavaY= diff --git a/vendor/modules.txt b/vendor/modules.txt index 8dc6c633f0..fd561accf7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -924,12 +924,12 @@ sigs.k8s.io/yaml yunion.io/x/executor/apis yunion.io/x/executor/client yunion.io/x/executor/server -# yunion.io/x/jsonutils v0.0.0-20200303051356-aa609aba0cda +# yunion.io/x/jsonutils v0.0.0-20200330063846-589d9924bb8b yunion.io/x/jsonutils -# yunion.io/x/log v0.0.0-20190629062853-9f6483a7103d +# yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3 yunion.io/x/log yunion.io/x/log/hooks -# yunion.io/x/pkg v0.0.0-20200304112442-9dae9351325e +# yunion.io/x/pkg v0.0.0-20200403115157-880d716ed624 yunion.io/x/pkg/errors yunion.io/x/pkg/gotypes yunion.io/x/pkg/prettytable diff --git a/vendor/yunion.io/x/jsonutils/consts.go b/vendor/yunion.io/x/jsonutils/consts.go new file mode 100644 index 0000000000..f5f2721bed --- /dev/null +++ b/vendor/yunion.io/x/jsonutils/consts.go @@ -0,0 +1,19 @@ +// 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 jsonutils + +const ( + TAG_DEPRECATED_BY = "deprecated-by" +) diff --git a/vendor/yunion.io/x/jsonutils/dict.go b/vendor/yunion.io/x/jsonutils/dict.go index 87db0ca23b..9f15f3c8c3 100644 --- a/vendor/yunion.io/x/jsonutils/dict.go +++ b/vendor/yunion.io/x/jsonutils/dict.go @@ -35,3 +35,46 @@ func (dict *JSONDict) UpdateDefault(json JSONObject) { } } } + +func Diff(a, b *JSONDict) (aNoB, aDiffB, aAndB, bNoA *JSONDict) { + keysA := a.SortedKeys() + keysB := b.SortedKeys() + aNoB = NewDict() + aDiffB = NewDict() + aAndB = NewDict() + bNoA = NewDict() + + i := 0 + j := 0 + for i < len(keysA) || j < len(keysB) { + if i < len(keysA) && j < len(keysB) { + keyA := keysA[i] + keyB := keysB[j] + if keyA > keyB { + aNoB.data[keyA] = a.data[keyA] + i += 1 + } else if keyA < keyB { + bNoA.data[keyB] = b.data[keyB] + j += 1 + } else { + valA := a.data[keysA[i]].String() + valB := b.data[keysB[i]].String() + if valA != valB { + aDiffB.data[keyA] = NewArray(a.data[keyA], b.data[keyB]) + } else { + aAndB.data[keyA] = a.data[keyA] + } + i += 1 + j += 1 + } + } else if i < len(keysA) { + aNoB.data[keysA[i]] = a.data[keysA[i]] + i = i + 1 + } else if j < len(keysB) { + bNoA.data[keysB[j]] = b.data[keysB[j]] + j = j + 1 + } + } + + return +} diff --git a/vendor/yunion.io/x/jsonutils/marshal.go b/vendor/yunion.io/x/jsonutils/marshal.go index 3392a8f018..e6bc0aeaeb 100644 --- a/vendor/yunion.io/x/jsonutils/marshal.go +++ b/vendor/yunion.io/x/jsonutils/marshal.go @@ -83,21 +83,60 @@ func marshalStruct(val reflect.Value, info *reflectutils.SStructFieldInfo) JSONO } } +func findValueByKey(pairs []JSONPair, key string) JSONObject { + for i := range pairs { + if pairs[i].key == key { + return pairs[i].val + } + } + return nil +} + func struct2JSONPairs(val reflect.Value) []JSONPair { fields := reflectutils.FetchStructFieldValueSet(val) objPairs := make([]JSONPair, 0, len(fields)) + depFields := make(map[string]string) for i := 0; i < len(fields); i += 1 { jsonInfo := fields[i].Info if jsonInfo.Ignore { continue } key := jsonInfo.MarshalName() + if deprecatedBy, ok := fields[i].Info.Tags[TAG_DEPRECATED_BY]; ok { + depFields[key] = deprecatedBy + continue + } val := marshalValue(fields[i].Value, jsonInfo) if val != nil && val != JSONNull { objPair := JSONPair{key: key, val: val} objPairs = append(objPairs, objPair) } } + depPairs := make([]JSONPair, 0, len(depFields)) + for depKey, key := range depFields { + findLoop := false + for { + if okey, ok := depFields[key]; ok { + if okey == depKey { + // loop detected + findLoop = true + break + } + key = okey + } else { + break + } + } + if findLoop { + continue + } + val := findValueByKey(objPairs, key) + if val != nil { + objPair := JSONPair{key: depKey, val: val} + depPairs = append(depPairs, objPair) + } + } + objPairs = append(objPairs, depPairs...) return objPairs } diff --git a/vendor/yunion.io/x/jsonutils/unmarshal.go b/vendor/yunion.io/x/jsonutils/unmarshal.go index 7739088a02..8c4911ea46 100644 --- a/vendor/yunion.io/x/jsonutils/unmarshal.go +++ b/vendor/yunion.io/x/jsonutils/unmarshal.go @@ -539,7 +539,7 @@ func setStructFieldAt(key string, v JSONObject, fieldValues reflectutils.SStruct if err != nil { return errors.Wrap(err, "JSONDict.unmarshalStruct") } - depInfo, ok := fieldValues[index].Info.Tags["deprecated-by"] + depInfo, ok := fieldValues[index].Info.Tags[TAG_DEPRECATED_BY] if ok { err := setStructFieldAt(depInfo, v, fieldValues, visited) if err != nil { diff --git a/vendor/yunion.io/x/log/log.go b/vendor/yunion.io/x/log/log.go index 7e51dabc4c..a26d3e439c 100644 --- a/vendor/yunion.io/x/log/log.go +++ b/vendor/yunion.io/x/log/log.go @@ -149,9 +149,18 @@ func DisableColors() { } func AddHookFormatter(logger *logrus.Logger) { - pcs := make([]uintptr, 2) - runtime.Callers(0, pcs) - logrusPackage := hooks.GetPackageName(runtime.FuncForPC(pcs[1]).Name()) + pcs := make([]uintptr, 1) + npcs := runtime.Callers(1, pcs) + frames := runtime.CallersFrames(pcs[:npcs]) + var myName string + for { + f, more := frames.Next() + myName = f.Function + if !more { + break + } + } + logrusPackage := hooks.GetPackageName(myName) logger.Hooks.Add(&hooks.CallerHook{logrusPackage}) logger.Formatter = &TextFormatter{ TimestampFormat: "060102 15:04:05", diff --git a/vendor/yunion.io/x/pkg/errors/errors.go b/vendor/yunion.io/x/pkg/errors/errors.go index 108cbf6db7..9a6ada341a 100644 --- a/vendor/yunion.io/x/pkg/errors/errors.go +++ b/vendor/yunion.io/x/pkg/errors/errors.go @@ -33,3 +33,7 @@ func Wrapf(err error, format string, args ...interface{}) error { func Cause(err error) error { return errors.Cause(err) } + +func Errorf(format string, args ...interface{}) error { + return errors.Errorf(format, args...) +} diff --git a/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go b/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go index 8871c1431f..9b70f0d4a2 100644 --- a/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go +++ b/vendor/yunion.io/x/pkg/util/reflectutils/jsonfield.go @@ -44,6 +44,10 @@ type SStructFieldInfo struct { Tags map[string]string } +func (s *SStructFieldInfo) updateTags(k, v string) { + s.Tags[k] = v +} + func (s SStructFieldInfo) deepCopy() *SStructFieldInfo { scopy := SStructFieldInfo{ Ignore: s.Ignore, @@ -134,11 +138,11 @@ type SStructFieldValue struct { type SStructFieldValueSet []SStructFieldValue func FetchStructFieldValueSet(dataValue reflect.Value) SStructFieldValueSet { - return fetchStructFieldValueSet(dataValue, false) + return fetchStructFieldValueSet(dataValue, false, nil) } func FetchStructFieldValueSetForWrite(dataValue reflect.Value) SStructFieldValueSet { - return fetchStructFieldValueSet(dataValue, true) + return fetchStructFieldValueSet(dataValue, true, nil) } type sStructFieldInfoMap map[string]SStructFieldInfo @@ -182,7 +186,7 @@ func fetchStructFieldInfos(dataType reflect.Type) sStructFieldInfoMap { return smap } -func fetchStructFieldValueSet(dataValue reflect.Value, allocatePtr bool) SStructFieldValueSet { +func fetchStructFieldValueSet(dataValue reflect.Value, allocatePtr bool, tags map[string]string) SStructFieldValueSet { fields := SStructFieldValueSet{} dataType := dataValue.Type() fieldInfos := fetchCacheStructFieldInfos(dataType) @@ -218,7 +222,8 @@ func fetchStructFieldValueSet(dataValue reflect.Value, allocatePtr bool) SStruct // different from how encoding/json handles struct // field of interface type. if fv.Kind() == reflect.Struct && sf.Type != gotypes.TimeType { - subfields := fetchStructFieldValueSet(fv, allocatePtr) + anonymousTags := utils.TagMap(sf.Tag) + subfields := fetchStructFieldValueSet(fv, allocatePtr, anonymousTags) fields = append(fields, subfields...) continue } @@ -231,6 +236,23 @@ func fetchStructFieldValueSet(dataValue reflect.Value, allocatePtr bool) SStruct }) } } + if len(tags) > 0 { + for i := range fields { + fieldName := fields[i].Info.MarshalName() + for k, v := range tags { + target := "" + pos := strings.Index(k, "->") + if pos > 0 { + target = k[:pos] + k = k[pos+2:] + } + if len(target) > 0 && target != fieldName { + continue + } + fields[i].Info.updateTags(k, v) + } + } + } return fields } From df00cdbd04d80f3a6bc1e4feffb5416934f189be Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Mon, 13 Apr 2020 13:00:11 +0800 Subject: [PATCH 3/3] update vendor --- cmd/climc/shell/proxysettings.go | 20 -------------------- pkg/apis/cloudcommon/proxy/proxy.go | 5 +---- pkg/cloudcommon/db/proxy/proxysetting.go | 6 +++--- pkg/compute/models/cloudaccounts.go | 6 +++--- pkg/mcclient/options/cloudaccounts.go | 4 ++-- pkg/mcclient/options/proxysettings.go | 10 ---------- 6 files changed, 9 insertions(+), 42 deletions(-) diff --git a/cmd/climc/shell/proxysettings.go b/cmd/climc/shell/proxysettings.go index 88c6cf25f5..dbee6aefc3 100644 --- a/cmd/climc/shell/proxysettings.go +++ b/cmd/climc/shell/proxysettings.go @@ -15,8 +15,6 @@ package shell import ( - "yunion.io/x/jsonutils" - "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/mcclient/options" @@ -80,22 +78,4 @@ func init() { printObject(proxysetting) return nil }) - R(&options.ProxySettingPublicOptions{}, "proxysetting-public", "Make proxysetting public", func(s *mcclient.ClientSession, opts *options.ProxySettingPublicOptions) error { - params := jsonutils.Marshal(opts) - result, err := modules.ProxySettings.PerformAction(s, opts.ID, "public", params) - if err != nil { - return err - } - printObject(result) - return nil - }) - R(&options.ProxySettingPrivateOptions{}, "proxysetting-private", "Make proxysetting private", func(s *mcclient.ClientSession, opts *options.ProxySettingPrivateOptions) error { - params := jsonutils.Marshal(opts) - result, err := modules.ProxySettings.PerformAction(s, opts.ID, "private", params) - if err != nil { - return err - } - printObject(result) - return nil - }) } diff --git a/pkg/apis/cloudcommon/proxy/proxy.go b/pkg/apis/cloudcommon/proxy/proxy.go index c29875d7a4..e26844bfcc 100644 --- a/pkg/apis/cloudcommon/proxy/proxy.go +++ b/pkg/apis/cloudcommon/proxy/proxy.go @@ -42,10 +42,7 @@ func (ps *SProxySetting) IsZero() bool { type ProxySettingResourceInput struct { // 代理配置 - ProxySetting string `json:"proxy_setting"` - // Deprecated - // swagger:ignore - ProxySettingId string `json:"proxy_setting_id" deprecated-by:"proxy_setting"` + ProxySettingId string `json:"proxy_setting_id"` } type ProxySettingTestInput struct { diff --git a/pkg/cloudcommon/db/proxy/proxysetting.go b/pkg/cloudcommon/db/proxy/proxysetting.go index 632ee4cfda..d33a7f2476 100644 --- a/pkg/cloudcommon/db/proxy/proxysetting.go +++ b/pkg/cloudcommon/db/proxy/proxysetting.go @@ -206,14 +206,14 @@ func RegisterReferrer(man db.IModelManager) { } func ValidateProxySettingResourceInput(userCred mcclient.TokenCredential, input proxyapi.ProxySettingResourceInput) (*SProxySetting, proxyapi.ProxySettingResourceInput, error) { - m, err := ProxySettingManager.FetchByIdOrName(userCred, input.ProxySetting) + m, err := ProxySettingManager.FetchByIdOrName(userCred, input.ProxySettingId) if err != nil { if errors.Cause(err) == sql.ErrNoRows { - return nil, input, errors.Wrapf(httperrors.ErrResourceNotFound, "%s %s", ProxySettingManager.Keyword(), input.ProxySetting) + return nil, input, errors.Wrapf(httperrors.ErrResourceNotFound, "%s %s", ProxySettingManager.Keyword(), input.ProxySettingId) } else { return nil, input, errors.Wrapf(err, "ProxySettingManager.FetchByIdOrName") } } - input.ProxySetting = m.GetId() + input.ProxySettingId = m.GetId() return m.(*SProxySetting), input, nil } diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index 44c6b44af8..e0e177e95b 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -401,13 +401,13 @@ func (manager *SCloudaccountManager) ValidateCreateData(ctx context.Context, use var proxyFunc httputils.TransportProxyFunc { - if input.ProxySetting == "" { - input.ProxySetting = proxyapi.ProxySettingId_DIRECT + if input.ProxySettingId == "" { + input.ProxySettingId = proxyapi.ProxySettingId_DIRECT } var proxySetting *proxy.SProxySetting proxySetting, input.ProxySettingResourceInput, err = proxy.ValidateProxySettingResourceInput(userCred, input.ProxySettingResourceInput) if err != nil { - return input, httperrors.NewInputParameterError("fetch proxysetting %s: %s", input.ProxySetting, err) + return input, httperrors.NewInputParameterError("ValidateProxySettingResourceInput %s", err) } proxyFunc = proxySetting.HttpTransportProxyFunc() } diff --git a/pkg/mcclient/options/cloudaccounts.go b/pkg/mcclient/options/cloudaccounts.go index 32106a2a53..10e00ff056 100644 --- a/pkg/mcclient/options/cloudaccounts.go +++ b/pkg/mcclient/options/cloudaccounts.go @@ -84,7 +84,7 @@ type SCloudAccountCreateBaseOptions struct { ProjectDomain string `help:"domain for this account"` - ProxySetting string `help:"proxy setting id or name" json:"proxy_setting"` + ProxySettingId string `help:"proxy setting id or name" json:"proxy_setting_id"` } type SVMwareCloudAccountCreateOptions struct { @@ -231,7 +231,7 @@ type SCloudAccountUpdateBaseOptions struct { SyncIntervalSeconds int `help:"auto synchornize interval in seconds"` AutoCreateProject *bool `help:"automatically create local project for new remote project"` - ProxySetting string `help:"proxy setting name or id" json:"proxy_setting"` + ProxySettingId string `help:"proxy setting name or id" json:"proxy_setting_id"` Desc string `help:"Description" json:"description" token:"desc"` } diff --git a/pkg/mcclient/options/proxysettings.go b/pkg/mcclient/options/proxysettings.go index aff1019ada..22a98ac920 100644 --- a/pkg/mcclient/options/proxysettings.go +++ b/pkg/mcclient/options/proxysettings.go @@ -46,13 +46,3 @@ type ProxySettingTestOptions struct { type ProxySettingListOptions struct { BaseListOptions } - -type ProxySettingPublicOptions struct { - ProxySettingGetOptions - Scope string `json:"scope" help:"share scope" choices:"domain|system"` - SharedDomain []string `json:"share"` -} - -type ProxySettingPrivateOptions struct { - ProxySettingGetOptions -}