Skip to content

Commit

Permalink
add validator for body and identity fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 23, 2024
1 parent 07e1387 commit 9019fc3
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 4 deletions.
3 changes: 3 additions & 0 deletions internal/services/azapi_data_plane_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func (r *DataPlaneResource) Schema(ctx context.Context, request resource.SchemaR
Optional: true,
Computed: true,
Default: defaults.DynamicDefault(types.StringValue("{}")),
Validators: []validator.Dynamic{
myvalidator.BodyValidator(),
},
PlanModifiers: []planmodifier.Dynamic{
myplanmodifier.DynamicUseStateWhen(bodySemanticallyEqual),
},
Expand Down
4 changes: 4 additions & 0 deletions internal/services/azapi_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func (r *AzapiResource) Schema(ctx context.Context, _ resource.SchemaRequest, re
Optional: true,
Computed: true,
Default: defaults.DynamicDefault(types.StringValue("{}")),
Validators: []validator.Dynamic{
myvalidator.BodyValidator(),
},
PlanModifiers: []planmodifier.Dynamic{
myplanmodifier.DynamicUseStateWhen(bodySemanticallyEqual),
},
Expand Down Expand Up @@ -214,6 +217,7 @@ func (r *AzapiResource) Schema(ctx context.Context, _ resource.SchemaRequest, re
Blocks: map[string]schema.Block{
"identity": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Validators: []validator.Object{myvalidator.IdentityValidator()},
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Required: true,
Expand Down
3 changes: 3 additions & 0 deletions internal/services/azapi_resource_action_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (r *ResourceActionDataSource) Schema(ctx context.Context, request datasourc
// TODO: Remove the support for JSON string in the next major release
"body": schema.DynamicAttribute{
Optional: true,
Validators: []validator.Dynamic{
myvalidator.BodyValidator(),
},
},

"response_export_values": schema.ListAttribute{
Expand Down
3 changes: 3 additions & 0 deletions internal/services/azapi_resource_action_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (r *ActionResource) Schema(ctx context.Context, request resource.SchemaRequ
Optional: true,
Computed: true,
Default: defaults.DynamicDefault(types.StringValue("{}")),
Validators: []validator.Dynamic{
myvalidator.BodyValidator(),
},
PlanModifiers: []planmodifier.Dynamic{
myplanmodifier.DynamicUseStateWhen(bodySemanticallyEqual),
},
Expand Down
3 changes: 3 additions & 0 deletions internal/services/azapi_update_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ func (r *AzapiUpdateResource) Schema(ctx context.Context, request resource.Schem
Optional: true,
Computed: true,
Default: defaults.DynamicDefault(types.StringValue("{}")),
Validators: []validator.Dynamic{
myvalidator.BodyValidator(),
},
PlanModifiers: []planmodifier.Dynamic{
myplanmodifier.DynamicUseStateWhen(bodySemanticallyEqual),
},
Expand Down
40 changes: 40 additions & 0 deletions internal/services/myvalidator/body_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package myvalidator

import (
"context"
"encoding/json"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type bodyValidator struct{}

func (v bodyValidator) Description(ctx context.Context) string {
return "validate the identity block"
}

func (v bodyValidator) MarkdownDescription(ctx context.Context) string {
return "validate the identity block"
}

func (_ bodyValidator) ValidateDynamic(ctx context.Context, req validator.DynamicRequest, resp *validator.DynamicResponse) {
raw := req.ConfigValue

if raw.IsUnknown() || raw.IsNull() {
return
}

switch value := raw.UnderlyingValue().(type) {

Check failure on line 28 in internal/services/myvalidator/body_validator.go

View workflow job for this annotation

GitHub Actions / golint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case types.String:
var out interface{}
err := json.Unmarshal([]byte(value.ValueString()), &out)
if err != nil {
resp.Diagnostics.AddAttributeError(req.Path, "Invalid JSON", err.Error())
}
}
}

func BodyValidator() validator.Dynamic {
return bodyValidator{}
}
40 changes: 40 additions & 0 deletions internal/services/myvalidator/identity_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package myvalidator

import (
"context"

"github.com/Azure/terraform-provider-azapi/internal/azure/identity"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

type identityValidator struct{}

func (v identityValidator) Description(ctx context.Context) string {
return "validate the identity block"
}

func (v identityValidator) MarkdownDescription(ctx context.Context) string {
return "validate the identity block"
}

func (_ identityValidator) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
value := req.ConfigValue

if value.IsUnknown() || value.IsNull() {
return
}

var model identity.Model
if resp.Diagnostics.Append(value.As(ctx, &model, basetypes.ObjectAsOptions{})...); resp.Diagnostics.HasError() {
return
}

if _, err := identity.ExpandIdentity(model); err != nil {
resp.Diagnostics.AddAttributeError(req.Path, "Invalid `identity` block", err.Error())
}
}

func IdentityValidator() validator.Object {
return identityValidator{}
}
2 changes: 1 addition & 1 deletion internal/services/myvalidator/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (_ stringIsResourceID) ValidateString(ctx context.Context, req validator.St
}
}

func StringIsResourceID() stringIsResourceID {
func StringIsResourceID() validator.String {
return stringIsResourceID{}
}
2 changes: 1 addition & 1 deletion internal/services/myvalidator/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (_ stringIsResourceType) ValidateString(ctx context.Context, req validator.
}
}

func StringIsResourceType() stringIsResourceType {
func StringIsResourceType() validator.String {
return stringIsResourceType{}
}
2 changes: 1 addition & 1 deletion internal/services/myvalidator/string_is_UUID.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (_ stringIsUUID) ValidateString(ctx context.Context, req validator.StringRe
}
}

func StringIsUUID() stringIsUUID {
func StringIsUUID() validator.String {
return stringIsUUID{}
}
2 changes: 1 addition & 1 deletion internal/services/myvalidator/string_is_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (_ stringIsJSON) ValidateString(ctx context.Context, req validator.StringRe
}
}

func StringIsJSON() stringIsJSON {
func StringIsJSON() validator.String {
return stringIsJSON{}
}

0 comments on commit 9019fc3

Please sign in to comment.