/
dataSource_group.go
91 lines (85 loc) · 2.25 KB
/
dataSource_group.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
package provider
import (
"context"
"github.com/devoteamgcloud/terraform-provider-looker/pkg/lookergo"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var (
queryKey = []string{
"id",
"name",
}
)
func dataSourceGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "Search group based on id.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: queryKey,
},
"name": {
Description: "Search group based on name.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: queryKey,
},
"user_count": {
Type: schema.TypeInt,
Computed: true,
},
"parent_groups": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"roles": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
c := m.(*Config).Api // .(*lookergo.Client)
tflog.Info(ctx, "Querying Looker Group")
var group = lookergo.Group{}
if groupIDKey, exists := d.GetOk("id"); exists { // Query using ID
groups, _, err := c.Groups.ListById(ctx, []int{idAsInt(groupIDKey)}, nil)
if err != nil {
return diag.FromErr(err)
}
if len(groups) == 1 {
group = groups[0]
} else {
return diag.Errorf("No results found for: %v", groupIDKey)
}
} else if groupNameKey, exists := d.GetOk("name"); exists { // Query using Name
groups, _, err := c.Groups.ListByName(ctx, groupNameKey.(string), nil)
if err != nil {
return diag.FromErr(err)
}
if len(groups) == 1 {
group = groups[0]
} else {
return diag.Errorf("No results found for: %v", groupNameKey)
}
} else {
return diag.Errorf("Neither name, nor id provided.")
}
d.SetId(idAsString(group.Id))
d.Set("name", group.Name)
d.Set("user_count", group.UserCount)
d.Set("parent_groups", group.ParentGroupIds.ToSliceOfStrings())
d.Set("roles", group.RoleIds.ToSliceOfStrings())
return diags
}