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

Feature: Organizations support #292

Merged
merged 24 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6b16ca3
Initial commit
CGoodwin90 Sep 26, 2023
ccb628f
Add Org cmd
CGoodwin90 Sep 27, 2023
a77b506
Delete organization functionality
CGoodwin90 Sep 28, 2023
ac2723b
Fixed duplicate cmd
CGoodwin90 Sep 28, 2023
fe9078e
Included update Org functionality
CGoodwin90 Sep 28, 2023
4fac406
Extends add & update cmd
CGoodwin90 Oct 3, 2023
8b836cb
List cmd for groups & projects by org id
CGoodwin90 Oct 4, 2023
da89300
Add project to org
CGoodwin90 Oct 6, 2023
d8464e3
Pointing to correct machinery version
CGoodwin90 Oct 9, 2023
994ac3a
Updated group organization cmd
CGoodwin90 Oct 11, 2023
bbb3db5
Included deploy targets & users cmds
CGoodwin90 Oct 13, 2023
d31a6fb
cmd & flag cleanup
CGoodwin90 Oct 16, 2023
2a6d686
Moved org list commands to sub commands
CGoodwin90 Oct 18, 2023
bb8acfe
Introduced addProjectToOrganization & updated add organizations to us…
CGoodwin90 Oct 19, 2023
3a55ad3
add & remove deploytarget
CGoodwin90 Oct 20, 2023
55fdf00
RemoveUserCmd & implement shared function
CGoodwin90 Oct 20, 2023
86e5fb4
Updated add project cmd + various fixes
CGoodwin90 Oct 23, 2023
bbb125b
Updated to account for negative quota
CGoodwin90 Oct 23, 2023
32d3884
Updated listOrganizationDeployTargetsCmd & included listOrganizationsCmd
CGoodwin90 Nov 28, 2023
3d8bd61
Updated to machinery v0.0.13
CGoodwin90 Nov 28, 2023
e0318db
Resolved test err
CGoodwin90 Nov 28, 2023
58a2fdd
Merge branch 'main' into feature/organizations
CGoodwin90 Nov 28, 2023
b692d1d
Merged main & generated docs
CGoodwin90 Dec 5, 2023
1d6fdc9
Merge branch 'feature/organizations' of https://github.com/uselagoon/…
CGoodwin90 Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@ 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)
addCmd.AddCommand(addProjectCmd)
addCmd.AddCommand(addProjectToGroupCmd)
addCmd.AddCommand(addNotificationCmd)
addCmd.AddCommand(addUserCmd)
addCmd.AddCommand(addOrganizationCmd)
addCmd.AddCommand(addUserToGroupCmd)
addCmd.AddCommand(addUserSSHKeyCmd)
addCmd.AddCommand(addVariableCmd)
Expand Down
10 changes: 10 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ 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 @@ -34,4 +43,5 @@ func init() {
deleteCmd.AddCommand(deleteUserFromGroupCmd)
deleteCmd.AddCommand(deleteVariableCmd)
deleteCmd.AddCommand(deleteDeployTargetConfigCmd)
deleteCmd.AddCommand(deleteOrganizationCmd)
}
126 changes: 125 additions & 1 deletion cmd/deploytarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package cmd
import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/internal/schema"
"github.com/uselagoon/lagoon-cli/pkg/output"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"
"strconv"
)

var addDeployTargetCmd = &cobra.Command{
Expand Down Expand Up @@ -319,6 +322,121 @@ var deleteDeployTargetCmd = &cobra.Command{
},
}

var addDeployTargetToOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"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")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {
return err
}

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

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

deployTargetInput := s.AddDeployTargetToOrganizationInput{
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,
},
}
output.RenderResult(resultData, outputOptions)
return nil
},
}

var RemoveDeployTargetFromOrganizationCmd = &cobra.Command{
Use: "deploytarget",
Aliases: []string{"dt"},
Short: "Remove a deploy target from 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")
if err != nil {
return err
}
if err := requiredInputCheck("Deploy Target", strconv.Itoa(int(deployTarget))); err != nil {
return err
}

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

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

