-
Notifications
You must be signed in to change notification settings - Fork 31
/
context_root.go
96 lines (88 loc) · 4.77 KB
/
context_root.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
/**
* @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"
)
type ContextRoot struct {
ContributionType ContributionType `json:"contributionType"` // Possible Values: `OriginalValue`, `OverrideValue`, `TransformURL`, `TransformValue`
SegmentCount *int `json:"segmentCount,omitempty"` // The number of segments of the URL to be kept. The URL is divided by slashes (/), the indexing starts with 1 at context root. For example, if you specify 2 for the `www.dynatrace.com/support/help/dynatrace-api/` URL, the value of `support/help` is used.
Transformations ReducedTransformations `json:"transformations,omitempty"` // Choose how to transform a value before it contributes to the Service Id. Note that all of the Transformations are always applied. Transformations are applied in the order they are specified, and the output of the previous transformation is the input for the next one. The resulting value contributes to the Service Id and can be found on the **Service overview page** under **Properties and tags**.
ValueOverride *ValueOverride `json:"valueOverride,omitempty"` // The value to be used instead of the detected value.
}
func (me *ContextRoot) Schema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"contribution_type": {
Type: schema.TypeString,
Description: "Possible Values: `OriginalValue`, `OverrideValue`, `TransformURL`, `TransformValue`",
Required: true,
},
"segment_count": {
Type: schema.TypeInt,
Description: "The number of segments of the URL to be kept. The URL is divided by slashes (/), the indexing starts with 1 at context root. For example, if you specify 2 for the `www.dynatrace.com/support/help/dynatrace-api/` URL, the value of `support/help` is used.",
Optional: true, // precondition
},
"transformations": {
Type: schema.TypeList,
Description: "Choose how to transform a value before it contributes to the Service Id. Note that all of the Transformations are always applied. Transformations are applied in the order they are specified, and the output of the previous transformation is the input for the next one. The resulting value contributes to the Service Id and can be found on the **Service overview page** under **Properties and tags**.",
Optional: true, // precondition & minobjects == 0
Elem: &schema.Resource{Schema: new(ReducedTransformations).Schema()},
MinItems: 1,
MaxItems: 1,
},
"value_override": {
Type: schema.TypeList,
Description: "The value to be used instead of the detected value.",
Optional: true, // precondition
Elem: &schema.Resource{Schema: new(ValueOverride).Schema()},
MinItems: 1,
MaxItems: 1,
},
}
}
func (me *ContextRoot) MarshalHCL(properties hcl.Properties) error {
return properties.EncodeAll(map[string]any{
"contribution_type": me.ContributionType,
"segment_count": me.SegmentCount,
"transformations": me.Transformations,
"value_override": me.ValueOverride,
})
}
func (me *ContextRoot) HandlePreconditions() error {
if me.SegmentCount == nil && (string(me.ContributionType) == "TransformURL") {
me.SegmentCount = opt.NewInt(0)
}
if me.ValueOverride == nil && (string(me.ContributionType) == "OverrideValue") {
return fmt.Errorf("'value_override' must be specified if 'contribution_type' is set to '%v'", me.ContributionType)
}
if me.ValueOverride != nil && (string(me.ContributionType) != "OverrideValue") {
return fmt.Errorf("'value_override' must not be specified if 'contribution_type' is set to '%v'", me.ContributionType)
}
// ---- Transformations ReducedTransformations -> {"expectedValues":["TransformValue","TransformURL"],"property":"contributionType","type":"IN"}
return nil
}
func (me *ContextRoot) UnmarshalHCL(decoder hcl.Decoder) error {
return decoder.DecodeAll(map[string]any{
"contribution_type": &me.ContributionType,
"segment_count": &me.SegmentCount,
"transformations": &me.Transformations,
"value_override": &me.ValueOverride,
})
}