Skip to content

Commit a9c70ed

Browse files
introduced dynatrace_service_anomalies, dynatrace_application_anomalies, dynatrace_host_anomalies. dynatrace_database_anomalies, dynatrace_custom_anomalies, dynatrace_disk_anomalies
1 parent 5484c96 commit a9c70ed

40 files changed

+2412
-106
lines changed

.github/workflows/tests.yml

+50-1
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,53 @@ jobs:
113113
DYNATRACE_DEBUG: true
114114
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
115115
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
116-
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/notifications
116+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/notifications
117+
- name: TestMetricEventAnomalies
118+
env:
119+
GOPROXY: "https://proxy.golang.org"
120+
TF_ACC: true
121+
DYNATRACE_DEBUG: true
122+
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
123+
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
124+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/anomalies/metrics
125+
- name: TestDiskEventAnomalies
126+
env:
127+
GOPROXY: "https://proxy.golang.org"
128+
TF_ACC: true
129+
DYNATRACE_DEBUG: true
130+
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
131+
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
132+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/anomalies/disks
133+
- name: TestApplicationAnomalies
134+
env:
135+
GOPROXY: "https://proxy.golang.org"
136+
TF_ACC: true
137+
DYNATRACE_DEBUG: true
138+
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
139+
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
140+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/anomalies/applications
141+
- name: TestDatabasesAnomalies
142+
env:
143+
GOPROXY: "https://proxy.golang.org"
144+
TF_ACC: true
145+
DYNATRACE_DEBUG: true
146+
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
147+
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
148+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/anomalies/databases
149+
- name: TestHostAnomalies
150+
env:
151+
GOPROXY: "https://proxy.golang.org"
152+
TF_ACC: true
153+
DYNATRACE_DEBUG: true
154+
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
155+
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
156+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/anomalies/hosts
157+
- name: TestServiceAnomalies
158+
env:
159+
GOPROXY: "https://proxy.golang.org"
160+
TF_ACC: true
161+
DYNATRACE_DEBUG: true
162+
DYNATRACE_ENV_URL: ${{ secrets.DYNATRACE_ENV_URL }}
163+
DYNATRACE_API_TOKEN: ${{ secrets.DYNATRACE_API_TOKEN }}
164+
run: go test -v github.com/dynatrace-oss/terraform-provider-dynatrace/anomalies/services
165+

