Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: form to json review #163

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5f50292
change: form to json
CarlosRoGuerra Jun 9, 2021
ced8e54
change: form to json
CarlosRoGuerra Jun 14, 2021
8371153
change: form to json
CarlosRoGuerra Jun 21, 2021
f1631ea
change: form to json
CarlosRoGuerra Jun 22, 2021
44a5f3e
change: form to json
CarlosRoGuerra Jun 24, 2021
e64ca58
change:form to json-> appStart,appStop,appRestart in apps.go, volumeU…
CarlosRoGuerra Jul 8, 2021
898957a
change: Use json.NewDescoder in tsuru client tests
CarlosRoGuerra Jul 13, 2021
cbcc43e
fix:volumeUpdate in volume.go;Add: ServiceInstanceUpdate to json
CarlosRoGuerra Jul 26, 2021
e70f3c0
fix:volumeUpdate in volume.go;Add: ServiceInstanceUpdate to json
CarlosRoGuerra Jul 26, 2021
06f0328
fix:volumeUpdate in volume.go;Add: ServiceInstanceUpdate to json
CarlosRoGuerra Jul 26, 2021
1427bf6
fix:volumeUpdate in volume.go;Add: ServiceInstanceUpdate to json
CarlosRoGuerra Jul 26, 2021
0ff04cd
change:var name changePassword to changedPassword in auth.go
CarlosRoGuerra Jul 27, 2021
b4cfeb8
Merge branch 'tsuru:master' into master
CarlosRoGuerra Aug 2, 2021
147ae47
change form to json
CarlosRoGuerra Aug 3, 2021
8a4b8af
change form to json
CarlosRoGuerra Aug 3, 2021
792f0ee
change form to json
CarlosRoGuerra Aug 3, 2021
24d08e5
change form to json
CarlosRoGuerra Aug 3, 2021
698f07f
add: certificate and permission to client
CarlosRoGuerra Aug 13, 2021
93fd277
add: certificate and permission to client
CarlosRoGuerra Aug 13, 2021
8d5a611
refactor: form to json
CarlosRoGuerra Oct 19, 2021
811c43b
refactor: json to form
CarlosRoGuerra Oct 20, 2021
6e2b5e4
Merge branch 'master' into refactor-json
CarlosRoGuerra Oct 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tsuru/.DS_Store
Binary file not shown.
187 changes: 93 additions & 94 deletions tsuru/client/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
"text/template"
"time"

"github.com/ajg/form"
"github.com/tsuru/gnuflag"
"github.com/tsuru/go-tsuruclient/pkg/client"
tsuruClient "github.com/tsuru/go-tsuruclient/pkg/client"
"github.com/tsuru/go-tsuruclient/pkg/tsuru"
"github.com/tsuru/tablecli"
"github.com/tsuru/tsuru/cmd"
Expand Down Expand Up @@ -162,52 +161,46 @@ func (c *AppCreate) Flags() *gnuflag.FlagSet {
return c.fs
}

func (c *AppCreate) Run(context *cmd.Context, client *cmd.Client) error {
var platform string
appName := context.Args[0]
if len(context.Args) > 1 {
platform = context.Args[1]
}
v, err := form.EncodeToValues(map[string]interface{}{"routeropts": c.routerOpts})
if err != nil {
return err
}
v.Set("name", appName)
v.Set("platform", platform)
v.Set("plan", c.plan)
v.Set("teamOwner", c.teamOwner)
v.Set("pool", c.pool)
v.Set("description", c.description)
for _, tag := range c.tags {
v.Add("tag", tag)
func (c *AppCreate) InputApp(appName string, platform string) tsuru.InputApp {
inputApp := tsuru.InputApp{
Name: appName,
Platform: platform,
Pool: c.pool,
Description: c.description,
Plan: c.plan,
TeamOwner: c.teamOwner,
Tags: c.tags,
Router: c.router,
Routeropts: c.routerOpts,
}
v.Set("router", c.router)
b := strings.NewReader(v.Encode())
u, err := cmd.GetURL("/apps")
if err != nil {
return err
}
request, err := http.NewRequest("POST", u, b)
if err != nil {
return err
return inputApp
}
func (c *AppCreate) Run(ctx *cmd.Context, client *cmd.Client) error {
ctx.RawOutput()
var platform string
appName := ctx.Args[0]
if len(ctx.Args) > 1 {
platform = ctx.Args[1]
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}
defer response.Body.Close()
result, err := ioutil.ReadAll(response.Body)
inputApp := c.InputApp(appName, platform)
_, response, err := apiClient.AppApi.AppCreate(context.TODO(), inputApp)
if err != nil {
return err
}
out := make(map[string]string)
err = json.Unmarshal(result, &out)
err = cmd.StreamJSONResponse(ctx.Stdout, response)

if err != nil {
return err
}
fmt.Fprintf(context.Stdout, "App %q has been created!\n", appName)
fmt.Fprintln(context.Stdout, "Use app info to check the status of the app and its units.")

fmt.Fprintf(ctx.Stdout, "App %q has been created!\n", appName)
fmt.Fprintln(ctx.Stdout, "Use app info to check the status of the app and its units.")
return nil
}

Expand Down Expand Up @@ -267,7 +260,7 @@ func (c *AppUpdate) Flags() *gnuflag.FlagSet {
func (c *AppUpdate) Run(ctx *cmd.Context, cli *cmd.Client) error {
ctx.RawOutput()

apiClient, err := client.ClientFromEnvironment(&tsuru.Configuration{
apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: cli.HTTPClient,
})
if err != nil {
Expand Down Expand Up @@ -1029,7 +1022,7 @@ func (f *appFilter) queryString(cli *cmd.Client) (url.Values, error) {
}

func currentUserEmail(cli *cmd.Client) (string, error) {
apiClient, err := client.ClientFromEnvironment(&tsuru.Configuration{
apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: cli.HTTPClient,
})
if err != nil {
Expand Down Expand Up @@ -1196,6 +1189,14 @@ func (c *AppStop) Run(context *cmd.Context, client *cmd.Client) error {
if err != nil {
return err
}
// apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
// HTTPClient: client.HTTPClient,
// })
// if err != nil {
// return err
// }
// _, err = apiClient.AppApi.AppRestart()

request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
if err != nil {
Expand Down Expand Up @@ -1394,21 +1395,19 @@ func addCName(cnames []string, g cmd.AppNameMixIn, client *cmd.Client) error {
if err != nil {
return err
}
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/cname", appName))
var appCname tsuru.AppCName
appCname.Cname = cnames

apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}
v := url.Values{}
for _, cname := range cnames {
v.Add("cname", cname)
}
b := strings.NewReader(v.Encode())
request, err := http.NewRequest("POST", u, b)
_, err = apiClient.AppApi.AppCnameAdd(context.TODO(), appName, appCname)
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = client.Do(request)
return err
}

Expand Down Expand Up @@ -1438,32 +1437,35 @@ func (c *UnitAdd) Flags() *gnuflag.FlagSet {
}
return c.fs
}

func (c *UnitAdd) Run(context *cmd.Context, client *cmd.Client) error {
context.RawOutput()
appName, err := c.AppName()
if err != nil {
return err
func (c *UnitAdd) unitDelta(ctx *cmd.Context, AppName string) tsuru.UnitsDelta {
unitDelta := tsuru.UnitsDelta{
Units: ctx.Args[0],
Process: c.process,
Version: c.version,
}
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/units", appName))
return unitDelta
}
func (c *UnitAdd) Run(ctx *cmd.Context, client *cmd.Client) error {
ctx.RawOutput()
appName, err := c.AppName()
if err != nil {
return err
}
val := url.Values{}
val.Add("units", context.Args[0])
val.Add("process", c.process)
val.Set("version", c.version)
request, err := http.NewRequest("PUT", u, bytes.NewBufferString(val.Encode()))

apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)

unitDelta := c.unitDelta(ctx, appName)

response, err := apiClient.AppApi.UnitsAdd(context.TODO(), appName, unitDelta)
if err != nil {
return err
}
defer response.Body.Close()
return cmd.StreamJSONResponse(context.Stdout, response)
return cmd.StreamJSONResponse(ctx.Stdout, response)
}

type UnitRemove struct {
Expand Down Expand Up @@ -1545,9 +1547,17 @@ func (c *UnitSet) Flags() *gnuflag.FlagSet {
}
return c.fs
}
func (c *UnitSet) unitDelta(units int) tsuru.UnitsDelta {
unitDelta := tsuru.UnitsDelta{
Units: strconv.Itoa(units),
Process: c.process,
Version: strconv.Itoa(c.version),
}
return unitDelta
}

func (c *UnitSet) Run(context *cmd.Context, client *cmd.Client) error {
context.RawOutput()
func (c *UnitSet) Run(ctx *cmd.Context, client *cmd.Client) error {
ctx.RawOutput()
appName, err := c.AppName()
if err != nil {
return err
Expand Down Expand Up @@ -1610,62 +1620,51 @@ func (c *UnitSet) Run(context *cmd.Context, client *cmd.Client) error {
}
}

desiredUnits, err := strconv.Atoi(context.Args[0])
desiredUnits, err := strconv.Atoi(ctx.Args[0])
if err != nil {
return err
}

if existingUnits < desiredUnits {
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/units", appName))
if err != nil {
return err
}

unitsToAdd := desiredUnits - existingUnits
val := url.Values{}
val.Add("units", strconv.Itoa(unitsToAdd))
val.Add("process", c.process)
val.Add("version", strconv.Itoa(c.version))
request, err := http.NewRequest(http.MethodPut, u, bytes.NewBufferString(val.Encode()))

unitsDelta := c.unitDelta(unitsToAdd)

apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}

request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
response, err := apiClient.AppApi.UnitsAdd(context.TODO(), appName, unitsDelta)
if err != nil {
return err
}

defer response.Body.Close()
return cmd.StreamJSONResponse(context.Stdout, response)
return cmd.StreamJSONResponse(ctx.Stdout, response)
}

if existingUnits > desiredUnits {
unitsToRemove := existingUnits - desiredUnits
val := url.Values{}
val.Add("units", strconv.Itoa(unitsToRemove))
val.Add("process", c.process)
val.Add("version", strconv.Itoa(c.version))
u, err := cmd.GetURL(fmt.Sprintf("/apps/%s/units?%s", appName, val.Encode()))
if err != nil {
return err
}

request, err := http.NewRequest(http.MethodDelete, u, nil)
unitsDelta := c.unitDelta(unitsToRemove)

apiClient, err := tsuruClient.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: client.HTTPClient,
})
if err != nil {
return err
}

response, err := client.Do(request)
response, err := apiClient.AppApi.UnitsRemove(context.TODO(), appName, unitsDelta)
if err != nil {
return err
}

defer response.Body.Close()
return cmd.StreamJSONResponse(context.Stdout, response)
return cmd.StreamJSONResponse(ctx.Stdout, response)
}

fmt.Fprintf(context.Stdout, "The process %s, version %d already has %d units.\n", c.process, c.version, existingUnits)
fmt.Fprintf(ctx.Stdout, "The process %s, version %d already has %d units.\n", c.process, c.version, existingUnits)
return nil
}
Loading