/
resource_file_ca_cert.go
148 lines (141 loc) · 4.93 KB
/
resource_file_ca_cert.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
package thunder
import (
"context"
"github.com/a10networks/terraform-provider-thunder/thunder/axapi"
"github.com/a10networks/terraform-provider-thunder/thunder/axapi/file"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
// Not direct mapping to any axapi endpoint
func resourceFileCaCert() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFileCaCertCreate,
ReadContext: resourceFileCaCertRead,
UpdateContext: resourceFileCaCertUpdate,
DeleteContext: resourceFileCaCertDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString, Required: true, ForceNew: true, Description: "Local file name",
ValidateFunc: validation.StringLenBetween(1, 63),
},
"protocol": {
Type: schema.TypeString, Required: true, Description: "Transfer protocol",
ValidateFunc: validation.StringInSlice([]string{"ftp", "http", "https", "scp", "sftp", "tftp"}, false),
},
"host": {
Type: schema.TypeString, Required: true, Description: "Remote site (IP or domain name)",
ValidateFunc: validation.StringLenBetween(1, 63),
},
"path": {
Type: schema.TypeString, Required: true, Description: "Remote path",
ValidateFunc: axapi.IsAbsolutePath,
},
"username": {
Type: schema.TypeString, Optional: true, Description: "Username for the remote site",
ValidateFunc: validation.StringLenBetween(1, 63),
},
"password": {
Type: schema.TypeString, Optional: true, Description: "Password for the remote site",
ValidateFunc: validation.StringLenBetween(1, 128),
},
"use_mgmt_port": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Use management port as source port",
ValidateFunc: validation.IntBetween(0, 1),
},
"overwrite": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Overwrite existing file",
ValidateFunc: validation.IntBetween(0, 1),
},
"certificate_type": {
Type: schema.TypeString, Optional: true, Default: "pem", Description: "Certificate format",
ValidateFunc: validation.StringInSlice([]string{"pem", "der", "p7b", "pfx"}, false),
},
"pfx_password": {
Type: schema.TypeString, Optional: true, Description: "Password for pfx format",
ValidateFunc: validation.StringLenBetween(1, 128),
},
"secured": {
Type: schema.TypeInt, Optional: true, Default: 0, Description: "Mark as non-exportable (for pfx format)",
ValidateFunc: validation.IntBetween(0, 1),
},
},
}
}
func resourceFileCaCertCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceFileCaCertCreate()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToFileCaCert(d)
d.SetId(obj.GetId())
err := obj.Post(client.Token, client.Host, logger)
if err != nil {
return diag.FromErr(err)
}
return resourceFileCaCertRead(ctx, d, meta)
}
return diags
}
func resourceFileCaCertRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceFileCaCertRead()")
var diags diag.Diagnostics
if client.Host != "" {
obj := file.CaCert{}
err := obj.Get(client.Token, client.Host, d.Id(), logger)
if err != nil {
return diag.FromErr(err)
}
}
return diags
}
func resourceFileCaCertUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceFileCaCertUpdate()")
var diags diag.Diagnostics
if client.Host != "" {
obj := dataToFileCaCert(d)
//file.CaCert is immutable. Overwrite it.
obj.Overwrite = 1
err := obj.Post(client.Token, client.Host, logger)
if err != nil {
return diag.FromErr(err)
}
return resourceFileCaCertRead(ctx, d, meta)
}
return diags
}
func resourceFileCaCertDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(Thunder)
logger := client.log
logger.Println("resourceFileCaCertDelete()")
var diags diag.Diagnostics
if client.Host != "" {
obj := file.CaCert{}
obj.CertificateType = d.Get("certificate_type").(string)
err := obj.Delete(client.Token, client.Host, d.Id(), logger)
if err != nil {
return diag.FromErr(err)
}
}
return diags
}
func dataToFileCaCert(d *schema.ResourceData) file.CaCert {
var ret file.CaCert
ret.Name = d.Get("name").(string)
ret.Protocol = d.Get("protocol").(string)
ret.Host = d.Get("host").(string)
ret.Path = d.Get("path").(string)
ret.Username = d.Get("username").(string)
ret.Password = d.Get("password").(string)
ret.UseMgmtPort = d.Get("use_mgmt_port").(int)
ret.Overwrite = d.Get("overwrite").(int)
ret.CertificateType = d.Get("certificate_type").(string)
ret.PfxPassword = d.Get("pfx_password").(string)
ret.Secured = d.Get("secured").(int)
return ret
}