Skip to content

Commit

Permalink
feat: add integration types validation
Browse files Browse the repository at this point in the history
Service integrations field gets only allowed integration types (from the API),
like `read_replica` or `disaster_recovery`.
Most of service types just do not support those integrations,
hence, it shouldn't be even possible to set them.
  • Loading branch information
byashimov committed Dec 5, 2024
1 parent 4768e70 commit 01ff219
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/schemautil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ func ServiceCommonSchemaWithUserConfig(kind string) map[string]*schema.Schema {

// Assigns the integration types that are allowed to be set when creating a service
integrations := getBootstrapIntegrationTypes(kind)
if len(integrations) > 0 {
r := s["service_integrations"].Elem.(*schema.Resource)
r.Schema["integration_type"].Description = userconfig.Desc(r.Schema["integration_type"].Description).PossibleValuesString(FlattenToString(integrations)...).Build()
integrationType := s["service_integrations"].Elem.(*schema.Resource).Schema["integration_type"]
switch len(integrations) {
case 0:
// Disables the service integrations field if there are no integrations supported
integrationType.ValidateFunc = func(v any, _ string) ([]string, []error) {
return nil, []error{fmt.Errorf("service integration %s can't be specified here", v)}
}
default:
integrationType.Description = userconfig.Desc(integrationType.Description).PossibleValuesString(FlattenToString(integrations)...).Build()
integrationType.ValidateFunc = validation.StringInSlice(FlattenToString(integrations), false)
}

return s
Expand Down Expand Up @@ -639,6 +646,10 @@ func getTechnicalEmailsForTerraform(s *service.ServiceGetOut) *schema.Set {
// flattenIntegrations converts the service integrations into a list of maps
func flattenIntegrations(integrations []service.ServiceIntegrationOut, kinds ...service.IntegrationType) []map[string]interface{} {
result := make([]map[string]any, 0)
if len(integrations) == 0 || len(kinds) == 0 {
return result
}

for _, v := range integrations {
if slices.Contains(kinds, v.IntegrationType) {
result = append(result, map[string]any{
Expand Down

0 comments on commit 01ff219

Please sign in to comment.