Skip to content

Commit fe439d1

Browse files
committed
fix import and update acc tests
1 parent 764e2e4 commit fe439d1

6 files changed

+220
-161
lines changed

internal/provider/resource_tfe_opa_version.go

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,26 @@ func (r *OPAVersionResource) Create(ctx context.Context, req resource.CreateRequ
204204
}
205205

206206
opaVersion.ID = types.StringValue(v.ID)
207-
208-
// ensure there are no unknown values
209-
if v.URL == "" {
210-
opaVersion.URL = types.StringNull()
207+
opaVersion.Version = types.StringValue(v.Version)
208+
opaVersion.Official = types.BoolValue(v.Official)
209+
opaVersion.Enabled = types.BoolValue(v.Enabled)
210+
opaVersion.Beta = types.BoolValue(v.Beta)
211+
opaVersion.Deprecated = types.BoolValue(v.Deprecated)
212+
if v.DeprecatedReason != nil {
213+
opaVersion.DeprecatedReason = types.StringValue(*v.DeprecatedReason)
211214
} else {
212-
opaVersion.URL = types.StringValue(v.URL)
215+
opaVersion.DeprecatedReason = types.StringNull()
213216
}
214-
if v.SHA == "" {
215-
opaVersion.SHA = types.StringNull()
217+
if v.URL != "" {
218+
opaVersion.URL = types.StringValue(v.URL)
216219
} else {
220+
opaVersion.URL = types.StringNull()
221+
}
222+
if v.SHA != "" {
217223
opaVersion.SHA = types.StringValue(v.SHA)
224+
} else {
225+
opaVersion.SHA = types.StringNull()
218226
}
219-
220227
opaVersion.Archs = convertAPIArchsToFrameworkSet(v.Archs)
221228

222229
resp.Diagnostics.Append(resp.State.Set(ctx, &opaVersion)...)
@@ -230,7 +237,8 @@ func (r *OPAVersionResource) Read(ctx context.Context, req resource.ReadRequest,
230237
}
231238

232239
tflog.Debug(ctx, "Read configuration of OPA version", map[string]interface{}{
233-
"id": opaVersion.ID.ValueString()})
240+
"id": opaVersion.ID.ValueString(),
241+
})
234242

