Skip to content
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

Namespace Resource: Improve diagnostics handling #179

Merged
merged 4 commits into from
Nov 15, 2024
Merged
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
90 changes: 63 additions & 27 deletions internal/provider/namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,21 @@ func (r *namespaceResource) Create(ctx context.Context, req resource.CreateReque
ctx, cancel := context.WithTimeout(ctx, createTimeout)
defer cancel()

regions := getRegionsFromModel(ctx, resp.Diagnostics, &plan)
regions, d := getRegionsFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
certFilters := getCertFiltersFromModel(ctx, resp.Diagnostics, &plan)
certFilters, d := getCertFiltersFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
var codecServer *namespacev1.CodecServerSpec
if !plan.CodecServer.IsNull() {
codecServer = getCodecServerFromModel(ctx, resp.Diagnostics, &plan)
var d diag.Diagnostics
codecServer, d = getCodecServerFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -348,7 +352,11 @@ func (r *namespaceResource) Create(ctx context.Context, req resource.CreateReque
return
}

updateModelFromSpec(ctx, resp.Diagnostics, &plan, ns.Namespace)
resp.Diagnostics.Append(updateModelFromSpec(ctx, &plan, ns.Namespace)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

Expand All @@ -368,7 +376,11 @@ func (r *namespaceResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

updateModelFromSpec(ctx, resp.Diagnostics, &state, model.Namespace)
resp.Diagnostics.Append(updateModelFromSpec(ctx, &state, model.Namespace)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}

Expand All @@ -380,11 +392,13 @@ func (r *namespaceResource) Update(ctx context.Context, req resource.UpdateReque
return
}

regions := getRegionsFromModel(ctx, resp.Diagnostics, &plan)
regions, d := getRegionsFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
certFilters := getCertFiltersFromModel(ctx, resp.Diagnostics, &plan)
certFilters, d := getCertFiltersFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -396,9 +410,15 @@ func (r *namespaceResource) Update(ctx context.Context, req resource.UpdateReque
resp.Diagnostics.AddError("Failed to get current namespace status", err.Error())
return
}
codecServer := getCodecServerFromModel(ctx, resp.Diagnostics, &plan)
if resp.Diagnostics.HasError() {
return

var codecServer *namespacev1.CodecServerSpec
if !plan.CodecServer.IsNull() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug was uncovered by the acceptance tests after fixing the diagnostic errors

var d diag.Diagnostics
codecServer, d = getCodecServerFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
}

var spec = &namespacev1.NamespaceSpec{
Expand Down Expand Up @@ -460,7 +480,11 @@ func (r *namespaceResource) Update(ctx context.Context, req resource.UpdateReque
return
}

updateModelFromSpec(ctx, resp.Diagnostics, &plan, ns.Namespace)
resp.Diagnostics.Append(updateModelFromSpec(ctx, &plan, ns.Namespace)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

Expand Down Expand Up @@ -505,28 +529,31 @@ func (r *namespaceResource) ImportState(ctx context.Context, req resource.Import
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func getRegionsFromModel(ctx context.Context, diags diag.Diagnostics, plan *namespaceResourceModel) []string {
func getRegionsFromModel(ctx context.Context, plan *namespaceResourceModel) ([]string, diag.Diagnostics) {
var diags diag.Diagnostics
regions := make([]types.String, 0, len(plan.Regions.Elements()))
diags.Append(plan.Regions.ElementsAs(ctx, &regions, false)...)
if diags.HasError() {
return nil
return nil, diags
}

requestRegions := make([]string, len(regions))
for i, region := range regions {
requestRegions[i] = region.ValueString()
}

return requestRegions
return requestRegions, diags
}

func updateModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *namespaceResourceModel, ns *namespacev1.Namespace) {
func updateModelFromSpec(ctx context.Context, state *namespaceResourceModel, ns *namespacev1.Namespace) diag.Diagnostics {
var diags diag.Diagnostics

state.ID = types.StringValue(ns.GetNamespace())
state.Name = types.StringValue(ns.GetSpec().GetName())
planRegions, listDiags := types.ListValueFrom(ctx, types.StringType, ns.GetSpec().GetRegions())
diags.Append(listDiags...)
if diags.HasError() {
return
return diags
}

certificateFilter := types.ListNull(types.ObjectType{AttrTypes: namespaceCertificateFilterAttrs})
Expand All @@ -542,15 +569,15 @@ func updateModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *nam
obj, diag := types.ObjectValueFrom(ctx, namespaceCertificateFilterAttrs, model)
diags.Append(diag...)
if diags.HasError() {
return
return diags
}
certificateFilterObjects[i] = obj
}

filters, diag := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: namespaceCertificateFilterAttrs}, certificateFilterObjects)
diags.Append(diag...)
if diags.HasError() {
return
return diags
}

certificateFilter = filters
Expand Down Expand Up @@ -579,6 +606,10 @@ func updateModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *nam

state, objectDiags := types.ObjectValueFrom(ctx, codecServerAttrs, codecServer)
diags.Append(objectDiags...)
if diags.HasError() {
return diags
}

codecServerState = state
} else {
codecServerState = types.ObjectNull(codecServerAttrs)
Expand All @@ -593,32 +624,36 @@ func updateModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *nam
endpointsState, objectDiags := types.ObjectValueFrom(ctx, endpointsAttrs, endpoints)
diags.Append(objectDiags...)
if diags.HasError() {
return
return diags
}

state.Endpoints = endpointsState
state.Regions = planRegions
state.CertificateFilters = certificateFilter
state.RetentionDays = types.Int64Value(int64(ns.GetSpec().GetRetentionDays()))

return diags
}

func getCertFiltersFromModel(ctx context.Context, diags diag.Diagnostics, model *namespaceResourceModel) []*namespacev1.CertificateFilterSpec {
func getCertFiltersFromModel(ctx context.Context, model *namespaceResourceModel) ([]*namespacev1.CertificateFilterSpec, diag.Diagnostics) {
var diags diag.Diagnostics

elements := make([]types.Object, 0, len(model.CertificateFilters.Elements()))
diags.Append(model.CertificateFilters.ElementsAs(ctx, &elements, false)...)
if diags.HasError() {
return nil
return nil, diags
}

if len(elements) == 0 {
return nil
return nil, diags
}

certificateFilters := make([]*namespacev1.CertificateFilterSpec, len(elements))
for i, filter := range elements {
var model namespaceCertificateFilterModel
diags.Append(filter.As(ctx, &model, basetypes.ObjectAsOptions{})...)
if diags.HasError() {
return nil
return nil, diags
}

certificateFilters[i] = &namespacev1.CertificateFilterSpec{
Expand All @@ -629,20 +664,21 @@ func getCertFiltersFromModel(ctx context.Context, diags diag.Diagnostics, model
}
}

return certificateFilters
return certificateFilters, diags
}

func getCodecServerFromModel(ctx context.Context, diags diag.Diagnostics, model *namespaceResourceModel) *namespacev1.CodecServerSpec {
func getCodecServerFromModel(ctx context.Context, model *namespaceResourceModel) (*namespacev1.CodecServerSpec, diag.Diagnostics) {
var diags diag.Diagnostics
var codecServer codecServerModel
diags.Append(model.CodecServer.As(ctx, &codecServer, basetypes.ObjectAsOptions{})...)
if diags.HasError() {
return nil
return nil, diags
}
return &namespacev1.CodecServerSpec{
Endpoint: codecServer.Endpoint.ValueString(),
PassAccessToken: codecServer.PassAccessToken.ValueBool(),
IncludeCrossOriginCredentials: codecServer.IncludeCrossOriginCredentials.ValueBool(),
}
}, diags
}

func stringOrNull(s string) types.String {
Expand Down
Loading