/
sweepers.go
55 lines (44 loc) · 1.28 KB
/
sweepers.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
package acceptance
import (
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/linode/helper"
)
func TestMain(m *testing.M) {
resource.TestMain(m)
}
func GetClientForSweepers() (*linodego.Client, error) {
token := os.Getenv("LINODE_TOKEN")
if token == "" {
return nil, fmt.Errorf("LINODE_TOKEN must be set for acceptance tests")
}
config := &helper.Config{AccessToken: token, APIVersion: "v4beta"}
client, err := config.Client()
if err != nil {
return nil, err
}
return client, nil
}
func SweeperListOptions(prefix, field string) *linodego.ListOptions {
filterFmt := "{ %q : {\"+contains\": %q }}"
filter := fmt.Sprintf(filterFmt, field, prefix)
listOpts := linodego.NewListOptions(0, filter)
return listOpts
}
func ShouldSweep(prefix, name string) bool {
loweredName := strings.ToLower(name)
if len(prefix) < 3 {
log.Printf("Ignoring Resource %q because sweeper prefix is too short %q", name, prefix)
return false
}
if !strings.HasPrefix(loweredName, prefix) && !strings.HasPrefix(loweredName, "renamed-"+prefix) {
log.Printf("Ignoring Resource %q as it doesn't start with `(renamed-)?%s`", name, prefix)
return false
}
return true
}