/
data_akamai_property_include_rules.go
151 lines (137 loc) · 4.09 KB
/
data_akamai_property_include_rules.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package property
import (
"context"
"encoding/json"
"fmt"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/papi"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/tf"
"github.com/akamai/terraform-provider-akamai/v6/pkg/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourcePropertyIncludeRules() *schema.Resource {
return &schema.Resource{
ReadContext: dataPropertyIncludeRulesRead,
Schema: map[string]*schema.Schema{
"contract_id": {
Type: schema.TypeString,
Required: true,
Description: "Identifies the contract under which the include was created",
},
"group_id": {
Type: schema.TypeString,
Required: true,
Description: "Identifies the group under which the include was created",
},
"include_id": {
Type: schema.TypeString,
Required: true,
Description: "The unique identifier of the property include",
},
"version": {
Type: schema.TypeInt,
Required: true,
Description: "The version of the include that a rule tree represents",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "A descriptive name for the include",
},
"rules": {
Type: schema.TypeString,
Computed: true,
Description: "Property Rules as JSON",
},
"rule_errors": {
Type: schema.TypeString,
Computed: true,
Description: "Rules validation errors",
},
"rule_warnings": {
Type: schema.TypeString,
Computed: true,
Description: "Rules validation warnings",
},
"rule_format": {
Type: schema.TypeString,
Computed: true,
Description: "Indicates the versioned set of features and criteria that are currently applied to a rule tree",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies the type of the include, either `MICROSERVICES` or `COMMON_SETTINGS`",
},
},
}
}
func dataPropertyIncludeRulesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := meta.Must(m)
client := Client(meta)
log := meta.Log("PAPI", "dataPropertyIncludeRulesRead")
ctx = session.ContextWithOptions(ctx, session.WithContextLog(log))
groupID, err := tf.GetStringValue("group_id", d)
if err != nil {
return diag.FromErr(err)
}
contractID, err := tf.GetStringValue("contract_id", d)
if err != nil {
return diag.FromErr(err)
}
includeID, err := tf.GetStringValue("include_id", d)
if err != nil {
return diag.FromErr(err)
}
includeVersion, err := tf.GetIntValue("version", d)
if err != nil {
return diag.FromErr(err)
}
includeRuleTree, err := client.GetIncludeRuleTree(ctx, papi.GetIncludeRuleTreeRequest{
ContractID: contractID,
GroupID: groupID,
IncludeID: includeID,
IncludeVersion: includeVersion,
ValidateRules: true,
})
if err != nil {
return diag.FromErr(err)
}
rules, err := json.MarshalIndent(papi.RulesUpdate{
Rules: includeRuleTree.Rules,
Comments: includeRuleTree.Comments,
}, "", " ")
if err != nil {
return diag.FromErr(err)
}
attrs := map[string]interface{}{
"name": includeRuleTree.IncludeName,
"rules": string(rules),
"rule_format": includeRuleTree.RuleFormat,
"type": includeRuleTree.IncludeType,
}
var ruleErrors string
if len(includeRuleTree.Errors) > 0 {
rulesErrorsJSON, err := json.MarshalIndent(includeRuleTree.Errors, "", " ")
if err != nil {
return diag.FromErr(err)
}
ruleErrors = string(rulesErrorsJSON)
}
attrs["rule_errors"] = ruleErrors
var ruleWarnings string
if len(includeRuleTree.Warnings) > 0 {
rulesWarningsJSON, err := json.MarshalIndent(includeRuleTree.Warnings, "", " ")
if err != nil {
return diag.FromErr(err)
}
ruleWarnings = string(rulesWarningsJSON)
}
attrs["rule_warnings"] = ruleWarnings
if err := tf.SetAttrs(d, attrs); err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%s:%d", includeID, includeVersion))
return nil
}