parent
460c18c202
commit
53a3697850
124 changed files with 8342 additions and 6042 deletions
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
|
||||
|
||||
"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"
|
||||
|
|
@ -30,11 +29,13 @@ var (
|
|||
_ resource.Resource = &networkInterfaceAttachResource{}
|
||||
_ resource.ResourceWithConfigure = &networkInterfaceAttachResource{}
|
||||
_ resource.ResourceWithImportState = &networkInterfaceAttachResource{}
|
||||
_ resource.ResourceWithModifyPlan = &networkInterfaceAttachResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
ServerId types.String `tfsdk:"server_id"`
|
||||
NetworkInterfaceId types.String `tfsdk:"network_interface_id"`
|
||||
}
|
||||
|
|
@ -46,7 +47,8 @@ func NewNetworkInterfaceAttachResource() resource.Resource {
|
|||
|
||||
// networkInterfaceAttachResource is the resource implementation.
|
||||
type networkInterfaceAttachResource struct {
|
||||
client *iaas.APIClient
|
||||
client *iaas.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the resource type name.
|
||||
|
|
@ -54,14 +56,45 @@ func (r *networkInterfaceAttachResource) Metadata(_ context.Context, req resourc
|
|||
resp.TypeName = req.ProviderTypeName + "_server_network_interface_attach"
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *networkInterfaceAttachResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel Model
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
}
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var planModel Model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the resource.
|
||||
func (r *networkInterfaceAttachResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
||||
apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -71,13 +104,13 @@ func (r *networkInterfaceAttachResource) Configure(ctx context.Context, req reso
|
|||
|
||||
// Schema defines the schema for the resource.
|
||||
func (r *networkInterfaceAttachResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
description := "Network interface attachment resource schema. Attaches a network interface to a server. Must have a `region` specified in the provider configuration. The attachment only takes full effect after server reboot."
|
||||
description := "Network interface attachment resource schema. Attaches a network interface to a server. The attachment only takes full effect after server reboot."
|
||||
resp.Schema = schema.Schema{
|
||||
MarkdownDescription: description,
|
||||
Description: description,
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`server_id`,`network_interface_id`\".",
|
||||
Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`server_id`,`network_interface_id`\".",
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.UseStateForUnknown(),
|
||||
|
|
@ -94,6 +127,15 @@ func (r *networkInterfaceAttachResource) Schema(_ context.Context, _ resource.Sc
|
|||
validate.NoSeparator(),
|
||||
},
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Description: "The resource region. If not defined, the provider region is used.",
|
||||
Optional: true,
|
||||
// must be computed to allow for storing the override value from the provider
|
||||
Computed: true,
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
"server_id": schema.StringAttribute{
|
||||
Description: "The server ID.",
|
||||
Required: true,
|
||||
|
|
@ -133,14 +175,16 @@ func (r *networkInterfaceAttachResource) Create(ctx context.Context, req resourc
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
serverId := model.ServerId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
networkInterfaceId := model.NetworkInterfaceId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
ctx = tflog.SetField(ctx, "network_interface_id", networkInterfaceId)
|
||||
|
||||
// Create new network interface attachment
|
||||
err := r.client.AddNicToServer(ctx, projectId, serverId, networkInterfaceId).Execute()
|
||||
err := r.client.AddNicToServer(ctx, projectId, region, serverId, networkInterfaceId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error attaching network interface to server", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -148,7 +192,8 @@ func (r *networkInterfaceAttachResource) Create(ctx context.Context, req resourc
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(projectId, serverId, networkInterfaceId)
|
||||
model.Id = utils.BuildInternalTerraformId(projectId, region, serverId, networkInterfaceId)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, model)
|
||||
|
|
@ -171,13 +216,14 @@ func (r *networkInterfaceAttachResource) Read(ctx context.Context, req resource.
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
serverId := model.ServerId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
networkInterfaceId := model.NetworkInterfaceId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
ctx = tflog.SetField(ctx, "network_interface_id", networkInterfaceId)
|
||||
|
||||
nics, err := r.client.ListServerNics(ctx, projectId, serverId).Execute()
|
||||
nics, err := r.client.ListServerNICs(ctx, projectId, region, serverId).Execute()
|
||||
if err != nil {
|
||||
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
|
|
@ -200,12 +246,17 @@ func (r *networkInterfaceAttachResource) Read(ctx context.Context, req resource.
|
|||
if nic.Id == nil || (nic.Id != nil && *nic.Id != networkInterfaceId) {
|
||||
continue
|
||||
}
|
||||
|
||||
model.Id = utils.BuildInternalTerraformId(projectId, region, serverId, networkInterfaceId)
|
||||
model.Region = types.StringValue(region)
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Network interface attachment read")
|
||||
return
|
||||
}
|
||||
|
|
@ -233,14 +284,16 @@ func (r *networkInterfaceAttachResource) Delete(ctx context.Context, req resourc
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
serverId := model.ServerId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
network_interfaceId := model.NetworkInterfaceId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
ctx = tflog.SetField(ctx, "network_interface_id", network_interfaceId)
|
||||
|
||||
// Remove network_interface from server
|
||||
err := r.client.RemoveNicFromServer(ctx, projectId, serverId, network_interfaceId).Execute()
|
||||
err := r.client.RemoveNicFromServer(ctx, projectId, region, serverId, network_interfaceId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error removing network interface from server", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -256,23 +309,20 @@ func (r *networkInterfaceAttachResource) Delete(ctx context.Context, req resourc
|
|||
func (r *networkInterfaceAttachResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
"Error importing network_interface attachment",
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[server_id],[network_interface_id] Got: %q", req.ID),
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[region],[server_id],[network_interface_id] Got: %q", req.ID),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
projectId := idParts[0]
|
||||
serverId := idParts[1]
|
||||
network_interfaceId := idParts[2]
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "server_id", serverId)
|
||||
ctx = tflog.SetField(ctx, "network_interface_id", network_interfaceId)
|
||||
utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]interface{}{
|
||||
"project_id": idParts[0],
|
||||
"region": idParts[1],
|
||||
"server_id": idParts[2],
|
||||
"network_interface_id": idParts[3],
|
||||
})
|
||||
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("server_id"), serverId)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("network_interface_id"), network_interfaceId)...)
|
||||
tflog.Info(ctx, "Network interface attachment state imported")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue