Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
973ab31
commit 77c8533
Showing
9 changed files
with
313 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package api | ||
|
||
func (client *Client) CreatePostgresCluster(organizationID string, name string) (*TemplateDeployment, error) { | ||
query := ` | ||
mutation($input: CreatePostgresClusterInput!) { | ||
createPostgresCluster(input: $input) { | ||
templateDeployment { | ||
id | ||
status | ||
apps { | ||
nodes { | ||
name | ||
state | ||
status | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
req := client.NewRequest(query) | ||
req.Var("input", map[string]string{ | ||
"organizationId": organizationID, | ||
"name": name, | ||
}) | ||
|
||
data, err := client.Run(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &data.CreatePostgresCluster.TemplateDeployment, nil | ||
} | ||
|
||
func (client *Client) GetTemplateDeployment(id string) (*TemplateDeployment, error) { | ||
query := ` | ||
query($id: ID!) { | ||
templateDeploymentNode: node(id: $id) { | ||
... on TemplateDeployment { | ||
id | ||
status | ||
apps { | ||
nodes { | ||
name | ||
state | ||
status | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
req := client.NewRequest(query) | ||
req.Var("id", id) | ||
|
||
data, err := client.Run(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data.TemplateDeploymentNode, nil | ||
} | ||
|
||
func (client *Client) AttachPostgresCluster(input AttachPostgresClusterInput) (*App, *App, error) { | ||
query := ` | ||
mutation($input: AttachPostgresClusterInput!) { | ||
attachPostgresCluster(input: $input) { | ||
app { | ||
name | ||
} | ||
postgresClusterApp { | ||
name | ||
} | ||
} | ||
} | ||
` | ||
|
||
req := client.NewRequest(query) | ||
req.Var("input", input) | ||
|
||
data, err := client.Run(req) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return &data.AttachPostgresCluster.App, &data.AttachPostgresCluster.PostgresClusterApp, nil | ||
} | ||
|
||
func (client *Client) DetachPostgresCluster(postgresAppName string, appName string) error { | ||
query := ` | ||
mutation($input: DetachPostgresClusterInput!) { | ||
detachPostgresCluster(input: $input) { | ||
clientMutationId | ||
} | ||
} | ||
` | ||
|
||
req := client.NewRequest(query) | ||
req.Var("input", map[string]string{ | ||
"postgresClusterAppId": postgresAppName, | ||
"appId": appName, | ||
}) | ||
|
||
_, err := client.Run(req) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/superfly/flyctl/api" | ||
"github.com/superfly/flyctl/cmd/presenters" | ||
"github.com/superfly/flyctl/cmdctx" | ||
"github.com/superfly/flyctl/docstrings" | ||
) | ||
|
||
func newPostgresCommand() *Command { | ||
domainsStrings := docstrings.Get("postgres") | ||
cmd := BuildCommandKS(nil, nil, domainsStrings, os.Stdout, requireSession) | ||
|
||
listStrings := docstrings.Get("postgres.list") | ||
listCmd := BuildCommandKS(cmd, runPostgresList, listStrings, os.Stdout, requireSession) | ||
listCmd.Args = cobra.MaximumNArgs(1) | ||
|
||
createStrings := docstrings.Get("postgres.create") | ||
createCmd := BuildCommandKS(cmd, runCreatePostgresCluster, createStrings, os.Stdout, requireSession) | ||
createCmd.AddStringFlag(StringFlagOpts{Name: "organization"}) | ||
createCmd.AddStringFlag(StringFlagOpts{Name: "name"}) | ||
|
||
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"}) | ||
|
||
return cmd | ||
} | ||
|
||
func runPostgresList(ctx *cmdctx.CmdContext) error { | ||
apps, err := ctx.Client.API().GetApps(api.StringPointer("postgres_cluster")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if ctx.OutputJSON() { | ||
ctx.WriteJSON(apps) | ||
return nil | ||
} | ||
|
||
return ctx.Render(&presenters.Apps{Apps: apps}) | ||
} | ||
|
||
func runCreatePostgresCluster(ctx *cmdctx.CmdContext) error { | ||
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) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
td, err = ctx.Client.API().GetTemplateDeployment(td.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if td.Status == "failed" { | ||
fmt.Fprintf(ctx.Out, "Failed to create postgres cluster, please try again\n") | ||
break | ||
} else if td.Status == "completed" { | ||
app := td.Apps.Nodes[0] | ||
if app.Status == "running" && app.State == "DEPLOYED" { | ||
fmt.Fprintf(ctx.Out, "Postgres cluster created: %s\n", td.Apps.Nodes[0].Name) | ||
break | ||
} | ||
|
||
} | ||
|
||
fmt.Println("launching...") | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func runAttachPostgresCluster(ctx *cmdctx.CmdContext) error { | ||
postgresAppName, _ := ctx.Config.GetString("postgres-app") | ||
appName := ctx.AppName | ||
|
||
input := api.AttachPostgresClusterInput{ | ||
AppID: appName, | ||
PostgresClusterAppID: postgresAppName, | ||
} | ||
|
||
if dbName, _ := ctx.Config.GetString("database-name"); dbName != "" { | ||
input.DatabaseName = api.StringPointer(dbName) | ||
} | ||
if varName, _ := ctx.Config.GetString("variable-name"); varName != "" { | ||
input.VariableName = api.StringPointer(varName) | ||
} | ||
|
||
app, postgresApp, err := ctx.Client.API().AttachPostgresCluster(input) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Postgres cluster %s is now attached to %s\n", postgresApp.Name, app.Name) | ||
|
||
return nil | ||
} | ||
|
||
// func runPostgresClusterAttach(ctx *cmdctx.CmdContext) error { | ||
// 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) | ||
|
||
// return nil | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters