generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 43
/
add_user_to_group.go
123 lines (105 loc) · 3.2 KB
/
add_user_to_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package user
import (
"context"
"errors"
"fmt"
"log"
"strings"
"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 ResourceAddUserToGroup() *schema.Resource {
return &schema.Resource{
// This description is used by the documentation generator and the language server.
Description: "Resource for adding a user to a group.",
CreateContext: resourceAddUserToGroupCreate,
ReadContext: resourceAddUserToGroupRead,
DeleteContext: resourceAddUserToGroupDelete,
Schema: map[string]*schema.Schema{
"user_id": {
Description: "Unique identifier of the user.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"group_id": {
Description: "The name of the user.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
// <user_id>/<group_id>
parts := strings.Split(d.Id(), "/")
d.Set("user_id", parts[0])
d.Set("group_id", parts[1])
return []*schema.ResourceData{d}, nil
},
},
}
}
func resourceAddUserToGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
userId := d.Get("user_id").(string)
groupId := d.Get("group_id").(string)
log.Printf("[DEBUG] Check if user is already in group")
ok, err := c.UserClient.IsUserInGroup(userId, groupId)
if err != nil {
return diag.FromErr(err)
}
if !ok {
log.Printf("[DEBUG] User is not in group, adding user to group")
ok, err := c.UserClient.AddUserToGroup(userId, groupId)
if err != nil {
return diag.FromErr(err)
}
if !ok {
return diag.FromErr(errors.New("failed to add user to group"))
}
}
d.SetId(fmt.Sprintf("%s:%s", userId, groupId))
return nil
}
func resourceAddUserToGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
userId := d.Get("user_id").(string)
groupId := d.Get("group_id").(string)
ok, err := c.UserClient.IsUserInGroup(userId, groupId)
if err != nil {
return diag.FromErr(err)
}
if !ok {
log.Printf("[WARN] Removing from state because user %s is no longer in group %s", userId, groupId)
d.SetId("")
return nil
}
d.SetId(fmt.Sprintf("%s:%s", userId, groupId))
return nil
}
func resourceAddUserToGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*internal.Session).CDClient
if c == nil {
return diag.Errorf(utils.CDClientAPIKeyError)
}
userId := d.Get("user_id").(string)
groupId := d.Get("group_id").(string)
ok, err := c.UserClient.RemoveUserFromGroup(userId, groupId)
if err != nil {
return diag.FromErr(err)
}
if !ok {
return diag.FromErr(errors.New("failed to remove user from group"))
}
d.SetId("")
return nil
}