-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_tokens.go
44 lines (40 loc) · 1.17 KB
/
data_source_tokens.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
package main
import (
"fmt"
"time"
"github.com/hashicorp/terraform/helper/schema"
scalyr "github.com/scalyr/terraform-provider-dataset/scalyr-go"
)
func datasourceTokens() *schema.Resource {
return &schema.Resource{
Read: datasourceTokenRead,
Schema: map[string]*schema.Schema{
"tokens": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeMap, Elem: &schema.Schema{Type: schema.TypeString}},
},
},
}
}
func datasourceTokenRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*scalyr.ScalyrConfig)
tokens, err := client.ListTokens()
tfTokens := make([]map[string]string, len(*tokens))
for i, token := range *tokens {
tfTokens[i] = make(map[string]string)
tfTokens[i]["creator"] = token.Creator
tfTokens[i]["permission"] = token.Permission
tfTokens[i]["id"] = token.ID
tfTokens[i]["label"] = token.Label
tfTokens[i]["create_date"] = token.CreateDate.String()
}
if err != nil {
return fmt.Errorf("Error retrieving tokens: %s", err)
}
if err := d.Set("tokens", tfTokens); err != nil {
return fmt.Errorf("Error setting tokens: %s", err)
}
d.SetId(time.Now().UTC().String())
return nil
}