Skip to content

Commit

Permalink
Moved some validations to new cluster validators.go file
Browse files Browse the repository at this point in the history
Using the common functions to generate the validators.
Removed unneeded files.

Signed-off-by: Sagi Dayan <[email protected]>
  • Loading branch information
sagidayan committed Oct 1, 2023
1 parent 3ed5c96 commit 441d0b1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 100 deletions.
4 changes: 2 additions & 2 deletions provider/clusterrosaclassic/cluster_rosa_classic_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (r *ClusterRosaClassicResource) Schema(ctx context.Context, req resource.Sc
ElementType: types.StringType,
Optional: true,
Computed: true,
Validators: []validator.Map{PropertiesValidator()},
Validators: []validator.Map{propertiesValidator},
},
"ocm_properties": schema.MapAttribute{
Description: "Merged properties defined by OCM and the user defined 'properties'.",
Expand Down Expand Up @@ -466,7 +466,7 @@ func (r *ClusterRosaClassicResource) Schema(ctx context.Context, req resource.Sc
objectvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("base_dns_domain")),
objectvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("availability_zones")),
objectvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("aws_subnet_ids")),
PrivateHZValidator(),
privateHZValidator,
},
},
},
Expand Down
49 changes: 0 additions & 49 deletions provider/clusterrosaclassic/private_hz_validator.go

This file was deleted.

49 changes: 0 additions & 49 deletions provider/clusterrosaclassic/properties_validator.go

This file was deleted.

46 changes: 46 additions & 0 deletions provider/clusterrosaclassic/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/terraform-redhat/terraform-provider-rhcs/provider/common"
"github.com/terraform-redhat/terraform-provider-rhcs/provider/common/attrvalidators"
)

Expand All @@ -26,3 +28,47 @@ var availabilityZoneValidator = attrvalidators.NewStringValidator("", func(ctx c
return
}
})

var privateHZValidator = attrvalidators.NewObjectValidator("proxy map should not include an hard coded OCM proxy",
func(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

privateHZ := PrivateHostedZone{}
d := req.ConfigValue.As(ctx, &privateHZ, basetypes.ObjectAsOptions{})
if d.HasError() {
// No attribute to validate
return
}
errSum := "Invalid private_hosted_zone attribute assignment"

// validate ID and ARN are not empty
if common.IsStringAttributeEmpty(privateHZ.ID) || common.IsStringAttributeEmpty(privateHZ.RoleARN) {
resp.Diagnostics.AddError(errSum, "Invalid configuration. 'private_hosted_zone.id' and 'private_hosted_zone.arn' are required")
return
}

})

var propertiesValidator = attrvalidators.NewMapValidator("properties map should not include an hard coded OCM properties",
func(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
propertiesElements := make(map[string]types.String, len(req.ConfigValue.Elements()))
d := req.ConfigValue.ElementsAs(ctx, &propertiesElements, false)
if d.HasError() {
// No attribute to validate
return
}

for k, _ := range propertiesElements {
if _, isDefaultKey := OCMProperties[k]; isDefaultKey {
errHead := "Invalid property key."
errDesc := fmt.Sprintf("Can not override reserved properties keys. %s is a reserved property key", k)
resp.Diagnostics.AddError(errHead, errDesc)
return
}
}
})
29 changes: 29 additions & 0 deletions provider/common/attrvalidators/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package attrvalidators

import (
"context"

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

type mapValidator struct {
desc string
validator func(context.Context, validator.MapRequest, *validator.MapResponse)
}

func (v *mapValidator) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) {
v.validator(ctx, req, resp)
}
func (v *mapValidator) Description(ctx context.Context) string {
return v.desc
}
func (v *mapValidator) MarkdownDescription(ctx context.Context) string {
return v.desc
}

func NewMapValidator(desc string, validator func(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse)) validator.Map {
return &mapValidator{
desc: desc,
validator: validator,
}
}

0 comments on commit 441d0b1

Please sign in to comment.