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_signing_key.go
126 lines (102 loc) · 3.43 KB
/
resource_signing_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
package form3
import (
"fmt"
"github.com/form3tech-oss/terraform-provider-form3/client/platformsecurityapi"
"log"
form3 "github.com/form3tech-oss/terraform-provider-form3/api"
"github.com/form3tech-oss/terraform-provider-form3/models"
"github.com/go-openapi/strfmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceForm3SigningKey() *schema.Resource {
return &schema.Resource{
Create: resourceSigningKeyCreate,
Read: resourceSigningKeyRead,
Delete: resourceSigningKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"signing_key_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"organisation_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceSigningKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
id := d.Get("signing_key_id").(string)
log.Printf("[INFO] Creating signing_key with id: %s", id)
signingKey, err := createSigningKeyFromResourceData(d)
if err != nil {
return err
}
log.Printf("[DEBUG] signing_key create: %#v", signingKey)
createdSigningKey, err := client.PlatformClient.Platformsecurityapi.PostPlatformSecuritySigningKeys(platformsecurityapi.NewPostPlatformSecuritySigningKeysParams().WithData(
&models.SigningKeysCreation{
Data: signingKey,
},
))
if err != nil {
return fmt.Errorf("failed to create signing_key: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId(createdSigningKey.Payload.Data.ID.String())
log.Printf("[INFO] signing_key id: %s", d.Id())
return resourceSigningKeyRead(d, meta)
}
func resourceSigningKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*form3.AuthenticatedClient)
key := d.Id()
signingKeyId, _ := GetUUIDOK(d, "signing_key_id")
if signingKeyId == "" {
signingKeyId = strfmt.UUID(key)
log.Printf("[INFO] Importing signing_key id: %s ", key)
} else {
log.Printf("[INFO] Reading signing_key id: %s", key)
}
signingKey, err := client.PlatformClient.Platformsecurityapi.GetPlatformSecuritySigningKeysSigningkeyID(
platformsecurityapi.NewGetPlatformSecuritySigningKeysSigningkeyIDParams().WithSigningkeyID(signingKeyId))
if err != nil {
if !form3.IsJsonErrorStatusCode(err, 404) {
return fmt.Errorf("couldn't find signing_key: %s", form3.JsonErrorPrettyPrint(err))
}
d.SetId("")
return nil
}
d.Set("id", signingKey.Payload.Data.ID.String())
return nil
}
func resourceSigningKeyUpdate(d *schema.ResourceData, meta interface{}) error {
d.Partial(false)
if d.HasChange("signing_key_id") {
return fmt.Errorf("error updating signing_key as they can not be changed")
}
return nil
}
func resourceSigningKeyDelete(d *schema.ResourceData, meta interface{}) error {
// you can't delete a signing key, just remove it from state?
log.Printf("[INFO] Deleting signing_key id: %s",
d.Get("id"))
return nil
}
func createSigningKeyFromResourceData(d *schema.ResourceData) (*models.SigningKeysRequestData, error) {
signingKey := models.SigningKeysRequestData{}
objectTtype := "signing_keys"
signingKey.Type = &objectTtype
if attr, ok := GetUUIDOK(d, "signing_key_id"); ok {
id := attr.String()
signingKey.ID = &id
}
if attr, ok := GetUUIDOK(d, "organisation_id"); ok {
orgId := attr.String()
signingKey.OrganisationID = &orgId
}
return &signingKey, nil
}