Skip to content

Commit 9adc31d

Browse files
committed
chore: modernize zero_trust_access_mtls_hostname_settings tests
1 parent 98682f0 commit 9adc31d

File tree

3 files changed

+342
-45
lines changed

3 files changed

+342
-45
lines changed

internal/services/stream/list_data_source_model.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77

88
"github.com/cloudflare/cloudflare-go/v5"
99
"github.com/cloudflare/cloudflare-go/v5/stream"
10-
"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
1110
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1211
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
1312
"github.com/hashicorp/terraform-plugin-framework/diag"
1413
"github.com/hashicorp/terraform-plugin-framework/types"
14+
15+
"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
1516
)
1617

1718
type StreamsResultListDataSourceEnvelope struct {
@@ -67,9 +68,9 @@ func (m *StreamsDataSourceModel) toListParams(_ context.Context) (params stream.
6768
if !m.Type.IsNull() {
6869
params.Type = cloudflare.F(m.Type.ValueString())
6970
}
70-
if !m.VideoName.IsNull() {
71-
params.VideoName = cloudflare.F(m.VideoName.ValueString())
72-
}
71+
//if !m.VideoName.IsNull() {
72+
// params.VideoName = cloudflare.F(m.VideoName.ValueString())
73+
//}
7374

7475
return
7576
}

internal/services/zero_trust_access_mtls_hostname_settings/resource.go

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"time"
1011

1112
"github.com/cloudflare/cloudflare-go/v5"
1213
"github.com/cloudflare/cloudflare-go/v5/option"
1314
"github.com/cloudflare/cloudflare-go/v5/zero_trust"
1415
"github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
1516
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
17+
"github.com/hashicorp/terraform-plugin-framework/path"
1618
"github.com/hashicorp/terraform-plugin-framework/resource"
19+
"github.com/hashicorp/terraform-plugin-framework/types"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1721
)
1822

1923
// Ensure provider defined types fully satisfy framework interfaces.
2024
var _ resource.ResourceWithConfigure = (*ZeroTrustAccessMTLSHostnameSettingsResource)(nil)
2125
var _ resource.ResourceWithModifyPlan = (*ZeroTrustAccessMTLSHostnameSettingsResource)(nil)
26+
var _ resource.ResourceWithImportState = (*ZeroTrustAccessMTLSHostnameSettingsResource)(nil)
2227

2328
func NewResource() resource.Resource {
2429
return &ZeroTrustAccessMTLSHostnameSettingsResource{}
@@ -76,13 +81,23 @@ func (r *ZeroTrustAccessMTLSHostnameSettingsResource) Create(ctx context.Context
7681
params.ZoneID = cloudflare.F(data.ZoneID.ValueString())
7782
}
7883

79-
_, err = r.client.ZeroTrust.Access.Certificates.Settings.Update(
80-
ctx,
81-
params,
82-
option.WithRequestBody("application/json", dataBytes),
83-
option.WithResponseBodyInto(&res),
84-
option.WithMiddleware(logging.Middleware(ctx)),
85-
)
84+
err = retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
85+
_, retryErr := r.client.ZeroTrust.Access.Certificates.Settings.Update(
86+
ctx,
87+
params,
88+
option.WithRequestBody("application/json", dataBytes),
89+
option.WithResponseBodyInto(&res),
90+
option.WithMiddleware(logging.Middleware(ctx)),
91+
)
92+
if retryErr != nil {
93+
// Check if it's a conflict error that should be retried
94+
if res != nil && res.StatusCode == 409 {
95+
return retry.RetryableError(retryErr)
96+
}
97+
return retry.NonRetryableError(retryErr)
98+
}
99+
return nil
100+
})
86101
if err != nil {
87102
resp.Diagnostics.AddError("failed to make http request", err.Error())
88103
return
@@ -130,13 +145,23 @@ func (r *ZeroTrustAccessMTLSHostnameSettingsResource) Update(ctx context.Context
130145
params.ZoneID = cloudflare.F(data.ZoneID.ValueString())
131146
}
132147

133-
_, err = r.client.ZeroTrust.Access.Certificates.Settings.Update(
134-
ctx,
135-
params,
136-
option.WithRequestBody("application/json", dataBytes),
137-
option.WithResponseBodyInto(&res),
138-
option.WithMiddleware(logging.Middleware(ctx)),
139-
)
148+
err = retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
149+
_, retryErr := r.client.ZeroTrust.Access.Certificates.Settings.Update(
150+
ctx,
151+
params,
152+
option.WithRequestBody("application/json", dataBytes),
153+
option.WithResponseBodyInto(&res),
154+
option.WithMiddleware(logging.Middleware(ctx)),
155+
)
156+
if retryErr != nil {
157+
// Check if it's a conflict error that should be retried
158+
if res != nil && res.StatusCode == 409 {
159+
return retry.RetryableError(retryErr)
160+
}
161+
return retry.NonRetryableError(retryErr)
162+
}
163+
return nil
164+
})
140165
if err != nil {
141166
resp.Diagnostics.AddError("failed to make http request", err.Error())
142167
return
@@ -201,6 +226,52 @@ func (r *ZeroTrustAccessMTLSHostnameSettingsResource) Delete(ctx context.Context
201226

202227
}
203228

229+
func (r *ZeroTrustAccessMTLSHostnameSettingsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
230+
var data *ZeroTrustAccessMTLSHostnameSettingsModel = new(ZeroTrustAccessMTLSHostnameSettingsModel)
231+
importID := req.ID
232+
233+
// Check if it's a zone ID or account ID and set the appropriate field
234+
if len(importID) == 32 {
235+
// Account ID
236+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("account_id"), importID)...)
237+
data.AccountID = types.StringValue(importID)
238+
} else {
239+
// Zone ID
240+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("zone_id"), importID)...)
241+
data.ZoneID = types.StringValue(importID)
242+
}
243+
244+
if resp.Diagnostics.HasError() {
245+
return
246+
}
247+
248+
// Since import just passes the identifier, we need to read the current state
249+
// from the API. We'll do this by creating a mock ReadRequest and calling Read()
250+
var fullData *ZeroTrustAccessMTLSHostnameSettingsModel = new(ZeroTrustAccessMTLSHostnameSettingsModel)
251+
if !data.AccountID.IsNull() {
252+
fullData.AccountID = data.AccountID
253+
} else {
254+
fullData.ZoneID = data.ZoneID
255+
}
256+
257+
// Set the full data in state first so Read can process it
258+
resp.Diagnostics.Append(resp.State.Set(ctx, fullData)...)
259+
if resp.Diagnostics.HasError() {
260+
return
261+
}
262+
263+
// Create a Read request and response to fetch the full state
264+
readReq := resource.ReadRequest{State: resp.State}
265+
readResp := &resource.ReadResponse{State: resp.State, Diagnostics: resp.Diagnostics}
266+
267+
// Call the Read method to populate the full state
268+
r.Read(ctx, readReq, readResp)
269+
270+
// Copy back the results
271+
resp.State = readResp.State
272+
resp.Diagnostics = readResp.Diagnostics
273+
}
274+
204275
func (r *ZeroTrustAccessMTLSHostnameSettingsResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
205276
if req.State.Raw.IsNull() {
206277
resp.Diagnostics.AddWarning(

0 commit comments

Comments
 (0)