-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsite_resource.go
182 lines (156 loc) · 4.82 KB
/
site_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
package provider
import (
"context"
"fmt"
"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"
"github.com/netlify/terraform-provider-netlify/internal/netlifyapi"
)
var (
_ resource.Resource = &siteResource{}
_ resource.ResourceWithConfigure = &siteResource{}
_ resource.ResourceWithImportState = &siteResource{}
)
func NewSiteResource() resource.Resource {
return &siteResource{}
}
type siteResource struct {
data NetlifyProviderData
}
type siteResourceModel struct {
ID types.String `tfsdk:"id"`
TeamSlug types.String `tfsdk:"team_slug"`
Name types.String `tfsdk:"name"`
}
func (r *siteResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_site"
}
func (r *siteResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(NetlifyProviderData)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected NetlifyProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.data = data
}
func (r *siteResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Netlify site",
MarkdownDescription: "Netlify site. [Read more](https://docs.netlify.com/git/overview/)",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"team_slug": schema.StringAttribute{
Required: true,
},
"name": schema.StringAttribute{
Required: true,
},
},
}
}
func (r *siteResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var state siteResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
ns := netlifyapi.Site{
Name: state.Name.ValueString(),
}
site, _, err := r.data.client.SitesAPI.
CreateSite(ctx).
Site(ns).
Execute()
if err != nil {
resp.Diagnostics.AddError(
"Error creating site",
fmt.Sprintf("Could not create site: %q", err.Error()),
)
return
}
state.ID = types.StringValue(site.Id)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *siteResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state siteResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
site, _, err := r.data.client.SitesAPI.GetSite(ctx, state.ID.ValueString()).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Error reading site",
fmt.Sprintf(
"Could not read site %q: %q",
state.ID.ValueString(),
err.Error(),
),
)
return
}
state.Name = types.StringValue(site.Name)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *siteResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var state siteResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
site, _, err := r.data.client.SitesAPI.
UpdateSite(ctx, state.ID.ValueString()).
PartialSite(netlifyapi.PartialSite{
Name: state.Name.ValueStringPointer(),
}).
Execute()
if err != nil {
resp.Diagnostics.AddError(
"Error updating site",
fmt.Sprintf("Could not update site %q: %q", state.ID.ValueString(), err.Error()),
)
return
}
state.Name = types.StringValue(site.Name)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *siteResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state siteResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.data.client.SitesAPI.DeleteSite(ctx, state.ID.ValueString()).Execute()
if err != nil {
resp.Diagnostics.AddError(
"Error deleting site",
fmt.Sprintf(
"Could not delete site %q: %q",
state.ID.ValueString(),
err.Error(),
),
)
return
}
}
func (r *siteResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), req.ID)...)
}