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 9914c69 commit f824a38
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 @@ -76,9 +76,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[serviceIntegrationsKey].Elem.(*schema.Resource)
r.Schema["integration_type"].Description = userconfig.Desc(r.Schema["integration_type"].Description).PossibleValuesString(FlattenToString(integrations)...).Build()
integrationResource := s[serviceIntegrationsKey].Elem.(*schema.Resource).Schema["integration_type"]
switch len(integrations) {
case 0:
// Disables the service integrations field if there are no integrations supported
integrationResource.ValidateFunc = func(v any, _ string) ([]string, []error) {
return nil, []error{fmt.Errorf("service integration %s can't be specified here", v)}
}
default:
integrationResource.Description = userconfig.Desc(integrationResource.Description).PossibleValuesString(FlattenToString(integrations)...).Build()
integrationResource.ValidateFunc = validation.StringInSlice(FlattenToString(integrations), false)
}

return s
Expand Down Expand Up @@ -641,6 +648,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 f824a38

Please sign in to comment.