Skip to content

Commit

Permalink
feat: added read functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Dec 13, 2024
1 parent bb85c20 commit bc992a6
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion internal/provider/integration_domain_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -41,6 +43,51 @@ func (r *integrationDomainResource) Metadata(ctx context.Context, req resource.M
resp.TypeName = req.ProviderTypeName + "_integration_domain"
}

// OneRequiredValidator ensures at only one of two boolean attributes is set to true.
type OneRequiredValidator struct {
OtherAttribute string
}

// ValidateBool performs the validation for the boolean attribute.
func (v OneRequiredValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
// Retrieve the other attribute's value
var otherAttr types.Bool
diags := req.Config.GetAttribute(ctx, path.Root(v.OtherAttribute), &otherAttr)
// Check if at least one of the attributes is set to true
if diags.HasError() || !req.ConfigValue.ValueBool() && !otherAttr.ValueBool() {
resp.Diagnostics.AddAttributeError(
req.Path,
"At Least One Required",
"Either 'http' or 'https' must be set to true.",
)
}
// Check if both attributes are set to true
if req.ConfigValue.ValueBool() && otherAttr.ValueBool() {
resp.Diagnostics.AddAttributeError(
req.Path,
"Invalid Attribute Combination",
"Only one of 'http' or 'https' can be set to true.",
)
}
}

// Description returns a plain-text description of the validator's purpose.
func (v OneRequiredValidator) Description(ctx context.Context) string {
return "Ensures that at only one of 'http' or 'https' is set to true."
}

// MarkdownDescription returns a markdown-formatted description of the validator's purpose.
func (v OneRequiredValidator) MarkdownDescription(ctx context.Context) string {
return "Ensures that at only one of `http` or `https` is set to `true`."
}

// NewOneRequiredValidator is a convenience function to create an instance of the validator.
func NewOneRequiredValidator(otherAttribute string) validator.Bool {
return &OneRequiredValidator{
OtherAttribute: otherAttribute,
}
}

func (r *integrationDomainResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `Continuously scan endpoints to evaluate domain TLS, SSL, HTTP, and HTTPS security`,
Expand Down Expand Up @@ -73,10 +120,20 @@ func (r *integrationDomainResource) Schema(ctx context.Context, req resource.Sch
"https": schema.BoolAttribute{
MarkdownDescription: "Enable HTTPS port.",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Validators: []validator.Bool{
NewOneRequiredValidator("http"),
},
},
"http": schema.BoolAttribute{
MarkdownDescription: "Enable HTTP port.",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
Validators: []validator.Bool{
NewOneRequiredValidator("https"),
},
},
},
}
Expand Down Expand Up @@ -173,9 +230,22 @@ func (r *integrationDomainResource) Read(ctx context.Context, req resource.ReadR
}

// Read API call logic
integration, err := r.client.GetClientIntegration(ctx, data.Mrn.ValueString())
if err != nil {
resp.State.RemoveResource(ctx)
return
}

model := integrationDomainResourceModel{
SpaceID: types.StringValue(integration.SpaceID()),
Mrn: types.StringValue(integration.Mrn),
Host: types.StringValue(integration.ConfigurationOptions.HostConfigurationOptions.Host),
Https: types.BoolValue(integration.ConfigurationOptions.HostConfigurationOptions.HTTPS),
Http: types.BoolValue(integration.ConfigurationOptions.HostConfigurationOptions.HTTP),
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

func (r *integrationDomainResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down

0 comments on commit bc992a6

Please sign in to comment.