From 25641f49c967ebce96b7c000a32ba8bbbce2f131 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 05:22:59 +0000 Subject: [PATCH 1/9] mcclient: options: set details correctly details=false is already the default --- pkg/mcclient/options/base.go | 3 --- pkg/mcclient/options/base_test.go | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/mcclient/options/base.go b/pkg/mcclient/options/base.go index c6d6927c68..eac81c7b70 100644 --- a/pkg/mcclient/options/base.go +++ b/pkg/mcclient/options/base.go @@ -197,8 +197,5 @@ func (opts *BaseListOptions) Params() (*jsonutils.JSONDict, error) { params.Set("admin", jsonutils.JSONTrue) } } - if opts.Details == nil { - params.Set("details", jsonutils.JSONFalse) - } return params, nil } diff --git a/pkg/mcclient/options/base_test.go b/pkg/mcclient/options/base_test.go index 9383385f62..9e221f925a 100644 --- a/pkg/mcclient/options/base_test.go +++ b/pkg/mcclient/options/base_test.go @@ -201,3 +201,24 @@ func TestOptionsStructToParams(t *testing.T) { testSs(t, cases) }) } + +func TestBaseListOptions(t *testing.T) { + t.Run("pending-delete-all", func(t *testing.T) { + opts := &BaseListOptions{ + PendingDeleteAll: Bool(true), + } + params, err := opts.Params() + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + for _, f := range []string{"details", "admin"} { + got, err := params.Bool(f) + if err != nil { + t.Fatalf("getting %s field failed: %s", f, err) + } + if !got { + t.Errorf("expecting %s=true, got false", f) + } + } + }) +} From 2e0c9741b144deda1e2a273e93dcbd27d14a03b7 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 06:25:16 +0000 Subject: [PATCH 2/9] mcclient: base: guard against empty repsonse panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x768977] goroutine 1 [running]: yunion.io/x/onecloud/pkg/mcclient/modules.(*BaseManager)._list(0x11a8100, 0xc00026b730, 0xc0005ba0e0, 0x69, 0xb794a3, 0x14, 0x69, 0x0, 0x0) /home/yunion/go/src/yunion.io/x/onecloud/pkg/mcclient/modules/base.go:118 +0x1d7 yunion.io/x/onecloud/pkg/mcclient/modules.(*ResourceManager).ListInContexts(0x11a8100, 0xc00026b730, 0xc1cbe0, 0xc00000ef28, 0x0, 0x0, 0x0, 0xc00010ad80, 0xa919c0, 0xc00010ad80) /home/yunion/go/src/yunion.io/x/onecloud/pkg/mcclient/modules/resource.go:254 +0x150 yunion.io/x/onecloud/pkg/mcclient/modules.(*ResourceManager).List(0x11a8100, 0xc00026b730, 0xc1cbe0, 0xc00000ef28, 0x0, 0xc0005a79e0, 0x6) /home/yunion/go/src/yunion.io/x/onecloud/pkg/mcclient/modules/resource.go:239 +0x5a yunion.io/x/onecloud/cmd/climc/shell.init.40.func3(0xc00026b730, 0xc00010ad80, 0x0, 0x0) /home/yunion/go/src/yunion.io/x/onecloud/cmd/climc/shell/loadbalancerbackends.go:35 +0x7e reflect.Value.call(0xa7e6c0, 0xb98170, 0x13, 0xb6b4b8, 0x4, 0xc00059c120, 0x2, 0x2, 0xc000297dc8, 0xc00003c6a0, ...) /home/yunion/.usr/go/goroot-1.11.1/src/reflect/value.go:447 +0x449 reflect.Value.Call(0xa7e6c0, 0xb98170, 0x13, 0xc00059c120, 0x2, 0x2, 0x1, 0x2, 0x18) /home/yunion/.usr/go/goroot-1.11.1/src/reflect/value.go:308 +0xa4 yunion.io/x/onecloud/vendor/yunion.io/x/structarg.(*SubcommandArgument).Invoke(0xc000174620, 0xc0005a7e60, 0x2, 0x2, 0xa67760, 0xc000297db0) /home/yunion/go/src/yunion.io/x/onecloud/vendor/yunion.io/x/structarg/structarg.go:702 +0x1fb main.executeSubcommand(0xc000174620, 0xc0000f19d0, 0xc000174540, 0xc000262ab0) /home/yunion/go/src/yunion.io/x/onecloud/cmd/climc/climc.go:220 +0xcf main.main() /home/yunion/go/src/yunion.io/x/onecloud/cmd/climc/climc.go:271 +0x256 --- pkg/mcclient/modules/base.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/mcclient/modules/base.go b/pkg/mcclient/modules/base.go index 3759b231e9..3a71f43eac 100644 --- a/pkg/mcclient/modules/base.go +++ b/pkg/mcclient/modules/base.go @@ -113,6 +113,9 @@ func (this *BaseManager) _list(session *mcclient.ClientSession, path, responseKe if err != nil { return nil, err } + if body == nil { + return nil, fmt.Errorf("empty response") + } rets, err := body.GetArray(responseKey) if err != nil { return nil, err From 9388748e4542b090d3f8322b41aa90dfca5b2ce6 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 06:26:22 +0000 Subject: [PATCH 3/9] pkg: httputils: ParseJSONResponse(): rewrite for clearness Make it more clear that it's possible the function can return with body==nil and err==nil --- pkg/util/httputils/httputils.go | 66 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/pkg/util/httputils/httputils.go b/pkg/util/httputils/httputils.go index d8cfbb5569..bc1ffad0e6 100644 --- a/pkg/util/httputils/httputils.go +++ b/pkg/util/httputils/httputils.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "net/url" + "os" "strconv" "strings" @@ -15,7 +16,6 @@ import ( "github.com/moul/http2curl" "yunion.io/x/jsonutils" - "yunion.io/x/log" "yunion.io/x/pkg/gotypes" "yunion.io/x/pkg/trace" @@ -189,62 +189,58 @@ func ParseJSONResponse(resp *http.Response, err error, debug bool) (http.Header, } rbody, err := ioutil.ReadAll(resp.Body) if debug { - fmt.Println(string(rbody)) + fmt.Fprintf(os.Stderr, "%s\n", string(rbody)) } if err != nil { return nil, nil, fmt.Errorf("Fail to read body: %s", err) } + var jrbody jsonutils.JSONObject = nil if len(rbody) > 0 { + var err error jrbody, err = jsonutils.Parse(rbody) - if err != nil && debug { - log.Errorf("parse JSON body %s fail: %s", rbody, err) - } - ///// XXX: ignore error case - // if err != nil && resp.StatusCode < 300 { - // return nil, nil, fmt.Errorf("Fail to decode body: %s", err) - // } - if jrbody != nil && debug { - fmt.Println(jrbody) + fmt.Fprintf(os.Stderr, "parsing json failed: %s", err) } } - if resp.StatusCode >= 300 && resp.StatusCode < 400 { + + if resp.StatusCode < 300 { + return resp.Header, jrbody, nil + } else if resp.StatusCode >= 300 && resp.StatusCode < 400 { ce := JSONClientError{} ce.Code = resp.StatusCode ce.Details = resp.Header.Get("Location") ce.Class = "redirect" return nil, nil, &ce - } else if resp.StatusCode >= 400 { + } else { ce := JSONClientError{} + if jrbody == nil { ce.Code = resp.StatusCode ce.Details = resp.Status return nil, nil, &ce - } else { - jrbody2, e := jrbody.Get("error") - if e == nil { - ecode, e := jrbody2.Int("code") - if e == nil { - ce.Code = int(ecode) - ce.Details, _ = jrbody2.GetString("message") - ce.Class, _ = jrbody2.GetString("title") - return nil, nil, &ce - } else { - ce.Code = resp.StatusCode - ce.Details = jrbody2.String() - return nil, nil, &ce - } + } + + jrbody2, err := jrbody.Get("error") + if err == nil { + ecode, err := jrbody2.Int("code") + if err == nil { + ce.Code = int(ecode) + ce.Details, _ = jrbody2.GetString("message") + ce.Class, _ = jrbody2.GetString("title") + return nil, nil, &ce } else { - err = jrbody.Unmarshal(&ce) - if err != nil { - return nil, nil, err - } else { - return nil, nil, &ce - } + ce.Code = resp.StatusCode + ce.Details = jrbody2.String() + return nil, nil, &ce } } - } else { - return resp.Header, jrbody, nil + + err = jrbody.Unmarshal(&ce) + if err != nil { + return nil, nil, err + } else { + return nil, nil, &ce + } } } From 7acc86b021b470b5417b1ff0694dfd3597c543a5 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 05:21:18 +0000 Subject: [PATCH 4/9] lbagent: fix unused imports --- pkg/lbagent/models/keepalived.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/lbagent/models/keepalived.go b/pkg/lbagent/models/keepalived.go index 8632692ec9..4b4c95ff5a 100644 --- a/pkg/lbagent/models/keepalived.go +++ b/pkg/lbagent/models/keepalived.go @@ -2,13 +2,10 @@ package models import ( "bytes" - "fmt" "io/ioutil" "path/filepath" "text/template" - "yunion.io/x/log" - agentutils "yunion.io/x/onecloud/pkg/lbagent/utils" ) From 9a73d5c095d00b9232a5f1b302def544cf5670bc Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 05:21:38 +0000 Subject: [PATCH 5/9] lbagent: list with details=true explicitly Previously we depend on .Params() of mcclient to set it for us. Set it explicitly just in case --- pkg/lbagent/models/reflect.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/lbagent/models/reflect.go b/pkg/lbagent/models/reflect.go index de4dfaec90..d412ebd2ce 100644 --- a/pkg/lbagent/models/reflect.go +++ b/pkg/lbagent/models/reflect.go @@ -87,6 +87,7 @@ func GetModels(opts *GetModelsOptions) error { listOptions := options.BaseListOptions{ Admin: options.Bool(true), + Details: options.Bool(true), Filter: []string{minUpdatedAtFilter(minUpdatedAt)}, OrderBy: []string{"updated_at", "id"}, Order: "asc", From 24172a26f4b374578139072ecea4fb4f650de062 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 10:01:52 +0000 Subject: [PATCH 6/9] loadbalancers: more compact backendGroup validation --- .../models/loadbalancerlistenerrules.go | 6 ++---- pkg/compute/models/loadbalancerlisteners.go | 18 ++++++------------ pkg/compute/models/loadbalancers.go | 2 +- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/pkg/compute/models/loadbalancerlistenerrules.go b/pkg/compute/models/loadbalancerlistenerrules.go index ad75e9d90a..e4f5455e0c 100644 --- a/pkg/compute/models/loadbalancerlistenerrules.go +++ b/pkg/compute/models/loadbalancerlistenerrules.go @@ -112,8 +112,7 @@ func (man *SLoadbalancerListenerRuleManager) ValidateCreateData(ctx context.Cont return nil, fmt.Errorf("listener type must be http/https, got %s", listenerType) } { - backendGroup := backendGroupV.Model.(*SLoadbalancerBackendGroup) - if backendGroup.LoadbalancerId != listener.LoadbalancerId { + if backendGroup, ok := backendGroupV.Model.(*SLoadbalancerBackendGroup); ok && backendGroup.LoadbalancerId != listener.LoadbalancerId { return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s instead of %s", backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, listener.LoadbalancerId) } @@ -136,14 +135,13 @@ func (lbr *SLoadbalancerListenerRule) ValidateUpdateData(ctx context.Context, us if err != nil { return nil, err } - if backendGroupV.Model != nil { + if backendGroup, ok := backendGroupV.Model.(*SLoadbalancerBackendGroup); ok && backendGroup.Id != lbr.BackendGroupId { listenerM, err := LoadbalancerListenerManager.FetchById(lbr.ListenerId) if err != nil { return nil, httperrors.NewInputParameterError("loadbalancerlistenerrule %s(%s): fetching listener %s failed", lbr.Name, lbr.Id, lbr.ListenerId) } listener := listenerM.(*SLoadbalancerListener) - backendGroup := backendGroupV.Model.(*SLoadbalancerBackendGroup) if backendGroup.LoadbalancerId != listener.LoadbalancerId { return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s instead of %s", backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, listener.LoadbalancerId) diff --git a/pkg/compute/models/loadbalancerlisteners.go b/pkg/compute/models/loadbalancerlisteners.go index d94323422a..b202a8f5c7 100644 --- a/pkg/compute/models/loadbalancerlisteners.go +++ b/pkg/compute/models/loadbalancerlisteners.go @@ -217,12 +217,9 @@ func (man *SLoadbalancerListenerManager) ValidateCreateData(ctx context.Context, } } { - if backendGroupV.Model != nil { - backendGroup := backendGroupV.Model.(*SLoadbalancerBackendGroup) - if backendGroup.LoadbalancerId != lb.Id { - return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s instead of %s", - backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, lb.Id) - } + if backendGroup, ok := backendGroupV.Model.(*SLoadbalancerBackendGroup); ok && backendGroup.LoadbalancerId != lb.Id { + return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s instead of %s", + backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, lb.Id) } } { @@ -348,12 +345,9 @@ func (lblis *SLoadbalancerListener) ValidateUpdateData(ctx context.Context, user } } { - if backendGroupV.Model != nil { - backendGroup := backendGroupV.Model.(*SLoadbalancerBackendGroup) - if backendGroup.LoadbalancerId != lblis.LoadbalancerId { - return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s instead of %s", - backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, lblis.LoadbalancerId) - } + if backendGroup, ok := backendGroupV.Model.(*SLoadbalancerBackendGroup); ok && backendGroup.LoadbalancerId != lblis.LoadbalancerId { + return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s instead of %s", + backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, lblis.LoadbalancerId) } } return lblis.SVirtualResourceBase.ValidateUpdateData(ctx, userCred, query, data) diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index f57d678758..787a4b3159 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -169,7 +169,7 @@ func (lb *SLoadbalancer) ValidateUpdateData(ctx context.Context, userCred mcclie if err != nil { return nil, err } - if backendGroup := backendGroupV.Model.(*SLoadbalancerBackendGroup); backendGroup.LoadbalancerId != lb.Id { + if backendGroup, ok := backendGroupV.Model.(*SLoadbalancerBackendGroup); ok && backendGroup.LoadbalancerId != lb.Id { return nil, httperrors.NewInputParameterError("backend group %s(%s) belongs to loadbalancer %s, not %s", backendGroup.Name, backendGroup.Id, backendGroup.LoadbalancerId, lb.Id) } From 13bc224202a7b5269233eef507dfccd2cea52191 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 10:31:32 +0000 Subject: [PATCH 7/9] mcclient: loadbalancers: allow updating names - loadbalanceracls: allow updating only name --- cmd/climc/shell/loadbalancerbackendgroups.go | 12 ++++++++++++ pkg/compute/models/loadbalanceracls.go | 9 ++++++--- pkg/mcclient/options/loadbalanceracls.go | 14 ++++++++++---- pkg/mcclient/options/loadbalanceragents.go | 3 ++- pkg/mcclient/options/loadbalancerbackendgroups.go | 8 ++++++++ pkg/mcclient/options/loadbalancerbackends.go | 3 ++- pkg/mcclient/options/loadbalancercertificates.go | 3 ++- pkg/mcclient/options/loadbalancerlistenerrules.go | 4 +++- pkg/mcclient/options/loadbalancerlisteners.go | 3 ++- pkg/mcclient/options/loadbalancers.go | 4 +++- 10 files changed, 50 insertions(+), 13 deletions(-) diff --git a/cmd/climc/shell/loadbalancerbackendgroups.go b/cmd/climc/shell/loadbalancerbackendgroups.go index 8cf38963fa..068ac36bc4 100644 --- a/cmd/climc/shell/loadbalancerbackendgroups.go +++ b/cmd/climc/shell/loadbalancerbackendgroups.go @@ -39,6 +39,18 @@ func init() { printList(result, modules.LoadbalancerBackendGroups.GetColumns(s)) return nil }) + R(&options.LoadbalancerBackendGroupUpdateOptions{}, "lbbackendgroup-update", "Update lbbackendgroup", func(s *mcclient.ClientSession, opts *options.LoadbalancerBackendGroupUpdateOptions) error { + params, err := options.StructToParams(opts) + if err != nil { + return err + } + lbbackendgroup, err := modules.LoadbalancerBackendGroups.Update(s, opts.ID, params) + if err != nil { + return err + } + printObject(lbbackendgroup) + return nil + }) R(&options.LoadbalancerBackendGroupDeleteOptions{}, "lbbackendgroup-delete", "Show lbbackendgroup", func(s *mcclient.ClientSession, opts *options.LoadbalancerBackendGroupDeleteOptions) error { lbbackendgroup, err := modules.LoadbalancerBackendGroups.Delete(s, opts.ID, nil) if err != nil { diff --git a/pkg/compute/models/loadbalanceracls.go b/pkg/compute/models/loadbalanceracls.go index 245ea94453..67dd190c6c 100644 --- a/pkg/compute/models/loadbalanceracls.go +++ b/pkg/compute/models/loadbalanceracls.go @@ -101,9 +101,12 @@ type SLoadbalancerAcl struct { AclEntries *SLoadbalancerAclEntries `list:"user" update:"user" create:"required"` } -func loadbalancerAclsValidateAclEntries(data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { +func loadbalancerAclsValidateAclEntries(data *jsonutils.JSONDict, update bool) (*jsonutils.JSONDict, error) { aclEntries := SLoadbalancerAclEntries{} aclEntriesV := validators.NewStructValidator("acl_entries", &aclEntries) + if update { + aclEntriesV.Optional(true) + } err := aclEntriesV.Validate(data) if err != nil { return nil, err @@ -112,7 +115,7 @@ func loadbalancerAclsValidateAclEntries(data *jsonutils.JSONDict) (*jsonutils.JS } func (man *SLoadbalancerAclManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerProjId string, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - data, err := loadbalancerAclsValidateAclEntries(data) + data, err := loadbalancerAclsValidateAclEntries(data, false) if err != nil { return nil, err } @@ -124,7 +127,7 @@ func (lbacl *SLoadbalancerAcl) AllowPerformStatus(ctx context.Context, userCred } func (lbacl *SLoadbalancerAcl) ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *jsonutils.JSONDict) (*jsonutils.JSONDict, error) { - data, err := loadbalancerAclsValidateAclEntries(data) + data, err := loadbalancerAclsValidateAclEntries(data, true) if err != nil { return nil, err } diff --git a/pkg/mcclient/options/loadbalanceracls.go b/pkg/mcclient/options/loadbalanceracls.go index dce9a15b94..dc704c18fa 100644 --- a/pkg/mcclient/options/loadbalanceracls.go +++ b/pkg/mcclient/options/loadbalanceracls.go @@ -69,7 +69,9 @@ type LoadbalancerAclListOptions struct { } type LoadbalancerAclUpdateOptions struct { - ID string `json:"-"` + ID string `json:"-"` + Name string + AclEntry []string `help:"acl entry with cidr and comment separated by #, e.g. 10.9.0.0/16#no comment" json:"-"` } @@ -99,9 +101,13 @@ func (opts *LoadbalancerAclUpdateOptions) Params() (*jsonutils.JSONDict, error) if err != nil { return nil, err } - aclEntries := NewAclEntries(opts.AclEntry) - aclEntriesJson := jsonutils.Marshal(aclEntries) - params.Set("acl_entries", aclEntriesJson) + // - when it's nil, we leave it alone without updating + // - when it's non-nil, we update it as a whole + if opts.AclEntry != nil { + aclEntries := NewAclEntries(opts.AclEntry) + aclEntriesJson := jsonutils.Marshal(aclEntries) + params.Set("acl_entries", aclEntriesJson) + } return params, nil } diff --git a/pkg/mcclient/options/loadbalanceragents.go b/pkg/mcclient/options/loadbalanceragents.go index 5ea716efbc..ab3b230818 100644 --- a/pkg/mcclient/options/loadbalanceragents.go +++ b/pkg/mcclient/options/loadbalanceragents.go @@ -86,7 +86,8 @@ type LoadbalancerAgentGetOptions struct { } type LoadbalancerAgentUpdateOptions struct { - ID string + ID string + Name string HbTimeout *int diff --git a/pkg/mcclient/options/loadbalancerbackendgroups.go b/pkg/mcclient/options/loadbalancerbackendgroups.go index 58051415aa..495acf870b 100644 --- a/pkg/mcclient/options/loadbalancerbackendgroups.go +++ b/pkg/mcclient/options/loadbalancerbackendgroups.go @@ -4,12 +4,20 @@ type LoadbalancerBackendGroupCreateOptions struct { NAME string Loadbalancer string } + type LoadbalancerBackendGroupGetOptions struct { ID string } + +type LoadbalancerBackendGroupUpdateOptions struct { + ID string + Name string +} + type LoadbalancerBackendGroupDeleteOptions struct { ID string } + type LoadbalancerBackendGroupListOptions struct { BaseListOptions Loadbalancer string diff --git a/pkg/mcclient/options/loadbalancerbackends.go b/pkg/mcclient/options/loadbalancerbackends.go index 7b89b874d0..7e9f66527a 100644 --- a/pkg/mcclient/options/loadbalancerbackends.go +++ b/pkg/mcclient/options/loadbalancerbackends.go @@ -19,7 +19,8 @@ type LoadbalancerBackendListOptions struct { } type LoadbalancerBackendUpdateOptions struct { - ID string + ID string + Name string Weight *int Port *int diff --git a/pkg/mcclient/options/loadbalancercertificates.go b/pkg/mcclient/options/loadbalancercertificates.go index 70d920b657..b93dabedaa 100644 --- a/pkg/mcclient/options/loadbalancercertificates.go +++ b/pkg/mcclient/options/loadbalancercertificates.go @@ -70,7 +70,8 @@ type LoadbalancerCertificateListOptions struct { } type LoadbalancerCertificateUpdateOptions struct { - ID string + ID string + Name string Cert string `json:"-" help:"path to certificate file"` Pkey string `json:"-" help:"path to private key file"` diff --git a/pkg/mcclient/options/loadbalancerlistenerrules.go b/pkg/mcclient/options/loadbalancerlistenerrules.go index 5b85f67bac..352450dfd5 100644 --- a/pkg/mcclient/options/loadbalancerlistenerrules.go +++ b/pkg/mcclient/options/loadbalancerlistenerrules.go @@ -18,7 +18,9 @@ type LoadbalancerListenerRuleListOptions struct { } type LoadbalancerListenerRuleUpdateOptions struct { - ID string + ID string + Name string + BackendGroup string } diff --git a/pkg/mcclient/options/loadbalancerlisteners.go b/pkg/mcclient/options/loadbalancerlisteners.go index 1f5e8ff314..7b232ae365 100644 --- a/pkg/mcclient/options/loadbalancerlisteners.go +++ b/pkg/mcclient/options/loadbalancerlisteners.go @@ -97,7 +97,8 @@ type LoadbalancerListenerListOptions struct { } type LoadbalancerListenerUpdateOptions struct { - ID string + ID string + Name string BackendGroup string diff --git a/pkg/mcclient/options/loadbalancers.go b/pkg/mcclient/options/loadbalancers.go index 185c321dbd..e6ea70ca87 100644 --- a/pkg/mcclient/options/loadbalancers.go +++ b/pkg/mcclient/options/loadbalancers.go @@ -11,7 +11,9 @@ type LoadbalancerGetOptions struct { } type LoadbalancerUpdateOptions struct { - ID string + ID string + Name string + BackendGroup string } From 73ec3d67fa36f80a574e9766504c25a83ec8b817 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 12:20:41 +0000 Subject: [PATCH 8/9] loadbalancers: show loadbalancer name for listener and backend group --- .../models/loadbalancerbackendgroups.go | 20 ++++++++++++++ pkg/compute/models/loadbalancerlisteners.go | 27 +++++++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/pkg/compute/models/loadbalancerbackendgroups.go b/pkg/compute/models/loadbalancerbackendgroups.go index 676ee2d435..ff4917c158 100644 --- a/pkg/compute/models/loadbalancerbackendgroups.go +++ b/pkg/compute/models/loadbalancerbackendgroups.go @@ -5,6 +5,7 @@ import ( "fmt" "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/sqlchemy" "yunion.io/x/onecloud/pkg/cloudcommon/db" @@ -102,6 +103,25 @@ func (lbbg *SLoadbalancerBackendGroup) ValidateDeleteCondition(ctx context.Conte return nil } +func (lbbg *SLoadbalancerBackendGroup) GetCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict { + extra := lbbg.SVirtualResourceBase.GetCustomizeColumns(ctx, userCred, query) + { + lb, err := LoadbalancerManager.FetchById(lbbg.LoadbalancerId) + if err != nil { + log.Errorf("loadbalancer backend group %s(%s): fetch loadbalancer (%s) error: %s", + lbbg.Name, lbbg.Id, lbbg.LoadbalancerId, err) + return extra + } + extra.Set("loadbalancer", jsonutils.NewString(lb.GetName())) + } + return extra +} + +func (lbbg *SLoadbalancerBackendGroup) GetExtraDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict { + extra := lbbg.GetCustomizeColumns(ctx, userCred, query) + return extra +} + func (lbbg *SLoadbalancerBackendGroup) PreDelete(ctx context.Context, userCred mcclient.TokenCredential) { lbbg.DoPendingDelete(ctx, userCred) lbbg.PreDeleteSubs(ctx, userCred) diff --git a/pkg/compute/models/loadbalancerlisteners.go b/pkg/compute/models/loadbalancerlisteners.go index b202a8f5c7..137f594698 100644 --- a/pkg/compute/models/loadbalancerlisteners.go +++ b/pkg/compute/models/loadbalancerlisteners.go @@ -355,16 +355,27 @@ func (lblis *SLoadbalancerListener) ValidateUpdateData(ctx context.Context, user func (lblis *SLoadbalancerListener) GetCustomizeColumns(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) *jsonutils.JSONDict { extra := lblis.SVirtualResourceBase.GetCustomizeColumns(ctx, userCred, query) - if lblis.BackendGroupId == "" { - return extra + { + lb, err := LoadbalancerManager.FetchById(lblis.LoadbalancerId) + if err != nil { + log.Errorf("loadbalancer listener %s(%s): fetch loadbalancer (%s) error: %s", + lblis.Name, lblis.Id, lblis.LoadbalancerId, err) + return extra + } + extra.Set("loadbalancer", jsonutils.NewString(lb.GetName())) } - lbbg, err := LoadbalancerBackendGroupManager.FetchById(lblis.BackendGroupId) - if err != nil { - log.Errorf("loadbalancer listener %s(%s): fetch backend group (%s) error: %s", - lblis.Name, lblis.Id, lblis.BackendGroupId, err) - return extra + { + if lblis.BackendGroupId == "" { + return extra + } + lbbg, err := LoadbalancerBackendGroupManager.FetchById(lblis.BackendGroupId) + if err != nil { + log.Errorf("loadbalancer listener %s(%s): fetch backend group (%s) error: %s", + lblis.Name, lblis.Id, lblis.BackendGroupId, err) + return extra + } + extra.Set("backend_group", jsonutils.NewString(lbbg.GetName())) } - extra.Set("backend_group", jsonutils.NewString(lbbg.GetName())) return extra } From 09e511883cf481bf8142ee9ed3f0892fe1418821 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 8 Nov 2018 15:53:10 +0000 Subject: [PATCH 9/9] cloudcommon: validators: return 400 for struct validation failure --- pkg/cloudcommon/validators/validators.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cloudcommon/validators/validators.go b/pkg/cloudcommon/validators/validators.go index 01e5acb3d4..9594138a5b 100644 --- a/pkg/cloudcommon/validators/validators.go +++ b/pkg/cloudcommon/validators/validators.go @@ -511,7 +511,7 @@ func (v *ValidatorStruct) Validate(data *jsonutils.JSONDict) error { if valueValidator, ok := v.Value.(IValidatorBase); ok { err = valueValidator.Validate(data) if err != nil { - return err + return newInvalidValueError(v.Key, err.Error()) } } data.Set(v.Key, jsonutils.Marshal(v.Value))