Skip to content

Commit c1c8d23

Browse files
authored
Merge pull request #564 from ovh/dev/aamstutz/autogenerated-resource
feat: Add first generated resource: ovh_cloud_project_alerting
2 parents d2c1d6a + 63525f1 commit c1c8d23

40 files changed

+4194
-1
lines changed

.cds/terraform-provider-ovh.yml

+12
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ workflow:
257257
pipeline: terraform-provider-ovh-testacc
258258
when:
259259
- success
260+
Tests_CloudProjectAlerting:
261+
application: terraform-provider-ovh
262+
depends_on:
263+
- terraform-provider-ovh-pre-sweepers
264+
environment: acctests
265+
one_at_a_time: true
266+
parameters:
267+
testargs: -run CloudProjectAlerting
268+
pipeline: terraform-provider-ovh-testacc
269+
when:
270+
- success
260271
Tests_CloudProjectKubeNodePool:
261272
application: terraform-provider-ovh
262273
depends_on:
@@ -624,6 +635,7 @@ workflow:
624635
terraform-provider-ovh-post-sweepers:
625636
application: terraform-provider-ovh
626637
depends_on:
638+
- Tests_CloudProjectAlerting
627639
- Tests_CloudProjectCapabilities
628640
- Tests_CloudProjectContainer
629641
- Tests_CloudProjectDatabaseCapabilities

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/hashicorp/logutils v1.0.0 // indirect
3737
github.com/hashicorp/terraform-exec v0.19.0 // indirect
3838
github.com/hashicorp/terraform-json v0.18.0 // indirect
39+
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 // indirect
3940
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
4041
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
4142
github.com/hashicorp/terraform-svchost v0.1.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ github.com/hashicorp/terraform-json v0.18.0 h1:pCjgJEqqDESv4y0Tzdqfxr/edOIGkjs8k
6565
github.com/hashicorp/terraform-json v0.18.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
6666
github.com/hashicorp/terraform-plugin-framework v1.4.2 h1:P7a7VP1GZbjc4rv921Xy5OckzhoiO3ig6SGxwelD2sI=
6767
github.com/hashicorp/terraform-plugin-framework v1.4.2/go.mod h1:GWl3InPFZi2wVQmdVnINPKys09s9mLmTZr95/ngLnbY=
68+
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
69+
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
6870
github.com/hashicorp/terraform-plugin-go v0.20.0 h1:oqvoUlL+2EUbKNsJbIt3zqqZ7wi6lzn4ufkn/UA51xQ=
6971
github.com/hashicorp/terraform-plugin-go v0.20.0/go.mod h1:Rr8LBdMlY53a3Z/HpP+ZU3/xCDqtKNCkeI9qOyT10QE=
7072
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=

ovh/provider_new.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ func (p *OvhProvider) DataSources(_ context.Context) []func() datasource.DataSou
166166

167167
// Resources defines the resources implemented in the provider.
168168
func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
169-
return nil
169+
return []func() resource.Resource{
170+
NewCloudProjectAlertingResource,
171+
}
170172
}
171173

172174
type ovhProviderModel struct {
+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/resource"
9+
)
10+
11+
var _ resource.ResourceWithConfigure = (*cloudProjectAlertingResource)(nil)
12+
13+
func NewCloudProjectAlertingResource() resource.Resource {
14+
return &cloudProjectAlertingResource{}
15+
}
16+
17+
type cloudProjectAlertingResource struct {
18+
config *Config
19+
}
20+
21+
func (r *cloudProjectAlertingResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
22+
resp.TypeName = req.ProviderTypeName + "_cloud_project_alerting"
23+
}
24+
25+
func (d *cloudProjectAlertingResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
26+
if req.ProviderData == nil {
27+
return
28+
}
29+
30+
config, ok := req.ProviderData.(*Config)
31+
if !ok {
32+
resp.Diagnostics.AddError(
33+
"Unexpected Resource Configure Type",
34+
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
35+
)
36+
return
37+
}
38+
39+
d.config = config
40+
}
41+
42+
func (d *cloudProjectAlertingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
43+
resp.Schema = CloudProjectAlertingResourceSchema(ctx)
44+
}
45+
46+
func (r *cloudProjectAlertingResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
47+
var data, responseData CloudProjectAlertingModel
48+
49+
// Read Terraform plan data into the model
50+
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
51+
if resp.Diagnostics.HasError() {
52+
return
53+
}
54+
55+
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting"
56+
if err := r.config.OVHClient.Post(endpoint, data.ToCreate(), &responseData); err != nil {
57+
resp.Diagnostics.AddError(
58+
fmt.Sprintf("Error calling Post %s", endpoint),
59+
err.Error(),
60+
)
61+
return
62+
}
63+
64+
responseData.MergeWith(&data)
65+
66+
// Save data into Terraform state
67+
resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...)
68+
}
69+
70+
func (r *cloudProjectAlertingResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
71+
var data, responseData CloudProjectAlertingModel
72+
73+
// Read Terraform prior state data into the model
74+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
75+
if resp.Diagnostics.HasError() {
76+
return
77+
}
78+
79+
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
80+
81+
if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
82+
resp.Diagnostics.AddError(
83+
fmt.Sprintf("Error calling Get %s", endpoint),
84+
err.Error(),
85+
)
86+
return
87+
}
88+
89+
data.MergeWith(&responseData)
90+
91+
// Save updated data into Terraform state
92+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
93+
}
94+
95+
func (r *cloudProjectAlertingResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
96+
var data, planData, responseData CloudProjectAlertingModel
97+
98+
// Read Terraform plan data into the model
99+
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
100+
if resp.Diagnostics.HasError() {
101+
return
102+
}
103+
104+
// Read Terraform prior state data into the model
105+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
106+
if resp.Diagnostics.HasError() {
107+
return
108+
}
109+
110+
// Update resource
111+
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
112+
if err := r.config.OVHClient.Put(endpoint, planData.ToUpdate(), nil); err != nil {
113+
resp.Diagnostics.AddError(
114+
fmt.Sprintf("Error calling Put %s", endpoint),
115+
err.Error(),
116+
)
117+
return
118+
}
119+
120+
// Read updated resource
121+
endpoint = "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
122+
if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
123+
resp.Diagnostics.AddError(
124+
fmt.Sprintf("Error calling Get %s", endpoint),
125+
err.Error(),
126+
)
127+
return
128+
}
129+
130+
responseData.MergeWith(&planData)
131+
132+
// Save updated data into Terraform state
133+
resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...)
134+
}
135+
136+
func (r *cloudProjectAlertingResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
137+
var data CloudProjectAlertingModel
138+
139+
// Read Terraform prior state data into the model
140+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
141+
142+
if resp.Diagnostics.HasError() {
143+
return
144+
}
145+
146+
// Delete API call logic
147+
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
148+
if err := r.config.OVHClient.Delete(endpoint, nil); err != nil {
149+
resp.Diagnostics.AddError(
150+
fmt.Sprintf("Error calling Delete %s", endpoint),
151+
err.Error(),
152+
)
153+
}
154+
}

0 commit comments

Comments
 (0)