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

Add import to ovh_iploadbalancing_udp_frontend + Create resource ovh_iploadbalancing_udp_farm and ovh_iploadbalancing_udp_farm_server #719

Merged
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
create resource_iploadbalancing_udp_farm
  • Loading branch information
beluc committed Sep 11, 2024
commit 2695a67c157d3b0edc8bdecb5a0c1d2c7fc88bb0
2 changes: 2 additions & 0 deletions ovh/provider_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
NewIpFirewallResource,
NewIpFirewallRuleResource,
NewIploadbalancingUdpFrontendResource,
NewIploadbalancingUdpFarmResource,
NewIploadbalancingUdpFarmServerResource,
NewIpMitigationResource,
NewVpsResource,
}
Expand Down
178 changes: 178 additions & 0 deletions ovh/resource_iploadbalancing_udp_farm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package ovh

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

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

var (
_ resource.ResourceWithConfigure = (*iploadbalancingUdpFarmResource)(nil)
_ resource.ResourceWithImportState = (*iploadbalancingUdpFarmResource)(nil)
)

func NewIploadbalancingUdpFarmResource() resource.Resource {
return &iploadbalancingUdpFarmResource{}
}

type iploadbalancingUdpFarmResource struct {
config *Config
}

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

func (d *iploadbalancingUdpFarmResource) 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 *iploadbalancingUdpFarmResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = IploadbalancingUdpFarmResourceSchema(ctx)
}

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

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

endpoint := "/ipLoadbalancing/" + url.PathEscape(data.ServiceName.ValueString()) + "/udp/farm"
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 *iploadbalancingUdpFarmResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data, responseData IploadbalancingUdpFarmModel

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

endpoint := "/ipLoadbalancing/" + url.PathEscape(data.ServiceName.ValueString()) + "/udp/farm/" + strconv.FormatInt(data.FarmId.ValueInt64(), 10)

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 *iploadbalancingUdpFarmResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data, planData, responseData IploadbalancingUdpFarmModel

// 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 := "/ipLoadbalancing/" + url.PathEscape(data.ServiceName.ValueString()) + "/udp/farm/" + strconv.FormatInt(data.FarmId.ValueInt64(), 10)
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 = "/ipLoadbalancing/" + url.PathEscape(data.ServiceName.ValueString()) + "/udp/farm/" + strconv.FormatInt(data.FarmId.ValueInt64(), 10)
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 *iploadbalancingUdpFarmResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data IploadbalancingUdpFarmModel

// 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 := "/ipLoadbalancing/" + url.PathEscape(data.ServiceName.ValueString()) + "/udp/farm/" + strconv.FormatInt(data.FarmId.ValueInt64(), 10)
if err := r.config.OVHClient.Delete(endpoint, nil); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Delete %s", endpoint),
err.Error(),
)
}
}

func (r *iploadbalancingUdpFarmResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splits := strings.Split(req.ID, "/")
if len(splits) != 2 {
resp.Diagnostics.AddError("Given ID is malformed", "ID must be formatted like the following: <service_name>/<farmId>")
return
}

serviceName := splits[0]
farmId, err := strconv.Atoi(splits[1])
if err != nil {
resp.Diagnostics.AddError("Given ID is malformed", "ID must be formatted like the following: <service_name>/<farmId> where farmId is a number")
return
}

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("service_name"), serviceName)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("farm_id"), farmId)...)
}
141 changes: 141 additions & 0 deletions ovh/resource_iploadbalancing_udp_farm_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading