forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exists.go
49 lines (39 loc) · 1.57 KB
/
exists.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
package helpers
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/acceptance/types"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
)
func DoesNotExistInAzure(client *clients.Client, testResource types.TestResource, resourceName string) pluginsdk.TestCheckFunc {
return existsFunc(false)(client, testResource, resourceName)
}
func ExistsInAzure(client *clients.Client, testResource types.TestResource, resourceName string) pluginsdk.TestCheckFunc {
return existsFunc(true)(client, testResource, resourceName)
}
func existsFunc(shouldExist bool) func(*clients.Client, types.TestResource, string) pluginsdk.TestCheckFunc {
return func(client *clients.Client, testResource types.TestResource, resourceName string) pluginsdk.TestCheckFunc {
return func(s *terraform.State) error {
ctx := client.StopContext
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("%q was not found in the state", resourceName)
}
result, err := testResource.Exists(ctx, client, rs.Primary)
if err != nil {
return fmt.Errorf("running exists func for %q: %+v", resourceName, err)
}
if result == nil {
return fmt.Errorf("received nil for exists for %q", resourceName)
}
if *result != shouldExist {
if !shouldExist {
return fmt.Errorf("%q still exists", resourceName)
}
return fmt.Errorf("%q did not exist", resourceName)
}
return nil
}
}
}