generated from davidji99/terraform-provider-scaffolding
/
data_source_herokux_space_apps.go
114 lines (94 loc) · 2.61 KB
/
data_source_herokux_space_apps.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package herokux
import (
"context"
heroku "github.com/davidji99/heroku-go/v5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
"regexp"
"time"
)
func dataSourceHerokuxSpaceApps() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceHerokuxSpaceAppsRead,
Schema: map[string]*schema.Schema{
"space_regex": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsValidRegExp,
},
"apps": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"web_url": {
Type: schema.TypeString,
Computed: true,
},
"stack": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceHerokuxSpaceAppsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
var appsFiltered []heroku.App
platformAPI := meta.(*Config).PlatformAPI
spaceRegex := d.Get("space_regex").(string)
log.Printf("[DEBUG] space_regex: %s", spaceRegex)
// Return all addons that the authenticated token can access
// TODO: figure out a way to paginate.
allApps, appsListErr := platformAPI.AppList(ctx, &heroku.ListRange{Max: 1000, Field: "id"})
if appsListErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to retrieve apps",
Detail: appsListErr.Error(),
})
return diags
}
regex := regexp.MustCompile(spaceRegex)
log.Printf("[DEBUG] Found %d apps", len(allApps))
for _, a := range allApps {
if a.Space == nil {
continue
}
if regex.MatchString(a.Space.ID) || regex.MatchString(a.Space.Name) {
appsFiltered = append(appsFiltered, a)
}
}
log.Printf("[DEBUG] Number of apps after filtering by space name: %d", len(appsFiltered))
// Set data source to be a random ID given this data source encompasses multiple appsFiltered.
d.SetId(time.Now().String())
var apps []map[string]string
for _, app := range appsFiltered {
apps = append(apps, map[string]string{
"id": app.ID,
"name": app.Name,
"web_url": app.WebURL,
"stack": app.Stack.Name,
"region": app.Region.Name,
})
}
d.Set("apps", apps)
return nil
}