generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 40
/
user_group_data_source.go
78 lines (67 loc) · 2.12 KB
/
user_group_data_source.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
package user
import (
"context"
"strings"
"github.com/harness/harness-go-sdk/harness/cd/graphql"
"github.com/harness/terraform-provider-harness/internal"
"github.com/harness/terraform-provider-harness/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceUserGroup() *schema.Resource {
return &schema.Resource{
// This description is used by the documentation generator and the language server.
Description: "Data source for retrieving a Harness user group",
ReadContext: dataSourceUserGroupRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "Unique identifier of the user group",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "name"},
ConflictsWith: []string{"name"},
},
"name": {
Description: "The name of the user group.",
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"id", "name"},
ConflictsWith: []string{"id"},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.EqualFold(old, new)
},
},
},
}
}
func dataSourceUserGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// use the meta value to retrieve your client from the provider configure method
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
var userGroup *graphql.UserGroup
var err error
if id := d.Get("id").(string); id != "" {
// Try lookup by Id first
userGroup, err = c.UserClient.GetUserGroupById(id)
if err != nil {
return diag.FromErr(err)
}
} else if name := d.Get("name").(string); name != "" {
// Fallback to lookup by name
userGroup, err = c.UserClient.GetUserGroupByName(name)
if err != nil {
return diag.FromErr(err)
}
} else {
// Throw error if neither are set
return diag.Errorf("id or name must be set")
}
if userGroup == nil {
return diag.Errorf("user group not found")
}
d.SetId(userGroup.Id)
d.Set("name", userGroup.Name)
return nil
}