backend/remote: Support replacements in plans

This commit is contained in:
Chris Arcand 2021-05-04 09:35:11 -05:00 committed by CJ Horton
parent a436c7fa2f
commit 2171f8a1a6
1 changed files with 27 additions and 8 deletions

View File

@ -98,6 +98,22 @@ func (b *Remote) opPlan(stopCtx, cancelCtx context.Context, op *backend.Operatio
// equivalent to an API version < 2.3. // equivalent to an API version < 2.3.
currentAPIVersion, parseErr := version.NewVersion(b.client.RemoteAPIVersion()) currentAPIVersion, parseErr := version.NewVersion(b.client.RemoteAPIVersion())
if len(op.Targets) != 0 {
desiredAPIVersion, _ := version.NewVersion("2.3")
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Resource targeting is not supported",
fmt.Sprintf(
`The host %s does not support the -target option for `+
`remote plans.`,
b.hostname,
),
))
}
}
if !op.PlanRefresh { if !op.PlanRefresh {
desiredAPIVersion, _ := version.NewVersion("2.4") desiredAPIVersion, _ := version.NewVersion("2.4")
@ -114,15 +130,15 @@ func (b *Remote) opPlan(stopCtx, cancelCtx context.Context, op *backend.Operatio
} }
} }
if len(op.Targets) != 0 { if len(op.ForceReplace) != 0 {
desiredAPIVersion, _ := version.NewVersion("2.3") desiredAPIVersion, _ := version.NewVersion("2.4")
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) { if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
diags = diags.Append(tfdiags.Sourceless( diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error, tfdiags.Error,
"Resource targeting is not supported", "Planning resource replacements is not supported",
fmt.Sprintf( fmt.Sprintf(
`The host %s does not support the -target option for `+ `The host %s does not support the -replace option for `+
`remote plans.`, `remote plans.`,
b.hostname, b.hostname,
), ),
@ -273,14 +289,17 @@ in order to capture the filesystem context the remote workspace expects:
if len(op.Targets) != 0 { if len(op.Targets) != 0 {
runOptions.TargetAddrs = make([]string, 0, len(op.Targets)) runOptions.TargetAddrs = make([]string, 0, len(op.Targets))
for _, addr := range op.Targets { for _, addr := range op.Targets {
// The API client wants the normal string representation of a
// target address, which will ultimately get inserted into a
// -target option when Terraform CLI is launched in the
// Cloud/Enterprise execution environment.
runOptions.TargetAddrs = append(runOptions.TargetAddrs, addr.String()) runOptions.TargetAddrs = append(runOptions.TargetAddrs, addr.String())
} }
} }
if len(op.ForceReplace) != 0 {
runOptions.ReplaceAddrs = make([]string, 0, len(op.ForceReplace))
for _, addr := range op.ForceReplace {
runOptions.ReplaceAddrs = append(runOptions.ReplaceAddrs, addr.String())
}
}
r, err := b.client.Runs.Create(stopCtx, runOptions) r, err := b.client.Runs.Create(stopCtx, runOptions)
if err != nil { if err != nil {
return r, generalError("Failed to create run", err) return r, generalError("Failed to create run", err)