fixes fn set/unset for routes & apps

was posting a whole config and not letting the server do the merging, now the
server does the merging instead. touch tested this, works as originally
intended now.

closes !80
closes #80
This commit is contained in:
Reed Allman
2017-06-28 21:19:01 -07:00
parent 26806ccafd
commit 4e7a497696
2 changed files with 11 additions and 110 deletions

View File

@@ -7,7 +7,6 @@ import (
"os"
"context"
"github.com/funcy/functions_go"
fnclient "github.com/funcy/functions_go/client"
apiapps "github.com/funcy/functions_go/client/apps"
"github.com/funcy/functions_go/models"
@@ -165,7 +164,7 @@ func (a *appsCmd) create(c *cli.Context) error {
func (a *appsCmd) update(c *cli.Context) error {
appName := c.Args().First()
patchedApp := &functions.App{
patchedApp := &models.App{
Config: extractEnvConfig(c.StringSlice("config")),
}
@@ -183,7 +182,7 @@ func (a *appsCmd) configSet(c *cli.Context) error {
key := c.Args().Get(1)
value := c.Args().Get(2)
app := &functions.App{
app := &models.App{
Config: make(map[string]string),
}
@@ -201,11 +200,11 @@ func (a *appsCmd) configUnset(c *cli.Context) error {
appName := c.Args().Get(0)
key := c.Args().Get(1)
app := &functions.App{
app := &models.App{
Config: make(map[string]string),
}
app.Config["-"+key] = ""
app.Config[key] = ""
if err := a.patchApp(appName, app); err != nil {
return fmt.Errorf("error updating app configuration: %v", err)
@@ -215,45 +214,11 @@ func (a *appsCmd) configUnset(c *cli.Context) error {
return nil
}
func (a *appsCmd) patchApp(appName string, app *functions.App) error {
resp, err := a.client.Apps.GetAppsApp(&apiapps.GetAppsAppParams{
func (a *appsCmd) patchApp(appName string, app *models.App) error {
_, err := a.client.Apps.PatchAppsApp(&apiapps.PatchAppsAppParams{
Context: context.Background(),
App: appName,
})
if err != nil {
switch err.(type) {
case *apiapps.GetAppsAppNotFound:
return fmt.Errorf("error: %v", err.(*apiapps.GetAppsAppNotFound).Payload.Error.Message)
case *apiapps.GetAppsAppDefault:
return fmt.Errorf("unexpected error: %v", err.(*apiapps.GetAppsAppDefault).Payload.Error.Message)
}
return fmt.Errorf("unexpected error: %v", err)
}
if resp.Payload.App.Config == nil {
resp.Payload.App.Config = map[string]string{}
}
resp.Payload.App.Name = ""
if app != nil {
if app.Config != nil {
for k, v := range app.Config {
if string(k[0]) == "-" {
delete(resp.Payload.App.Config, string(k[1:]))
continue
}
resp.Payload.App.Config[k] = v
}
}
}
body := &models.AppWrapper{App: resp.Payload.App}
_, err = a.client.Apps.PatchAppsApp(&apiapps.PatchAppsAppParams{
Context: context.Background(),
App: appName,
Body: body,
Body: &models.AppWrapper{App: app},
})
if err != nil {

View File

@@ -299,7 +299,7 @@ func routeWithFuncFile(c *cli.Context, ff *funcfile, rt *fnmodels.Route) error {
if rt.Path == "" && ff.Path != "" {
rt.Path = ff.Path
}
if rt.Type == nil && ff.Type != nil && *ff.Type != "" {
if rt.Type == "" && ff.Type != nil && *ff.Type != "" {
rt.Type = *ff.Type
}
@@ -359,75 +359,11 @@ func (a *routesCmd) postRoute(c *cli.Context, appName string, rt *fnmodels.Route
}
func (a *routesCmd) patchRoute(c *cli.Context, appName, routePath string, r *fnmodels.Route) error {
// TODO: this getting the old version and merging should be on the server side, not here on the client.
resp, err := a.client.Routes.GetAppsAppRoutesRoute(&apiroutes.GetAppsAppRoutesRouteParams{
_, err := a.client.Routes.PatchAppsAppRoutesRoute(&apiroutes.PatchAppsAppRoutesRouteParams{
Context: context.Background(),
App: appName,
Route: routePath,
})
if err != nil {
switch err.(type) {
case *apiroutes.GetAppsAppRoutesRouteNotFound:
// return fmt.Errorf("error: %s", err.(*apiroutes.GetAppsAppRoutesRouteNotFound).Payload.Error.Message)
// then insert it
return a.postRoute(c, appName, r)
case *apiroutes.GetAppsAppRoutesDefault:
return fmt.Errorf("unexpected error: %s", err.(*apiroutes.GetAppsAppRoutesDefault).Payload.Error.Message)
}
return fmt.Errorf("unexpected error: %s", err)
}
if resp.Payload.Route.Config == nil {
resp.Payload.Route.Config = map[string]string{}
}
if resp.Payload.Route.Headers == nil {
resp.Payload.Route.Headers = map[string][]string{}
}
resp.Payload.Route.Path = ""
if r != nil {
if r.Config != nil {
for k, v := range r.Config {
if string(k[0]) == "-" {
delete(resp.Payload.Route.Config, string(k[1:]))
continue
}
resp.Payload.Route.Config[k] = v
}
}
if r.Headers != nil {
for k, v := range r.Headers {
if string(k[0]) == "-" {
delete(resp.Payload.Route.Headers, k)
continue
}
resp.Payload.Route.Headers[k] = v
}
}
if r.Image != "" {
resp.Payload.Route.Image = r.Image
}
if r.Format != "" {
resp.Payload.Route.Format = r.Format
}
if r.Type != "" {
resp.Payload.Route.Type = r.Type
}
if r.Memory > 0 {
resp.Payload.Route.Memory = r.Memory
}
if r.Timeout != nil {
resp.Payload.Route.Timeout = r.Timeout
}
}
_, err = a.client.Routes.PatchAppsAppRoutesRoute(&apiroutes.PatchAppsAppRoutesRouteParams{
Context: context.Background(),
App: appName,
Route: routePath,
Body: resp.Payload,
Body: &fnmodels.RouteWrapper{Route: r},
})
if err != nil {
@@ -496,7 +432,7 @@ func (a *routesCmd) configUnset(c *cli.Context) error {
Config: make(map[string]string),
}
patchRoute.Config["-"+key] = ""
patchRoute.Config[key] = ""
err := a.patchRoute(c, appName, route, &patchRoute)
if err != nil {