This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
/
resource_pingaccess_rule.go
155 lines (134 loc) · 5.22 KB
/
resource_pingaccess_rule.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
152
153
154
155
package sdkv2provider
import (
"context"
"encoding/json"
"github.com/iwarapter/pingaccess-sdk-go/v62/pingaccess/models"
"github.com/iwarapter/pingaccess-sdk-go/v62/services/rules"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourcePingAccessRule() *schema.Resource {
return &schema.Resource{
CreateContext: resourcePingAccessRuleCreate,
ReadContext: resourcePingAccessRuleRead,
UpdateContext: resourcePingAccessRuleUpdate,
DeleteContext: resourcePingAccessRuleDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: resourcePingAccessRuleSchema(),
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff, m interface{}) error {
className := d.Get("class_name").(string)
if err := ruleDescriptorsHasClassName(className, m.(paClient).RuleDescriptions); err != nil {
return err
}
return validateRulesConfiguration(className, d, m.(paClient).RuleDescriptions)
},
Description: `Provides configuration for Rules within PingAccess.
-> The PingAccess API does not provider repeatable means of querying a sensitive value, we are unable to detect configuration drift of any sensitive fields in the configuration block.`,
}
}
func resourcePingAccessRuleSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"class_name": {
Type: schema.TypeString,
Required: true,
Description: "The rule's class name.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The rule's name.",
},
"supported_destinations": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Deprecated: "This field is read-only and should not be set, future versions of the provider will enforce this.",
Description: "The supported destinations for this rule.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"configuration": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppressEquivalentJSONDiffs,
Description: "The rule's configuration data.",
},
}
}
func resourcePingAccessRuleCreate(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
svc := m.(paClient).Rules
input := rules.AddRuleCommandInput{
Body: *resourcePingAccessRuleReadData(d),
}
result, _, err := svc.AddRuleCommand(&input)
if err != nil {
return diag.Errorf("unable to create Rule: %s", err)
}
d.SetId(result.Id.String())
return resourcePingAccessRuleReadResult(d, result, m.(paClient).RuleDescriptions)
}
func resourcePingAccessRuleRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
svc := m.(paClient).Rules
input := &rules.GetRuleCommandInput{
Id: d.Id(),
}
result, _, err := svc.GetRuleCommand(input)
if err != nil {
return diag.Errorf("unable to read Rule: %s", err)
}
return resourcePingAccessRuleReadResult(d, result, m.(paClient).RuleDescriptions)
}
func resourcePingAccessRuleUpdate(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
svc := m.(paClient).Rules
input := rules.UpdateRuleCommandInput{
Body: *resourcePingAccessRuleReadData(d),
Id: d.Id(),
}
result, _, err := svc.UpdateRuleCommand(&input)
if err != nil {
return diag.Errorf("unable to update Rule: %s", err)
}
d.SetId(result.Id.String())
return resourcePingAccessRuleReadResult(d, result, m.(paClient).RuleDescriptions)
}
func resourcePingAccessRuleDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
svc := m.(paClient).Rules
_, err := svc.DeleteRuleCommand(&rules.DeleteRuleCommandInput{Id: d.Id()})
if err != nil {
return diag.Errorf("unable to delete Rule: %s", err)
}
return nil
}
func resourcePingAccessRuleReadResult(d *schema.ResourceData, input *models.RuleView, desc *models.RuleDescriptorsView) diag.Diagnostics {
var diags diag.Diagnostics
b, _ := json.Marshal(input.Configuration)
config := string(b)
originalConfig := d.Get("configuration").(string)
//Search the Rules descriptors for CONCEALED fields, and update the original value back as we cannot use the
//encryptedValue provided by the API, whilst this gives us a stable plan - we cannot determine if a CONCEALED value
//has changed and needs updating
config = maskConfigFromRuleDescriptors(desc, input.ClassName, originalConfig, config)
setResourceDataStringWithDiagnostic(d, "name", input.Name, &diags)
setResourceDataStringWithDiagnostic(d, "class_name", input.ClassName, &diags)
setResourceDataStringWithDiagnostic(d, "configuration", &config, &diags)
if err := d.Set("supported_destinations", input.SupportedDestinations); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
return diags
}
func resourcePingAccessRuleReadData(d *schema.ResourceData) *models.RuleView {
config := d.Get("configuration").(string)
var dat map[string]interface{}
_ = json.Unmarshal([]byte(config), &dat)
supDests := expandStringList(d.Get("supported_destinations").(*schema.Set).List())
rule := &models.RuleView{
Name: String(d.Get("name").(string)),
ClassName: String(d.Get("class_name").(string)),
Configuration: dat,
SupportedDestinations: &supDests,
}
return rule
}