/
resource_onepasswordorg_vault.go
208 lines (177 loc) · 5.6 KB
/
resource_onepasswordorg_vault.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
201
202
203
204
205
206
207
208
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/slok/terraform-provider-onepasswordorg/internal/model"
"github.com/slok/terraform-provider-onepasswordorg/internal/provider/attributeutils"
)
type resourceVaultType struct{}
func (r resourceVaultType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Description: `
Provides a vault resource.
`,
Attributes: map[string]tfsdk.Attribute{
"id": {
Type: types.StringType,
Computed: true,
},
"name": {
Type: types.StringType,
Required: true,
PlanModifiers: tfsdk.AttributePlanModifiers{tfsdk.RequiresReplace()},
Validators: []tfsdk.AttributeValidator{attributeutils.NonEmptyString},
Description: "The name of the vault.",
},
"description": {
Type: types.StringType,
Optional: true,
Computed: true,
PlanModifiers: tfsdk.AttributePlanModifiers{attributeutils.DefaultValue(types.String{Value: "Managed by Terraform"})},
Description: "The description of the vault.",
},
},
}, nil
}
func (r resourceVaultType) NewResource(_ context.Context, p tfsdk.Provider) (tfsdk.Resource, diag.Diagnostics) {
prv := p.(*provider)
return resourceVault{
p: *prv,
}, nil
}
type resourceVault struct {
p provider
}
func (r resourceVault) Create(ctx context.Context, req tfsdk.CreateResourceRequest, resp *tfsdk.CreateResourceResponse) {
if !r.p.configured {
resp.Diagnostics.AddError("Provider not configured", "The provider hasn't been configured before apply.")
return
}
// Retrieve values from plan.
var tfVault Vault
diags := req.Plan.Get(ctx, &tfVault)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Create vault.
v := mapTfToModelVault(tfVault)
newVault, err := r.p.repo.CreateVault(ctx, v)
if err != nil {
resp.Diagnostics.AddError("Error creating vault", "Could not create vault, unexpected error: "+err.Error())
return
}
// Map to tf model.
newTfVault := mapModelToTfVault(*newVault)
diags = resp.State.Set(ctx, newTfVault)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceVault) Read(ctx context.Context, req tfsdk.ReadResourceRequest, resp *tfsdk.ReadResourceResponse) {
if !r.p.configured {
resp.Diagnostics.AddError("Provider not configured", "The provider hasn't been configured before apply.")
return
}
// Retrieve values from plan.
var tfVault Vault
diags := req.State.Get(ctx, &tfVault)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get resource.
id := tfVault.ID.Value
vault, err := r.p.repo.GetVaultByID(ctx, id)
if err != nil {
resp.Diagnostics.AddError("Error reading vault", fmt.Sprintf("Could not get vault %q, unexpected error: %s", id, err.Error()))
return
}
// Map resource to tf model.
readTfVault := mapModelToTfVault(*vault)
diags = resp.State.Set(ctx, readTfVault)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceVault) Update(ctx context.Context, req tfsdk.UpdateResourceRequest, resp *tfsdk.UpdateResourceResponse) {
if !r.p.configured {
resp.Diagnostics.AddError("Provider not configured", "The provider hasn't been configured before apply.")
return
}
// Get plan values.
var plan Vault
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get current state.
var state Vault
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Use plan group as the new data and set ID from state.
v := mapTfToModelVault(plan)
v.ID = state.ID.Value
newVault, err := r.p.repo.EnsureVault(ctx, v)
if err != nil {
resp.Diagnostics.AddError("Error updating vault", "Could not update vault, unexpected error: "+err.Error())
return
}
// Map vault to tf model.
readTfVault := mapModelToTfVault(*newVault)
diags = resp.State.Set(ctx, readTfVault)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceVault) Delete(ctx context.Context, req tfsdk.DeleteResourceRequest, resp *tfsdk.DeleteResourceResponse) {
if !r.p.configured {
resp.Diagnostics.AddError("Provider not configured", "The provider hasn't been configured before apply.")
return
}
// Retrieve values from plan.
var tfVault Vault
diags := req.State.Get(ctx, &tfVault)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Delete resource.
id := tfVault.ID.Value
err := r.p.repo.DeleteVault(ctx, id)
if err != nil {
resp.Diagnostics.AddError("Error deleting vault", fmt.Sprintf("Could not delete vault %q, unexpected error: %s", id, err.Error()))
return
}
// Remove resource from state.
resp.State.RemoveResource(ctx)
}
func (r resourceVault) ImportState(ctx context.Context, req tfsdk.ImportResourceStateRequest, resp *tfsdk.ImportResourceStateResponse) {
// Save the import identifier in the id attribute.
tfsdk.ResourceImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
func mapTfToModelVault(v Vault) model.Vault {
return model.Vault{
ID: v.ID.Value,
Name: v.Name.Value,
Description: v.Description.Value,
}
}
func mapModelToTfVault(u model.Vault) Vault {
return Vault{
ID: types.String{Value: u.ID},
Name: types.String{Value: u.Name},
Description: types.String{Value: u.Description},
}
}