Skip to content

Commit

Permalink
scale command
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldwan committed Feb 13, 2020
1 parent c5bc127 commit 2422e38
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/resource_scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

func (c *Client) ScaleApp(appID string, regions []ScaleRegionInput) ([]ScaleRegionChange, error) {
query := `
mutation ($input: ScaleAppInput!) {
scaleApp(input: $input) {
placement {
region
count
}
delta {
region
fromCount
toCount
}
}
}
`

req := c.NewRequest(query)

req.Var("input", ScaleAppInput{AppID: appID, Regions: regions})

data, err := c.Run(req)
if err != nil {
return nil, err
}

return data.ScaleApp.Delta, nil
}
26 changes: 26 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type Query struct {
ReleaseIPAddress struct {
App App
}
ScaleApp struct {
App App
Placement []RegionPlacement
Delta []ScaleRegionChange
}
}

type Definition map[string]interface{}
Expand Down Expand Up @@ -328,6 +333,27 @@ type ReleaseIPAddressInput struct {
IPAddressID string `json:"ipAddressId"`
}

type ScaleAppInput struct {
AppID string `json:"appId"`
Regions []ScaleRegionInput `json:"regions"`
}

type ScaleRegionInput struct {
Region string `json:"region"`
Count int `json:"count"`
}

type ScaleRegionChange struct {
Region string
FromCount int
ToCount int
}

type RegionPlacement struct {
Region string
Count int
}

type AllocationStatus struct {
ID string
IDShort string
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func init() {
newDocsCommand(),
newIPAddressesCommand(),
newConfigCommand(),
newAppScaleCommand(),
)

initConfig()
Expand Down
65 changes: 65 additions & 0 deletions cmd/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/docstrings"

"github.com/spf13/cobra"
)

func newAppScaleCommand() *Command {
scaleStrings := docstrings.Get("scale")

cmd := BuildCommand(nil, runAppScale, scaleStrings.Usage, scaleStrings.Short, scaleStrings.Long, true, os.Stdout, requireAppName)

cmd.Args = cobra.MinimumNArgs(1)

return cmd
}

func runAppScale(ctx *CmdContext) error {
regionInput := []api.ScaleRegionInput{}

for _, pair := range ctx.Args {
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("Changes must be provided as REGION=COUNT pairs (%s is invalid)", pair)
}

val, err := strconv.Atoi(parts[1])
if err != nil {
return fmt.Errorf("Counts must be numbers (%s is invalid)", pair)
}

regionInput = append(regionInput, api.ScaleRegionInput{
Region: parts[0],
Count: val,
})
}

if len(regionInput) < 1 {
return errors.New("Requires at least one REGION=COUNT pair")
}

changes, err := ctx.FlyClient.ScaleApp(ctx.AppName, regionInput)
if err != nil {
return err
}

if len(changes) == 0 {
fmt.Println("No changes made")
return nil
}

for _, change := range changes {
fmt.Printf("Scaled %s from %d to %d\n", change.Region, change.FromCount, change.ToCount)
}

return nil
}
7 changes: 7 additions & 0 deletions helpgen/flyctlhelp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ longHelp="""Show the release details for a specific release. A version parameter
Versions can be seen in the output of flyctl releases.
"""

[scale]
usage="scale [flags] REGION=COUNT REGION=COUNT ..."
shortHelp="Change region placement and instance count"
longHelp="""Manually configure the region placement and instance count for an app.
"""

[secrets]
usage="secrets"
shortHelp="Manage app secrets"
Expand Down Expand Up @@ -285,3 +291,4 @@ usage="version"
shortHelp="Show flyctl version information"
longHelp="""Shows version information for the flyctl command itself, including version number and build date.
"""

0 comments on commit 2422e38

Please sign in to comment.