Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
config.externalname: add context to GetIDFn so that if a query needs …
Browse files Browse the repository at this point in the history
…to be made, it can be done. For example, account ID in AWS to construct the full ARN

Signed-off-by: Muvaffak Onus <me@muvaf.com>
  • Loading branch information
muvaf committed Nov 17, 2021
1 parent 36c9793 commit 4d1ef88
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
6 changes: 4 additions & 2 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/terraform/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/terraform/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/terraform/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down

0 comments on commit 4d1ef88

Please sign in to comment.