@@ -34,6 +34,7 @@ type CreateFunctionRequest struct {
3434 Payload io.Reader
3535}
3636
37+ // UpdateFunctionRequest is used when updating an existing function
3738type UpdateFunctionRequest struct {
3839 ID string
3940 Name string
@@ -123,7 +124,7 @@ func (api *API) CreateFunction(opts *CreateFunctionRequest) (*Function, error) {
123124 Driver : opts .Driver ,
124125 Payload : base64 .StdEncoding .EncodeToString (functionBody ),
125126 }
126-
127+ // TODO: consider splitting function uploading and creation into separate reqs
127128 resp , err := api .makeRequest ("POST" , "/functions" , createOpts )
128129 if err != nil {
129130 return nil , err
@@ -137,14 +138,32 @@ func (api *API) CreateFunction(opts *CreateFunctionRequest) (*Function, error) {
137138}
138139
139140// UpdateFunction - update function
140- func (api * API ) UpdateFunction (f * FunctionRequest ) (* Function , error ) {
141+ func (api * API ) UpdateFunction (options * UpdateFunctionRequest ) (* Function , error ) {
142+
143+ if options .ID != "" {
144+ // ok
145+ } else if options .Name != "" {
146+ fID , err := api .ensureFunctionID (options .ID )
147+ if err != nil {
148+ return nil , err
149+ }
150+ options .ID = fID
151+ } else {
152+ return nil , fmt .Errorf ("either name or ID has to be set" )
153+ }
141154
142- fID , err := api . ensureFunctionID ( f . ID )
155+ functionBody , err := ioutil . ReadAll ( options . Payload )
143156 if err != nil {
144- return nil , err
157+ return nil , errors . Wrap ( err , "failed to read function body" )
145158 }
146- f .ID = fID
147- resp , err := api .makeRequest ("PUT" , "/functions/" + f .ID , f )
159+
160+ updateOpts := & FunctionRequest {
161+ Name : options .Name ,
162+ Driver : options .Driver ,
163+ Payload : base64 .StdEncoding .EncodeToString (functionBody ),
164+ }
165+
166+ resp , err := api .makeRequest ("PUT" , "/functions/" + options .ID , updateOpts )
148167 if err != nil {
149168 return nil , err
150169 }
0 commit comments