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
|
package provider
import (
"context"
"errors"
"fmt"
"git.adyxax.org/adyxax/terraform-provider-eventline/external/evcli"
"github.com/exograd/eventline/pkg/eventline"
"github.com/exograd/eventline/pkg/ksuid"
"github.com/hashicorp/terraform-plugin-framework/path"
"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"
)
type ProjectResource struct {
client *evcli.Client
}
var _ resource.Resource = &ProjectResource{} // Ensure provider defined types fully satisfy framework interfaces
var _ resource.ResourceWithImportState = &ProjectResource{} // Ensure provider defined types fully satisfy framework interfaces
func NewProjectResource() resource.Resource {
return &ProjectResource{}
}
type ProjectResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
}
func (r *ProjectResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project"
}
func (r *ProjectResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Project Id",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Project name",
Required: true,
},
},
MarkdownDescription: "Eventline project resource",
}
}
func (r *ProjectResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
r.client, _ = req.ProviderData.(*evcli.Client)
}
func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *ProjectResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
project := eventline.Project{Name: data.Name.ValueString()}
if err := r.client.CreateProject(&project); err != nil {
resp.Diagnostics.AddError("CreateProject", fmt.Sprintf("Unable to create project, got error: %s\nTry importing the resource instead?", err))
return
}
data.Id = types.StringValue(project.Id.String())
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *ProjectResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var id ksuid.KSUID
if err := id.Parse(data.Id.ValueString()); err != nil {
resp.Diagnostics.AddError("KsuidParse", fmt.Sprintf("Unable to parse project id, got error: %s", err))
return
}
project, err := r.client.FetchProjectById(id)
if err != nil {
var e *evcli.APIError
if errors.As(err, &e) && e.Code == "unknown_project" {
resp.State.RemoveResource(ctx) // The project does not exist
return
}
resp.Diagnostics.AddError("FetchProjectById", fmt.Sprintf("Unable to fetch project by id, got error: %s", err))
return
}
data.Id = types.StringValue(project.Id.String())
data.Name = types.StringValue(project.Name)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *ProjectResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var id ksuid.KSUID
if err := id.Parse(data.Id.ValueString()); err != nil {
resp.Diagnostics.AddError("KsuidParse", fmt.Sprintf("Unable to parse project id, got error: %s", err))
return
}
project := eventline.Project{Id: id, Name: data.Name.ValueString()}
if err := r.client.UpdateProject(&project); err != nil {
resp.Diagnostics.AddError("UpdateProject", fmt.Sprintf("Unable to update project, got error: %s", err))
return
}
data.Id = types.StringValue(project.Id.String())
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *ProjectResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var id ksuid.KSUID
if err := id.Parse(data.Id.ValueString()); err != nil {
resp.Diagnostics.AddError("KsuidParse", fmt.Sprintf("Unable to parse project id, got error: %s", err))
return
}
if err := r.client.DeleteProject(id); err != nil {
var e *evcli.APIError
if errors.As(err, &e) && e.Code == "unknown_project" {
return // the project does not exist, that is what we want
}
resp.Diagnostics.AddError("DeleteProject", fmt.Sprintf("Unable to delete project by id, got error: %s", err))
return
}
}
func (r *ProjectResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
|