Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ func (cfg *Config) DeleteContext(name string) bool {
return false
}

// DeleteTenant removes a tenant by name.
func (cfg *Config) DeleteTenant(name string) bool {
for index, tenant := range cfg.Tenants {
if tenant.Name == name {
cfg.Tenants = append(cfg.Tenants[:index], cfg.Tenants[index+1:]...)
return true
}
}

return false
}

// DeleteCredential removes a credential by name.
func (cfg *Config) DeleteCredential(name string) bool {
for index, credential := range cfg.Credentials {
if credential.Name == name {
cfg.Credentials = append(cfg.Credentials[:index], cfg.Credentials[index+1:]...)
return true
}
}

return false
}

// RenameContext renames an existing context.
func (cfg *Config) RenameContext(oldName, newName string) bool {
for index, context := range cfg.Contexts {
Expand All @@ -131,6 +155,86 @@ func (cfg *Config) RenameContext(oldName, newName string) bool {
return false
}

// RenameTenant renames an existing tenant. It does not update contexts that
// reference the tenant; use RetargetTenant for that.
func (cfg *Config) RenameTenant(oldName, newName string) bool {
for index, tenant := range cfg.Tenants {
if tenant.Name == oldName {
cfg.Tenants[index].Name = newName
return true
}
}

return false
}

// RenameCredential renames an existing credential. It does not update contexts
// that reference the credential; use RetargetCredential for that.
func (cfg *Config) RenameCredential(oldName, newName string) bool {
for index, credential := range cfg.Credentials {
if credential.Name == oldName {
cfg.Credentials[index].Name = newName
return true
}
}

return false
}

// RetargetTenant rewrites every context that references oldName to reference
// newName, returning the number of contexts updated.
func (cfg *Config) RetargetTenant(oldName, newName string) int {
updated := 0
for index := range cfg.Contexts {
if cfg.Contexts[index].Details.Tenant == oldName {
cfg.Contexts[index].Details.Tenant = newName
updated++
}
}

return updated
}

// RetargetCredential rewrites every context that references oldName to
// reference newName, returning the number of contexts updated.
func (cfg *Config) RetargetCredential(oldName, newName string) int {
updated := 0
for index := range cfg.Contexts {
if cfg.Contexts[index].Details.Credential == oldName {
cfg.Contexts[index].Details.Credential = newName
updated++
}
}

return updated
}

// ContextsReferencingTenant returns the names of contexts referencing the
// tenant, in declaration order.
func (cfg *Config) ContextsReferencingTenant(name string) []string {
var names []string
for _, context := range cfg.Contexts {
if context.Details.Tenant == name {
names = append(names, context.Name)
}
}

return names
}

// ContextsReferencingCredential returns the names of contexts referencing the
// credential, in declaration order.
func (cfg *Config) ContextsReferencingCredential(name string) []string {
var names []string
for _, context := range cfg.Contexts {
if context.Details.Credential == name {
names = append(names, context.Name)
}
}

return names
}

// Merge merges another config into this config.
func (cfg *Config) Merge(next *Config) error {
if cfg == nil || next == nil {
Expand Down
88 changes: 88 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ func TestUpsertsAndDeletes(t *testing.T) {
assert.False(t, cfg.DeleteContext(ctx2.Name))
}

func TestDeleteTenant(t *testing.T) {
cfg := newTestConfig(t).
withTenant("corp").
build()

assert.True(t, cfg.DeleteTenant("corp"))
assert.Empty(t, cfg.Tenants)
assert.False(t, cfg.DeleteTenant("corp"))
}

func TestDeleteCredential(t *testing.T) {
cfg := newTestConfig(t).
withUserCredential("me").
build()

assert.True(t, cfg.DeleteCredential("me"))
assert.Empty(t, cfg.Credentials)
assert.False(t, cfg.DeleteCredential("me"))
}

func TestRenameContext(t *testing.T) {
cfg := newTestConfig(t).
withContext("old", "corp", "ci").
Expand All @@ -86,6 +106,74 @@ func TestRenameContext(t *testing.T) {
assert.False(t, cfg.RenameContext("old", "other"))
}

func TestRenameTenant(t *testing.T) {
cfg := newTestConfig(t).
withTenant("corp").
build()

assert.True(t, cfg.RenameTenant("corp", "corporate"))
assert.Equal(t, "corporate", cfg.Tenants[0].Name)
assert.False(t, cfg.RenameTenant("corp", "other"))
}

func TestRenameCredential(t *testing.T) {
cfg := newTestConfig(t).
withUserCredential("ci").
build()

assert.True(t, cfg.RenameCredential("ci", "ci-sp"))
assert.Equal(t, "ci-sp", cfg.Credentials[0].Name)
assert.False(t, cfg.RenameCredential("ci", "other"))
}

func TestRetargetTenant(t *testing.T) {
cfg := newTestConfig(t).
withTenant("corp").
withUserCredential("ci").
withContext("dev", "corp", "ci").
withContext("prod", "corp", "ci").
withContext("other", "platform", "ci").
build()

assert.Equal(t, 2, cfg.RetargetTenant("corp", "corporate"))
assert.Equal(t, "corporate", cfg.Contexts[0].Details.Tenant)
assert.Equal(t, "corporate", cfg.Contexts[1].Details.Tenant)
assert.Equal(t, "platform", cfg.Contexts[2].Details.Tenant)
}

func TestRetargetCredential(t *testing.T) {
cfg := newTestConfig(t).
withUserCredential("ci").
withContext("dev", "corp", "ci").
withContext("prod", "corp", "other").
build()

assert.Equal(t, 1, cfg.RetargetCredential("ci", "ci-sp"))
assert.Equal(t, "ci-sp", cfg.Contexts[0].Details.Credential)
assert.Equal(t, "other", cfg.Contexts[1].Details.Credential)
}

func TestContextsReferencingTenant(t *testing.T) {
cfg := newTestConfig(t).
withContext("dev", "corp", "ci").
withContext("prod", "corp", "ci").
withContext("other", "platform", "ci").
build()

assert.Equal(t, []string{"dev", "prod"}, cfg.ContextsReferencingTenant("corp"))
assert.Nil(t, cfg.ContextsReferencingTenant("missing"))
}

func TestContextsReferencingCredential(t *testing.T) {
cfg := newTestConfig(t).
withContext("dev", "corp", "ci").
withContext("prod", "corp", "other").
build()

assert.Equal(t, []string{"dev"}, cfg.ContextsReferencingCredential("ci"))
assert.Nil(t, cfg.ContextsReferencingCredential("missing"))
}

func TestMerge(t *testing.T) {
base := newTestConfig(t).
withTenant("corp").
Expand Down
Loading
Loading