Skip to content

Commit

Permalink
First orgs PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Jul 30, 2020
1 parent 34b61a5 commit 1133759
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 13 deletions.
21 changes: 11 additions & 10 deletions api/http.go
Expand Up @@ -21,16 +21,17 @@ func newHTTPClient() (*http.Client, error) {
rehttp.RetryAny(
rehttp.RetryTemporaryErr(),
rehttp.RetryIsErr(func(err error) bool {
if err == nil {
return true
}
msg := err.Error()
for _, retryError := range retryErrors {
if strings.Contains(msg, retryError) {
return true
}
}
return false
return err != nil && strings.Contains(err.Error(), "INTERNAL_ERROR")
// if err == nil {
// return true
// }
// msg := err.Error()
// for _, retryError := range retryErrors {
// if strings.Contains(msg, retryError) {
// return true
// }
// }
// return false
}),
),
),
Expand Down
58 changes: 58 additions & 0 deletions api/resource_organizations.go
Expand Up @@ -23,3 +23,61 @@ func (client *Client) GetOrganizations() ([]Organization, error) {

return data.Organizations.Nodes, nil
}

func (client *Client) GetOrganization(slug string) ([]Organization, error) {
q := `
{
organizations {
nodes {
id
slug
name
type
}
}
}
`

req := client.NewRequest(q)

data, err := client.Run(req)
if err != nil {
return []Organization{}, err
}

return data.Organizations.Nodes, nil
}

func (client *Client) GetCurrentOrganizations() (Organization, []Organization, error) {
query := `
query {
userOrganizations:currentUser {
personalOrganization {
id
slug
name
type
viewerRole
}
organizations {
nodes {
id
slug
name
type
viewerRole
}
}
}
}
`

req := client.NewRequest(query)

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

return data.UserOrganizations.PersonalOrganization, data.UserOrganizations.Organizations.Nodes, nil
}
8 changes: 8 additions & 0 deletions api/types.go
Expand Up @@ -19,6 +19,7 @@ type Query struct {
Organizations struct {
Nodes []Organization
}
UserOrganizations UserOrganizations

Build Build

Expand Down Expand Up @@ -337,6 +338,13 @@ type AppCertificate struct {
}
}

type UserOrganizations struct {
PersonalOrganization Organization
Organizations struct {
Nodes []Organization
}
}

