forked from whitepages/terraform-provider-stingray
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_ssl_server_key.go
138 lines (111 loc) · 3.04 KB
/
resource_ssl_server_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
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/atlassian/go-vtm"
"github.com/hashicorp/terraform/helper/schema"
"log"
)
func resourceSSLServerKey() *schema.Resource {
return &schema.Resource{
Create: resourceSSLServerKeyCreate,
Read: resourceSSLServerKeyRead,
Update: resourceSSLServerKeyUpdate,
Delete: resourceSSLServerKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"note": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"private": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: diffCurrentAndRemoteSSLKey,
},
"public": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"request": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func diffCurrentAndRemoteSSLKey(k, old, new string, d *schema.ResourceData) bool {
shaOfContent := sha256.Sum256([]byte(new))
base64Content := base64.StdEncoding.EncodeToString(shaOfContent[:])
log.Printf("[DEBUG] [%s] %s == %s is %v", d.Id(), base64Content, old, base64Content == old)
return base64Content == old
}
func resourceSSLServerKeyCreate(d *schema.ResourceData, meta interface{}) error {
err := resourceSSLServerKeySet(d, meta)
if err != nil {
return err
}
return resourceSSLServerKeyRead(d, meta)
}
func resourceSSLServerKeyRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r, resp, err := c.GetSSLServerKey(d.Id())
if err != nil {
if resp != nil && resp.StatusCode == 404 {
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error reading resource: %s", err)
}
if d.Get("name") == nil {
d.Set("name", d.Id())
}
d.Set("note", string(*r.Basic.Note))
d.Set("private", string(*r.Basic.Private))
d.Set("public", string(*r.Basic.Public))
d.Set("request", string(*r.Basic.Request))
return nil
}
func resourceSSLServerKeyUpdate(d *schema.ResourceData, meta interface{}) error {
err := resourceSSLServerKeySet(d, meta)
if err != nil {
return err
}
return resourceSSLServerKeyRead(d, meta)
}
func resourceSSLServerKeyDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r := stingray.NewSSLServerKey(d.Id())
_, err := c.Delete(r)
if err != nil {
return err
}
return nil
}
func resourceSSLServerKeySet(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r := stingray.NewSSLServerKey(d.Get("name").(string))
setString(&r.Basic.Note, d, "note")
setString(&r.Basic.Private, d, "private")
setString(&r.Basic.Public, d, "public")
setString(&r.Basic.Request, d, "request")
_, err := c.Set(r)
if err != nil {
return err
}
d.SetId(d.Get("name").(string))
return nil
}