235243
v, err := r.config.Client.Admin.OPAVersions.Read(ctx, opaVersion.ID.ValueString())
236244
if err != nil {
@@ -246,11 +254,12 @@ func (r *OPAVersionResource) Read(ctx context.Context, req resource.ReadRequest,
246254
}
247255

248256
opaVersion.ID = types.StringValue(v.ID)
257+
opaVersion.Version = types.StringValue(v.Version)
249258
opaVersion.Official = types.BoolValue(v.Official)
250259
opaVersion.Enabled = types.BoolValue(v.Enabled)
251260
opaVersion.Beta = types.BoolValue(v.Beta)
252261
opaVersion.Deprecated = types.BoolValue(v.Deprecated)
253-
if v.DeprecatedReason != nil {
262+
if v.DeprecatedReason != nil && *v.DeprecatedReason != "" {
254263
opaVersion.DeprecatedReason = types.StringValue(*v.DeprecatedReason)
255264
} else {
256265
opaVersion.DeprecatedReason = types.StringNull()
@@ -260,12 +269,11 @@ func (r *OPAVersionResource) Read(ctx context.Context, req resource.ReadRequest,
260269
} else {
261270
opaVersion.URL = types.StringValue(v.URL)
262271
}
263-
if v.SHA == "" {
264-
opaVersion.SHA = types.StringNull()
265-
} else {
272+
if v.SHA != "" {
266273
opaVersion.SHA = types.StringValue(v.SHA)
274+
} else {
275+
opaVersion.SHA = types.StringNull()
267276
}
268-
269277
opaVersion.Archs = convertAPIArchsToFrameworkSet(v.Archs)
270278

271279
resp.Diagnostics.Append(resp.State.Set(ctx, &opaVersion)...)
@@ -281,7 +289,6 @@ func (r *OPAVersionResource) Update(ctx context.Context, req resource.UpdateRequ
281289
return
282290
}
283291

284-
// Use the ID from the state
285292
opaVersion.ID = state.ID
286293

287294
tflog.Debug(ctx, "Updating OPA version resource", map[string]interface{}{
@@ -318,33 +325,27 @@ func (r *OPAVersionResource) Update(ctx context.Context, req resource.UpdateRequ
318325
return
319326
}
320327

321-
// Set ID and other attributes
322328
opaVersion.ID = types.StringValue(v.ID)
323329
opaVersion.Version = types.StringValue(v.Version)
324-
325-
// IMPORTANT: Set explicit values for URL and SHA
330+
opaVersion.Official = types.BoolValue(v.Official)
331+
opaVersion.Enabled = types.BoolValue(v.Enabled)
332+
opaVersion.Beta = types.BoolValue(v.Beta)
333+
opaVersion.Deprecated = types.BoolValue(v.Deprecated)
334+
if v.DeprecatedReason != nil {
335+
opaVersion.DeprecatedReason = types.StringValue(*v.DeprecatedReason)
336+
} else {
337+
opaVersion.DeprecatedReason = types.StringNull()
338+
}
326339
if v.URL != "" {
327340
opaVersion.URL = types.StringValue(v.URL)
328341
} else {
329342
opaVersion.URL = types.StringNull()
330343
}
331-
332344
if v.SHA != "" {
333345
opaVersion.SHA = types.StringValue(v.SHA)
334346
} else {
335347
opaVersion.SHA = types.StringNull()
336348
}
337-
338-
opaVersion.Official = types.BoolValue(v.Official)
339-
opaVersion.Enabled = types.BoolValue(v.Enabled)
340-
opaVersion.Beta = types.BoolValue(v.Beta)
341-
opaVersion.Deprecated = types.BoolValue(v.Deprecated)
342-
if v.DeprecatedReason != nil {
343-
opaVersion.DeprecatedReason = types.StringValue(*v.DeprecatedReason)
344-
} else {
345-
opaVersion.DeprecatedReason = types.StringNull()
346-
}
347-
348349
opaVersion.Archs = convertAPIArchsToFrameworkSet(v.Archs)
349350

350351
resp.Diagnostics.Append(resp.State.Set(ctx, &opaVersion)...)
@@ -379,6 +380,7 @@ func (r *OPAVersionResource) Delete(ctx context.Context, req resource.DeleteRequ
379380
}
380381

381382
func (r *OPAVersionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
383+
var id string
382384
// Splitting by '-' and checking if the first elem is equal to tool
383385
// determines if the string is a tool version ID
384386
s := strings.Split(req.ID, "-")
@@ -394,19 +396,9 @@ func (r *OPAVersionResource) ImportState(ctx context.Context, req resource.Impor
394396
)
395397
return
396398
}
397-
398-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), versionID)...)
399+
id = versionID
400+
} else {
401+
id = req.ID
399402
}
403+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
400404
}
401-
402-
// // Make OPAVersionResource implement the ToolVersionValidator interface
403-
// func (r *OPAVersionResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
404-
// var config modelAdminOPAVersion
405-
// resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
406-
// if resp.Diagnostics.HasError() {
407-
// return
408-
// }
409-
410-
// // Use the simplified validation function
411-
// resp.Diagnostics.Append(ValidateToolVersion(ctx, config.URL, config.SHA, config.Archs, "OPA Version")...)
412-
// }

internal/provider/resource_tfe_opa_version_test.go

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func TestAccTFEOPAVersion_import(t *testing.T) {
7474
},
7575
})
7676
}
77-
7877
func TestAccTFEOPAVersion_full(t *testing.T) {
7978
skipIfCloud(t)
8079

@@ -118,7 +117,8 @@ func TestAccTFEOPAVersion_archs(t *testing.T) {
118117
skipIfCloud(t)
119118

120119
opaVersion := &tfe.AdminOPAVersion{}
121-
sha := genOPASha(t, "secret", "data")
120+
amd64Sha := genOPASha(t, "secret", "data")
121+
arm64Sha := genOPASha(t, "another-secret", "data")
122122
version := genSafeRandomOPAVersion()
123123

124124
resource.Test(t, resource.TestCase{
@@ -127,24 +127,32 @@ func TestAccTFEOPAVersion_archs(t *testing.T) {
127127
CheckDestroy: testAccCheckTFEOPAVersionDestroy,
128128
Steps: []resource.TestStep{
129129
{
130-
Config: resourceTFEOPAVersion_archs(version, sha),
130+
Config: resourceTFEOPAVersion_archs(version, amd64Sha, arm64Sha),
131131
Check: resource.ComposeTestCheckFunc(
132132
testAccCheckTFEOPAVersionExists("tfe_opa_version.foobar", opaVersion),
133-
testAccCheckTFEOPAVersionAttributesArchs(opaVersion, version, sha),
133+
testAccCheckTFEOPAVersionAttributesArchs(opaVersion, version, amd64Sha, arm64Sha),
134134
resource.TestCheckResourceAttr(
135135
"tfe_opa_version.foobar", "version", version),
136136
resource.TestCheckResourceAttr(
137-
"tfe_opa_version.foobar", "url", ""),
137+
"tfe_opa_version.foobar", "url", "https://www.hashicorp.com"),
138138
resource.TestCheckResourceAttr(
139-
"tfe_opa_version.foobar", "sha", ""),
139+
"tfe_opa_version.foobar", "sha", amd64Sha),
140140
resource.TestCheckResourceAttr(
141141
"tfe_opa_version.foobar", "archs.0.url", "https://www.hashicorp.com"),
142142
resource.TestCheckResourceAttr(
143-
"tfe_opa_version.foobar", "archs.0.sha", sha),
143+
"tfe_opa_version.foobar", "archs.0.sha", amd64Sha),
144144
resource.TestCheckResourceAttr(
145145
"tfe_opa_version.foobar", "archs.0.os", "linux"),
146146
resource.TestCheckResourceAttr(
147-
"tfe_opa_version.foobar", "archs.0.arch", "arm64"),
147+
"tfe_opa_version.foobar", "archs.0.arch", "amd64"),
148+
resource.TestCheckResourceAttr(
149+
"tfe_opa_version.foobar", "archs.1.url", "https://www.hashicorp.com"),
150+
resource.TestCheckResourceAttr(
151+
"tfe_opa_version.foobar", "archs.1.sha", arm64Sha),
152+
resource.TestCheckResourceAttr(
153+
"tfe_opa_version.foobar", "archs.1.os", "linux"),
154+
resource.TestCheckResourceAttr(
155+
"tfe_opa_version.foobar", "archs.1.arch", "arm64"),
148156
),
149157
},
150158
},
@@ -252,26 +260,40 @@ func testAccCheckTFEOPAVersionAttributesFull(opaVersion *tfe.AdminOPAVersion, ve
252260
}
253261
}
254262

