forked from rework-space-com/terraform-provider-freeipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_host_hostgroup_membership.go
188 lines (155 loc) · 4.65 KB
/
resource_host_hostgroup_membership.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"fmt"
"log"
"strings"
ipa "github.com/RomanButsiy/go-freeipa/freeipa"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceFreeIPAHostHostGroupMembership() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFreeIPAHostHostGroupMembershipCreate,
ReadContext: resourceFreeIPAHostHostGroupMembershipRead,
DeleteContext: resourceFreeIPAHostHostGroupMembershipDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Group name",
},
"host": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"hostgroup"},
Description: "Host to add",
},
"hostgroup": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"host"},
Description: "HostGroup to add",
},
},
}
}
func resourceFreeIPAHostHostGroupMembershipCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] Creating freeipa the host group membership")
client, err := meta.(*Config).Client()
if err != nil {
return diag.Errorf("Error creating freeipa identity client: %s", err)
}
host_id := "h"
optArgs := ipa.HostgroupAddMemberOptionalArgs{}
args := ipa.HostgroupAddMemberArgs{
Cn: d.Get("name").(string),
}
if _v, ok := d.GetOkExists("host"); ok {
v := []string{_v.(string)}
optArgs.Host = &v
host_id = "h"
}
if _v, ok := d.GetOkExists("hostgroup"); ok {
v := []string{_v.(string)}
optArgs.Hostgroup = &v
host_id = "hg"
}
_, err = client.HostgroupAddMember(&args, &optArgs)
if err != nil {
return diag.Errorf("Error creating freeipa the host group membership: %s", err)
}
switch host_id {
case "hg":
id := fmt.Sprintf("%s/hg/%s", d.Get("name").(string), d.Get("hostgroup").(string))
d.SetId(id)
case "h":
id := fmt.Sprintf("%s/h/%s", d.Get("name").(string), d.Get("host").(string))
d.SetId(id)
}
return resourceFreeIPAHostHostGroupMembershipRead(ctx, d, meta)
}
func resourceFreeIPAHostHostGroupMembershipRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] Read freeipa the host group membership")
name, typeId, hostId, err := parseHostMembershipID(d.Id())
if err != nil {
return diag.Errorf("Error parsing ID of freeipa_host_group_membership: %s", err)
}
client, err := meta.(*Config).Client()
if err != nil {
return diag.Errorf("Error creating freeipa identity client: %s", err)
}
optArgs := ipa.HostgroupFindOptionalArgs{
Cn: &name,
}
switch typeId {
case "hg":
v := []string{hostId}
optArgs.Hostgroup = &v
case "h":
v := []string{hostId}
optArgs.Host = &v
}
res, err := client.HostgroupFind("", &ipa.HostgroupFindArgs{}, &optArgs)
if err != nil {
return diag.Errorf("Error find freeipa the host group membership: %s", err)
}
if strings.Contains(*res.Summary, "0 groups matched") {
log.Printf("[DEBUG] Warning! Hostgroup or Host membership not exist")
d.Set("host", "")
d.Set("hostgroup", "")
d.SetId("")
}
return nil
}
func resourceFreeIPAHostHostGroupMembershipDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] Delete freeipa the host group membership")
client, err := meta.(*Config).Client()
if err != nil {
return diag.Errorf("Error creating freeipa identity client: %s", err)
}
optArgs := ipa.HostgroupRemoveMemberOptionalArgs{}
nameId, typeId, hostId, err := parseHostMembershipID(d.Id())
if err != nil {
return diag.Errorf("Error parsing ID of freeipa_user_group_membership: %s", err)
}
args := ipa.HostgroupRemoveMemberArgs{
Cn: nameId,
}
switch typeId {
case "hg":
v := []string{hostId}
optArgs.Hostgroup = &v
case "h":
v := []string{hostId}
optArgs.Host = &v
}
_, err = client.HostgroupRemoveMember(&args, &optArgs)
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
d.SetId("")
log.Printf("[DEBUG] Hostgroup not found")
return nil
} else {
return diag.Errorf("Error delete freeipa the host group membership: %s", err)
}
}
d.SetId("")
return nil
}
func parseHostMembershipID(id string) (string, string, string, error) {
idParts := strings.Split(id, "/")
if len(idParts) < 3 {
return "", "", "", fmt.Errorf("Unable to determine host membership ID %s", id)
}
name := idParts[0]
_type := idParts[1]
host := idParts[2]
return name, _type, host, nil
}