Skip to content

Commit 6c2bfb6

Browse files
Merge pull request #179 from overmindtech/mapping-problems
Fixed mapping issues for resources in modules
2 parents d1d71bf + 1e4a191 commit 6c2bfb6

File tree

3 files changed

+442
-3
lines changed

3 files changed

+442
-3
lines changed

cmd/changes_submit_plan.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,13 @@ func mappedItemDiffsFromPlan(ctx context.Context, fileName string, lf log.Fields
409409
WithError(err).
410410
Error("Failed to parse overmind_mappings output")
411411
} else {
412-
currentProviderMappings, ok := mappings[configResource.ProviderConfigKey]
412+
// We need to split out the module section of the name
413+
// here. If the resource isn't in a module, the
414+
// ProviderConfigKey will be something like
415+
// "kubernetes", however if it's in a module it's be
416+
// something like "module.something:kubernetes"
417+
providerName := extractProviderNameFromConfigKey(configResource.ProviderConfigKey)
418+
currentProviderMappings, ok := mappings[providerName]
413419

414420
if ok {
415421
log.WithContext(ctx).
@@ -509,6 +515,16 @@ func mappedItemDiffsFromPlan(ctx context.Context, fileName string, lf log.Fields
509515
return plannedChangeGroupsVar.MappedItemDiffs(), nil
510516
}
511517

518+
// Returns the name of the provider from the config key. If the resource isn't
519+
// in a module, the ProviderConfigKey will be something like "kubernetes",
520+
// however if it's in a module it's be something like
521+
// "module.something:kubernetes". In both scenarios we want to return
522+
// "kubernetes"
523+
func extractProviderNameFromConfigKey(providerConfigKey string) string {
524+
sections := strings.Split(providerConfigKey, ":")
525+
return sections[len(sections)-1]
526+
}
527+
512528
func changeTitle(arg string) string {
513529
if arg != "" {
514530
// easy, return the user's choice

cmd/submitplan_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
2424
t.Error(err)
2525
}
2626

27-
if len(mappedItemDiffs) != 3 {
28-
t.Errorf("Expected 3 changes, got %v:", len(mappedItemDiffs))
27+
if len(mappedItemDiffs) != 4 {
28+
t.Errorf("Expected 4 changes, got %v:", len(mappedItemDiffs))
2929
for _, diff := range mappedItemDiffs {
3030
t.Errorf(" %v", diff)
3131
}
@@ -34,6 +34,7 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
3434
var nats_box_deployment *sdp.MappedItemDiff
3535
var api_server_deployment *sdp.MappedItemDiff
3636
var aws_iam_policy *sdp.MappedItemDiff
37+
var secret *sdp.MappedItemDiff
3738

3839
for _, diff := range mappedItemDiffs {
3940
item := diff.GetItem().GetBefore()
@@ -52,6 +53,8 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
5253
api_server_deployment = diff
5354
} else if item.GetType() == "iam-policy" {
5455
aws_iam_policy = diff
56+
} else if item.GetType() == "Secret" {
57+
secret = diff
5558
}
5659
}
5760

@@ -144,6 +147,15 @@ func TestMappedItemDiffsFromPlan(t *testing.T) {
144147
if aws_iam_policy.GetMappingQuery().GetQuery() != "arn:aws:iam::123456789012:policy/test-alb-ingress" {
145148
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())
146149
}
150+
151+
// check secret
152+
t.Logf("secret: %v", secret)
153+
if secret == nil {
154+
t.Fatalf("Expected secret to be set, but it's not")
155+
}
156+
if secret.GetMappingQuery().GetScope() != "dogfood.default" {
157+
t.Errorf("Expected secret query scope to be 'dogfood.default', got '%v'", secret.GetMappingQuery().GetScope())
158+
}
147159
}
148160

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

250262
})
251263
}
264+
265+
func TestExtractProviderNameFromConfigKey(t *testing.T) {
266+
tests := []struct {
267+
ConfigKey string
268+
Expected string
269+
}{
270+
{
271+
ConfigKey: "kubernetes",
272+
Expected: "kubernetes",
273+
},
274+
{
275+
ConfigKey: "module.core:kubernetes",
276+
Expected: "kubernetes",
277+
},
278+
}
279+
280+
for _, test := range tests {
281+
t.Run(test.ConfigKey, func(t *testing.T) {
282+
actual := extractProviderNameFromConfigKey(test.ConfigKey)
283+
if actual != test.Expected {
284+
t.Errorf("Expected %v, got %v", test.Expected, actual)
285+
}
286+
})
287+
}
288+
}

0 commit comments

Comments
 (0)