Skip to content

Commit

Permalink
Add set env var (#80)
Browse files Browse the repository at this point in the history
* Added a command set env var

* Added a command set env var, updated radix-api client

* Updated radix-api client
  • Loading branch information
satr authored Jan 2, 2024
1 parent 04b58c4 commit 7357b04
Show file tree
Hide file tree
Showing 108 changed files with 2,430 additions and 2,658 deletions.
33 changes: 12 additions & 21 deletions cmd/setEnvironmentSecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,13 @@ import (
"github.com/spf13/cobra"
)

const (
applicationOption = "application"
environmentOption = "environment"
componentOption = "component"
secretOption = "secret"
valueOption = "value"
awaitReconcileOption = "await-reconcile"
)

// setEnvironmentSecretCmd represents the setEnvironmentSecretCmd command
var setEnvironmentSecretCmd = &cobra.Command{
Use: "environment-secret",
Short: "Will set an environment secret",
Long: `Will set an environment secret`,
RunE: func(cmd *cobra.Command, args []string) error {
appName, err := getAppNameFromConfigOrFromParameter(cmd, applicationOption)
appName, err := getAppNameFromConfigOrFromParameter(cmd, "application")
if err != nil {
return err
}
Expand All @@ -50,8 +41,8 @@ var setEnvironmentSecretCmd = &cobra.Command{
return errors.New("application name is required")
}

secretName, _ := cmd.Flags().GetString(secretOption)
secretValue, _ := cmd.Flags().GetString(valueOption)
secretName, _ := cmd.Flags().GetString("secret")
secretValue, _ := cmd.Flags().GetString("value")

if secretName == "" {
return errors.New("secret is required")
Expand All @@ -61,18 +52,18 @@ var setEnvironmentSecretCmd = &cobra.Command{
return errors.New("value is required")
}

environmentName, _ := cmd.Flags().GetString(environmentOption)
environmentName, _ := cmd.Flags().GetString("environment")

if environmentName == "" {
return errors.New("`environment` is required")
}

component, _ := cmd.Flags().GetString(componentOption)
component, _ := cmd.Flags().GetString("component")
if component == "" {
return errors.New("`component` is required")
}

awaitReconcile, _ := cmd.Flags().GetBool(awaitReconcileOption)
awaitReconcile, _ := cmd.Flags().GetBool("await-reconcile")

cmd.SilenceUsage = true

Expand Down Expand Up @@ -138,11 +129,11 @@ func isComponentSecretReconciled(apiClient *apiclient.Radixapi, appName, environ

func init() {
setCmd.AddCommand(setEnvironmentSecretCmd)
setEnvironmentSecretCmd.Flags().StringP(applicationOption, "a", "", "Name of the application to set secret for")
setEnvironmentSecretCmd.Flags().StringP(environmentOption, "e", "", "Environment to set secret in")
setEnvironmentSecretCmd.Flags().String(componentOption, "", "Component to set the secret for")
setEnvironmentSecretCmd.Flags().StringP(secretOption, "s", "", "Name of the secret to set")
setEnvironmentSecretCmd.Flags().StringP(valueOption, "v", "", "Value of the secret to set")
setEnvironmentSecretCmd.Flags().Bool(awaitReconcileOption, true, "Await reconciliation in Radix. Default is true")
setEnvironmentSecretCmd.Flags().StringP("application", "a", "", "Name of the application to set secret for")
setEnvironmentSecretCmd.Flags().StringP("environment", "e", "", "Environment to set secret in")
setEnvironmentSecretCmd.Flags().String("component", "", "Component to set the secret for")
setEnvironmentSecretCmd.Flags().StringP("secret", "s", "", "Name of the secret to set")
setEnvironmentSecretCmd.Flags().StringP("value", "v", "", "Value of the secret to set")
setEnvironmentSecretCmd.Flags().Bool("await-reconcile", true, "Await reconciliation in Radix. Default is true")
setContextSpecificPersistentFlags(setEnvironmentSecretCmd)
}
144 changes: 144 additions & 0 deletions cmd/setEnvironmentVariable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright © 2023
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"errors"
"fmt"
"os"

apiclient "github.com/equinor/radix-cli/generated-client/client"
"github.com/equinor/radix-cli/generated-client/client/component"
"github.com/equinor/radix-cli/generated-client/client/environment"
"github.com/equinor/radix-cli/generated-client/models"
"github.com/equinor/radix-cli/pkg/client"
"github.com/spf13/cobra"
)

// setEnvironmentVariableCmd represents the setEnvironmentVariableCmd command
var setEnvironmentVariableCmd = &cobra.Command{
Use: "environment-variable",
Short: "Will set an environment variable",
Long: `Will set an environment variable
Example:
rx set environment-variable --application your-application-name --environment test --component component-abc --variable LOG_LEVEL --value INFO
`,
RunE: func(cmd *cobra.Command, args []string) error {
appName, err := getAppNameFromConfigOrFromParameter(cmd, "application")
if err != nil {
return err
}

if appName == nil || *appName == "" {
return errors.New("application name is required")
}

variableName, _ := cmd.Flags().GetString("variable")
variableValue, _ := cmd.Flags().GetString("value")

if variableName == "" {
return errors.New("variable is required")
}

if variableValue == "" {
return errors.New("value is required")
}

environmentName, _ := cmd.Flags().GetString("environment")

if environmentName == "" {
return errors.New("`environment` is required")
}

componentName, _ := cmd.Flags().GetString("component")
if componentName == "" {
return errors.New("`component` is required")
}

awaitReconcile, _ := cmd.Flags().GetBool("await-reconcile")

cmd.SilenceUsage = true

apiClient, err := client.GetForCommand(cmd)
if err != nil {
return err
}

if awaitReconcile {
reconciledOk := awaitReconciliation(func() bool {
return isComponentVariableReconciled(apiClient, *appName, environmentName, componentName, variableName)
})

if !reconciledOk {
return fmt.Errorf("component was not reconciled within time: either component %s does not exist in the environment %s or the component does not have a variable %s",
componentName, environmentName, variableName)
}
}

componentVariable := models.EnvVarParameter{}
componentVariable.Name = &variableName
componentVariable.Value = &variableValue

changeComponentVariableParameters := component.NewChangeEnvVarParams()
changeComponentVariableParameters.SetAppName(*appName)
changeComponentVariableParameters.SetEnvName(environmentName)
changeComponentVariableParameters.SetComponentName(componentName)
changeComponentVariableParameters.SetEnvVarParameter([]*models.EnvVarParameter{&componentVariable})

_, err = apiClient.Component.ChangeEnvVar(changeComponentVariableParameters, nil)
return err
},
}

func isComponentVariableReconciled(apiClient *apiclient.Radixapi, appName, environmentName, componentName, variableName string) bool {
getEnvironmentParameters := environment.NewGetEnvironmentParams()
getEnvironmentParameters.SetAppName(appName)
getEnvironmentParameters.SetEnvName(environmentName)

env, err := apiClient.Environment.GetEnvironment(getEnvironmentParameters, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return false
}

if env.Payload != nil &&
env.Payload.ActiveDeployment != nil &&
env.Payload.ActiveDeployment.Components != nil {
for _, component := range env.Payload.ActiveDeployment.Components {
if *component.Name == componentName {
for name := range component.Variables {
if name == variableName {
return true
}
}
}
}

}

return false
}

func init() {
setCmd.AddCommand(setEnvironmentVariableCmd)
setEnvironmentVariableCmd.Flags().StringP("application", "a", "", "Name of the application to set variable for")
setEnvironmentVariableCmd.Flags().StringP("environment", "e", "", "Environment to set variable in")
setEnvironmentVariableCmd.Flags().String("component", "", "Component to set the variable for")
setEnvironmentVariableCmd.Flags().StringP("variable", "", "", "Name of the variable to set")
setEnvironmentVariableCmd.Flags().StringP("value", "v", "", "Value of the variable to set")
setEnvironmentVariableCmd.Flags().Bool("await-reconcile", true, "Await reconciliation in Radix. Default is true")
setContextSpecificPersistentFlags(setEnvironmentVariableCmd)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7357b04

Please sign in to comment.