-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This addresses issue #144 by providing a `replace-provider` action. Note that, unlike other `state` actions, the `replace-provider` action does not support a `-state-out`: ``` terraform state replace-provider \ -state=terraform.tfstate \ -state-out=out.tfstate \ registry.terraform.io/hashicorp/local registy.mdb.io/hashicorp/local Error parsing command-line flags: flag provided but not defined: -state-out ``` Signed-off-by: Mike Ball <mikedball@gmail.com>
- Loading branch information
Showing
12 changed files
with
641 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Providers prints out a tree of modules in the referenced configuration annotated with | ||
// their provider requirements. | ||
func (c *terraformCLI) Providers(ctx context.Context) (string, error) { | ||
args := []string{"providers"} | ||
|
||
stdout, _, err := c.Run(ctx, args...) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return stdout, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
) | ||
|
||
var providersStdout = ` | ||
Providers required by configuration: | ||
. | ||
└── provider[registry.terraform.io/hashicorp/null] | ||
Providers required by state: | ||
provider[registry.terraform.io/hashicorp/null] | ||
` | ||
|
||
var legacyProvidersStdout = `. | ||
└── provider.null | ||
` | ||
|
||
func TestTerraformCLIProviders(t *testing.T) { | ||
cases := []struct { | ||
desc string | ||
mockCommands []*mockCommand | ||
addresses []string | ||
want string | ||
ok bool | ||
}{ | ||
{ | ||
desc: "basic invocation", | ||
mockCommands: []*mockCommand{ | ||
{ | ||
stdout: providersStdout, | ||
exitCode: 0, | ||
}, | ||
}, | ||
want: providersStdout, | ||
ok: true, | ||
}, | ||
{ | ||
desc: "failed to run terraform providers", | ||
mockCommands: []*mockCommand{ | ||
{ | ||
exitCode: 1, | ||
}, | ||
}, | ||
want: "", | ||
ok: false, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
tc.mockCommands[0].args = []string{"terraform", "providers"} | ||
e := NewMockExecutor(tc.mockCommands) | ||
terraformCLI := NewTerraformCLI(e) | ||
got, err := terraformCLI.Providers(context.Background()) | ||
if tc.ok && err != nil { | ||
t.Fatalf("unexpected err: %s", err) | ||
} | ||
if !tc.ok && err == nil { | ||
t.Fatal("expected to return an error, but no error") | ||
} | ||
if tc.ok && !reflect.DeepEqual(got, tc.want) { | ||
t.Errorf("got: %v, want: %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAccTerraformCLIProviders(t *testing.T) { | ||
SkipUnlessAcceptanceTestEnabled(t) | ||
|
||
source := ` | ||
resource "null_resource" "foo" {} | ||
resource "null_resource" "bar" {} | ||
` | ||
e := SetupTestAcc(t, source) | ||
terraformCLI := NewTerraformCLI(e) | ||
|
||
err := terraformCLI.Init(context.Background(), "-input=false", "-no-color") | ||
if err != nil { | ||
t.Fatalf("failed to run terraform init: %s", err) | ||
} | ||
|
||
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve") | ||
if err != nil { | ||
t.Fatalf("failed to run terraform apply: %s", err) | ||
} | ||
|
||
got, err := terraformCLI.Providers(context.Background()) | ||
if err != nil { | ||
t.Fatalf("failed to run terraform state list: %s", err) | ||
} | ||
|
||
v, err := terraformCLI.Version(context.Background()) | ||
if err != nil { | ||
t.Fatalf("unexpected version error: %s", err) | ||
} | ||
|
||
constraints, err := version.NewConstraint(fmt.Sprintf(">= %s", MinimumTerraformVersionForStateReplaceProvider)) | ||
if err != nil { | ||
t.Fatalf("unexpected version constraint error: %s", err) | ||
} | ||
|
||
want := providersStdout | ||
if !constraints.Check(v) { | ||
want = legacyProvidersStdout | ||
} | ||
|
||
if got != want { | ||
t.Errorf("got: %s, want: %s", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hashicorp/go-version" | ||
) | ||
|
||
const ( | ||
// MinimumTerraformVersionForStateReplaceProvider specifies the minimum | ||
// supported Terraform version for StateReplaceProvider. | ||
MinimumTerraformVersionForStateReplaceProvider = "0.13" | ||
) | ||
|
||
// StateReplaceProvider replaces providers from source to destination address. | ||
// If a state argument is given, use it for the input state. | ||
// It returns the given state. | ||
func (c *terraformCLI) StateReplaceProvider(ctx context.Context, state *State, source string, destination string, opts ...string) (*State, error) { | ||
constraints, err := version.NewConstraint(fmt.Sprintf(">= %s", MinimumTerraformVersionForStateReplaceProvider)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
v, err := c.Version(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !constraints.Check(v) { | ||
return nil, fmt.Errorf("configuration uses Terraform version %s; replace-provider action requires Terraform version %s", v, constraints) | ||
} | ||
|
||
args := []string{"state", "replace-provider"} | ||
|
||
var tmpState *os.File | ||
|
||
if state != nil { | ||
if hasPrefixOptions(opts, "-state=") { | ||
return nil, fmt.Errorf("failed to build options. The state argument (!= nil) and the -state= option cannot be set at the same time: state=%v, opts=%v", state, opts) | ||
} | ||
tmpState, err = writeTempFile(state.Bytes()) | ||
defer os.Remove(tmpState.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
args = append(args, "-state="+tmpState.Name()) | ||
} | ||
|
||
args = append(args, opts...) | ||
args = append(args, source, destination) | ||
|
||
_, _, err = c.Run(ctx, args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Read updated states | ||
var updatedState *State | ||
|
||
if state != nil { | ||
bytes, err := os.ReadFile(tmpState.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
updatedState = NewState(bytes) | ||
} | ||
|
||
return updatedState, nil | ||
} |
Oops, something went wrong.