-
Notifications
You must be signed in to change notification settings - Fork 51
/
resource_fusionauth_user_registration.go
244 lines (216 loc) · 8.16 KB
/
resource_fusionauth_user_registration.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package fusionauth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/FusionAuth/go-client/pkg/fusionauth"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func newRegistration() *schema.Resource {
return &schema.Resource{
CreateContext: createRegistration,
ReadContext: readRegistration,
UpdateContext: updateRegistration,
DeleteContext: deleteRegistration,
Schema: map[string]*schema.Schema{
"user_id": {
Type: schema.TypeString,
Required: true,
Description: "The Id of the User that is registering for the Application.",
ValidateFunc: validation.IsUUID,
ForceNew: true,
},
"authentication_token": {
Type: schema.TypeString,
Optional: true,
Description: "The authentication token that may be used in place of the User’s password when authenticating against this application represented by this registration. This parameter is ignored if generateAuthenticationToken is set to true and instead the value will be generated.",
ValidateFunc: validation.IsUUID,
Computed: true,
Sensitive: true,
},
"generate_authentication_token": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Determines if FusionAuth should generate an Authentication Token for this registration.",
},
"application_id": {
Type: schema.TypeString,
Required: true,
Description: "The Id of the Application that this registration is for.",
ValidateFunc: validation.IsUUID,
ForceNew: true,
},
"data": {
Type: schema.TypeMap,
Optional: true,
Description: "An object that can hold any information about the User for this registration that should be persisted.",
},
"preferred_languages": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "An array of locale strings that give, in order, the User’s preferred languages for this registration. These are important for email templates and other localizable text.",
},
"roles": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "The list of roles that the User has for this registration.",
},
"timezone": {
Type: schema.TypeString,
Optional: true,
Description: "The User’s preferred timezone for this registration. The string will be in an IANA time zone format.",
},
"username": {
Type: schema.TypeString,
Optional: true,
Description: "The username of the User for this registration only.",
},
"skip_registration_validation": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Indicates to FusionAuth that it should skip registration verification even if it is enabled for the Application.",
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func buildRegistration(data *schema.ResourceData) fusionauth.RegistrationRequest {
return fusionauth.RegistrationRequest{
Registration: fusionauth.UserRegistration{
ApplicationId: data.Get("application_id").(string),
Data: data.Get("data").(map[string]interface{}),
PreferredLanguages: handleStringSlice("preferred_languages", data),
Roles: handleStringSlice("roles", data),
Timezone: data.Get("timezone").(string),
Username: data.Get("username").(string),
},
GenerateAuthenticationToken: data.Get("generate_authentication_token").(bool),
SkipRegistrationVerification: data.Get("skip_registration_validation").(bool),
}
}
func createRegistration(_ context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
reg := struct {
Registration fusionauth.UserRegistration `json:"registration,omitempty"`
SkipRegistrationVerification bool `json:"skipRegistrationVerification"`
}{
Registration: fusionauth.UserRegistration{
ApplicationId: data.Get("application_id").(string),
AuthenticationToken: data.Get("authentication_token").(string),
Data: data.Get("data").(map[string]interface{}),
PreferredLanguages: handleStringSlice("preferred_languages", data),
Roles: handleStringSlice("roles", data),
Timezone: data.Get("timezone").(string),
Username: data.Get("username").(string),
},
SkipRegistrationVerification: data.Get("skip_registration_validation").(bool),
}
client := i.(Client)
b, _ := json.Marshal(reg)
b, err := sendCreateRegistration(b, data.Get("user_id").(string), data.Get("application_id").(string), client)
if err != nil {
return diag.Errorf("register err: %v", err)
}
_ = json.Unmarshal(b, ®)
data.SetId(reg.Registration.Id)
return buildResourceDataFromRegistration(reg.Registration, data)
}
func sendCreateRegistration(b []byte, uid string, aid string, client Client) ([]byte, error) {
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("%s/%s/%s/%s", strings.TrimRight(client.Host, "/"), "api/user/registration", uid, aid),
bytes.NewBuffer(b),
)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", client.APIKey)
req.Header.Add("Content-Type", "application/json")
hc := http.Client{
Timeout: 30 * time.Second,
}
resp, err := hc.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, _ = ioutil.ReadAll(resp.Body)
if err := checkResponse(resp.StatusCode, nil); err != nil {
return nil, err
}
return b, nil
}
func readRegistration(_ context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
client := i.(Client)
resp, faErrs, err := client.FAClient.RetrieveRegistration(data.Get("user_id").(string), data.Get("application_id").(string))
if err != nil {
return diag.Errorf("RetrieveRegistration err: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
data.SetId("")
return nil
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return diag.FromErr(err)
}
return buildResourceDataFromRegistration(resp.Registration, data)
}
func buildResourceDataFromRegistration(r fusionauth.UserRegistration, data *schema.ResourceData) diag.Diagnostics {
if err := data.Set("authentication_token", r.AuthenticationToken); err != nil {
return diag.Errorf("registration.authentication_token: %s", err.Error())
}
if err := data.Set("application_id", r.ApplicationId); err != nil {
return diag.Errorf("registration.application_id: %s", err.Error())
}
if err := data.Set("data", r.Data); err != nil {
return diag.Errorf("registration.data: %s", err.Error())
}
if err := data.Set("preferred_languages", r.PreferredLanguages); err != nil {
return diag.Errorf("registration.preferred_languages: %s", err.Error())
}
if err := data.Set("roles", r.Roles); err != nil {
return diag.Errorf("registration.roles: %s", err.Error())
}
if err := data.Set("timezone", r.Timezone); err != nil {
return diag.Errorf("registration.timezone: %s", err.Error())
}
if err := data.Set("username", r.Username); err != nil {
return diag.Errorf("registration.username: %s", err.Error())
}
return nil
}
func updateRegistration(_ context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
client := i.(Client)
ur := buildRegistration(data)
resp, faErrs, err := client.FAClient.UpdateRegistration(data.Get("user_id").(string), ur)
if err != nil {
return diag.Errorf("UpdateRegistration err: %v", err)
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return diag.FromErr(err)
}
return nil
}
func deleteRegistration(_ context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics {
client := i.(Client)
resp, faErrs, err := client.FAClient.DeleteRegistration(data.Get("user_id").(string), data.Get("application_id").(string))
if err != nil {
return diag.FromErr(err)
}
if err := checkResponse(resp.StatusCode, faErrs); err != nil {
return diag.FromErr(err)
}
return nil
}