255-
func testAccCheckTFEOPAVersionAttributesArchs(opaVersion *tfe.AdminOPAVersion, version string, sha string) resource.TestCheckFunc {
263+
func testAccCheckTFEOPAVersionAttributesArchs(opaVersion *tfe.AdminOPAVersion, version string, amd64Sha string, arm64Sha string) resource.TestCheckFunc {
256264
return func(s *terraform.State) error {
257265
if opaVersion.Version != version {
258266
return fmt.Errorf("bad version: %s", opaVersion.Version)
259267
}
260-
if len(opaVersion.Archs) != 1 {
268+
if len(opaVersion.Archs) != 2 {
261269
return fmt.Errorf("expected 1 arch, got %d", len(opaVersion.Archs))
262270
}
263271
if opaVersion.Archs[0].OS != "linux" {
264272
return fmt.Errorf("bad value for OS: %s", opaVersion.Archs[0].OS)
265273
}
266-
if opaVersion.Archs[0].Arch != "arm64" {
274+
if opaVersion.Archs[0].Arch != "amd64" {
267275
return fmt.Errorf("bad value for Arch: %s", opaVersion.Archs[0].Arch)
268276
}
269277
if opaVersion.Archs[0].URL != "https://www.hashicorp.com" {
270278
return fmt.Errorf("bad value for URL: %s", opaVersion.Archs[0].URL)
271279
}
272-
if opaVersion.Archs[0].Sha != sha {
280+
if opaVersion.Archs[0].Sha != amd64Sha {
273281
return fmt.Errorf("bad value for Sha: %v", opaVersion.Archs[0].Sha)
274282
}
283+
284+
if opaVersion.Archs[1].OS != "linux" {
285+
return fmt.Errorf("bad value for OS: %s", opaVersion.Archs[1].OS)
286+
}
287+
if opaVersion.Archs[1].Arch != "arm64" {
288+
return fmt.Errorf("bad value for Arch: %s", opaVersion.Archs[1].Arch)
289+
}
290+
if opaVersion.Archs[1].URL != "https://www.hashicorp.com" {
291+
return fmt.Errorf("bad value for URL: %s", opaVersion.Archs[1].URL)
292+
}
293+
if opaVersion.Archs[1].Sha != arm64Sha {
294+
return fmt.Errorf("bad value for Sha: %v", opaVersion.Archs[1].Sha)
295+
}
296+
275297
return nil
276298
}
277299
}
@@ -299,20 +321,26 @@ resource "tfe_opa_version" "foobar" {
299321
}`, version, sha)
300322
}
301323

302-
func resourceTFEOPAVersion_archs(version string, sha string) string {
324+
func resourceTFEOPAVersion_archs(version string, amd64Sha string, arm64Sha string) string {
303325
return fmt.Sprintf(`
304326
resource "tfe_opa_version" "foobar" {
305327
version = "%s"
306328
official = false
307329
enabled = true
308330
309-
archs {
331+
archs = [{
332+
os = "linux"
333+
arch = "amd64"
334+
url = "https://www.hashicorp.com"
335+
sha = "%s"
336+
},
337+
{
310338
os = "linux"
311339
arch = "arm64"
312340
url = "https://www.hashicorp.com"
313-
sha = "%s"
314-
}
315-
}`, version, sha)
341+
sha = "%s"}]
342+
}
343+
`, version, amd64Sha, arm64Sha)
316344
}
317345

318346
// Helper functions

internal/provider/resource_tfe_sentinel_version.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,16 @@ func (r *sentinelVersionResource) Create(ctx context.Context, req resource.Creat
204204
}
205205

206206
sentinelVersion.ID = types.StringValue(v.ID)
207-
208-
// ensure there are no unknown values
207+
sentinelVersion.Version = types.StringValue(v.Version)
208+
sentinelVersion.Official = types.BoolValue(v.Official)
209+
sentinelVersion.Enabled = types.BoolValue(v.Enabled)
210+
sentinelVersion.Beta = types.BoolValue(v.Beta)
211+
sentinelVersion.Deprecated = types.BoolValue(v.Deprecated)
212+
if v.DeprecatedReason != nil {
213+
sentinelVersion.DeprecatedReason = types.StringValue(*v.DeprecatedReason)
214+
} else {
215+
sentinelVersion.DeprecatedReason = types.StringNull()
216+
}
209217
if v.URL == "" {
210218
sentinelVersion.URL = types.StringNull()
211219
} else {
@@ -246,6 +254,7 @@ func (r *sentinelVersionResource) Read(ctx context.Context, req resource.ReadReq
246254
}
247255

248256
sentinelVersion.ID = types.StringValue(v.ID)
257+
sentinelVersion.Version = types.StringValue(v.Version)
249258
sentinelVersion.Official = types.BoolValue(v.Official)
250259
sentinelVersion.Enabled = types.BoolValue(v.Enabled)
251260
sentinelVersion.Beta = types.BoolValue(v.Beta)
@@ -280,7 +289,6 @@ func (r *sentinelVersionResource) Update(ctx context.Context, req resource.Updat
280289
return
281290
}
282291

283-
// Use the ID from the state
284292
sentinelVersion.ID = state.ID
285293

286294
tflog.Debug(ctx, "Updating sentinel version resource", map[string]interface{}{
@@ -317,20 +325,6 @@ func (r *sentinelVersionResource) Update(ctx context.Context, req resource.Updat
317325

318326
sentinelVersion.ID = types.StringValue(v.ID)
319327
sentinelVersion.Version = types.StringValue(v.Version)
320-
321-
if v.URL != "" {
322-
sentinelVersion.URL = types.StringValue(v.URL)
323-
} else {
324-
sentinelVersion.URL = types.StringNull()
325-
}
326-
327-
if v.SHA != "" {
328-
sentinelVersion.SHA = types.StringValue(v.SHA)
329-
} else {
330-
sentinelVersion.SHA = types.StringNull()
331-
}
332-
333-
// Set remaining attributes
334328
sentinelVersion.Official = types.BoolValue(v.Official)
335329
sentinelVersion.Enabled = types.BoolValue(v.Enabled)
336330
sentinelVersion.Beta = types.BoolValue(v.Beta)
@@ -340,6 +334,16 @@ func (r *sentinelVersionResource) Update(ctx context.Context, req resource.Updat
340334
} else {
341335
sentinelVersion.DeprecatedReason = types.StringNull()
342336
}
337+
if v.URL == "" {
338+
sentinelVersion.URL = types.StringNull()
339+
} else {
340+
sentinelVersion.URL = types.StringValue(v.URL)
341+
}
342+
if v.SHA == "" {
343+
sentinelVersion.SHA = types.StringNull()
344+
} else {
345+
sentinelVersion.SHA = types.StringValue(v.SHA)
346+
}
343347
sentinelVersion.Archs = convertAPIArchsToFrameworkSet(v.Archs)
344348

345349
resp.Diagnostics.Append(resp.State.Set(ctx, &sentinelVersion)...)
@@ -374,6 +378,7 @@ func (r *sentinelVersionResource) Delete(ctx context.Context, req resource.Delet
374378
}
375379

376380
func (r *sentinelVersionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
381+
var id string
377382
// Splitting by '-' and checking if the first elem is equal to tool
378383
// determines if the string is a tool version ID
379384
s := strings.Split(req.ID, "-")
@@ -390,6 +395,10 @@ func (r *sentinelVersionResource) ImportState(ctx context.Context, req resource.
390395
return
391396
}
392397

393-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), versionID)...)
398+
id = versionID
399+
} else {
400+
id = req.ID
394401
}
402+
403+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
395404
}

0 commit comments

Comments
 (0)