/
helpers.go
62 lines (52 loc) · 1.62 KB
/
helpers.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
package acctest
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/api"
"github.com/CheckPointSW/terraform-provider-infinity-next/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
const (
resourceNameLength = 6
)
var resourceNameChartSet = acctest.CharSetAlpha + strings.ToUpper(acctest.CharSetAlpha)
func GenerateResourceName() string {
return acctest.RandStringFromCharSet(resourceNameLength, resourceNameChartSet)
}
func MustReadFile(path string) string {
b, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return string(b)
}
func CheckResourceDestroyed(resourcesNames []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, resourceName := range resourcesNames {
resourceType, _, found := strings.Cut(resourceName, ".")
if !found {
return fmt.Errorf("resource name %s missing '.'", resourceName)
}
if rs, ok := s.RootModule().Resources[resourceName]; ok {
rd := schema.ResourceData{}
rd.SetId(rs.Primary.ID)
ctx := context.WithValue(context.Background(), utils.ExpectResourceNotFound, true)
diags := Provider.ResourcesMap[resourceType].ReadContext(ctx, &rd, Provider.Meta())
if diags.HasError() {
for _, d := range diags {
if !strings.Contains(d.Summary, api.ErrorNotFound.Error()) {
return errors.New(d.Summary)
}
}
}
}
}
return nil
}
}