Skip to content

Commit

Permalink
Fixed mapping issues for resources in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanratcliffe committed Feb 15, 2024
1 parent f53b002 commit bedc4b9
Show file tree
Hide file tree
Showing 3 changed files with 442 additions and 3 deletions.
18 changes: 17 additions & 1 deletion cmd/changes_submit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,13 @@ func mappedItemDiffsFromPlan(ctx context.Context, fileName string, lf log.Fields
WithError(err).
Error("Failed to parse overmind_mappings output")
} else {
currentProviderMappings, ok := mappings[configResource.ProviderConfigKey]
// We need to split out the module section of the name
// here. If the resource isn't in a module, the
// ProviderConfigKey will be something like
// "kubernetes", however if it's in a module it's be
// something like "module.something:kubernetes"
providerName := extractProviderNameFromConfigKey(configResource.ProviderConfigKey)
currentProviderMappings, ok := mappings[providerName]

if ok {
log.WithContext(ctx).
Expand Down Expand Up @@ -509,6 +515,16 @@ func mappedItemDiffsFromPlan(ctx context.Context, fileName string, lf log.Fields
return plannedChangeGroupsVar.MappedItemDiffs(), nil
}

// Returns the name of the provider from the config key. If the resource isn't
// in a module, the ProviderConfigKey will be something like "kubernetes",
// however if it's in a module it's be something like
// "module.something:kubernetes". In both scenarios we want to return
// "kubernetes"
func extractProviderNameFromConfigKey(providerConfigKey string) string {
sections := strings.Split(providerConfigKey, ":")
return sections[len(sections)-1]
}

func changeTitle(arg string) string {
if arg != "" {
// easy, return the user's choice
Expand Down
41 changes: 39 additions & 2 deletions cmd/submitplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
t.Error(err)
}

if len(mappedItemDiffs) != 3 {
t.Errorf("Expected 3 changes, got %v:", len(mappedItemDiffs))
if len(mappedItemDiffs) != 4 {
t.Errorf("Expected 4 changes, got %v:", len(mappedItemDiffs))
for _, diff := range mappedItemDiffs {
t.Errorf(" %v", diff)
}
Expand All @@ -34,6 +34,7 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
var nats_box_deployment *sdp.MappedItemDiff
var api_server_deployment *sdp.MappedItemDiff
var aws_iam_policy *sdp.MappedItemDiff
var secret *sdp.MappedItemDiff

for _, diff := range mappedItemDiffs {
item := diff.GetItem().GetBefore()
Expand All @@ -52,6 +53,8 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
api_server_deployment = diff
} else if item.GetType() == "iam-policy" {
aws_iam_policy = diff
} else if item.GetType() == "Secret" {
secret = diff
}
}

Expand Down Expand Up @@ -144,6 +147,15 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
if aws_iam_policy.GetMappingQuery().GetQuery() != "arn:aws:iam::123456789012:policy/test-alb-ingress" {
t.Errorf("Expected aws_iam_policy query query to be 'arn:aws:iam::123456789012:policy/test-alb-ingress', got '%v'", aws_iam_policy.GetMappingQuery().GetQuery())
}

// check secret
t.Logf("secret: %v", secret)
if secret == nil {
t.Fatalf("Expected secret to be set, but it's not")
}
if secret.MappingQuery.GetScope() != "dogfood.default" {

Check failure on line 156 in cmd/submitplan_test.go

View workflow job for this annotation

GitHub Actions / lint

avoid direct access to proto field secret.MappingQuery.GetScope(), use secret.GetMappingQuery().GetScope() instead (protogetter)
t.Errorf("Expected secret query scope to be 'dogfood.default', got '%v'", secret.MappingQuery.GetScope())

Check failure on line 157 in cmd/submitplan_test.go

View workflow job for this annotation

GitHub Actions / lint

avoid direct access to proto field secret.MappingQuery.GetScope(), use secret.GetMappingQuery().GetScope() instead (protogetter)
}
}

// note that these tests need to allocate the input map for every test to avoid
Expand Down Expand Up @@ -249,3 +261,28 @@ func TestMaskSensitiveData(t *testing.T) {

})
}

func TestExtractProviderNameFromConfigKey(t *testing.T) {
tests := []struct {
ConfigKey string
Expected string
}{
{
ConfigKey: "kubernetes",
Expected: "kubernetes",
},
{
ConfigKey: "module.core:kubernetes",
Expected: "kubernetes",
},
}

for _, test := range tests {
t.Run(test.ConfigKey, func(t *testing.T) {
actual := extractProviderNameFromConfigKey(test.ConfigKey)
if actual != test.Expected {
t.Errorf("Expected %v, got %v", test.Expected, actual)
}
})
}
}
Loading

0 comments on commit bedc4b9

Please sign in to comment.