deployTargetInput := s.RemoveDeployTargetFromOrganizationInput{
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)) {
_, err := l.RemoveDeployTargetFromOrganization(context.TODO(), &deployTargetInput, lc)
handleError(err)
resultData := output.Result{
Result: "success",
ResultData: map[string]interface{}{
"Deploy Target": deployTarget,
"Organization Name": organizationName,
},
}
output.RenderResult(resultData, outputOptions)
}
return nil
},
}

func init() {
addDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
addDeployTargetCmd.Flags().StringP("name", "", "", "Name of DeployTarget")
Expand All @@ -332,9 +450,15 @@ func init() {
addDeployTargetCmd.Flags().StringP("ssh-port", "", "", "DeployTarget ssh port")
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")

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")

updateDeployTargetCmd.Flags().UintP("id", "", 0, "ID of the DeployTarget")
updateDeployTargetCmd.Flags().StringP("console-url", "", "", "DeployTarget console URL")
updateDeployTargetCmd.Flags().StringP("token", "", "", "DeployTarget token")
Expand Down
16 changes: 1 addition & 15 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var updateEnvironmentCmd = &cobra.Command{
Route: nullStrCheck(route),
Routes: nullStrCheck(routes),
DeployTitle: nullStrCheck(deployTitle),
Openshift: nullIntCheck(openShift),
Openshift: nullUintCheck(openShift),
}
if environmentAutoIdleProvided {
environmentFlags.AutoIdle = &environmentAutoIdle
Expand Down Expand Up @@ -158,20 +158,6 @@ var updateEnvironmentCmd = &cobra.Command{
},
}

func nullStrCheck(s string) *string {
if s == "" {
return nil
}
return &s
}

func nullIntCheck(i uint) *uint {
if i == 0 {
return nil
}
return &i
}

func checkFlags(f *pflag.Flag) {
if f.Name == "auto-idle" {
environmentAutoIdleProvided = true
Expand Down
63 changes: 60 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
"os"
"strconv"

l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
)
Expand Down Expand Up @@ -218,10 +218,66 @@ var getToken = &cobra.Command{
},
}

var getOrganizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Get details about an organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(cmdLagoon)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
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
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
organization, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
handleError(err)

if organization.Name == "" {
output.RenderInfo(fmt.Sprintf("No organization found for '%s'", organizationName), outputOptions)
return nil
}

data := []output.Data{}
data = append(data, []string{
strconv.Itoa(int(organization.ID)),
organization.Name,
organization.Description,
strconv.Itoa(int(organization.QuotaProject)),
strconv.Itoa(int(organization.QuotaGroup)),
strconv.Itoa(int(organization.QuotaNotification)),
})

dataMain := output.Table{
Header: []string{"ID", "Name", "Description", "Project Quota", "Group Quota", "Notification Quota"},
Data: data,
}

output.RenderOutput(dataMain, outputOptions)
return nil
},
}

func init() {
getCmd.AddCommand(getAllUserKeysCmd)
getCmd.AddCommand(getDeploymentCmd)
getCmd.AddCommand(getEnvironmentCmd)
getCmd.AddCommand(getOrganizationCmd)
getCmd.AddCommand(getProjectCmd)
getCmd.AddCommand(getProjectKeyCmd)
getCmd.AddCommand(getUserKeysCmd)
Expand All @@ -231,4 +287,5 @@ func init() {
getTaskByID.Flags().BoolP("logs", "L", false, "Show the task logs if available")
getProjectKeyCmd.Flags().BoolVarP(&revealValue, "reveal", "", false, "Reveal the variable values")
getDeploymentCmd.Flags().StringVarP(&remoteID, "remoteid", "R", "", "The remote ID of the deployment")
getOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization")
}
Loading
Loading