From 4d1ef888295d4f6ce644c67bafacbf8b09d2c45b Mon Sep 17 00:00:00 2001 From: Muvaffak Onus Date: Thu, 18 Nov 2021 01:31:14 +0300 Subject: [PATCH] config.externalname: add context to GetIDFn so that if a query needs to be made, it can be done. For example, account ID in AWS to construct the full ARN Signed-off-by: Muvaffak Onus --- pkg/config/resource.go | 6 ++++-- pkg/terraform/files.go | 4 ++-- pkg/terraform/files_test.go | 5 +++-- pkg/terraform/store.go | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/config/resource.go b/pkg/config/resource.go index b7b64d83..5671ebda 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -17,6 +17,8 @@ limitations under the License. package config import ( + "context" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/pkg/errors" ) @@ -31,10 +33,10 @@ var NopSetIdentifierArgument SetIdentifierArgumentsFn = func(_ map[string]interf // GetIDFn returns the ID to be used in TF State file, i.e. "id" field in // terraform.tfstate. -type GetIDFn func(externalName string, parameters map[string]interface{}, providerConfig map[string]interface{}) (string, error) +type GetIDFn func(ctx context.Context, externalName string, parameters map[string]interface{}, providerConfig map[string]interface{}) (string, error) // ExternalNameAsID returns the name to be used as ID in TF State file. -var ExternalNameAsID GetIDFn = func(externalName string, _ map[string]interface{}, _ map[string]interface{}) (string, error) { +var ExternalNameAsID GetIDFn = func(_ context.Context, externalName string, _ map[string]interface{}, _ map[string]interface{}) (string, error) { return externalName, nil } diff --git a/pkg/terraform/files.go b/pkg/terraform/files.go index a895f7c4..adc020b3 100644 --- a/pkg/terraform/files.go +++ b/pkg/terraform/files.go @@ -91,7 +91,7 @@ type FileProducer struct { // WriteTFState writes the Terraform state that should exist in the filesystem to // start any Terraform operation. -func (fp *FileProducer) WriteTFState() error { +func (fp *FileProducer) WriteTFState(ctx context.Context) error { base := make(map[string]interface{}) // NOTE(muvaf): Since we try to produce the current state, observation // takes precedence over parameters. @@ -101,7 +101,7 @@ func (fp *FileProducer) WriteTFState() error { for k, v := range fp.observation { base[k] = v } - id, err := fp.Config.ExternalName.GetIDFn(meta.GetExternalName(fp.Resource), fp.parameters, fp.Setup.Configuration) + id, err := fp.Config.ExternalName.GetIDFn(ctx, meta.GetExternalName(fp.Resource), fp.parameters, fp.Setup.Configuration) if err != nil { return errors.Wrap(err, "cannot get id") } diff --git a/pkg/terraform/files_test.go b/pkg/terraform/files_test.go index e42c7a3a..021bfbdd 100644 --- a/pkg/terraform/files_test.go +++ b/pkg/terraform/files_test.go @@ -77,11 +77,12 @@ func TestWriteTFState(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { fs := afero.NewMemMapFs() - fp, err := NewFileProducer(context.TODO(), nil, dir, tc.args.tr, tc.args.s, config.DefaultResource("terrajet_resource", nil), WithFileSystem(fs)) + ctx := context.TODO() + fp, err := NewFileProducer(ctx, nil, dir, tc.args.tr, tc.args.s, config.DefaultResource("terrajet_resource", nil), WithFileSystem(fs)) if err != nil { t.Errorf("cannot initialize a file producer: %s", err.Error()) } - err = fp.WriteTFState() + err = fp.WriteTFState(ctx) if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { t.Errorf("\n%s\nWriteTFState(...): -want error, +got error:\n%s", tc.reason, diff) } diff --git a/pkg/terraform/store.go b/pkg/terraform/store.go index 95d1195e..0a1c9deb 100644 --- a/pkg/terraform/store.go +++ b/pkg/terraform/store.go @@ -110,7 +110,7 @@ func (ws *WorkspaceStore) Workspace(ctx context.Context, c resource.SecretClient return nil, errors.Wrap(err, "cannot stat terraform.tfstate file") } if os.IsNotExist(err) { - if err := fp.WriteTFState(); err != nil { + if err := fp.WriteTFState(ctx); err != nil { return nil, errors.Wrap(err, "cannot reproduce tfstate file") } }