Skip to content

Commit

Permalink
detach pg cluster, add region on create
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldwan committed Jan 21, 2021
1 parent 77c8533 commit d3c6773
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
3 changes: 2 additions & 1 deletion api/resource_postgres.go
@@ -1,6 +1,6 @@
package api

func (client *Client) CreatePostgresCluster(organizationID string, name string) (*TemplateDeployment, error) {
func (client *Client) CreatePostgresCluster(organizationID string, name string, region string) (*TemplateDeployment, error) {
query := `
mutation($input: CreatePostgresClusterInput!) {
createPostgresCluster(input: $input) {
Expand All @@ -23,6 +23,7 @@ func (client *Client) CreatePostgresCluster(organizationID string, name string)
req.Var("input", map[string]string{
"organizationId": organizationID,
"name": name,
"region": region,
})

data, err := client.Run(req)
Expand Down
44 changes: 33 additions & 11 deletions cmd/postgres.go
Expand Up @@ -23,14 +23,19 @@ func newPostgresCommand() *Command {

createStrings := docstrings.Get("postgres.create")
createCmd := BuildCommandKS(cmd, runCreatePostgresCluster, createStrings, os.Stdout, requireSession)
createCmd.AddStringFlag(StringFlagOpts{Name: "organization"})
createCmd.AddStringFlag(StringFlagOpts{Name: "name"})
createCmd.AddStringFlag(StringFlagOpts{Name: "organization", Description: "the organization that will own the app"})
createCmd.AddStringFlag(StringFlagOpts{Name: "name", Description: "the name of the new app"})
createCmd.AddStringFlag(StringFlagOpts{Name: "region", Description: "the region to launch the new app in"})

attachStrngs := docstrings.Get("postgres.attach")
attachCmd := BuildCommandKS(cmd, runAttachPostgresCluster, attachStrngs, os.Stdout, requireSession, requireAppName)
attachCmd.AddStringFlag(StringFlagOpts{Name: "postgres-app"})
attachCmd.AddStringFlag(StringFlagOpts{Name: "database-name"})
attachCmd.AddStringFlag(StringFlagOpts{Name: "variable-name"})
attachCmd.AddStringFlag(StringFlagOpts{Name: "postgres-app", Description: "the postgres cluster to attach to the app"})
attachCmd.AddStringFlag(StringFlagOpts{Name: "database-name", Description: "database to use, defaults to a new database with the same name as the app"})
attachCmd.AddStringFlag(StringFlagOpts{Name: "variable-name", Description: "the env variable name that will be added to the app. Defaults to DATABASE_URL"})

detachStrngs := docstrings.Get("postgres.detach")
detachCmd := BuildCommandKS(cmd, runDetachPostgresCluster, detachStrngs, os.Stdout, requireSession, requireAppName)
detachCmd.AddStringFlag(StringFlagOpts{Name: "postgres-app", Description: "the postgres cluster to detach from the app"})

return cmd
}
Expand All @@ -50,20 +55,22 @@ func runPostgresList(ctx *cmdctx.CmdContext) error {
}

func runCreatePostgresCluster(ctx *cmdctx.CmdContext) error {
name, _ := ctx.Config.GetString("name")
if name == "" {
return errors.New("name is required")
}

region, _ := ctx.Config.GetString("region")

orgSlug, _ := ctx.Config.GetString("organization")
org, err := selectOrganization(ctx.Client.API(), orgSlug)
if err != nil {
return err
}

name, _ := ctx.Config.GetString("name")
if name == "" {
return errors.New("name is required")
}

fmt.Fprintf(ctx.Out, "Creating postgres cluster %s in organization %s, this will take a minute...\n", name, org.Slug)

td, err := ctx.Client.API().CreatePostgresCluster(org.ID, name)
td, err := ctx.Client.API().CreatePostgresCluster(org.ID, name, region)
if err != nil {
return err
}
Expand Down Expand Up @@ -121,6 +128,21 @@ func runAttachPostgresCluster(ctx *cmdctx.CmdContext) error {
return nil
}

func runDetachPostgresCluster(ctx *cmdctx.CmdContext) error {
postgresAppName, _ := ctx.Config.GetString("postgres-app")
appName := ctx.AppName

err := ctx.Client.API().DetachPostgresCluster(postgresAppName, appName)

if err != nil {
return err
}

fmt.Printf("Postgres cluster %s is now detached from %s\n", postgresAppName, appName)

return nil
}

// func runPostgresClusterAttach(ctx *cmdctx.CmdContext) error {
// name, _ := ctx.Config.GetString("name")
// if name == "" {
Expand Down
4 changes: 4 additions & 0 deletions docstrings/gen.go
Expand Up @@ -488,6 +488,10 @@ about the Fly platform.`,
return KeyStrings{"create", "Create a postgres cluster",
`Create a postgres cluster`,
}
case "postgres.detach":
return KeyStrings{"detach", "Detach a postgres cluster from an app",
`Detach a postgres cluster from an app`,
}
case "postgres.list":
return KeyStrings{"list", "list postgres clusters",
`list postgres clusters`,
Expand Down
4 changes: 4 additions & 0 deletions helpgen/flyctlhelp.toml
Expand Up @@ -500,6 +500,10 @@ longHelp = "Manage postgres clusters"
usage = "attach"
shortHelp = "Attach a postgres cluster to an app"
longHelp = "Attach a postgres cluster to an app"
[postgres.detach]
usage = "detach"
shortHelp = "Detach a postgres cluster from an app"
longHelp = "Detach a postgres cluster from an app"
[postgres.list]
usage = "list"
shortHelp = "list postgres clusters"
Expand Down

0 comments on commit d3c6773

Please sign in to comment.