55 "encoding/json"
66 "fmt"
77 "net/http"
8+ "strconv"
89 "strings"
910 "time"
1011
@@ -19,55 +20,55 @@ import (
1920)
2021
2122const (
22- FieldPodMutationOrganizationID = "organization_id"
23- FieldPodMutationClusterID = "cluster_id"
24- FieldPodMutationName = "name"
25- FieldPodMutationEnabled = "enabled"
26- FieldPodMutationObjectFilterV2 = "object_filter_v2"
27- FieldPodMutationLabels = "labels"
28- FieldPodMutationAnnotations = "annotations"
29- FieldPodMutationNodeSelector = "node_selector"
30- FieldPodMutationNodeSelectorAdd = "add"
31- FieldPodMutationNodeSelectorRemove = "remove"
32- FieldPodMutationTolerations = "tolerations"
33- FieldPodMutationTolerationKey = "key"
34- FieldPodMutationTolerationOperator = "operator"
35- FieldPodMutationTolerationValue = "value"
36- FieldPodMutationTolerationEffect = "effect"
37- FieldPodMutationTolerationSeconds = "toleration_seconds"
38- FieldPodMutationAffinity = "affinity"
39- FieldPodMutationNodeAffinity = "node_affinity"
40- FieldPodMutationPreferred = "preferred_during_scheduling_ignored_during_execution"
41- FieldPodMutationWeight = "weight"
42- FieldPodMutationPreference = "preference"
43- FieldPodMutationMatchExpressions = "match_expressions"
44- FieldPodMutationMatchExpressionsKey = "key"
45- FieldPodMutationMatchExpressionsOperator = "operator"
46- FieldPodMutationSpotType = "spot_type"
47- FieldPodMutationSpotDistributionPct = "spot_distribution_percentage"
48- FieldPodMutationNodeTemplates = "node_templates_to_consolidate"
49- FieldPodMutationRestartWorkloads = "restart_matching_workloads"
50- FieldPodMutationPatch = "patch"
51- FieldPodMutationDistributionGroups = "distribution_groups"
52- FieldPodMutationDistributionGroupName = "name"
53- FieldPodMutationDistributionGroupPct = "percentage"
54- FieldPodMutationDistributionGroupConfig = "config"
55- FieldPodMutationSource = "source"
56- FieldPodMutationFilterNames = "names"
57- FieldPodMutationFilterNamespaces = "namespaces"
58- FieldPodMutationFilterKinds = "kinds"
59- FieldPodMutationFilterLabelsFilter = "labels_filter"
60- FieldPodMutationFilterExcludeNames = "exclude_names"
61- FieldPodMutationFilterExcludeNamespaces = "exclude_namespaces"
62- FieldPodMutationFilterExcludeKinds = "exclude_kinds"
63- FieldPodMutationFilterExcludeLabels = "exclude_labels_filter"
64- FieldPodMutationMatcherType = "type"
65- FieldPodMutationMatcherValue = "value"
66- FieldPodMutationLabelsFilterOperator = "operator"
67- FieldPodMutationLabelsFilterMatchers = "matchers"
68- FieldPodMutationLabelMatcherKey = "key"
69- FieldPodMutationLabelMatcherValue = "value"
70- FieldPodMutationValues = "values"
23+ FieldPodMutationOrganizationID = "organization_id"
24+ FieldPodMutationClusterID = "cluster_id"
25+ FieldPodMutationName = "name"
26+ FieldPodMutationEnabled = "enabled"
27+ FieldPodMutationObjectFilterV2 = "object_filter_v2"
28+ FieldPodMutationLabels = "labels"
29+ FieldPodMutationAnnotations = "annotations"
30+ FieldPodMutationNodeSelector = "node_selector"
31+ FieldPodMutationNodeSelectorAdd = "add"
32+ FieldPodMutationNodeSelectorRemove = "remove"
33+ FieldPodMutationTolerations = "tolerations"
34+ FieldPodMutationTolerationKey = "key"
35+ FieldPodMutationTolerationOperator = "operator"
36+ FieldPodMutationTolerationValue = "value"
37+ FieldPodMutationTolerationEffect = "effect"
38+ FieldPodMutationTolerationSeconds = "toleration_seconds"
39+ FieldPodMutationAffinity = "affinity"
40+ FieldPodMutationNodeAffinity = "node_affinity"
41+ FieldPodMutationPreferred = "preferred_during_scheduling_ignored_during_execution"
42+ FieldPodMutationWeight = "weight"
43+ FieldPodMutationPreference = "preference"
44+ FieldPodMutationMatchExpressions = "match_expressions"
45+ FieldPodMutationMatchExpressionsKey = "key"
46+ FieldPodMutationMatchExpressionsOperator = "operator"
47+ FieldPodMutationSpotType = "spot_type"
48+ FieldPodMutationSpotDistributionPct = "spot_distribution_percentage"
49+ FieldPodMutationNodeTemplates = "node_templates_to_consolidate"
50+ FieldPodMutationRestartWorkloads = "restart_matching_workloads"
51+ FieldPodMutationPatch = "patch"
52+ FieldPodMutationDistributionGroups = "distribution_groups"
53+ FieldPodMutationDistributionGroupName = "name"
54+ FieldPodMutationDistributionGroupPct = "percentage"
55+ FieldPodMutationDistributionGroupConfig = "config"
56+ FieldPodMutationSource = "source"
57+ FieldPodMutationFilterNames = "names"
58+ FieldPodMutationFilterNamespaces = "namespaces"
59+ FieldPodMutationFilterKinds = "kinds"
60+ FieldPodMutationFilterLabelsFilter = "labels_filter"
61+ FieldPodMutationFilterExcludeNames = "exclude_names"
62+ FieldPodMutationFilterExcludeNamespaces = "exclude_namespaces"
63+ FieldPodMutationFilterExcludeKinds = "exclude_kinds"
64+ FieldPodMutationFilterExcludeLabels = "exclude_labels_filter"
65+ FieldPodMutationMatcherType = "type"
66+ FieldPodMutationMatcherValue = "value"
67+ FieldPodMutationLabelsFilterOperator = "operator"
68+ FieldPodMutationLabelsFilterMatchers = "matchers"
69+ FieldPodMutationLabelMatcherKey = "key"
70+ FieldPodMutationLabelMatcherValue = "value"
71+ FieldPodMutationValues = "values"
7172)
7273
7374var spotTypeValues = []string {
@@ -156,7 +157,7 @@ var tolerationSchema = &schema.Resource{
156157 Description : "Toleration effect." ,
157158 },
158159 FieldPodMutationTolerationSeconds : {
159- Type : schema .TypeString ,
160+ Type : schema .TypeInt ,
160161 Optional : true ,
161162 Description : "Toleration seconds." ,
162163 },
@@ -303,6 +304,7 @@ func resourcePodMutation() *schema.Resource {
303304 FieldPodMutationOrganizationID : {
304305 Type : schema .TypeString ,
305306 Optional : true ,
307+ Computed : true ,
306308 Description : "ID of the organization. If not provided, will be inferred from the API client." ,
307309 ValidateDiagFunc : validation .ToDiagFunc (validation .IsUUID ),
308310 },
@@ -327,7 +329,7 @@ func resourcePodMutation() *schema.Resource {
327329 },
328330 FieldPodMutationObjectFilterV2 : {
329331 Type : schema .TypeList ,
330- Optional : true ,
332+ Required : true ,
331333 MaxItems : 1 ,
332334 Description : "Advanced object filter with support for exact and regex matching." ,
333335 Elem : & schema.Resource {
@@ -883,8 +885,9 @@ func stateToTolerations(items []interface{}) []patching_engine.Toleration {
883885 if v := m [FieldPodMutationTolerationEffect ].(string ); v != "" {
884886 t .Effect = & v
885887 }
886- if v := m [FieldPodMutationTolerationSeconds ].(string ); v != "" {
887- t .TolerationSeconds = & v
888+ if v , ok := m [FieldPodMutationTolerationSeconds ].(int ); ok && v != 0 {
889+ s := strconv .Itoa (v )
890+ t .TolerationSeconds = & s
888891 }
889892 tolerations = append (tolerations , t )
890893 }
@@ -1044,7 +1047,12 @@ func podMutationToState(mutation *patching_engine.PodMutation, d *schema.Resourc
10441047 }
10451048
10461049 if mutation .Tolerations != nil {
1047- if err := d .Set (FieldPodMutationTolerations , flattenTolerations (* mutation .Tolerations )); err != nil {
1050+ flatTolerations , err := flattenTolerations (* mutation .Tolerations )
1051+ if err != nil {
1052+ return diag .Errorf ("flattening tolerations: %v" , err )
1053+ }
1054+
1055+ if err := d .Set (FieldPodMutationTolerations , flatTolerations ); err != nil {
10481056 return diag .FromErr (err )
10491057 }
10501058 }
@@ -1200,18 +1208,36 @@ func flattenPodMutationNodeSelector(ns *patching_engine.PatchOptions) []map[stri
12001208 return []map [string ]interface {}{m }
12011209}
12021210
1203- func flattenTolerations (tolerations []patching_engine.Toleration ) []map [string ]interface {} {
1211+ func flattenTolerations (tolerations []patching_engine.Toleration ) ( []map [string ]interface {}, error ) {
12041212 result := make ([]map [string ]interface {}, 0 , len (tolerations ))
12051213 for _ , t := range tolerations {
1214+ tolerationSeconds , err := tolerationSecondsToInt (t .TolerationSeconds )
1215+ if err != nil {
1216+ return nil , fmt .Errorf ("converting tolerationSeconds: %w" , err )
1217+ }
1218+
12061219 result = append (result , map [string ]interface {}{
12071220 FieldPodMutationTolerationKey : lo .FromPtr (t .Key ),
12081221 FieldPodMutationTolerationOperator : lo .FromPtr (t .Operator ),
12091222 FieldPodMutationTolerationValue : lo .FromPtr (t .Value ),
12101223 FieldPodMutationTolerationEffect : lo .FromPtr (t .Effect ),
1211- FieldPodMutationTolerationSeconds : lo . FromPtr ( t . TolerationSeconds ) ,
1224+ FieldPodMutationTolerationSeconds : tolerationSeconds ,
12121225 })
12131226 }
1214- return result
1227+ return result , nil
1228+ }
1229+
1230+ func tolerationSecondsToInt (s * string ) (int , error ) {
1231+ if s == nil {
1232+ return 0 , nil
1233+ }
1234+
1235+ v , err := strconv .Atoi (* s )
1236+ if err != nil {
1237+ return 0 , fmt .Errorf ("atoi tolerationSeconds: %w" , err )
1238+ }
1239+
1240+ return v , nil
12151241}
12161242
12171243func flattenAffinity (affinity * patching_engine.Affinity ) []map [string ]interface {} {
@@ -1275,7 +1301,12 @@ func flattenDistributionGroups(groups []patching_engine.DistributionGroup) ([]ma
12751301 configMap [FieldPodMutationNodeSelector ] = flattenPodMutationNodeSelector (g .Config .NodeSelector )
12761302 }
12771303 if g .Config .Tolerations != nil {
1278- configMap [FieldPodMutationTolerations ] = flattenTolerations (* g .Config .Tolerations )
1304+ flatTolerations , err := flattenTolerations (* g .Config .Tolerations )
1305+ if err != nil {
1306+ return nil , fmt .Errorf ("flattening tolerations: %w" , err )
1307+ }
1308+
1309+ configMap [FieldPodMutationTolerations ] = flatTolerations
12791310 }
12801311 if g .Config .Affinity != nil {
12811312 configMap [FieldPodMutationAffinity ] = flattenAffinity (g .Config .Affinity )
0 commit comments