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_publickey.go
149 lines (121 loc) · 4.54 KB
/
resource_credential_publickey.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
package form3
import (
"fmt"
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/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"strings"
)
func resourceForm3CredentialPublicKey() *schema.Resource {
return &schema.Resource{
Create: resourceCredentialPublicKeyCreate,
Read: resourceCredentialPublicKeyRead,
Delete: resourceCredentialPublicKeyDelete,
Importer: &schema.ResourceImporter{
State: resourceCredentialPublicKeyImport,
},
Schema: map[string]*schema.Schema{
"user_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceCredentialPublicKeyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("public key import id must be in form '<userId>/<publicKeyId>'")
}
d.SetId(parts[1])
d.Set("user_id", parts[0])
return []*schema.ResourceData{d}, nil
}
func resourceCredentialPublicKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
userID, _ := GetUUIDOK(d, "user_id")
log.Printf("[INFO] Creating credential public key for user id: %s", userID.String())
publicKey, err := createCredentialPublicKeyFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] credential public key create: %#v", publicKey)
createKeyParams := users.NewPostUsersUserIDCredentialsPublicKeyParams().WithUserID(userID).WithPublicKey(publicKey)
createdPublicKey, err := client.SecurityClient.Users.PostUsersUserIDCredentialsPublicKey(createKeyParams)
if err != nil {
return fmt.Errorf("failed to create credential public key with id: %s error: %s", publicKey.ID, err)
}
d.SetId(createdPublicKey.Payload.Data.ID.String())
log.Printf("[INFO] Credential public key with id: %s created", d.Id())
return nil
}
func resourceCredentialPublicKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
keyID := d.Id()
userID, _ := GetUUIDOK(d, "user_id")
log.Printf("[INFO] Reading credential public key for id: %s", keyID)
getKeyParams := users.NewGetUsersUserIDCredentialsPublicKeyPublicKeyIDParams().WithUserID(userID).WithPublicKeyID(strfmt.UUID(keyID))
publicKey, err := client.SecurityClient.Users.GetUsersUserIDCredentialsPublicKeyPublicKeyID(getKeyParams)
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("couldn't find credential public key for user %s with id:%s error %s", userID, keyID, err)
}
d.SetId(publicKey.Payload.ID.String())
d.Set("user_id", userID)
d.Set("public_key_id", publicKey.Payload.ID)
d.Set("organisation_id", publicKey.Payload.OrganisationID)
d.Set("public_key", publicKey.Payload.Attributes.PublicKey)
return nil
}
func resourceCredentialPublicKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
publicKey, err := createCredentialPublicKeyFromResourceData(d)
if err != nil {
return fmt.Errorf("error deleting credential public key: %s", err)
}
userID, _ := GetUUIDOK(d, "user_id")
log.Printf("[INFO] Deleting credential public key id: %s for user: %s", publicKey.ID.String(), userID.String())
_, err = client.SecurityClient.Users.DeleteUsersUserIDCredentialsPublicKeyPublicKeyID(users.NewDeleteUsersUserIDCredentialsPublicKeyPublicKeyIDParams().
WithUserID(userID).WithPublicKeyID(publicKey.ID))
if err != nil {
return fmt.Errorf("error deleting credential public key: %s", err)
}
return nil
}
func createCredentialPublicKeyFromResourceData(d *schema.ResourceData) (*models.PublicKey, error) {
publicKey := models.PublicKey{Attributes: &models.PublicKeyAttributes{}}
if attr, ok := GetUUIDOK(d, "public_key_id"); ok {
publicKey.ID = attr
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
publicKey.OrganisationID = attr
}
if attr, ok := d.GetOk("public_key"); ok {
publicKey.Attributes.PublicKey = attr.(string)
}
return &publicKey, nil
}