alerting/data_source.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package alerting
2+
3+
import (
4+
alertingapi "github.com/dtcookie/dynatrace/api/config/alerting"
5+
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
6+
"github.com/google/uuid"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func DataSource() *schema.Resource {
11+
return &schema.Resource{
12+
Read: DataSourceRead,
13+
Schema: map[string]*schema.Schema{
14+
"profiles": {
15+
Type: schema.TypeMap,
16+
Elem: &schema.Schema{Type: schema.TypeString},
17+
Optional: true,
18+
},
19+
},
20+
}
21+
}
22+
23+
func DataSourceRead(d *schema.ResourceData, m interface{}) error {
24+
d.SetId(uuid.New().String())
25+
conf := m.(*config.ProviderConfiguration)
26+
apiService := alertingapi.NewService(conf.DTenvURL, conf.APIToken)
27+
stubList, err := apiService.List()
28+
if err != nil {
29+
return err
30+
}
31+
if len(stubList.Values) > 0 {
32+
profiles := map[string]interface{}{}
33+
for _, stub := range stubList.Values {
34+
profiles[stub.Name] = stub.ID
35+
}
36+
d.Set("profiles", profiles)
37+
}
38+
return nil
39+
}
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Dynatrace LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package applications_test
19+
20+
import (
21+
"fmt"
22+
"io/ioutil"
23+
"strings"
24+
"testing"
25+
26+
"github.com/dtcookie/dynatrace/api/config/anomalies/applications"
27+
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
28+
"github.com/dynatrace-oss/terraform-provider-dynatrace/testbase"
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
32+
)
33+
34+
const ResourceName = "dynatrace_application_anomalies"
35+
const TestDataFolder = "../../test_data/anomalies/applications"
36+
const RequestPath = "%s/anomalyDetection/applications"
37+
38+
type TestStruct struct {
39+
resourceKey string
40+
}
41+
42+
func (test *TestStruct) Anonymize(m map[string]interface{}) {
43+
delete(m, "id")
44+
delete(m, "name")
45+
delete(m, "metadata")
46+
}
47+
48+
func (test *TestStruct) ResourceKey() string {
49+
return test.resourceKey
50+
}
51+
52+
func (test *TestStruct) CreateTestCase(file string, localJSONFile string, t *testing.T) (*resource.TestCase, error) {
53+
var content []byte
54+
var err error
55+
if content, err = ioutil.ReadFile(file); err != nil {
56+
return nil, err
57+
}
58+
config := string(content)
59+
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
60+
resourceName := test.ResourceKey() + "." + name
61+
config = strings.ReplaceAll(config, "#name#", name)
62+
return &resource.TestCase{
63+
PreCheck: func() { testbase.TestAccPreCheck(t) },
64+
IDRefreshName: resourceName,
65+
ProviderFactories: testbase.TestAccProviderFactories,
66+
CheckDestroy: test.CheckDestroy,
67+
Steps: []resource.TestStep{
68+
{
69+
Config: config,
70+
Check: resource.ComposeAggregateTestCheckFunc(
71+
CheckExists(resourceName, t),
72+
testbase.CompareLocalRemote(test, resourceName, localJSONFile, t),
73+
),
74+
},
75+
},
76+
}, nil
77+
}
78+
79+
func TestApplicationAnomalies(t *testing.T) {
80+
test := &TestStruct{resourceKey: ResourceName}
81+
var err error
82+
var testCase *resource.TestCase
83+
if testCase, err = test.CreateTestCase(
84+
TestDataFolder+"/example_a.tf",
85+
TestDataFolder+"/example_a.json",
86+
t,
87+
); err != nil {
88+
t.Fatal(err)
89+
return
90+
}
91+
resource.Test(t, *testCase)
92+
}
93+
94+
func (test *TestStruct) URL(id string) string {
95+
envURL := testbase.TestAccProvider.Meta().(*config.ProviderConfiguration).DTenvURL
96+
reqPath := RequestPath
97+
return fmt.Sprintf(reqPath, envURL)
98+
}
99+
100+
func (test *TestStruct) CheckDestroy(s *terraform.State) error {
101+
return nil
102+
}
103+
104+
func CheckExists(n string, t *testing.T) resource.TestCheckFunc {
105+
return func(s *terraform.State) error {
106+
providerConf := testbase.TestAccProvider.Meta().(*config.ProviderConfiguration)
107+
restClient := applications.NewService(providerConf.DTenvURL, providerConf.APIToken)
108+
109+
if _, ok := s.RootModule().Resources[n]; ok {
110+
if _, err := restClient.Get(); err != nil {
111+
return err
112+
}
113+
return nil
114+
}
115+
116+
return fmt.Errorf("Not found: %s", n)
117+
}
118+
}

anomalies/applications/resource.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Dynatrace LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package applications
19+
20+
import (
21+
"context"
22+
23+
"github.com/dtcookie/dynatrace/api/config/anomalies/applications"
24+
"github.com/dtcookie/dynatrace/rest"
25+
"github.com/dtcookie/hcl"
26+
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
27+
"github.com/dynatrace-oss/terraform-provider-dynatrace/hcl2sdk"
28+
"github.com/dynatrace-oss/terraform-provider-dynatrace/logging"
29+
30+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
31+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
32+
)
33+
34+
// Resource produces terraform resource definition for Management Zones
35+
func Resource() *schema.Resource {
36+
return &schema.Resource{
37+
Schema: hcl2sdk.Convert(new(applications.AnomalyDetection).Schema()),
38+
CreateContext: logging.Enable(Create),
39+
UpdateContext: logging.Enable(Update),
40+
ReadContext: logging.Enable(Read),
41+
DeleteContext: logging.Enable(Delete),
42+
Importer: &schema.ResourceImporter{StateContext: schema.ImportStatePassthroughContext},
43+
}
44+
}
45+
46+
func NewService(m interface{}) *applications.Service {
47+
conf := m.(*config.ProviderConfiguration)
48+
apiService := applications.NewService(conf.DTenvURL, conf.APIToken)
49+
rest.Verbose = config.HTTPVerbose
50+
return apiService
51+
}
52+
53+
// Create expects the configuration within the given ResourceData and sends it to the Dynatrace Server in order to create that resource
54+
func Create(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
55+
d.SetId("dynatrace_application_anomalies")
56+
config := new(applications.AnomalyDetection)
57+
if err := config.UnmarshalHCL(hcl.DecoderFrom(d)); err != nil {
58+
return diag.FromErr(err)
59+
}
60+
if err := NewService(m).Update(config); err != nil {
61+
return diag.FromErr(err)
62+
}
63+
d.SetId("dynatrace_application_anomalies")
64+
return Read(ctx, d, m)
65+
}
66+
67+
// Update expects the configuration within the given ResourceData and send them to the Dynatrace Server in order to update that resource
68+
func Update(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
69+
config := new(applications.AnomalyDetection)
70+
if err := config.UnmarshalHCL(hcl.DecoderFrom(d)); err != nil {
71+
return diag.FromErr(err)
72+
}
73+
if err := NewService(m).Update(config); err != nil {
74+
return diag.FromErr(err)
75+
}
76+
return Read(ctx, d, m)
77+
}
78+
79+
// Read queries the Dynatrace Server for the configuration
80+
func Read(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
81+
config, err := NewService(m).Get()
82+
if err != nil {
83+
return diag.FromErr(err)
84+
}
85+
marshalled, err := config.MarshalHCL(hcl.DecoderFrom(d))
86+
if err != nil {
87+
return diag.FromErr(err)
88+
}
89+
for k, v := range marshalled {
90+
d.Set(k, v)
91+
}
92+
return diag.Diagnostics{}
93+
}
94+
95+
// Delete the configuration
96+
func Delete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
97+
return diag.Diagnostics{}
98+
}

0 commit comments

Comments
 (0)