Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add first generated resource: ovh_cloud_project_alerting #564

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .cds/terraform-provider-ovh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ workflow:
pipeline: terraform-provider-ovh-testacc
when:
- success
Tests_CloudProjectAlerting:
application: terraform-provider-ovh
depends_on:
- terraform-provider-ovh-pre-sweepers
environment: acctests
one_at_a_time: true
parameters:
testargs: -run CloudProjectAlerting
pipeline: terraform-provider-ovh-testacc
when:
- success
Tests_CloudProjectKubeNodePool:
application: terraform-provider-ovh
depends_on:
Expand Down Expand Up @@ -624,6 +635,7 @@ workflow:
terraform-provider-ovh-post-sweepers:
application: terraform-provider-ovh
depends_on:
- Tests_CloudProjectAlerting
- Tests_CloudProjectCapabilities
- Tests_CloudProjectContainer
- Tests_CloudProjectDatabaseCapabilities
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.18.0 // indirect
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ github.com/hashicorp/terraform-json v0.18.0 h1:pCjgJEqqDESv4y0Tzdqfxr/edOIGkjs8k
github.com/hashicorp/terraform-json v0.18.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
github.com/hashicorp/terraform-plugin-framework v1.4.2 h1:P7a7VP1GZbjc4rv921Xy5OckzhoiO3ig6SGxwelD2sI=
github.com/hashicorp/terraform-plugin-framework v1.4.2/go.mod h1:GWl3InPFZi2wVQmdVnINPKys09s9mLmTZr95/ngLnbY=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.20.0 h1:oqvoUlL+2EUbKNsJbIt3zqqZ7wi6lzn4ufkn/UA51xQ=
github.com/hashicorp/terraform-plugin-go v0.20.0/go.mod h1:Rr8LBdMlY53a3Z/HpP+ZU3/xCDqtKNCkeI9qOyT10QE=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
4 changes: 3 additions & 1 deletion ovh/provider_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ func (p *OvhProvider) DataSources(_ context.Context) []func() datasource.DataSou

// Resources defines the resources implemented in the provider.
func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
return nil
return []func() resource.Resource{
NewCloudProjectAlertingResource,
}
}

type ovhProviderModel struct {
Expand Down
154 changes: 154 additions & 0 deletions ovh/resource_cloud_project_alerting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package ovh

import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-framework/resource"
)

var _ resource.ResourceWithConfigure = (*cloudProjectAlertingResource)(nil)

func NewCloudProjectAlertingResource() resource.Resource {
return &cloudProjectAlertingResource{}
}

type cloudProjectAlertingResource struct {
config *Config
}

func (r *cloudProjectAlertingResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cloud_project_alerting"
}

func (d *cloudProjectAlertingResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

config, ok := req.ProviderData.(*Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.config = config
}

func (d *cloudProjectAlertingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = CloudProjectAlertingResourceSchema(ctx)
}

func (r *cloudProjectAlertingResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data, responseData CloudProjectAlertingModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting"
if err := r.config.OVHClient.Post(endpoint, data.ToCreate(), &responseData); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Post %s", endpoint),
err.Error(),
)
return
}

responseData.MergeWith(&data)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...)
}

func (r *cloudProjectAlertingResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data, responseData CloudProjectAlertingModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())

if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

data.MergeWith(&responseData)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *cloudProjectAlertingResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data, planData, responseData CloudProjectAlertingModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
return
}

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

// Update resource
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
if err := r.config.OVHClient.Put(endpoint, planData.ToUpdate(), nil); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Put %s", endpoint),
err.Error(),
)
return
}

// Read updated resource
endpoint = "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

responseData.MergeWith(&planData)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &responseData)...)
}

func (r *cloudProjectAlertingResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data CloudProjectAlertingModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// Delete API call logic
endpoint := "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/alerting/" + url.PathEscape(data.Id.ValueString())
if err := r.config.OVHClient.Delete(endpoint, nil); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Delete %s", endpoint),
err.Error(),
)
}
}
Loading