-
Notifications
You must be signed in to change notification settings - Fork 10
/
usergroups_data_validation.go
76 lines (57 loc) · 2.66 KB
/
usergroups_data_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// usergroups_data_validation.go
package usergroups
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// mainCustomDiffFunc orchestrates all custom diff validations.
func mainCustomDiffFunc(ctx context.Context, diff *schema.ResourceDiff, i interface{}) error {
// Validate based on 'is_smart' attribute.
if err := validateIsSmartAttribute(ctx, diff, i); err != nil {
return err
}
// Validate the sequential order of 'priority' in 'criteria' blocks.
if err := validateCriteriaPrioritySequence(ctx, diff, i); err != nil {
return err
}
// Add more validation calls here as needed.
return nil
}
// validateIsSmartAttribute checks the conditions related to the 'is_smart' attribute.
func validateIsSmartAttribute(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
resourceName := diff.Get("name").(string) // Assuming 'name' is always set and is unique
isSmart, ok := diff.GetOkExists("is_smart")
if !ok {
return nil
}
usersBlockExists := len(diff.Get("users").([]interface{})) > 0
criteriaBlockExists := len(diff.Get("criteria").([]interface{})) > 0
if isSmart.(bool) && usersBlockExists {
return fmt.Errorf("in 'jamfpro_user_group.%s': 'users' block is not allowed when 'is_smart' is set to true", resourceName)
}
if !isSmart.(bool) && criteriaBlockExists {
return fmt.Errorf("in 'jamfpro_user_group.%s': 'criteria' block is not allowed when 'is_smart' is set to false", resourceName)
}
if isSmart.(bool) && !criteriaBlockExists {
return fmt.Errorf("in 'jamfpro_user_group.%s': 'criteria' block is required when 'is_smart' is set to true", resourceName)
}
if !isSmart.(bool) && !usersBlockExists {
return fmt.Errorf("in 'jamfpro_user_group.%s': 'users' block is required when 'is_smart' is set to false", resourceName)
}
return nil
}
// validateCriteriaPrioritySequence checks that the 'priority' fields in the 'criteria' blocks are sequential starting from 0.
func validateCriteriaPrioritySequence(_ context.Context, diff *schema.ResourceDiff, _ interface{}) error {
resourceName := diff.Get("name").(string) // Assuming 'name' is always set and is unique
if criteriaBlocks, ok := diff.GetOk("criteria"); ok {
criteriaList := criteriaBlocks.([]interface{})
for expectedPriority, criteria := range criteriaList {
criteriaMap := criteria.(map[string]interface{})
if actualPriority, ok := criteriaMap["priority"].(int); !ok || actualPriority != expectedPriority {
return fmt.Errorf("in 'jamfpro_user_group.%s': 'priority' value in 'criteria' block must be sequential starting from 0, found priority '%d' at position '%d'", resourceName, actualPriority, expectedPriority)
}
}
}
return nil
}