/
data_source_plugins.go
63 lines (55 loc) · 1.75 KB
/
data_source_plugins.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
package nomad
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func dataSourcePlugins() *schema.Resource {
return &schema.Resource{
Read: pluginsDataSourceRead,
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Optional: true,
Description: "Volume Type (currently only 'csi')",
Default: "csi",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"csi"}, false),
},
},
"plugins": {
Description: "Registered plugins",
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeMap},
},
},
}
}
func pluginsDataSourceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(ProviderConfig).client
log.Printf("[DEBUG] Reading list of dynamic plugins from Nomad")
resp, _, err := client.CSIPlugins().List(nil)
if err != nil {
return fmt.Errorf("error reading plugins from Nomad: %s", err)
}
plugins := make([]map[string]interface{}, 0, len(resp))
for _, p := range resp {
plugin := map[string]interface{}{
"id": p.ID,
"provider": p.Provider,
"controller_required": strconv.FormatBool(p.ControllerRequired),
"controllers_healthy": strconv.Itoa(p.ControllersHealthy),
"controllers_expected": strconv.Itoa(p.ControllersExpected),
"nodes_healthy": strconv.Itoa(p.NodesHealthy),
"nodes_expected": strconv.Itoa(p.NodesExpected),
}
plugins = append(plugins, plugin)
}
log.Printf("[DEBUG] Finished reading plugins from Nomad")
d.SetId(client.Address() + "/v1/plugins")
return d.Set("plugins", plugins)
}