-
Notifications
You must be signed in to change notification settings - Fork 33
/
reduced_transformation.go
134 lines (122 loc) · 5.02 KB
/
reduced_transformation.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
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package externalwebrequest
import (
"fmt"
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/opt"
"github.com/dynatrace-oss/terraform-provider-dynatrace/terraform/hcl"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"
)
type ReducedTransformations []*ReducedTransformation
func (me *ReducedTransformations) Schema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"transformation": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Description: "",
Elem: &schema.Resource{Schema: new(ReducedTransformation).Schema()},
},
}
}
func (me ReducedTransformations) MarshalHCL(properties hcl.Properties) error {
return properties.EncodeSlice("transformation", me)
}
func (me *ReducedTransformations) UnmarshalHCL(decoder hcl.Decoder) error {
return decoder.DecodeSlice("transformation", me)
}
type ReducedTransformation struct {
IncludeHexNumbers *bool `json:"includeHexNumbers,omitempty"` // include hexadecimal numbers
MinDigitCount *int `json:"minDigitCount,omitempty"` // min digit count
Prefix *string `json:"prefix,omitempty"`
ReplacementValue *string `json:"replacementValue,omitempty"` // replacement
Suffix *string `json:"suffix,omitempty"`
TransformationType ContextRootTransformationType `json:"transformationType"` // Possible Values: `BEFORE`, `REMOVE_CREDIT_CARDS`, `REMOVE_IBANS`, `REMOVE_IPS`, `REMOVE_NUMBERS`, `REPLACE_BETWEEN`
}
func (me *ReducedTransformation) Schema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"include_hex_numbers": {
Type: schema.TypeBool,
Description: "include hexadecimal numbers",
Optional: true, // precondition
},
"min_digit_count": {
Type: schema.TypeInt,
Description: "min digit count",
Optional: true, // precondition
},
"prefix": {
Type: schema.TypeString,
Description: "no documentation available",
Optional: true, // nullable & precondition
},
"replacement_value": {
Type: schema.TypeString,
Description: "replacement",
Optional: true, // nullable & precondition
},
"suffix": {
Type: schema.TypeString,
Description: "no documentation available",
Optional: true, // nullable & precondition
},
"transformation_type": {
Type: schema.TypeString,
Description: "Possible Values: `BEFORE`, `REMOVE_CREDIT_CARDS`, `REMOVE_IBANS`, `REMOVE_IPS`, `REMOVE_NUMBERS`, `REPLACE_BETWEEN`",
Required: true,
},
}
}
func (me *ReducedTransformation) MarshalHCL(properties hcl.Properties) error {
return properties.EncodeAll(map[string]any{
"include_hex_numbers": me.IncludeHexNumbers,
"min_digit_count": me.MinDigitCount,
"prefix": me.Prefix,
"replacement_value": me.ReplacementValue,
"suffix": me.Suffix,
"transformation_type": me.TransformationType,
})
}
func (me *ReducedTransformation) HandlePreconditions() error {
if me.IncludeHexNumbers == nil && (string(me.TransformationType) == "REMOVE_NUMBERS") {
me.IncludeHexNumbers = opt.NewBool(false)
}
if me.MinDigitCount == nil && (string(me.TransformationType) == "REMOVE_NUMBERS") {
return fmt.Errorf("'min_digit_count' must be specified if 'transformation_type' is set to '%v'", me.TransformationType)
}
if me.Prefix == nil && slices.Contains([]string{"REPLACE_BETWEEN"}, string(me.TransformationType)) {
return fmt.Errorf("'prefix' must be specified if 'transformation_type' is set to '%v'", me.TransformationType)
}
if me.ReplacementValue == nil && (string(me.TransformationType) == "REPLACE_BETWEEN") {
return fmt.Errorf("'replacement_value' must be specified if 'transformation_type' is set to '%v'", me.TransformationType)
}
if me.Suffix == nil && slices.Contains([]string{"BEFORE", "REPLACE_BETWEEN"}, string(me.TransformationType)) {
return fmt.Errorf("'suffix' must be specified if 'transformation_type' is set to '%v'", me.TransformationType)
}
return nil
}
func (me *ReducedTransformation) UnmarshalHCL(decoder hcl.Decoder) error {
return decoder.DecodeAll(map[string]any{
"include_hex_numbers": &me.IncludeHexNumbers,
"min_digit_count": &me.MinDigitCount,
"prefix": &me.Prefix,
"replacement_value": &me.ReplacementValue,
"suffix": &me.Suffix,
"transformation_type": &me.TransformationType,
})
}