Skip to content

Commit 0eea4e7

Browse files
committed
prevent targeting for unsupported API versions
1 parent 650a272 commit 0eea4e7

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

backend/remote/backend_apply.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99

1010
tfe "github.com/hashicorp/go-tfe"
11+
version "github.com/hashicorp/go-version"
1112
"github.com/hashicorp/terraform/backend"
1213
"github.com/hashicorp/terraform/terraform"
1314
"github.com/hashicorp/terraform/tfdiags"
@@ -94,6 +95,26 @@ func (b *Remote) opApply(stopCtx, cancelCtx context.Context, op *backend.Operati
9495
))
9596
}
9697

98+
if len(op.Targets) != 0 {
99+
// For API versions prior to 2.3, RemoteAPIVersion will return an empty string,
100+
// so if there's an error when parsing the RemoteAPIVersion, it's handled as
101+
// equivalent to an API version < 2.3.
102+
currentAPIVersion, parseErr := version.NewVersion(b.client.RemoteAPIVersion())
103+
desiredAPIVersion, _ := version.NewVersion("2.3")
104+
105+
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
106+
diags = diags.Append(tfdiags.Sourceless(
107+
tfdiags.Error,
108+
"Resource targeting is not supported",
109+
fmt.Sprintf(
110+
`The host %s does not support the -target option for `+
111+
`remote plans.`,
112+
b.hostname,
113+
),
114+
))
115+
}
116+
}
117+
97118
// Return if there are any errors.
98119
if diags.HasErrors() {
99120
return nil, diags.Err()

backend/remote/backend_plan.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
tfe "github.com/hashicorp/go-tfe"
18+
version "github.com/hashicorp/go-version"
1819
"github.com/hashicorp/terraform/backend"
1920
"github.com/hashicorp/terraform/tfdiags"
2021
)
@@ -98,6 +99,26 @@ func (b *Remote) opPlan(stopCtx, cancelCtx context.Context, op *backend.Operatio
9899
))
99100
}
100101

102+
if len(op.Targets) != 0 {
103+
// For API versions prior to 2.3, RemoteAPIVersion will return an empty string,
104+
// so if there's an error when parsing the RemoteAPIVersion, it's handled as
105+
// equivalent to an API version < 2.3.
106+
currentAPIVersion, parseErr := version.NewVersion(b.client.RemoteAPIVersion())
107+
desiredAPIVersion, _ := version.NewVersion("2.3")
108+
109+
if parseErr != nil || currentAPIVersion.LessThan(desiredAPIVersion) {
110+
diags = diags.Append(tfdiags.Sourceless(
111+
tfdiags.Error,
112+
"Resource targeting is not supported",
113+
fmt.Sprintf(
114+
`The host %s does not support the -target option for `+
115+
`remote plans.`,
116+
b.hostname,
117+
),
118+
))
119+
}
120+
}
121+
101122
// Return if there are any errors.
102123
if diags.HasErrors() {
103124
return nil, diags.Err()

backend/remote/testing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ func testServer(t *testing.T) *httptest.Server {
207207
}`, path.Base(r.URL.Path)))
208208
})
209209

210+
// Respond to pings to get the API version header.
211+
mux.HandleFunc("/api/v2/ping", func(w http.ResponseWriter, r *http.Request) {
212+
w.Header().Set("Content-Type", "application/json")
213+
w.Header().Set("TFP-API-Version", "2.3")
214+
})
215+
210216
// Respond to the initial query to read the hashicorp org entitlements.
211217
mux.HandleFunc("/api/v2/organizations/hashicorp/entitlement-set", func(w http.ResponseWriter, r *http.Request) {
212218
w.Header().Set("Content-Type", "application/vnd.api+json")

0 commit comments

Comments
 (0)