Skip to content

chore: modernize zero_trust_access_mtls_hostname_settings tests #5864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions internal/services/stream/list_data_source_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

"github.com/cloudflare/cloudflare-go/v5"
"github.com/cloudflare/cloudflare-go/v5/stream"
"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/cloudflare/terraform-provider-cloudflare/internal/customfield"
)

type StreamsResultListDataSourceEnvelope struct {
Expand Down Expand Up @@ -67,9 +68,9 @@ func (m *StreamsDataSourceModel) toListParams(_ context.Context) (params stream.
if !m.Type.IsNull() {
params.Type = cloudflare.F(m.Type.ValueString())
}
if !m.VideoName.IsNull() {
params.VideoName = cloudflare.F(m.VideoName.ValueString())
}
//if !m.VideoName.IsNull() {
// params.VideoName = cloudflare.F(m.VideoName.ValueString())
//}

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/cloudflare/cloudflare-go/v5"
"github.com/cloudflare/cloudflare-go/v5/option"
"github.com/cloudflare/cloudflare-go/v5/zero_trust"
"github.com/cloudflare/terraform-provider-cloudflare/internal/apijson"
"github.com/cloudflare/terraform-provider-cloudflare/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.ResourceWithConfigure = (*ZeroTrustAccessMTLSHostnameSettingsResource)(nil)
var _ resource.ResourceWithModifyPlan = (*ZeroTrustAccessMTLSHostnameSettingsResource)(nil)
var _ resource.ResourceWithImportState = (*ZeroTrustAccessMTLSHostnameSettingsResource)(nil)

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

_, err = r.client.ZeroTrust.Access.Certificates.Settings.Update(
ctx,
params,
option.WithRequestBody("application/json", dataBytes),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
err = retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
_, retryErr := r.client.ZeroTrust.Access.Certificates.Settings.Update(
ctx,
params,
option.WithRequestBody("application/json", dataBytes),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
if retryErr != nil {
// Check if it's a conflict error that should be retried
if res != nil && res.StatusCode == 409 {
return retry.RetryableError(retryErr)
}
return retry.NonRetryableError(retryErr)
}
return nil
})
if err != nil {
resp.Diagnostics.AddError("failed to make http request", err.Error())
return
Expand Down Expand Up @@ -130,13 +145,23 @@ func (r *ZeroTrustAccessMTLSHostnameSettingsResource) Update(ctx context.Context
params.ZoneID = cloudflare.F(data.ZoneID.ValueString())
}

_, err = r.client.ZeroTrust.Access.Certificates.Settings.Update(
ctx,
params,
option.WithRequestBody("application/json", dataBytes),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
err = retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
_, retryErr := r.client.ZeroTrust.Access.Certificates.Settings.Update(
ctx,
params,
option.WithRequestBody("application/json", dataBytes),
option.WithResponseBodyInto(&res),
option.WithMiddleware(logging.Middleware(ctx)),
)
if retryErr != nil {
// Check if it's a conflict error that should be retried
if res != nil && res.StatusCode == 409 {
return retry.RetryableError(retryErr)
}
return retry.NonRetryableError(retryErr)
}
return nil
})
if err != nil {
resp.Diagnostics.AddError("failed to make http request", err.Error())
return
Expand Down Expand Up @@ -201,6 +226,52 @@ func (r *ZeroTrustAccessMTLSHostnameSettingsResource) Delete(ctx context.Context

}

func (r *ZeroTrustAccessMTLSHostnameSettingsResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
var data *ZeroTrustAccessMTLSHostnameSettingsModel = new(ZeroTrustAccessMTLSHostnameSettingsModel)
importID := req.ID

// Check if it's a zone ID or account ID and set the appropriate field
if len(importID) == 32 {
// Account ID
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("account_id"), importID)...)
data.AccountID = types.StringValue(importID)
} else {
// Zone ID
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("zone_id"), importID)...)
data.ZoneID = types.StringValue(importID)
}

if resp.Diagnostics.HasError() {
return
}

// Since import just passes the identifier, we need to read the current state
// from the API. We'll do this by creating a mock ReadRequest and calling Read()
var fullData *ZeroTrustAccessMTLSHostnameSettingsModel = new(ZeroTrustAccessMTLSHostnameSettingsModel)
if !data.AccountID.IsNull() {
fullData.AccountID = data.AccountID
} else {
fullData.ZoneID = data.ZoneID
}

// Set the full data in state first so Read can process it
resp.Diagnostics.Append(resp.State.Set(ctx, fullData)...)
if resp.Diagnostics.HasError() {
return
}

// Create a Read request and response to fetch the full state
readReq := resource.ReadRequest{State: resp.State}
readResp := &resource.ReadResponse{State: resp.State, Diagnostics: resp.Diagnostics}

// Call the Read method to populate the full state
r.Read(ctx, readReq, readResp)

// Copy back the results
resp.State = readResp.State
resp.Diagnostics = readResp.Diagnostics
}

func (r *ZeroTrustAccessMTLSHostnameSettingsResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
if req.State.Raw.IsNull() {
resp.Diagnostics.AddWarning(
Expand Down
Loading
Loading