/
ecr_registry_resource.go
247 lines (203 loc) · 8.22 KB
/
ecr_registry_resource.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package provider
import (
"context"
"fmt"
workflow_pb "github.com/prodvana/prodvana-public/go/prodvana-sdk/proto/prodvana/workflow"
"github.com/prodvana/terraform-provider-prodvana/internal/provider/validators"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &ECRRegistryResource{}
func NewECRRegistryResource() resource.Resource {
return &ECRRegistryResource{}
}
// ECRRegistryResource defines the resource implementation.
type ECRRegistryResource struct {
client workflow_pb.WorkflowManagerClient
}
// ContainerRegistryResouceModel describes the resource link data model.
type ECRRegistryResourceModel struct {
Name types.String `tfsdk:"name"`
Id types.String `tfsdk:"id"`
Region types.String `tfsdk:"region"`
// this is encapsulated in its own nested object because we will
// support other authentication methods in the future
CredentialsAuth *CredentialAuthModel `tfsdk:"credentials_auth"`
}
type CredentialAuthModel struct {
AccessKeyID types.String `tfsdk:"access_key_id"`
SecretAccessKey types.String `tfsdk:"secret_access_key"`
}
func (r *ECRRegistryResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ecr_registry"
}
func (r *ECRRegistryResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "This resource allows you to link an [ECR registry](https://docs.prodvana.io/docs/ecr) to Prodvana.",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Name for the ECR registry, used to reference it in Prodvana configuration.",
Required: true,
Validators: validators.DefaultNameValidators(),
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "ECR Registry Identifier",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"region": schema.StringAttribute{
MarkdownDescription: "AWS region where the ECR registry is located.",
Required: true,
},
"credentials_auth": schema.SingleNestedAttribute{
MarkdownDescription: "Credentials to authenticate with the ECR registry.",
Required: true,
Attributes: map[string]schema.Attribute{
"access_key_id": schema.StringAttribute{
MarkdownDescription: "AWS Access Key ID with permissions to the ECR registry",
Required: true,
},
"secret_access_key": schema.StringAttribute{
MarkdownDescription: "AWS Secret Access Key with permissions to the ECR registry",
Required: true,
Sensitive: true,
},
},
},
},
}
}
func (r *ECRRegistryResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
conn, ok := req.ProviderData.(*grpc.ClientConn)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *grpc.ClientConn, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = workflow_pb.NewWorkflowManagerClient(conn)
}
func (r *ECRRegistryResource) refresh(ctx context.Context, diags diag.Diagnostics, data *ECRRegistryResourceModel) error {
resp, err := r.client.GetContainerRegistryIntegration(ctx, &workflow_pb.GetContainerRegistryIntegrationReq{
RegistryName: data.Name.ValueString(),
})
if err != nil {
return errors.Wrapf(err, "Unable to read ecr registry state for %s", data.Name.ValueString())
}
if resp.Registry.Type != workflow_pb.RegistryType_ECR.String() {
return fmt.Errorf("registry %s is not an ECR registry, registry of type %s found", data.Name.ValueString(), resp.Registry.Type)
}
data.Id = types.StringValue(resp.Registry.IntegrationId)
if resp.Registry.GetEcrInfo() != nil {
data.Region = types.StringValue(resp.Registry.GetEcrInfo().Region)
}
return nil
}
func (r *ECRRegistryResource) createOrUpdate(ctx context.Context, planData *ECRRegistryResourceModel) error {
createReq := &workflow_pb.CreateContainerRegistryIntegrationReq{
Name: planData.Name.ValueString(),
Type: workflow_pb.RegistryType_ECR,
RegistryOptions: &workflow_pb.CreateContainerRegistryIntegrationReq_EcrOptions{
EcrOptions: &workflow_pb.CreateContainerRegistryIntegrationReq_ECROptions{
Region: planData.Region.ValueString(),
AccessKey: planData.CredentialsAuth.AccessKeyID.ValueString(),
SecretKey: planData.CredentialsAuth.SecretAccessKey.ValueString(),
},
},
}
createResp, err := r.client.CreateContainerRegistryIntegration(ctx, createReq)
if err != nil {
return err
}
planData.Id = types.StringValue(createResp.IntegrationId)
return nil
}
func (r *ECRRegistryResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *ECRRegistryResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
err := r.createOrUpdate(ctx, data)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create ecr registry, got error: %s", err))
return
}
tflog.Trace(ctx, "created ecr registry resource")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ECRRegistryResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *ECRRegistryResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
err := r.refresh(ctx, resp.Diagnostics, data)
if err != nil {
// if registry doesn't exist anymore, remove the resource
if status.Code(err) == codes.NotFound {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read ecr registry state for %s, got error: %s", data.Name.ValueString(), err))
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ECRRegistryResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var planData *ECRRegistryResourceModel
var stateData *ECRRegistryResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
return
}
err := r.createOrUpdate(ctx, planData)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update ecr registry, got error: %s", err))
return
}
tflog.Trace(ctx, "updated ecr registry resource")
resp.Diagnostics.Append(resp.State.Set(ctx, &planData)...)
}
func (r *ECRRegistryResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *ECRRegistryResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.DeleteContainerRegistryIntegration(ctx, &workflow_pb.DeleteContainerRegistryIntegrationReq{
RegistryName: data.Name.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete ecr registry, got error: %s", err))
return
}
tflog.Trace(ctx, "deleted ecr registry resource")
}