diff --git a/clientbase/ops.go b/clientbase/ops.go index 36af41d6..f75d112d 100644 --- a/clientbase/ops.go +++ b/clientbase/ops.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "github.com/gorilla/websocket" "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) } +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 { + 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 { 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) } + 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 { updates = map[string]string{} } diff --git a/generator/type_template.go b/generator/type_template.go index 1dfd6262..7503859d 100644 --- a/generator/type_template.go +++ b/generator/type_template.go @@ -40,6 +40,7 @@ type {{.schema.CodeName}}Operations interface { List(opts *types.ListOpts) (*{{.schema.CodeName}}Collection, error) Create(opts *{{.schema.CodeName}}) (*{{.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) Delete(container *{{.schema.CodeName}}) error {{range $key, $value := .resourceActions}} @@ -84,6 +85,12 @@ func (c *{{.schema.CodeName}}Client) Update(existing *{{.schema.CodeName}}, upda 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) { resp := &{{.schema.CodeName}}Collection{} err := c.apiClient.Ops.DoList({{.schema.CodeName}}Type, opts, resp)