This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Organization CLI and fix tenancy issues
* Adds CLI for managing organizations * Adds a flag for specifying global policies (policies that work across organizations) * Update CHANGELOG * Misc fixes for handling service accounts in CLI
- Loading branch information
Showing
20 changed files
with
292 additions
and
33 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
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,74 @@ | ||
/////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) 2017 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/////////////////////////////////////////////////////////////////////// | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/vmware/dispatch/pkg/client" | ||
|
||
"github.com/vmware/dispatch/pkg/api/v1" | ||
"github.com/vmware/dispatch/pkg/dispatchcli/i18n" | ||
) | ||
|
||
var ( | ||
createOrganizationLong = i18n.T(`Create a dispatch organization`) | ||
|
||
createOrganizationExample = i18n.T(` | ||
# Create a organization | ||
dispatch iam create organization <organization_name> | ||
`) | ||
) | ||
|
||
// NewCmdIamCreateOrganization creates command responsible for org creation | ||
func NewCmdIamCreateOrganization(out, errOut io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: i18n.T(`organization ORGANIZATION_NAME`), | ||
Short: i18n.T(`Create organization`), | ||
Long: createOrganizationLong, | ||
Example: createOrganizationExample, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
c := identityManagerClient() | ||
err := createOrganization(out, errOut, cmd, args, c) | ||
CheckErr(err) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// CallCreateOrganization makes the api call to create a organization | ||
func callCreateOrganization(c client.IdentityClient) ModelAction { | ||
return func(p interface{}) error { | ||
organizationModel := p.(*v1.Organization) | ||
|
||
created, err := c.CreateOrganization(context.TODO(), "", organizationModel) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*organizationModel = *created | ||
return nil | ||
} | ||
} | ||
|
||
func createOrganization(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.IdentityClient) error { | ||
organizationName := args[0] | ||
|
||
organizationModel := &v1.Organization{ | ||
Name: &organizationName, | ||
} | ||
|
||
err := callCreateOrganization(c)(organizationModel) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(out, "Created organization: %s\n", *organizationModel.Name) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ dispatch iam create policy example_policy --subject [email protected] --subject | |
subjects *[]string | ||
actions *[]string | ||
resources *[]string | ||
global *bool | ||
) | ||
|
||
// NewCmdIamCreatePolicy creates command responsible for dispatch policy creation | ||
|
@@ -53,6 +54,7 @@ func NewCmdIamCreatePolicy(out io.Writer, errOut io.Writer) *cobra.Command { | |
subjects = cmd.Flags().StringSliceP("subject", "s", []string{""}, "subjects of policy rule, separated by comma") | ||
actions = cmd.Flags().StringSliceP("action", "a", []string{""}, "actions of policy rule, separated by comma") | ||
resources = cmd.Flags().StringSliceP("resource", "r", []string{""}, "resources of policy rule, separated by comma") | ||
global = cmd.Flags().Bool("global", false, "applies the policy globally across all organizations") | ||
return cmd | ||
} | ||
|
||
|
@@ -82,8 +84,9 @@ func createPolicy(out, errOut io.Writer, cmd *cobra.Command, args []string, c cl | |
} | ||
|
||
policyModel := &v1.Policy{ | ||
Name: &policyName, | ||
Rules: policyRules, | ||
Name: &policyName, | ||
Rules: policyRules, | ||
Global: *global, | ||
} | ||
|
||
err := CallCreatePolicy(c)(policyModel) | ||
|
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,74 @@ | ||
/////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) 2017 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/////////////////////////////////////////////////////////////////////// | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/vmware/dispatch/pkg/client" | ||
|
||
"github.com/vmware/dispatch/pkg/api/v1" | ||
"github.com/vmware/dispatch/pkg/dispatchcli/i18n" | ||
) | ||
|
||
var ( | ||
deleteOrganizationLong = i18n.T(`Delete a dispatch organization`) | ||
|
||
// TODO: add examples | ||
deleteOrganizationExample = i18n.T(``) | ||
) | ||
|
||
// NewCmdIamDeleteOrganization creates command for delete service accounts | ||
func NewCmdIamDeleteOrganization(out, errOut io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: i18n.T("organization ORGANIZATION_NAME"), | ||
Short: i18n.T("Delete organization"), | ||
Long: deleteOrganizationLong, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
c := identityManagerClient() | ||
err := deleteOrganization(out, errOut, cmd, args, c) | ||
CheckErr(err) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
// CallDeleteOrganization makes the API call to delete Organization | ||
func CallDeleteOrganization(c client.IdentityClient) ModelAction { | ||
return func(s interface{}) error { | ||
organizationModel := s.(*v1.Organization) | ||
|
||
deleted, err := c.DeleteOrganization(context.TODO(), "", *organizationModel.Name) | ||
if err != nil { | ||
return err | ||
} | ||
*organizationModel = *deleted | ||
return nil | ||
} | ||
} | ||
|
||
func deleteOrganization(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.IdentityClient) error { | ||
organizationModel := v1.Organization{ | ||
Name: &args[0], | ||
} | ||
|
||
err := CallDeleteOrganization(c)(&organizationModel) | ||
if err != nil { | ||
return err | ||
} | ||
if dispatchConfig.JSON { | ||
encoder := json.NewEncoder(out) | ||
encoder.SetIndent("", " ") | ||
return encoder.Encode(organizationModel) | ||
} | ||
fmt.Fprintf(out, "Deleted Organization: %s\n", *organizationModel.Name) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/////////////////////////////////////////////////////////////////////// | ||
// Copyright (c) 2017 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/////////////////////////////////////////////////////////////////////// | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"time" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
"github.com/spf13/cobra" | ||
"github.com/vmware/dispatch/pkg/client" | ||
|
||
"github.com/vmware/dispatch/pkg/api/v1" | ||
"github.com/vmware/dispatch/pkg/dispatchcli/i18n" | ||
) | ||
|
||
var ( | ||
getOrganizationsLong = i18n.T(`Get organizations`) | ||
|
||
// TODO: examples | ||
getOrganizationsExample = i18n.T(``) | ||
) | ||
|
||
// NewCmdIamGetOrganization creates command for getting organizations | ||
func NewCmdIamGetOrganization(out, errOut io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: i18n.T("organization [ORGANIZATION_NAME]"), | ||
Short: i18n.T("Get organizations"), | ||
Long: getOrganizationsLong, | ||
Args: cobra.MaximumNArgs(1), | ||
Aliases: []string{"organizations"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
c := identityManagerClient() | ||
if len(args) > 0 { | ||
err = getOrganization(out, errOut, cmd, args, c) | ||
} else { | ||
err = getOrganizations(out, errOut, cmd, c) | ||
} | ||
CheckErr(err) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func getOrganization(out, errOut io.Writer, cmd *cobra.Command, args []string, c client.IdentityClient) error { | ||
resp, err := c.GetOrganization(context.TODO(), "", args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return formatOrganizationOutput(out, false, []v1.Organization{*resp}) | ||
} | ||
|
||
func getOrganizations(out, errOut io.Writer, cmd *cobra.Command, c client.IdentityClient) error { | ||
resp, err := c.ListOrganizations(context.TODO(), "") | ||
if err != nil { | ||
return err | ||
} | ||
return formatOrganizationOutput(out, true, resp) | ||
} | ||
|
||
func formatOrganizationOutput(out io.Writer, list bool, organizations []v1.Organization) error { | ||
|
||
if dispatchConfig.JSON { | ||
encoder := json.NewEncoder(out) | ||
encoder.SetIndent("", " ") | ||
if list { | ||
return encoder.Encode(organizations) | ||
} | ||
return encoder.Encode(organizations[0]) | ||
} | ||
|
||
headers := []string{"Name", "Created Date"} | ||
table := tablewriter.NewWriter(out) | ||
table.SetHeader(headers) | ||
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false}) | ||
table.SetCenterSeparator("") | ||
for _, organization := range organizations { | ||
row := []string{*organization.Name, time.Unix(organization.CreatedTime, 0).Local().Format(time.UnixDate)} | ||
table.Append(row) | ||
} | ||
table.Render() | ||
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
Oops, something went wrong.