This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_credential.go
130 lines (102 loc) · 3.53 KB
/
resource_credential.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
package form3
import (
"fmt"
"log"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/users"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceForm3Credential() *schema.Resource {
return &schema.Resource{
Create: resourceCredentialCreate,
Read: resourceCredentialRead,
Delete: resourceCredentialDelete,
Schema: map[string]*schema.Schema{
"user_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"client_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"client_secret": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func resourceCredentialCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
userId, _ := GetUUIDOK(d, "user_id")
log.Printf("[INFO] Creating credential for user id: %s", userId.String())
credential, err := createCredentialFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] credential create: %#v", credential)
createdCredential, err := client.SecurityClient.Users.PostUsersUserIDCredentials(users.NewPostUsersUserIDCredentialsParams().
WithUserID(userId))
if err != nil {
return fmt.Errorf("failed to create credential: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdCredential.Payload.Data.ClientID.String())
log.Printf("[INFO] credential id: %s", d.Id())
d.Set("client_id", createdCredential.Payload.Data.ClientID.String())
d.Set("client_secret", createdCredential.Payload.Data.ClientSecret)
return nil
}
func resourceCredentialRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
userId, _ := GetUUIDOK(d, "user_id")
log.Printf("[INFO] Reading credential for client id: %s", key)
credentials, err := client.SecurityClient.Users.GetUsersUserIDCredentials(users.NewGetUsersUserIDCredentialsParams().
WithUserID(userId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, 404) {
return fmt.Errorf("couldn't find credential: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
for _, element := range credentials.Payload.Data {
if element.ClientID.String() == key {
d.Set("user_id", userId.String())
d.Set("client_id", key)
return nil
}
}
d.SetId("")
return nil
}
func resourceCredentialDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
credential, err := createCredentialFromResourceData(d)
if err != nil {
return fmt.Errorf("error deleting credential: %s", form3.JsonErrorPrettyPrint(err))
}
userId, _ := GetUUIDOK(d, "user_id")
log.Printf("[INFO] Deleting credtial id: %s for user: %s", credential.ClientID.String(), userId.String())
_, err = client.SecurityClient.Users.DeleteUsersUserIDCredentialsClientID(users.NewDeleteUsersUserIDCredentialsClientIDParams().
WithUserID(userId).
WithClientID(credential.ClientID))
if err != nil {
return fmt.Errorf("error deleting credential: %s", form3.JsonErrorPrettyPrint(err))
}
return nil
}
func createCredentialFromResourceData(d *schema.ResourceData) (*models.CredentialSecret, error) {
credential := &models.CredentialSecret{}
if attr, ok := GetUUIDOK(d, "client_id"); ok {
credential.ClientID = attr
}
if attr, ok := d.GetOk("client_secret"); ok {
credential.ClientSecret = attr.(string)
}
return credential, nil
}