1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-13 05:42:37 +00:00

Add replace to client

This commit is contained in:
Darren Shepherd
2018-06-19 11:19:23 -07:00
parent 2706ddc350
commit 23c18792d2
2 changed files with 27 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -183,7 +184,15 @@ func (a *APIOperations) DoCreate(schemaType string, createObj interface{}, respO
return a.DoModify("POST", a.Opts.URL+"/"+schemaType, createObj, respObject) return a.DoModify("POST", a.Opts.URL+"/"+schemaType, createObj, respObject)
} }
func (a *APIOperations) DoReplace(schemaType string, existing *types.Resource, updates interface{}, respObject interface{}) error {
return a.doUpdate(schemaType, true, existing, updates, respObject)
}
func (a *APIOperations) DoUpdate(schemaType string, existing *types.Resource, updates interface{}, respObject interface{}) error { func (a *APIOperations) DoUpdate(schemaType string, existing *types.Resource, updates interface{}, respObject interface{}) error {
return a.doUpdate(schemaType, false, existing, updates, respObject)
}
func (a *APIOperations) doUpdate(schemaType string, replace bool, existing *types.Resource, updates interface{}, respObject interface{}) error {
if existing == nil { if existing == nil {
return errors.New("Existing object is nil") return errors.New("Existing object is nil")
} }
@@ -193,6 +202,17 @@ func (a *APIOperations) DoUpdate(schemaType string, existing *types.Resource, up
return fmt.Errorf("failed to find self URL of [%v]", existing) return fmt.Errorf("failed to find self URL of [%v]", existing)
} }
if replace {
u, err := url.Parse(selfURL)
if err != nil {
return fmt.Errorf("failed to parse url %s: %v", selfURL, err)
}
q := u.Query()
q.Set("_replace", "true")
u.RawQuery = q.Encode()
selfURL = u.String()
}
if updates == nil { if updates == nil {
updates = map[string]string{} updates = map[string]string{}
} }

View File

@@ -40,6 +40,7 @@ type {{.schema.CodeName}}Operations interface {
List(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error) List(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error)
Create(opts *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error) Create(opts *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error)
Update(existing *{{.schema.CodeName}}, updates interface{}) (*{{.schema.CodeName}}, error) Update(existing *{{.schema.CodeName}}, updates interface{}) (*{{.schema.CodeName}}, error)
Replace(existing *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error)
ByID(id string) (*{{.schema.CodeName}}, error) ByID(id string) (*{{.schema.CodeName}}, error)
Delete(container *{{.schema.CodeName}}) error Delete(container *{{.schema.CodeName}}) error
{{range $key, $value := .resourceActions}} {{range $key, $value := .resourceActions}}
@@ -84,6 +85,12 @@ func (c *{{.schema.CodeName}}Client) Update(existing *{{.schema.CodeName}}, upda
return resp, err return resp, err
} }
func (c *{{.schema.CodeName}}Client) Replace(obj *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error) {
resp := &{{.schema.CodeName}}{}
err := c.apiClient.Ops.DoReplace({{.schema.CodeName}}Type, &obj.Resource, obj, resp)
return resp, err
}
func (c *{{.schema.CodeName}}Client) List(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error) { func (c *{{.schema.CodeName}}Client) List(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error) {
resp := &{{.schema.CodeName}}Collection{} resp := &{{.schema.CodeName}}Collection{}
err := c.apiClient.Ops.DoList({{.schema.CodeName}}Type, opts, resp) err := c.apiClient.Ops.DoList({{.schema.CodeName}}Type, opts, resp)