github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/common/environments.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/kubeshop/testkube/pkg/ui"
     7  
     8  	cloudclient "github.com/kubeshop/testkube/pkg/cloud/client"
     9  )
    10  
    11  type Environment struct {
    12  	Id   string
    13  	Name string
    14  }
    15  
    16  func GetEnvironments(url, token, orgID string) ([]cloudclient.Environment, error) {
    17  	c := cloudclient.NewEnvironmentsClient(url, token, orgID)
    18  	return c.List()
    19  }
    20  
    21  func GetEnvNames(envs []cloudclient.Environment) []string {
    22  	var names []string
    23  	for _, env := range envs {
    24  		names = append(names, env.Name)
    25  	}
    26  	return names
    27  }
    28  
    29  func FindEnvID(envs []cloudclient.Environment, name string) string {
    30  	for _, env := range envs {
    31  		if env.Name == name {
    32  			return env.Id
    33  		}
    34  	}
    35  	return ""
    36  }
    37  
    38  func UiGetEnvironmentID(url, token, orgID string) (string, string, error) {
    39  	// Choose organization from orgs available
    40  	envs, err := GetEnvironments(url, token, orgID)
    41  	if err != nil {
    42  		return "", "", fmt.Errorf("failed to get environments: %s", err.Error())
    43  	}
    44  
    45  	if len(envs) == 0 {
    46  		return "", "", fmt.Errorf("no environments available, please create one first")
    47  	}
    48  
    49  	envNames := GetEnvNames(envs)
    50  	envName := ui.Select("Choose organization", envNames)
    51  	envID := FindEnvID(envs, envName)
    52  
    53  	return envID, envName, nil
    54  }