Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: restructure organization commands #332

Merged
merged 14 commits into from
Jun 4, 2024
11 changes: 2 additions & 9 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ var addNotificationCmd = &cobra.Command{
},
}

var addOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add an organization, or add a group/project to an organization",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
addCmd.AddCommand(addDeployTargetCmd)
addCmd.AddCommand(addGroupCmd)
Expand All @@ -43,4 +34,6 @@ func init() {
addCmd.AddCommand(addUserSSHKeyCmd)
addCmd.AddCommand(addVariableCmd)
addCmd.AddCommand(addDeployTargetConfigCmd)
addCmd.AddCommand(addDeployTargetToOrganizationCmd)
addCmd.AddCommand(addAdministratorToOrganizationCmd)
}
12 changes: 3 additions & 9 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ var deleteNotificationCmd = &cobra.Command{
},
}

var deleteOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add an organization, or add a group/project to an organization",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
validateToken(lagoonCLIConfig.Current) // get a new token if the current one is invalid
},
}

func init() {
deleteCmd.AddCommand(deleteEnvCmd)
deleteCmd.AddCommand(deleteGroupCmd)
Expand All @@ -44,4 +35,7 @@ func init() {
deleteCmd.AddCommand(deleteVariableCmd)
deleteCmd.AddCommand(deleteDeployTargetConfigCmd)
deleteCmd.AddCommand(deleteOrganizationCmd)
deleteCmd.AddCommand(removeDeployTargetFromOrganizationCmd)
deleteCmd.AddCommand(removeAdministratorFromOrganizationCmd)
deleteCmd.AddCommand(removeProjectFromOrganizationCmd)
}
42 changes: 19 additions & 23 deletions cmd/deploytarget.go
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -324,28 +324,25 @@ var deleteDeployTargetCmd = &cobra.Command{
}

var addDeployTargetToOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
Use: "organization-deploytarget",
Aliases: []string{"org-dt"},
Short: "Add a deploy target to an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)

organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
deploytarget, err := cmd.Flags().GetUint("deploytarget")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {

if err := requiredInputCheck("Organization name", organizationName, "Deploy Target", strconv.Itoa(int(deploytarget))); err != nil {
return err
}

Expand All @@ -362,28 +359,27 @@ var addDeployTargetToOrganizationCmd = &cobra.Command{
handleError(err)

deployTargetInput := s.AddDeployTargetToOrganizationInput{
DeployTarget: deployTarget,
DeployTarget: deploytarget,
Organization: organization.ID,
}

deployTargetResponse, err := l.AddDeployTargetToOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)

resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTargetResponse.Name,
"Organization Name": organizationName,
"Deploy Target": deploytarget,
"Organization Name": deployTargetResponse.Name,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
var removeDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "organization-deploytarget",
Aliases: []string{"org-dt"},
Short: "Remove a deploy target from an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
Expand All @@ -399,11 +395,11 @@ var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
deployTarget, err := cmd.Flags().GetUint("deploy-target")
deploytarget, err := cmd.Flags().GetUint("deploytarget")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deploytarget))); err != nil {
return err
}

Expand All @@ -420,17 +416,17 @@ var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
handleError(err)

deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{
DeployTarget: deployTarget,
DeployTarget: deploytarget,
Organization: organization.ID,
}