type HostnameCheck struct {
ARecords []string `json:"aRecords"`
AAAARecords []string `json:"aaaaRecords"`
Expand Down
6 changes: 6 additions & 0 deletions cmd/command.go
Expand Up @@ -11,6 +11,7 @@ import (
"syscall"

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

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -150,6 +151,11 @@ type Option func(*Command) Initializer
// InitializerFn - A wrapper for an Initializer function that takes a command context
type InitializerFn func(*cmdctx.CmdContext) error

// BuildCommandKS - A wrapper for BuildCommand which takes the docs.KeyStrings bundle instead of the coder having to manually unwrap it
func BuildCommandKS(parent *Command, fn RunFn, keystrings docstrings.KeyStrings, out io.Writer, options ...Option) *Command {
return BuildCommand(parent, fn, keystrings.Usage, keystrings.Short, keystrings.Long, out, options...)
}

// BuildCommand - builds a functioning Command using all the initializers
func BuildCommand(parent *Command, fn RunFn, usageText string, shortHelpText string, longHelpText string, out io.Writer, options ...Option) *Command {
flycmd := &Command{
Expand Down
6 changes: 3 additions & 3 deletions cmd/init.go
Expand Up @@ -178,9 +178,6 @@ func runInit(commandContext *cmdctx.CmdContext) error {
}
}
}
if configPort != "" {
newAppConfig.SetInternalPort(internalPort)
}

// The creation magic happens here....
app, err := commandContext.Client.API().CreateApp(name, org.ID)
Expand All @@ -201,6 +198,9 @@ func runInit(commandContext *cmdctx.CmdContext) error {
} else {
newAppConfig.AppName = app.Name
newAppConfig.Definition = app.Config.Definition
if configPort != "" {
newAppConfig.SetInternalPort(internalPort)
}
}
fmt.Println()

Expand Down
102 changes: 102 additions & 0 deletions cmd/orgs.go
@@ -0,0 +1,102 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"

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

func newOrgsCommand() *Command {
orgsStrings := docstrings.KeyStrings{Usage: "orgs",
Short: "Commands for managing Fly organizations",
Long: `Commands for managing Fly organizations. list, create, show and
destroy organizations.
Organization admins can also invite or remove users from Organizations.
`}

orgscmd := BuildCommandKS(nil, nil, orgsStrings, os.Stdout, requireSession)

orgsListStrings := docstrings.KeyStrings{Usage: "list",
Short: "Lists organizations for current user",
Long: `Lists organizations available to current user.
`}

BuildCommandKS(orgscmd, runOrgsList, orgsListStrings, os.Stdout, requireSession)

orgsShowStrings := docstrings.KeyStrings{Usage: "show <org>", Short: "Show organization", Long: ""}
orgsShowCommand := BuildCommandKS(orgscmd, runOrgsShow, orgsShowStrings, os.Stdout, requireSession)
orgsShowCommand.Args = cobra.ExactArgs(1)

orgsInviteStrings := docstrings.KeyStrings{Usage: "invite <org> <email>", Short: "invite to organization", Long: ""}
orgsInviteCommand := BuildCommandKS(orgscmd, runOrgsInvite, orgsInviteStrings, os.Stdout, requireSession)
orgsInviteCommand.Args = cobra.MaximumNArgs(2)

orgsRevokeStrings := docstrings.KeyStrings{Usage: "revoke <org> <email>", Short: "revoke an invitation to an organization", Long: ""}
orgsRevokeCommand := BuildCommandKS(orgscmd, runOrgsRevoke, orgsRevokeStrings, os.Stdout, requireSession)
orgsRevokeCommand.Args = cobra.MaximumNArgs(2)

orgsRemoveStrings := docstrings.KeyStrings{Usage: "remove <org> <email>", Short: "revoke an invitation to an organization", Long: ""}
orgsRemoveCommand := BuildCommandKS(orgscmd, runOrgsRemove, orgsRemoveStrings, os.Stdout, requireSession)
orgsRemoveCommand.Args = cobra.MaximumNArgs(2)

orgsCreateStrings := docstrings.KeyStrings{Usage: "create <org> <email>", Short: "Create an Organization", Long: ""}
orgsCreateCommand := BuildCommandKS(orgscmd, runOrgsCreate, orgsCreateStrings, os.Stdout, requireSession)
orgsCreateCommand.Args = cobra.MaximumNArgs(1)

orgsDestroyStrings := docstrings.KeyStrings{Usage: "destroy <org>", Short: "Destroy an Organization", Long: ""}
orgsDestroyCommand := BuildCommandKS(orgscmd, runOrgsDestroy, orgsDestroyStrings, os.Stdout, requireSession)
orgsDestroyCommand.Args = cobra.MaximumNArgs(1)

return orgscmd
}

func runOrgsList(cmdctx *cmdctx.CmdContext) error {
asJSON := cmdctx.OutputJSON()

personalOrganization, organizations, err := cmdctx.Client.API().GetCurrentOrganizations()
if err != nil {
return err
}

if asJSON {
type MyOrgs struct {
PersonalOrganization api.Organization
Organizations []api.Organization
}
cmdctx.WriteJSON(MyOrgs{PersonalOrganization: personalOrganization, Organizations: organizations})
return nil
}

fmt.Println("Formatted Print Here")

return nil
}

func runOrgsShow(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Show Not implemented")
}

func runOrgsInvite(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Invite Not implemented")
}

func runOrgsCreate(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Create Not implemented")
}

func runOrgsRemove(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Remove Not implemented")
}

func runOrgsRevoke(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Revoke Not implemented")
}

func runOrgsDestroy(ctx *cmdctx.CmdContext) error {
return fmt.Errorf("Destroy Not implemented")
}
1 change: 1 addition & 0 deletions cmd/root.go
Expand Up @@ -91,6 +91,7 @@ func init() {
newStatusCommand(),
newSuspendCommand(),
newVersionCommand(),
newOrgsCommand(),
)

initConfig()
Expand Down

0 comments on commit 1133759

Please sign in to comment.