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_key.go
200 lines (167 loc) · 4.92 KB
/
resource_key.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
package form3
import (
"fmt"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/client/system"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func resourceForm3Key() *schema.Resource {
return &schema.Resource{
Create: resourceKeyCreate,
Read: resourceKeyRead,
Delete: resourceKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"key_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subject": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"certificate_signing_request": {
Type: schema.TypeString,
Computed: true,
Required: false,
ForceNew: true,
},
"private_key": {
Type: schema.TypeString,
Computed: true,
Required: false,
ForceNew: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
Required: false,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "RSA",
ForceNew: true,
},
"curve": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
log.Print("[INFO] Creating Key ")
certificateRequest, err := createKeyFromResourceData(d)
if err != nil {
return fmt.Errorf("failed to create Key: %s", err)
}
createdKey, err := client.SystemClient.System.PostKeys(
system.NewPostKeysParams().
WithKeyCreationRequest(&models.KeyCreation{
Data: certificateRequest,
}))
if err != nil {
return fmt.Errorf("failed to create Key: %s", err)
}
d.SetId(createdKey.Payload.Data.ID.String())
log.Printf("[INFO] Key key: %s", d.Id())
return resourceKeyRead(d, meta)
}
func resourceKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
keyId, _ := GetUUIDOK(d, "key_id")
if keyId == "" {
keyId = strfmt.UUID(d.Id())
log.Printf("[INFO] Importing key with resource id: %s.", keyId)
} else {
log.Printf("[INFO] Reading key with resource id: %s.", keyId)
}
response, err := client.SystemClient.System.GetKeysKeyID(
system.NewGetKeysKeyIDParams().WithKeyID(keyId))
if err != nil {
apiError, ok := err.(*runtime.APIError)
if ok && apiError.Code == 404 {
d.SetId("")
return nil
} else {
return err
}
}
d.Set("key_id", response.Payload.Data.ID.String())
d.Set("subject", response.Payload.Data.Attributes.Subject)
d.Set("organisation_id", response.Payload.Data.OrganisationID.String())
d.Set("certificate_signing_request", response.Payload.Data.Attributes.CertificateSigningRequest)
d.Set("public_key", response.Payload.Data.Attributes.PublicKey)
d.Set("private_key", response.Payload.Data.Attributes.PrivateKey)
d.Set("description", response.Payload.Data.Attributes.Description)
d.Set("type", response.Payload.Data.Attributes.Type)
d.Set("curve", response.Payload.Data.Attributes.Curve)
return nil
}
func resourceKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
response, err := client.SystemClient.System.GetKeysKeyID(
system.NewGetKeysKeyIDParams().WithKeyID(strfmt.UUID(d.Id())))
if err != nil {
return fmt.Errorf("error deleting Key: %s", err)
}
log.Printf("[INFO] Deleting Key for id: %s ", response.Payload.Data.ID)
_, err = client.SystemClient.System.DeleteKeysKeyID(
system.NewDeleteKeysKeyIDParams().
WithKeyID(response.Payload.Data.ID).
WithVersion(*response.Payload.Data.Version))
if err != nil {
return fmt.Errorf("error deleting Key: %s", err)
}
return nil
}
func createKeyFromResourceData(d *schema.ResourceData) (*models.Key, error) {
certificateRequest := &models.Key{
Type: "keys",
Attributes: &models.KeyAttributes{},
}
if attr, ok := GetUUIDOK(d, "key_id"); ok {
uuid := strfmt.UUID(attr.String())
certificateRequest.ID = uuid
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
uuid := strfmt.UUID(attr.String())
certificateRequest.OrganisationID = uuid
}
if attr, ok := d.GetOk("subject"); ok {
certificateRequest.Attributes.Subject = attr.(string)
}
if attr, ok := d.GetOk("description"); ok {
certificateRequest.Attributes.Description = attr.(string)
}
if attr, ok := d.GetOk("type"); ok {
keyType := attr.(string)
certificateRequest.Attributes.Type = &keyType
}
if attr, ok := d.GetOk("curve"); ok {
certificateRequest.Attributes.Curve = attr.(string)
}
return certificateRequest, nil
}