Skip to content

Commit

Permalink
#27 Remove if flag and set activecloud everytime
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Getz authored and victorgetz committed May 5, 2023
1 parent 6385ac7 commit 7b10a1b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 49 deletions.
77 changes: 28 additions & 49 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,36 @@ import (
var otcConfigPath = path.Join(GetHomeFolder(), ".otc-auth-config")

func LoadCloudConfig(domainName string) {
if !OtcConfigFileExists() {
RegisterCloudConfig(domainName)
}

otcConfig := getOtcConfigContent()
if !otcConfig.Clouds.ContainsCloud(domainName) {
RegisterCloudConfig(domainName)
}

otcConfig = getOtcConfigContent()
if otcConfig.Clouds.GetActiveCloud().Domain.Name != domainName {
clouds := otcConfig.Clouds
clouds.SetActiveByName(domainName)
otcConfig.Clouds = clouds
writeOtcConfigContentToFile(otcConfig)
otcConfig := getOtcConfig()
clouds := otcConfig.Clouds
if !clouds.ContainsCloud(domainName) {
clouds = registerNewCloud(domainName)
}
clouds.SetActiveByName(domainName)
otcConfig.Clouds = clouds
writeOtcConfigContentToFile(otcConfig)

_, err := fmt.Fprintf(os.Stdout, "Cloud %s loaded successfully and set to active.\n", domainName)
if err != nil {
common.OutputErrorToConsoleAndExit(err)
}
}

func RegisterCloudConfig(domainName string) {
cloud := Cloud{
func registerNewCloud(domainName string) Clouds {
otcConfig := getOtcConfig()
clouds := otcConfig.Clouds

newCloud := Cloud{
Domain: NameAndIdResource{
Name: domainName,
},
Active: true,
}

if !OtcConfigFileExists() {
createConfigFileWithCloudConfig(OtcConfigContent{Clouds: Clouds{cloud}})
println("info: cloud config created.")
return
if otcConfig.Clouds.ContainsCloud(newCloud.Domain.Name) {
common.OutputErrorMessageToConsoleAndExit(fmt.Sprintf("warning: cloud with name %s already exists.\n\nUse the cloud-config load command.", newCloud.Domain.Name))
return nil
}

appendCloudConfig(cloud)
return append(clouds, newCloud)
}

func IsAuthenticationValid() bool {
Expand All @@ -74,11 +66,7 @@ func IsAuthenticationValid() bool {
}

func RemoveCloudConfig(domainName string) {
if !OtcConfigFileExists() {
common.OutputErrorMessageToConsoleAndExit("fatal: config file does not exist.\n\nPlease try logging in and try again")
}

otcConfig := getOtcConfigContent()
otcConfig := getOtcConfig()
if !otcConfig.Clouds.ContainsCloud(domainName) {
common.OutputErrorMessageToConsoleAndExit(fmt.Sprintf("fatal: cloud with name %s does not exist in the config file.", domainName))
}
Expand All @@ -92,29 +80,29 @@ func RemoveCloudConfig(domainName string) {
}

func UpdateClusters(clusters Clusters) {
otcConfig := getOtcConfigContent()
otcConfig := getOtcConfig()
cloudIndex := otcConfig.Clouds.GetActiveCloudIndex()
otcConfig.Clouds[cloudIndex].Clusters = clusters
writeOtcConfigContentToFile(otcConfig)
}

func UpdateProjects(projects Projects) {
otcConfig := getOtcConfigContent()
otcConfig := getOtcConfig()
cloudIndex := otcConfig.Clouds.GetActiveCloudIndex()
otcConfig.Clouds[cloudIndex].Projects = projects
writeOtcConfigContentToFile(otcConfig)
}

func UpdateCloudConfig(updatedCloud Cloud) {
otcConfig := getOtcConfigContent()
otcConfig := getOtcConfig()
index := otcConfig.Clouds.GetActiveCloudIndex()
otcConfig.Clouds[index] = updatedCloud

writeOtcConfigContentToFile(otcConfig)
}

func GetActiveCloudConfig() Cloud {
otcConfig := getOtcConfigContent()
otcConfig := getOtcConfig()
clouds := otcConfig.Clouds
cloud, _, err := clouds.FindActiveCloudConfigOrNil()
if err != nil {
Expand All @@ -132,7 +120,12 @@ func OtcConfigFileExists() bool {
return !fileInfo.IsDir()
}

func getOtcConfigContent() OtcConfigContent {
func getOtcConfig() OtcConfigContent {
if !OtcConfigFileExists() {
createConfigFileWithCloudConfig(OtcConfigContent{})
println("info: cloud config created.")
}

var otcConfig OtcConfigContent
content := readFileContent()

Expand Down Expand Up @@ -206,22 +199,8 @@ func WriteConfigFile(content string, configPath string) {
}
}

func appendCloudConfig(cloud Cloud) {
otcConfig := getOtcConfigContent()
clouds := otcConfig.Clouds

if otcConfig.Clouds.ContainsCloud(cloud.Domain.Name) {
common.OutputErrorMessageToConsoleAndExit(fmt.Sprintf("warning: cloud with name %s already exists.\n\nUse the cloud-config load command.", cloud.Domain.Name))
return
}

clouds = append(clouds, cloud)
otcConfig.Clouds = clouds
writeOtcConfigContentToFile(otcConfig)
}

func removeCloudConfig(name string) {
otcConfig := getOtcConfigContent()
otcConfig := getOtcConfig()

otcConfig.Clouds.RemoveCloudByNameIfExists(name)
writeOtcConfigContentToFile(otcConfig)
Expand Down
43 changes: 43 additions & 0 deletions test/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package test

import (
"otc-auth/config"
"testing"
)

func TestLoadCloudConfig_init(t *testing.T) {
domainName := "first"

config.LoadCloudConfig(domainName)

result := config.GetActiveCloudConfig().Domain
if result.Name != domainName {
t.Errorf("Expected result to contain cloud: %s, but result contains: %s ", domainName, result.Name)
}

}

func TestLoadCloudConfig_two_domains(t *testing.T) {
firstDomain := "first"
secondDomain := "second"

config.LoadCloudConfig(firstDomain)
config.LoadCloudConfig(secondDomain)

result := config.GetActiveCloudConfig().Domain
if result.Name != secondDomain {
t.Errorf("Expected result to contain cloud: %s, but result contains: %s ", secondDomain, result.Name)
}
}

func TestLoadCloudConfig_make_domain_twice_active(t *testing.T) {
firstDomain := "first"

config.LoadCloudConfig(firstDomain)
config.LoadCloudConfig(firstDomain)

result := config.GetActiveCloudConfig().Domain
if result.Name != firstDomain {
t.Errorf("Expected result to contain cloud: %s, but result contains: %s ", firstDomain, result.Name)
}
}

0 comments on commit 7b10a1b

Please sign in to comment.