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

Add validation of viz objects #554

Conversation

klisiecka-splunk
Copy link
Contributor

Adding validation for dashboards, dashboard groups and charts: terraform provider should fail on plan if the payload is faulty.

Copy link

github-actions bot commented Jan 10, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.87%. Comparing base (338a555) to head (5b3f063).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #554   +/-   ##
=======================================
  Coverage   94.87%   94.87%           
=======================================
  Files          35       35           
  Lines        2107     2107           
=======================================
  Hits         1999     1999           
  Misses         89       89           
  Partials       19       19           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@MovieStoreGuy MovieStoreGuy left a comment

Choose a reason for hiding this comment

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

Please ensure that test are also included with changes. This is important to ensure behaviour is kept between versions and error cases are correctly handled.

@@ -812,6 +813,16 @@ func getDashboardFilters(d *schema.ResourceData) []*dashboard.ChartsSingleFilter
return filterList
}

func dashboardValidate(ctx context.Context, dashboardObject *schema.ResourceDiff, config interface{}) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func dashboardValidate(ctx context.Context, dashboardObject *schema.ResourceDiff, config interface{}) error {
func dashboardValidate(ctx context.Context, dashboardObject *schema.ResourceDiff, config any) error {

signalfx/util.go Outdated
Comment on lines 54 to 58
type ResourceDataAccess interface {
Get(key string) interface{}
GetOk(key string) (interface{}, bool)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would prefer to not add an interface within the utility file here, it will make it hard to maintain since it is somewhat hidden within the file of all the things that don't exactly fit within a resource object.

To be clear, the interface is fine but move it to a seperate file to make it easier to maintain and discover later on.

Moreover, Go doc style should take the form of:

// <Definition> ...
<Definition>

Please update to be reflect the go doc style.

signalfx/util.go Outdated
return err
}

return config.(*signalfxConfig).Client.ValidateChart(ctx, payload)
Copy link
Collaborator

Choose a reason for hiding this comment

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

One thing to be conscious of is that if the type isn't the expected value, this code will panic, not expecting that it will but if it were to, it is a terrible user experience.

I would suggest looking at type checking here.

signalfx/util.go Outdated
GetOk(key string) (interface{}, bool)
}

func chartValidate(ctx context.Context, chartResource *schema.ResourceDiff, config interface{}, getPayload func(ResourceDataAccess) (*chart.CreateUpdateChartRequest, error)) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if it is more worthwhile to abstract this further instead of passing in placed type params.

Something like:

type ChartValidatorFunc func(input) (ChartRequest, error)

func (fn ChartValidatorFunc) Validate(ctx, resource, config) error {
  if fn == nil {
   return errors.New("no data parser provided")
  }
  payload, err := fn(resource)
  if err != nil {
    return err
  }
  return config.ValidateChart(ctx, payload)
}

This is something that the standard lib does with http.HandlerFunc as a more concrete example.

@klisiecka-splunk
Copy link
Contributor Author

I've added tests and implemented the suggested changes. Please let me know if there are any areas that need further improvement.

@MovieStoreGuy
Copy link
Collaborator

Please make sure you sign the CLA.

@klisiecka-splunk
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@MovieStoreGuy MovieStoreGuy merged commit 3eb63b5 into splunk-terraform:main Jan 23, 2025
1 check passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants