Skip to content

Commit

Permalink
fix: Start fixing public IP update always removing NIC association
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopalet committed Nov 27, 2024
1 parent 0b1b13e commit 7de3078
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions stackit/internal/services/iaas/publicip/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -42,6 +43,7 @@ type Model struct {
PublicIpId types.String `tfsdk:"public_ip_id"`
Ip types.String `tfsdk:"ip"`
NetworkInterfaceId types.String `tfsdk:"network_interface_id"`
DisableAssociation types.Bool `tfsdk:"disable_association"`
Labels types.Map `tfsdk:"labels"`
}

Expand Down Expand Up @@ -153,11 +155,21 @@ func (r *publicIpResource) Schema(_ context.Context, _ resource.SchemaRequest, r
"network_interface_id": schema.StringAttribute{
Description: "Associates the public IP with a network interface or a virtual IP (ID).",
Optional: true,
Computed: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"disable_association": schema.BoolAttribute{
Description: "Disables the association of the public IP with a network interface or a virtual IP.",
Optional: true,
Validators: []validator.Bool{
boolvalidator.ConflictsWith(
path.MatchRoot("network_interface_id"),
),
},
},
"labels": schema.MapAttribute{
Description: "Labels are key-value string pairs which can be attached to a resource container",
ElementType: types.StringType,
Expand Down Expand Up @@ -188,7 +200,6 @@ func (r *publicIpResource) Create(ctx context.Context, req resource.CreateReques
}

// Create new public IP

publicIp, err := r.client.CreatePublicIP(ctx, projectId).CreatePublicIPPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating public IP", fmt.Sprintf("Calling API: %v", err))
Expand Down Expand Up @@ -424,8 +435,19 @@ func toUpdatePayload(ctx context.Context, model *Model, currentLabels types.Map)
return nil, fmt.Errorf("converting to Go map: %w", err)
}

return &iaas.UpdatePublicIPPayload{
Labels: &labels,
NetworkInterface: iaas.NewNullableString(conversion.StringValueToPointer(model.NetworkInterfaceId)),
}, nil
payload := &iaas.UpdatePublicIPPayload{
Labels: &labels,
}

if !model.NetworkInterfaceId.IsUnknown() {
payload.NetworkInterface = iaas.NewNullableString(conversion.StringValueToPointer(model.NetworkInterfaceId))
}

if model.DisableAssociation.ValueBool() {
payload.NetworkInterface = iaas.NewNullableString(nil)
}

tflog.Warn(ctx, fmt.Sprintf("payload.NetworkInterface: %v", payload.NetworkInterface))

return payload, nil
}

0 comments on commit 7de3078

Please sign in to comment.