Skip to content

Commit 7afda5d

Browse files
introduced dynatrace_autotag and dynatrace_notification
1 parent 59e8d62 commit 7afda5d

36 files changed

+1684
-97
lines changed

alerting_profile_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/**
22
* @license
33
* Copyright 2020 Dynatrace LLC
4-
*
4+
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
8-
*
8+
*
99
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
10+
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
16-
*/
17-
16+
*/
1817

1918
package main_test
2019

@@ -24,8 +23,9 @@ import (
2423
"strings"
2524
"testing"
2625

26+
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
27+
2728
"github.com/dtcookie/dynatrace/api/config/alertingprofiles"
28-
"github.com/dtcookie/terraform-provider-dynatrace/config"
2929
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
3030
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
3131
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

alerting_profiles.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/**
22
* @license
33
* Copyright 2020 Dynatrace LLC
4-
*
4+
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
8-
*
8+
*
99
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
10+
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
16-
*/
17-
16+
*/
1817

1918
package main
2019

@@ -23,13 +22,14 @@ import (
2322
"log"
2423
"reflect"
2524

25+
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
26+
"github.com/dynatrace-oss/terraform-provider-dynatrace/logging"
27+
2628
api "github.com/dtcookie/dynatrace/api/config"
2729
"github.com/dtcookie/dynatrace/api/config/alertingprofiles"
2830
"github.com/dtcookie/dynatrace/rest"
2931
"github.com/dtcookie/dynatrace/terraform"
3032
"github.com/dtcookie/opt"
31-
"github.com/dtcookie/terraform-provider-dynatrace/config"
32-
"github.com/dtcookie/terraform-provider-dynatrace/logging"
3333
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
3434
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3535
)

auto_tag_exporter.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 main
19+
20+
import (
21+
"io"
22+
23+
"github.com/dtcookie/dynatrace/api/config/autotags"
24+
"github.com/dtcookie/dynatrace/terraform"
25+
)
26+
27+
// AutoTagExporter has no documentation
28+
type AutoTagExporter struct {
29+
AutoTag *autotags.AutoTag
30+
}
31+
32+
// ToHCL has no documentation
33+
func (exporter *AutoTagExporter) ToHCL(w io.Writer) error {
34+
bytes, err := terraform.Marshal(exporter.AutoTag, "dynatrace_autotag", exporter.AutoTag.Name)
35+
if err != nil {
36+
return err
37+
}
38+
_, err = w.Write(bytes)
39+
return err
40+
}
41+
42+
// ToJSON has no documentation
43+
func (exporter *AutoTagExporter) ToJSON(w io.Writer) error {
44+
bytes, err := terraform.MarshalJSON(exporter.AutoTag, "dynatrace_autotag", exporter.AutoTag.Name)
45+
if err != nil {
46+
return err
47+
}
48+
_, err = w.Write(bytes)
49+
return err
50+
}

auto_tag_test.go

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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 main_test
19+
20+
import (
21+
"fmt"
22+
"io/ioutil"
23+
"strings"
24+
"testing"
25+
26+
"github.com/dynatrace-oss/terraform-provider-dynatrace/config"
27+
28+
"github.com/dtcookie/dynatrace/api/config/autotags"
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+
type AutoTagTest struct {
35+
resourceKey string
36+
testDataFld string
37+
reqPath string
38+
display string
39+
}
40+
41+
func NewAutoTagTest() *AutoTagTest {
42+
return &AutoTagTest{
43+
resourceKey: "dynatrace_autotag",
44+
testDataFld: "auto_tags",
45+
reqPath: "autoTags",
46+
display: "Auto Tag",
47+
}
48+
}
49+
50+
func (test *AutoTagTest) Anonymize(m map[string]interface{}) {
51+
delete(m, "id")
52+
delete(m, "name")
53+
delete(m, "metadata")
54+
}
55+
56+
func (test *AutoTagTest) ResourceKey() string {
57+
return test.resourceKey
58+
}
59+
60+
func (test *AutoTagTest) CreateTestCase(file string, localJSONFile string, t *testing.T) (*resource.TestCase, error) {
61+
var content []byte
62+
var err error
63+
if content, err = ioutil.ReadFile(file); err != nil {
64+
return nil, err
65+
}
66+
config := string(content)
67+
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
68+
resourceName := test.ResourceKey() + "." + name
69+
config = strings.ReplaceAll(config, "#name#", name)
70+
return &resource.TestCase{
71+
PreCheck: func() { testAccPreCheck(t) },
72+
IDRefreshName: resourceName,
73+
ProviderFactories: testAccProviderFactories,
74+
CheckDestroy: test.CheckDestroy,
75+
Steps: []resource.TestStep{
76+
{
77+
Config: config,
78+
Check: resource.ComposeAggregateTestCheckFunc(
79+
test.CheckExists(resourceName, t),
80+
compareLocalRemote(test, resourceName, localJSONFile, t),
81+
),
82+
},
83+
},
84+
}, nil
85+
}
86+
87+
func TestAccAutoTagExampleA(t *testing.T) {
88+
test := NewAutoTagTest()
89+
var err error
90+
var testCase *resource.TestCase
91+
if testCase, err = test.CreateTestCase(
92+
fmt.Sprintf("test_data/%s/example_a.tf", test.testDataFld),
93+
fmt.Sprintf("test_data/%s/example_a.json", test.testDataFld),
94+
t,
95+
); err != nil {
96+
t.Fatal(err)
97+
return
98+
}
99+
resource.Test(t, *testCase)
100+
}
101+
102+
func (test *AutoTagTest) URL(id string) string {
103+
envURL := testAccProvider.Meta().(*config.ProviderConfiguration).DTenvURL
104+
reqPath := "%s/" + test.reqPath + "/%s"
105+
return fmt.Sprintf(reqPath, envURL, id)
106+
}
107+
108+
func (test *AutoTagTest) CheckDestroy(s *terraform.State) error {
109+
providerConf := testAccProvider.Meta().(*config.ProviderConfiguration)
110+
restClient := autotags.NewService(providerConf.DTenvURL, providerConf.APIToken)
111+
112+
for _, rs := range s.RootModule().Resources {
113+
if rs.Type != test.resourceKey {
114+
continue
115+
}
116+
117+
id := rs.Primary.ID
118+
119+
if _, err := restClient.Get(id); err != nil {
120+
// HTTP Response "404 Not Found" signals a success
121+
if strings.Contains(err.Error(), `"code": 404`) {
122+
return nil
123+
}
124+
// any other error should fail the test
125+
return err
126+
}
127+
return fmt.Errorf("%s still exists: %s", test.display, rs.Primary.ID)
128+
}
129+
130+
return nil
131+
}
132+
133+
func (test *AutoTagTest) CheckExists(n string, t *testing.T) resource.TestCheckFunc {
134+
return func(s *terraform.State) error {
135+
providerConf := testAccProvider.Meta().(*config.ProviderConfiguration)
136+
restClient := autotags.NewService(providerConf.DTenvURL, providerConf.APIToken)
137+
138+
if rs, ok := s.RootModule().Resources[n]; ok {
139+
if _, err := restClient.Get(rs.Primary.ID); err != nil {
140+
return err
141+
}
142+
return nil
143+
}
144+
145+
return fmt.Errorf("Not found: %s", n)
146+
}
147+
}

0 commit comments

Comments
 (0)