-
Notifications
You must be signed in to change notification settings - Fork 117
/
resource_bigip_ssl_key.go
169 lines (159 loc) · 4.67 KB
/
resource_bigip_ssl_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
package bigip
import (
"context"
"errors"
"fmt"
"log"
"strings"
bigip "github.com/f5devcentral/go-bigip"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceBigipSslKey() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBigipSslKeyCreate,
ReadContext: resourceBigipSslKeyRead,
UpdateContext: resourceBigipSslKeyUpdate,
DeleteContext: resourceBigipSslKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of SSL Certificate key with .key extension",
ForceNew: true,
},
"content": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
//ForceNew: true,
Description: "Content of SSL certificate key present on local Disk",
},
"passphrase": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Passphrase on key.",
},
"partition": {
Type: schema.TypeString,
Optional: true,
Default: "Common",
Description: "Partition of ssl certificate key",
ValidateFunc: validatePartitionName,
},
"full_path": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Full Path Name of ssl key",
},
},
}
}
func resourceBigipSslKeyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Get("name").(string)
log.Println("[INFO] Certificate Key Name " + name)
certpath := d.Get("content").(string)
partition := d.Get("partition").(string)
passPhrase := d.Get("passphrase").(string)
/*if !strings.HasSuffix(name, ".key") {
name = name + ".key"
}*/
sourcePath, err := client.UploadKey(name, certpath)
if err != nil {
return diag.FromErr(fmt.Errorf("error in Uploading certificate key (%s): %s", name, err))
}
certkey := bigip.Key{
Name: name,
SourcePath: sourcePath,
Partition: partition,
Passphrase: passPhrase,
}
log.Printf("[DEBUG] certkey: %+v\n", certkey)
err = client.AddKey(&certkey)
if err != nil {
return diag.FromErr(err)
}
d.SetId(name)
return resourceBigipSslKeyRead(ctx, d, meta)
}
func resourceBigipSslKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Reading Certificate key: " + name)
/*if !strings.HasSuffix(name, ".key") {
name = name + ".key"
}*/
partition := d.Get("partition").(string)
if partition == "" {
if !strings.HasPrefix(name, "/") {
err := errors.New("the name must be in full_path format when partition is not specified")
fmt.Print(err)
}
} else {
if !strings.HasPrefix(name, "/") {
name = "/" + partition + "/" + name
}
}
certkey, err := client.GetKey(name)
if err != nil {
return diag.FromErr(err)
}
if certkey == nil {
return diag.Errorf("Reading Certificate key failed with key:%v", certkey)
}
log.Printf("[INFO] SSL key content:%+v", certkey)
_ = d.Set("name", certkey.Name)
_ = d.Set("partition", certkey.Partition)
_ = d.Set("full_path", certkey.FullPath)
return nil
}
func resourceBigipSslKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Certificate key Name " + name)
certpath := d.Get("content").(string)
/*if !strings.HasSuffix(name, ".key") {
name = name + ".key"
}*/
partition := d.Get("partition").(string)
passPhrase := d.Get("passphrase").(string)
sourcePath, err := client.UploadKey(name, certpath)
if err != nil {
return diag.FromErr(fmt.Errorf("error in Uploading certificate key (%s): %s", name, err))
}
certkey := bigip.Key{
Name: name,
SourcePath: sourcePath,
Partition: partition,
Passphrase: passPhrase,
}
keyName := fmt.Sprintf("/%s/%s", partition, name)
err = client.ModifyKey(keyName, &certkey)
if err != nil {
return diag.FromErr(err)
}
return resourceBigipSslKeyRead(ctx, d, meta)
}
func resourceBigipSslKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Deleting Certificate key" + name)
/*if !strings.HasSuffix(name, ".key") {
name = name + ".key"
}*/
partition := d.Get("partition").(string)
name = "/" + partition + "/" + name
err := client.DeleteKey(name)
if err != nil {
log.Printf("[ERROR] Unable to Delete Pool (%s) (%v) ", name, err)
return diag.FromErr(err)
}
d.SetId("")
return nil
}