if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deployTarget, organization.Name)) {
if yesNo(fmt.Sprintf("You are attempting to remove deploy target '%d' from organization '%s', are you sure?", deploytarget, organization.Name)) {
_, err := l.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)
resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTarget,
"Deploy Target": deploytarget,
"Organization Name": organizationName,
},
}
Expand All @@ -454,13 +450,13 @@ func init() {
addDeployTargetCmd.Flags().StringP("build-image", "", "", "DeployTarget build image to use (if different to the default)")

addDeployTargetToOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
addDeployTargetToOrganizationCmd.Flags().UintP("deploy-target", "D", 0, "ID of DeployTarget")
addDeployTargetToOrganizationCmd.Flags().UintP("deploytarget", "D", 0, "ID of DeployTarget")

deleteDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
deleteDeployTargetCmd.Flags().StringP("name", "", "", "Name of DeployTarget")

RemoveDeployTargetFromOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
RemoveDeployTargetFromOrganizationCmd.Flags().UintP("deploy-target", "D", 0, "ID of DeployTarget")
removeDeployTargetFromOrganizationCmd.Flags().StringP("name", "O", "", "Name of Organization")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to organization to match other flags in other commands. There may be other organization commands that use name, but if they can be updated to be organization that'd be wicked. That way there is no confusion across the flagset for organizations.

It was initially confusing to me when i went to use --name in one place (lagoon list organization-users --organization ${orgname}) only to find it was --organization and then vice versa when using a different command (lagoon delete organization-deploytarget --name ${orgname}).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only one that should be --name is maybe add organization.

Alternatively, if the flag is changed to organization-name then we cover all possibly cases on all commands

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I've changed the flag to organization-name.

removeDeployTargetFromOrganizationCmd.Flags().UintP("deploytarget", "D", 0, "ID of DeployTarget")

updateDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
updateDeployTargetCmd.Flags().StringP("console-url", "", "", "DeployTarget console URL")
Expand Down
4 changes: 2 additions & 2 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var updateEnvironmentCmd = &cobra.Command{
if err != nil {
return err
}
openShift, err := cmd.Flags().GetUint("deploy-target")
openShift, err := cmd.Flags().GetUint("deploytarget")
if err != nil {
return err
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func init() {
updateEnvironmentCmd.Flags().String("route", "", "Update the route for the selected environment")
updateEnvironmentCmd.Flags().String("routes", "", "Update the routes for the selected environment")
updateEnvironmentCmd.Flags().UintVarP(&environmentAutoIdle, "auto-idle", "a", 1, "Auto idle setting of the environment")
updateEnvironmentCmd.Flags().UintP("deploy-target", "d", 0, "Reference to OpenShift Object this Environment should be deployed to")
updateEnvironmentCmd.Flags().UintP("deploytarget", "d", 0, "Reference to OpenShift Object this Environment should be deployed to")
updateEnvironmentCmd.Flags().String("environment-type", "", "Update the environment type - production | development")
updateEnvironmentCmd.Flags().String("deploy-type", "", "Update the deploy type - branch | pullrequest | promote")
}
144 changes: 62 additions & 82 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,68 @@ func parseGroup(flags pflag.FlagSet) api.Group {
var addGroupCmd = &cobra.Command{
Use: "group",
Aliases: []string{"g"},
Short: "Add a group to lagoon",
Run: func(cmd *cobra.Command, args []string) {
groupFlags := parseGroup(*cmd.Flags())
if groupFlags.Name == "" {
fmt.Println("Missing arguments: Group name is not defined")
cmd.Help()
os.Exit(1)
}
var customReqResult []byte
var err error
customReqResult, err = uClient.AddGroup(groupFlags)
handleError(err)
returnResultData := map[string]interface{}{}
err = json.Unmarshal([]byte(customReqResult), &returnResultData)
Short: "Add a group to Lagoon, or add a group to an organization",
Long: "To add a group to an organization, you'll need to include the `organization` flag and provide the name of the organization. You need to be an owner of this organization to do this.\nIf you're the organization owner and want to grant yourself ownership to this group to be able to deploy projects that may be added to it, specify the `owner` flag, otherwise you will still be able to add and remove users without being an owner",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Group name", groupName); err != nil {
return err
}
orgOwner, err := cmd.Flags().GetBool("owner")
if err != nil {
return err
}
organizationName, err := cmd.Flags().GetString("organization")
if err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)

if organizationName != "" {
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
groupInput := s.AddGroupToOrganizationInput{
Name: groupName,
Organization: organization.ID,
AddOrgOwner: orgOwner,
}
_, err = l.AddGroupToOrganization(context.TODO(), &groupInput, lc)
handleError(err)
} else {
groupInput := s.AddGroupInput{
Name: groupName,
}
_, err = l.AddGroup(context.TODO(), &groupInput, lc)
handleError(err)
}

resultData := output.Result{
Result: "success",
ResultData: returnResultData,
Result: "success",
ResultData: map[string]interface{}{
"Group Name": groupName,
},
}
if organizationName != "" {
resultData.ResultData["Organization"] = organizationName
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

Expand Down Expand Up @@ -236,70 +278,10 @@ var deleteGroupCmd = &cobra.Command{
},
}

var addGroupToOrganizationCmd = &cobra.Command{
Use: "group",
Aliases: []string{"g"},
Short: "Add a group to an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
handleError(err)
orgOwner, err := cmd.Flags().GetBool("org-owner")
if err != nil {
return err
}
organizationName, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
if err := requiredInputCheck("Organization name", organizationName); err != nil {
return err
}
groupName, err := cmd.Flags().GetString("group")
if err != nil {
return err
}
if err := requiredInputCheck("Group name", groupName); err != nil {
return err
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
lagoonCLIConfig.Lagoons[current].Version,
&token,
debug)

organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)

groupInput := s.AddGroupToOrganizationInput{
Name: groupName,
Organization: organization.ID,
AddOrgOwner: orgOwner,
}
group := s.OrgGroup{}
err = lc.AddGroupToOrganization(context.TODO(), &groupInput, &group)
handleError(err)

resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Group Name": group.Name,
"Organization Name": organizationName,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

func init() {
addGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
addGroupCmd.Flags().StringP("name", "N", "", "Name of the group")
addGroupCmd.Flags().StringP("organization", "O", "", "Name of the organization")
addGroupCmd.Flags().Bool("owner", false, "Organization owner only: Flag to grant yourself ownership of this group")
addUserToGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
addUserToGroupCmd.Flags().StringP("role", "R", "", "Role in the group [owner, maintainer, developer, reporter, guest]")
addUserToGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
Expand All @@ -308,7 +290,5 @@ func init() {
deleteUserFromGroupCmd.Flags().StringVarP(&userEmail, "email", "E", "", "Email address of the user")
deleteProjectFromGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
deleteGroupCmd.Flags().StringVarP(&groupName, "name", "N", "", "Name of the group")
addGroupToOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization")
addGroupToOrganizationCmd.Flags().StringP("group", "G", "", "Name of the group")
addGroupToOrganizationCmd.Flags().Bool("org-owner", false, "Flag to add the user to the group as an owner")

}
Loading
Loading