Skip to content

Commit

Permalink
Introduced addProjectToOrganization & updated add organizations to us…
Browse files Browse the repository at this point in the history
…e sub commands
  • Loading branch information
CGoodwin90 committed Oct 19, 2023
1 parent 2a6d686 commit bb8acfe
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 15 deletions.
10 changes: 9 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ 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(addGroupToOrganizationCmd)
addCmd.AddCommand(addOrganizationCmd)
addCmd.AddCommand(addUserToGroupCmd)
addCmd.AddCommand(addUserSSHKeyCmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ var deleteGroupCmd = &cobra.Command{
}

var addGroupToOrganizationCmd = &cobra.Command{
Use: "organization-group",
Aliases: []string{"og", "orggroup"},
Use: "group",
Aliases: []string{"g"},
Short: "Add a group to an Organization",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
Expand Down
24 changes: 14 additions & 10 deletions cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
)

var addOrganizationCmd = &cobra.Command{
var addOrgCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"o"},
Short: "Add a new organization to Lagoon",
Expand All @@ -23,7 +23,7 @@ var addOrganizationCmd = &cobra.Command{
if err != nil {
return err
}
organizationName, err := cmd.Flags().GetString("name")
organizationName, err := cmd.Flags().GetString("organization")
if err != nil {
return err
}
Expand Down Expand Up @@ -220,14 +220,18 @@ var updateOrganizationCmd = &cobra.Command{
}

func init() {
addOrganizationCmd.Flags().String("name", "", "Name of the organization")
addOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization")
addOrganizationCmd.Flags().String("description", "", "Description of the organization")
addOrganizationCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization")
addOrganizationCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization")
addOrganizationCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization")
addOrganizationCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization")
addOrganizationCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization")
addOrganizationCmd.AddCommand(addOrgCmd)
addOrganizationCmd.AddCommand(addGroupToOrganizationCmd)
addOrganizationCmd.AddCommand(addProjectToOrganizationCmd)

addOrgCmd.Flags().StringP("organization", "O", "", "Name of the organization")
addOrgCmd.Flags().String("friendlyName", "", "Friendly name of the organization")
addOrgCmd.Flags().String("description", "", "Description of the organization")
addOrgCmd.Flags().Uint("quotaProject", 0, "Project quota for the organization")
addOrgCmd.Flags().Uint("quotaGroup", 0, "Group quota for the organization")
addOrgCmd.Flags().Uint("quotaNotification", 0, "Notification quota for the organization")
addOrgCmd.Flags().Uint("quotaEnvironment", 0, "Environment quota for the organization")
addOrgCmd.Flags().Uint("quotaRoute", 0, "Route quota for the organization")

updateOrganizationCmd.Flags().StringP("organization", "O", "", "Name of the organization to update")
updateOrganizationCmd.Flags().String("friendlyName", "", "Friendly name of the organization")
Expand Down
165 changes: 165 additions & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"
"os"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -364,6 +368,148 @@ var deleteProjectMetadataByKey = &cobra.Command{
},
}

// TODO - refactor once machinery is updated
var addProjectToOrganizationCmd = &cobra.Command{
Use: "project",
Aliases: []string{"p"},
Short: "Add a project 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)

requiredInputCheck("Project name", cmdProjectName)
organizationName, err := cmd.Flags().GetString("organization")
requiredInputCheck("Organization name", organizationName)
gitUrl, err := cmd.Flags().GetString("gitUrl")
requiredInputCheck("gitUrl", gitUrl)
if err != nil {
return err
}
productionEnvironment, err := cmd.Flags().GetString("productionEnvironment")
requiredInputCheck("Production Environment", productionEnvironment)
if err != nil {
return err
}
openshift, err := cmd.Flags().GetUint("openshift")
requiredInputCheck("openshift", strconv.Itoa(int(openshift)))
if err != nil {
return err
}
standbyProductionEnvironment, err := cmd.Flags().GetString("standbyProductionEnvironment")
if err != nil {
return err
}
branches, err := cmd.Flags().GetString("branches")
if err != nil {
return err
}
pullrequests, err := cmd.Flags().GetString("pullrequests")
if err != nil {
return err
}
openshiftProjectPattern, err := cmd.Flags().GetString("openshiftProjectPattern")
if err != nil {
return err
}
developmentEnvironmentsLimit, err := cmd.Flags().GetUint("developmentEnvironmentsLimit")
if err != nil {
return err
}
storageCalc, err := cmd.Flags().GetUint("storageCalc")
if err != nil {
return err
}
autoIdle, err := cmd.Flags().GetUint("autoIdle")
if err != nil {
return err
}
subfolder, err := cmd.Flags().GetString("subfolder")
if err != nil {
return err
}
privateKey, err := cmd.Flags().GetString("privateKey")
if err != nil {
return err
}
orgOwner, err := cmd.Flags().GetBool("orgOwner")
if err != nil {
return err
}
buildImage, err := cmd.Flags().GetString("buildImage")
if err != nil {
return err
}
availability, err := cmd.Flags().GetString("availability")
if err != nil {
return err
}

//factsUi, err := cmd.Flags().GetUint("factsUi")
//if err != nil {
// return err
//}
//problemsUi, err := cmd.Flags().GetUint("problemsUi")
//if err != nil {
// return err
//}
//routerPattern, err := cmd.Flags().GetString("routerPattern")
//if err != nil {
// return err
//}
//deploymentsDisabled, err := cmd.Flags().GetUint("deploymentsDisabled")
//if 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)

projectInput := s.AddProjectInput{
Name: cmdProjectName,
Organization: organization.ID,
AddOrgOwner: orgOwner,
BuildImage: buildImage,
Availability: s.ProjectAvailability(availability),
GitURL: gitUrl,
ProductionEnvironment: productionEnvironment,
StandbyProductionEnvironment: standbyProductionEnvironment,
Branches: branches,
PullRequests: pullrequests,
OpenshiftProjectPattern: openshiftProjectPattern,
Openshift: openshift,
DevelopmentEnvironmentsLimit: developmentEnvironmentsLimit,
StorageCalc: storageCalc,
AutoIdle: autoIdle,
Subfolder: subfolder,
PrivateKey: privateKey,
}
project := s.Project{}
err = lc.AddProject(context.TODO(), &projectInput, &project)
handleError(err)

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

func init() {
updateProjectCmd.Flags().StringVarP(&jsonPatch, "json", "j", "", "JSON string to patch")

Expand Down Expand Up @@ -418,4 +564,23 @@ func init() {
deleteProjectMetadataByKey.Flags().StringP("key", "K", "", "The key name of the metadata value you are querying on")

getCmd.AddCommand(getProjectMetadata)

addProjectToOrganizationCmd.Flags().String("buildImage", "", "Build Image for the project")
addProjectToOrganizationCmd.Flags().String("availability", "", "Availability of the project")
addProjectToOrganizationCmd.Flags().String("gitUrl", "", "GitURL of the project")
addProjectToOrganizationCmd.Flags().String("productionEnvironment", "", "Production Environment for the project")
addProjectToOrganizationCmd.Flags().String("standbyProductionEnvironment", "", "Standby Production Environment for the project")
addProjectToOrganizationCmd.Flags().String("subfolder", "", "Set if the .lagoon.yml should be found in a subfolder useful if you have multiple Lagoon projects per Git Repository")
addProjectToOrganizationCmd.Flags().String("privateKey", "", "Private key to use for the project")
addProjectToOrganizationCmd.Flags().String("branches", "", "branches")
addProjectToOrganizationCmd.Flags().String("pullrequests", "", "Which Pull Requests should be deployed")
addProjectToOrganizationCmd.Flags().StringP("organization", "O", "", "Organization to add the project to")
addProjectToOrganizationCmd.Flags().String("openshiftProjectPattern", "", "Pattern of OpenShift Project/Namespace that should be generated")

addProjectToOrganizationCmd.Flags().Uint("openshift", 0, "Reference to OpenShift Object this Project should be deployed to")
addProjectToOrganizationCmd.Flags().Uint("autoIdle", 0, "Auto idle setting of the project")
addProjectToOrganizationCmd.Flags().Uint("storageCalc", 0, "Should storage for this environment be calculated")
addProjectToOrganizationCmd.Flags().Uint("developmentEnvironmentsLimit", 0, "How many environments can be deployed at one time")

addProjectToOrganizationCmd.Flags().Bool("orgOwner", false, "Add the user as an owner of the project")
}
11 changes: 9 additions & 2 deletions cmd/shared.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cmd

import (
"fmt"
"github.com/uselagoon/lagoon-cli/pkg/output"
"os"
"strings"

"github.com/uselagoon/lagoon-cli/pkg/output"
)

// config vars
Expand Down Expand Up @@ -105,3 +105,10 @@ func nullUintCheck(i uint) *uint {
}
return &i
}

func requiredInputCheck(field string, value string) {
if value == "" || value == "0" {
fmt.Println(fmt.Sprintf("Missing argument: %s is not defined", field))
os.Exit(1)
}
}

0 comments on commit bb8acfe

Please sign in to comment.