Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tgc): Fixing the if block for existingConverterAsset and add test case #9914

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
15 changes: 7 additions & 8 deletions mmv1/third_party/tgc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func (c *Converter) addDelete(rc *tfjson.ResourceChange) error {
}

for _, converted := range convertedItems {

key := converted.Type + converted.Name
var existingConverterAsset *resources.Asset
if existing, exists := c.assets[key]; exists {
Expand All @@ -190,14 +189,14 @@ func (c *Converter) addDelete(rc *tfjson.ResourceChange) error {
} else {
existingConverterAsset = &asset
}
if existingConverterAsset != nil {
converted = converter.MergeDelete(*existingConverterAsset, converted)
augmented, err := c.augmentAsset(rd, c.cfg, converted)
if err != nil {
return err
}
c.assets[key] = augmented
}
if existingConverterAsset != nil {
converted = converter.MergeDelete(*existingConverterAsset, converted)
augmented, err := c.augmentAsset(rd, c.cfg, converted)
if err != nil {
return err
}
c.assets[key] = augmented
}
}
}
Expand Down
151 changes: 141 additions & 10 deletions mmv1/third_party/tgc/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/caiasset"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/ancestrymanager"
resources "github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/services/resourcemanager"
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/tfdata"
"github.com/google/go-cmp/cmp"
tfjson "github.com/hashicorp/terraform-json"
provider "github.com/hashicorp/terraform-provider-google-beta/google-beta/provider"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
Expand Down Expand Up @@ -635,18 +638,146 @@ func TestConvertWrapper(t *testing.T) {
},
}
for _, test := range tests {
_, err := convertWrapper(test.converter, test.rd, test.cfg)
if !test.wantErr {
t.Run(test.name, func(t *testing.T) {
_, err := convertWrapper(test.converter, test.rd, test.cfg)
if !test.wantErr {
if err != nil {
t.Errorf("convertWrapper() = %q, want = nil", err)
}
} else {
if err == nil {
t.Errorf("convertWrapper() = nil, want = %v", test.wantErrMsg)
} else if !strings.Contains(err.Error(), test.wantErrMsg) {
t.Errorf("convertWrapper() = %q, want containing %q", err, test.wantErrMsg)
}
}
})
}
}

func TestConvertMergingWithExistAsset(t *testing.T) {
tests := []struct {
name string
changes []*tfjson.ResourceChange
want []caiasset.Asset
}{
{
name: "CreateOrUpdateOrNoop",
changes: []*tfjson.ResourceChange{
{
Address: "google_project_iam_binding.test",
Mode: "managed",
Type: "google_project_iam_binding",
Name: "test",
ProviderName: "registry.terraform.io/hashicorp/google-beta",
Change: &tfjson.Change{
Actions: tfjson.Actions{"create"},
Before: nil,
After: map[string]interface{}{
"members": []interface{}{
"user:[email protected]",
},
"project": "example-project",
"role": "roles/editor",
},
},
},
},
want: []caiasset.Asset{
{
Name: "//cloudresourcemanager.googleapis.com/projects/example-project",
Type: "cloudresourcemanager.googleapis.com/Project",
IAMPolicy: &caiasset.IAMPolicy{
Bindings: []caiasset.IAMBinding{
{
Role: "roles/editor",
Members: []string{"user:[email protected]"},
},
},
},
},
},
},
{
name: "Delete",
changes: []*tfjson.ResourceChange{
{
Address: "google_project_iam_binding.test",
Mode: "managed",
Type: "google_project_iam_binding",
Name: "test",
ProviderName: "registry.terraform.io/hashicorp/google-beta",
Change: &tfjson.Change{
Actions: tfjson.Actions{"delete"},
After: nil,
Before: map[string]interface{}{
"members": []interface{}{
"user:[email protected]",
},
"project": "example-project",
"role": "roles/editor",
},
},
},
},
want: []caiasset.Asset{
{
Name: "//cloudresourcemanager.googleapis.com/projects/example-project",
Type: "cloudresourcemanager.googleapis.com/Project",
IAMPolicy: &caiasset.IAMPolicy{},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
converter, _, err := newTestConverter(false)
assert.Nil(t, err)

fetchFuncCalled := 0
fetchFunc := func(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (cai.Asset, error) {
fetchFuncCalled++
return resourcemanager.FetchProjectIamPolicy(d, config)
}

converter.converters["google_project_iam_binding"] = []cai.ResourceConverter{
{
AssetType: "cloudresourcemanager.googleapis.com/Project",
Convert: resourcemanager.GetProjectIamBindingCaiObject,
FetchFullResource: fetchFunc,
MergeCreateUpdate: resourcemanager.MergeProjectIamBinding,
MergeDelete: resourcemanager.MergeProjectIamBindingDelete,
},
}

// pre-populate the converter's asset cache
asset, err := converter.augmentAsset(nil, converter.cfg, cai.Asset{
Name: "//cloudresourcemanager.googleapis.com/projects/example-project",
Type: "cloudresourcemanager.googleapis.com/Project",
IAMPolicy: &cai.IAMPolicy{
Bindings: []cai.IAMBinding{
{
Role: "roles/editor",
Members: []string{"user:[email protected]"},
},
},
},
})
if err != nil {
t.Errorf("convertWrapper() = %q, want = nil", err)
t.Fatal(err)
}
} else {
if err == nil {
t.Errorf("convertWrapper() = nil, want = %v", test.wantErrMsg)
} else if !strings.Contains(err.Error(), test.wantErrMsg) {
t.Errorf("convertWrapper() = %q, want containing %q", err, test.wantErrMsg)
converter.assets = map[string]Asset{
"cloudresourcemanager.googleapis.com/Project//cloudresourcemanager.googleapis.com/projects/example-project": asset,
}
}
}

err = converter.AddResourceChanges(test.changes)
if err != nil {
t.Fatalf("AddResourceChanges() = %s, want = nil", err)
}
if diff := cmp.Diff(test.want, converter.Assets()); diff != "" {
t.Errorf("AddResourceChanges() returned unexpected diff (-want +got):\n%s", diff)
}
assert.Equal(t, 0, fetchFuncCalled)
})
}
}
Loading