Skip to content

Commit

Permalink
Feature: Include update environment functionality (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 authored Jul 14, 2023
1 parent c5478e4 commit c712b57
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 45 deletions.
168 changes: 149 additions & 19 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,28 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"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"
)

// @TODO re-enable this at some point if more environment based commands are made availab;e
// EnvironmentFlags .
// type EnvironmentFlags struct {
// Name string `json:"name,omitempty"`
// }

// func parseEnvironmentFlags(flags pflag.FlagSet) EnvironmentFlags {
// configMap := make(map[string]interface{})
// flags.VisitAll(func(f *pflag.Flag) {
// if flags.Changed(f.Name) {
// configMap[f.Name] = f.Value
// }
// })
// jsonStr, _ := json.Marshal(configMap)
// parsedFlags := EnvironmentFlags{}
// json.Unmarshal(jsonStr, &parsedFlags)
// return parsedFlags
// }
// @TODO re-enable this at some point if more environment based commands are made available
var environmentAutoIdle uint
var environmentAutoIdleProvided bool

var deleteEnvCmd = &cobra.Command{
Use: "environment",
Aliases: []string{"e"},
Short: "Delete an environment",
Run: func(cmd *cobra.Command, args []string) {
// environmentFlags := parseEnvironmentFlags(*cmd.Flags()) //@TODO re-enable this at some point if more environment based commands are made availab;e
// environmentFlags := parseEnvironmentFlags(*cmd.Flags()) //@TODO re-enable this at some point if more environment based commands are made available
if cmdProjectName == "" || cmdProjectEnvironment == "" {
fmt.Println("Missing arguments: Project name or environment name is not defined")
cmd.Help()
Expand All @@ -52,6 +42,136 @@ var deleteEnvCmd = &cobra.Command{
},
}

var updateEnvironmentCmd = &cobra.Command{
Use: "environment",
Aliases: []string{"e"},
Short: "Update an environment",
PreRunE: func(_ *cobra.Command, _ []string) error {
return validateTokenE(lagoonCLIConfig.Current)
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
deployBaseRef, err := cmd.Flags().GetString("deploy-base-ref")
if err != nil {
return err
}
deployHeadRef, err := cmd.Flags().GetString("deploy-head-ref")
if err != nil {
return err
}
namespace, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
}
route, err := cmd.Flags().GetString("route")
if err != nil {
return err
}
routes, err := cmd.Flags().GetString("routes")
if err != nil {
return err
}
environmentType, err := cmd.Flags().GetString("environment-type")
if err != nil {
return err
}
deployT, err := cmd.Flags().GetString("deploy-type")
if err != nil {
return err
}
openShift, err := cmd.Flags().GetUint("deploy-target")
if err != nil {
return err
}
deployTitle, err := cmd.Flags().GetString("deploy-title")
if err != nil {
return err
}

cmd.Flags().Visit(checkFlags)

if cmdProjectName == "" || cmdProjectEnvironment == "" {
fmt.Println("Missing arguments: Project name or environment name is not defined")
cmd.Help()
os.Exit(1)
}

current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
project, err := l.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if project.Name == "" {
err = fmt.Errorf("project not found")
}
handleError(err)
environment, err := l.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if environment.Name == "" {
err = fmt.Errorf("environment not found")
}
handleError(err)

environmentFlags := s.UpdateEnvironmentPatchInput{
DeployBaseRef: nullStrCheck(deployBaseRef),
DeployHeadRef: nullStrCheck(deployHeadRef),
OpenshiftProjectName: nullStrCheck(namespace),
Route: nullStrCheck(route),
Routes: nullStrCheck(routes),
DeployTitle: nullStrCheck(deployTitle),
Openshift: nullIntCheck(openShift),
}
if environmentAutoIdleProvided {
environmentFlags.AutoIdle = &environmentAutoIdle
}
if environmentType != "" {
envType := s.EnvType(strings.ToUpper(environmentType))
environmentFlags.EnvironmentType = &envType
}
if deployT != "" {
deployType := s.DeployType(strings.ToUpper(deployT))
environmentFlags.DeployType = &deployType
}

result, err := l.UpdateEnvironment(context.TODO(), environment.ID, environmentFlags, lc)
handleError(err)

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

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
}
}

var listBackupsCmd = &cobra.Command{
Use: "backups",
Aliases: []string{"b"},
Expand Down Expand Up @@ -169,4 +289,14 @@ This returns a direct URL to the backup, this is a signed download link with a l
func init() {
getCmd.AddCommand(getBackupCmd)
getBackupCmd.Flags().StringP("backup-id", "B", "", "The backup ID you want to restore")
updateEnvironmentCmd.Flags().String("deploy-base-ref", "", "Updates the deploy base ref for the selected environment")
updateEnvironmentCmd.Flags().String("deploy-head-ref", "", "Updates the deploy head ref for the selected environment")
updateEnvironmentCmd.Flags().String("deploy-title", "", "Updates the deploy title for the selected environment")
updateEnvironmentCmd.Flags().String("namespace", "", "Update the namespace for the selected environment")
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().String("environment-type", "", "Update the environment type - production | development")
updateEnvironmentCmd.Flags().String("deploy-type", "", "Update the deploy type - branch | pullrequest | promote")
}
1 change: 1 addition & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var updateNotificationCmd = &cobra.Command{

func init() {
updateCmd.AddCommand(updateProjectCmd)
updateCmd.AddCommand(updateEnvironmentCmd)
updateCmd.AddCommand(updateNotificationCmd)
updateCmd.AddCommand(updateUserCmd)
updateCmd.AddCommand(updateDeployTargetConfigCmd)
Expand Down
1 change: 1 addition & 0 deletions docs/commands/lagoon_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Update a resource
* [lagoon](lagoon.md) - Command line integration for Lagoon
* [lagoon update deploytarget](lagoon_update_deploytarget.md) - Update a DeployTarget in lagoon
* [lagoon update deploytarget-config](lagoon_update_deploytarget-config.md) - Update a deploytarget config
* [lagoon update environment](lagoon_update_environment.md) - Update an environment
* [lagoon update notification](lagoon_update_notification.md) - List all notifications or notifications on projects
* [lagoon update project](lagoon_update_project.md) - Update a project
* [lagoon update project-metadata](lagoon_update_project-metadata.md) - Update a projects metadata with a given key or key:value
Expand Down
49 changes: 49 additions & 0 deletions docs/commands/lagoon_update_environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## lagoon update environment

Update an environment

### Synopsis

Update an environment

```
lagoon update environment [flags]
```

### Options

```
-a, --auto-idle uint Auto idle setting of the environment (default 1)
--deploy-base-ref string Updates the deploy base ref for the selected environment
--deploy-head-ref string Updates the deploy head ref for the selected environment
-d, --deploy-target uint Reference to OpenShift Object this Environment should be deployed to
--deploy-title string Updates the deploy title for the selected environment
--deploy-type string Update the deploy type - branch | pullrequest | promote
--environment-type string Update the environment type - production | development
-h, --help help for environment
--namespace string Update the namespace for the selected environment
--route string Update the route for the selected environment
--routes string Update the routes for the selected environment
```

### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
-l, --lagoon string The Lagoon instance to interact with
--no-header No header on table (if supported)
--output-csv Output as CSV (if supported)
--output-json Output as JSON (if supported)
--pretty Make JSON pretty (if supported)
-p, --project string Specify a project to use
--skip-update-check Skip checking for updates
-i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication
```

### SEE ALSO

* [lagoon update](lagoon_update.md) - Update a resource

11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/golang/mock v1.6.0
github.com/google/go-github v0.0.0-20180716180158-c0b63e2f9bb1
github.com/google/uuid v1.1.1
github.com/hashicorp/go-version v1.2.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-version v1.6.0
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434
github.com/machinebox/graphql v0.2.3-0.20181106130121-3a9253180225
Expand All @@ -18,23 +18,22 @@ require (
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
gopkg.in/yaml.v2 v2.2.8
sigs.k8s.io/yaml v1.2.0
)

require (
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/guregu/null v4.0.0+incompatible
github.com/matryer/is v1.2.0 // indirect
// workaround for https://github.com/manifoldco/promptui/issues/98
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/uselagoon/machinery v0.0.8
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
rsc.io/quote/v3 v3.1.0 // indirect
)

// use this version for fixes to formatting of end header
Expand Down
Loading

0 comments on commit c712b57

Please sign in to comment.