-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CRUD organizations support
- Loading branch information
Showing
22 changed files
with
484 additions
and
46 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
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,55 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package organizations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/cli/cmd/cloudx/client" | ||
cloud "github.com/ory/client-go/1.2" | ||
|
||
"github.com/ory/x/cmdx" | ||
"github.com/ory/x/flagx" | ||
) | ||
|
||
func NewCreateOrganizationCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "organization label [--project=PROJECT_ID] [--domains=a.example.com,b.example.com]", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Create a new Ory Network organization", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
h, err := client.NewCommandHelper(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
projectID, err := client.ProjectOrDefault(cmd, h) | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
label := args[0] | ||
domains := flagx.MustGetStringSlice(cmd, "domains") | ||
|
||
organization, err := h.CreateOrganization(projectID, cloud.OrganizationBody{ | ||
Label: &label, | ||
Domains: domains, | ||
}) | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
|
||
_, _ = fmt.Fprintln(h.VerboseErrWriter, "Organization created successfully!") | ||
cmdx.PrintRow(cmd, output(*organization)) | ||
return nil | ||
}, | ||
} | ||
|
||
client.RegisterProjectFlag(cmd.Flags()) | ||
cmdx.RegisterFormatFlags(cmd.Flags()) | ||
cmd.Flags().StringSliceP("domains", "d", []string{}, "A list of domains that will be used for this organization.") | ||
|
||
return cmd | ||
} |
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,44 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package organizations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/cli/cmd/cloudx/client" | ||
"github.com/ory/x/cmdx" | ||
) | ||
|
||
func NewDeleteOrganizationCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "organization id [--project=PROJECT_ID]", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Delete the organization with the given ID", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
h, err := client.NewCommandHelper(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
projectID, err := client.ProjectOrDefault(cmd, h) | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
orgID := args[0] | ||
|
||
err = h.DeleteOrganization(projectID, orgID) | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
|
||
_, _ = fmt.Fprintln(h.VerboseErrWriter, "Organization deleted successfully!") | ||
return nil | ||
}, | ||
} | ||
|
||
client.RegisterProjectFlag(cmd.Flags()) | ||
return cmd | ||
} |
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,42 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package organizations | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/cli/cmd/cloudx/client" | ||
"github.com/ory/x/cmdx" | ||
) | ||
|
||
func NewListOrganizationsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "organizations", | ||
Args: cobra.NoArgs, | ||
Short: "List your Ory Network organizations", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
h, err := client.NewCommandHelper(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id, err := client.ProjectOrDefault(cmd, h) | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
|
||
organizations, err := h.ListOrganizations(id) | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
|
||
cmdx.PrintTable(cmd, &outputOrganizations{organizations}) | ||
return nil | ||
}, | ||
} | ||
|
||
client.RegisterProjectFlag(cmd.Flags()) | ||
cmdx.RegisterFormatFlags(cmd.Flags()) | ||
return cmd | ||
} |
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,62 @@ | ||
// Copyright © 2023 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package organizations_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ory/cli/cmd/cloudx/client" | ||
"github.com/ory/cli/cmd/cloudx/testhelpers" | ||
"github.com/ory/x/cmdx" | ||
) | ||
|
||
var ( | ||
project, defaultEmail, defaultPassword string | ||
defaultCmd *cmdx.CommandExecuter | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
_, defaultEmail, defaultPassword, _, project, defaultCmd = testhelpers.CreateDefaultAssets() | ||
testhelpers.RunAgainstStaging(m) | ||
} | ||
|
||
func TestNoUnauthenticated(t *testing.T) { | ||
t.Parallel() | ||
cases := []struct { | ||
verb string | ||
noun string | ||
arg string | ||
}{ | ||
{verb: "create", noun: "organization", arg: "my-org"}, | ||
{verb: "ls", noun: "organizations"}, | ||
{verb: "list", noun: "organizations"}, | ||
{verb: "delete", noun: "organization", arg: "some-uuid"}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run("verb="+tc.verb, func(t *testing.T) { | ||
configDir := testhelpers.NewConfigDir(t) | ||
cmd := testhelpers.ConfigAwareCmd(configDir) | ||
args := []string{tc.verb, tc.noun, "--quiet", "--project", project} | ||
if tc.arg != "" { | ||
args = append(args, tc.arg) | ||
} | ||
_, _, err := cmd.Exec(nil, args...) | ||
require.ErrorIsf(t, err, client.ErrNoConfigQuiet, "got error: %v", err) | ||
}) | ||
} | ||
} | ||
|
||
func TestCRUD(t *testing.T) { | ||
t.Parallel() | ||
|
||
defaultCmd.ExecNoErr(t, "use", project) | ||
|
||
// List organizations: Empty | ||
out := defaultCmd.ExecNoErr(t, "list", "organizations", "--format=json") | ||
assert.Equal(t, "[]\n", out) | ||
} |
Oops, something went wrong.