Skip to content
Draft
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
18 changes: 11 additions & 7 deletions stackit/internal/services/iaas/affinitygroup/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"regexp"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
Expand All @@ -28,12 +28,16 @@ var (
_ datasource.DataSourceWithConfigure = &affinityGroupDatasource{}
)

func NewAffinityGroupDatasource() datasource.DataSource {
return &affinityGroupDatasource{}
func NewAffinityGroupDatasource(clientFactory clientutils.ClientFactory) datasource.DataSource {
return &affinityGroupDatasource{
clientFactory: clientFactory,
}
}

type affinityGroupDatasource struct {
client *iaas.APIClient
clientFactory clientutils.ClientFactory

client iaas.DefaultAPI
providerData core.ProviderData
}

Expand All @@ -44,11 +48,11 @@ func (d *affinityGroupDatasource) Configure(ctx context.Context, req datasource.
return
}

apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
d.client = d.clientFactory.NewIaaSV2Client(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient

tflog.Info(ctx, "iaas client configured")
}

Expand Down Expand Up @@ -133,7 +137,7 @@ func (d *affinityGroupDatasource) Read(ctx context.Context, req datasource.ReadR
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)

affinityGroupResp, err := d.client.DefaultAPI.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
affinityGroupResp, err := d.client.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
23 changes: 13 additions & 10 deletions stackit/internal/services/iaas/affinitygroup/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"strings"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"

iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
Expand Down Expand Up @@ -47,13 +46,17 @@ type Model struct {
Members types.List `tfsdk:"members"`
}

func NewAffinityGroupResource() resource.Resource {
return &affinityGroupResource{}
func NewAffinityGroupResource(clientFactory clientutils.ClientFactory) resource.Resource {
return &affinityGroupResource{
clientFactory: clientFactory,
}
}

// affinityGroupResource is the resource implementation.
type affinityGroupResource struct {
client *iaas.APIClient
clientFactory clientutils.ClientFactory

client iaas.DefaultAPI
providerData core.ProviderData
}

Expand Down Expand Up @@ -100,11 +103,11 @@ func (r *affinityGroupResource) Configure(ctx context.Context, req resource.Conf
return
}

apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
r.client = r.clientFactory.NewIaaSV2Client(ctx, &r.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
r.client = apiClient

tflog.Info(ctx, "iaas client configured")
}

Expand Down Expand Up @@ -210,7 +213,7 @@ func (r *affinityGroupResource) Create(ctx context.Context, req resource.CreateR
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating affinity group", fmt.Sprintf("Creating API payload: %v", err))
return
}
affinityGroupResp, err := r.client.DefaultAPI.CreateAffinityGroup(ctx, projectId, region).CreateAffinityGroupPayload(*payload).Execute()
affinityGroupResp, err := r.client.CreateAffinityGroup(ctx, projectId, region).CreateAffinityGroupPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating affinity group", fmt.Sprintf("Calling API: %v", err))
return
Expand Down Expand Up @@ -258,7 +261,7 @@ func (r *affinityGroupResource) Read(ctx context.Context, req resource.ReadReque
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)

affinityGroupResp, err := r.client.DefaultAPI.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
affinityGroupResp, err := r.client.GetAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
Expand Down Expand Up @@ -309,7 +312,7 @@ func (r *affinityGroupResource) Delete(ctx context.Context, req resource.DeleteR
ctx = tflog.SetField(ctx, "affinity_group_id", affinityGroupId)

// Delete existing affinity group
err := r.client.DefaultAPI.DeleteAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
err := r.client.DeleteAffinityGroup(ctx, projectId, region, affinityGroupId).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
Expand Down
17 changes: 11 additions & 6 deletions stackit/internal/services/iaas/image/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -45,13 +46,17 @@ type DataSourceModel struct {
}

// NewImageDataSource is a helper function to simplify the provider implementation.
func NewImageDataSource() datasource.DataSource {
return &imageDataSource{}
func NewImageDataSource(clientFactory clientutils.ClientFactory) datasource.DataSource {
return &imageDataSource{
clientFactory: clientFactory,
}
}

// imageDataSource is the data source implementation.
type imageDataSource struct {
client *iaas.APIClient
clientFactory clientutils.ClientFactory

client iaas.DefaultAPI
providerData core.ProviderData
}

Expand All @@ -67,11 +72,11 @@ func (d *imageDataSource) Configure(ctx context.Context, req datasource.Configur
return
}

apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
d.client = d.clientFactory.NewIaaSV2Client(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient

tflog.Info(ctx, "iaas client configured")
}

Expand Down Expand Up @@ -231,7 +236,7 @@ func (d *imageDataSource) Read(ctx context.Context, req datasource.ReadRequest,
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "image_id", imageId)

imageResp, err := d.client.DefaultAPI.GetImage(ctx, projectId, region, imageId).Execute()
imageResp, err := d.client.GetImage(ctx, projectId, region, imageId).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
31 changes: 18 additions & 13 deletions stackit/internal/services/iaas/image/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"

iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"

Expand Down Expand Up @@ -108,13 +109,17 @@ var checksumTypes = map[string]attr.Type{
}

// NewImageResource is a helper function to simplify the provider implementation.
func NewImageResource() resource.Resource {
return &imageResource{}
func NewImageResource(clientFactory clientutils.ClientFactory) resource.Resource {
return &imageResource{
clientFactory: clientFactory,
}
}

// imageResource is the resource implementation.
type imageResource struct {
client *iaas.APIClient
clientFactory clientutils.ClientFactory

client iaas.DefaultAPI
providerData core.ProviderData
}

Expand Down Expand Up @@ -161,11 +166,11 @@ func (r *imageResource) Configure(ctx context.Context, req resource.ConfigureReq
return
}

apiClient := iaasUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
r.client = r.clientFactory.NewIaaSV2Client(ctx, &r.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
r.client = apiClient

tflog.Info(ctx, "iaas client configured")
}

Expand Down Expand Up @@ -436,7 +441,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
}

// Create new image
imageCreateResp, err := r.client.DefaultAPI.CreateImage(ctx, projectId, region).CreateImagePayload(*payload).Execute()
imageCreateResp, err := r.client.CreateImage(ctx, projectId, region).CreateImagePayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
return
Expand All @@ -447,7 +452,7 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
ctx = tflog.SetField(ctx, "image_id", imageCreateResp.Id)

// Get the image object, as the creation response does not contain all fields
image, err := r.client.DefaultAPI.GetImage(ctx, projectId, region, imageCreateResp.Id).Execute()
image, err := r.client.GetImage(ctx, projectId, region, imageCreateResp.Id).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Calling API: %v", err))
return
Expand Down Expand Up @@ -475,8 +480,8 @@ func (r *imageResource) Create(ctx context.Context, req resource.CreateRequest,
}

// Wait for image to become available
waiter := wait.UploadImageWaitHandler(ctx, r.client.DefaultAPI, projectId, region, imageCreateResp.Id) //nolint:tfwriteid // false positive - id fields are actually stored already using the mapFields() call above
waiter = waiter.SetTimeout(7 * 24 * time.Hour) // Set timeout to one week, to make the timeout useless
waiter := wait.UploadImageWaitHandler(ctx, r.client, projectId, region, imageCreateResp.Id) //nolint:tfwriteid // false positive - id fields are actually stored already using the mapFields() call above
waiter = waiter.SetTimeout(7 * 24 * time.Hour) // Set timeout to one week, to make the timeout useless
waitResp, err := waiter.WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating image", fmt.Sprintf("Waiting for image to become available: %v", err))
Expand Down Expand Up @@ -523,7 +528,7 @@ func (r *imageResource) Read(ctx context.Context, req resource.ReadRequest, resp
ctx = tflog.SetField(ctx, "region", region)
ctx = tflog.SetField(ctx, "image_id", imageId)

imageResp, err := r.client.DefaultAPI.GetImage(ctx, projectId, region, imageId).Execute()
imageResp, err := r.client.GetImage(ctx, projectId, region, imageId).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
Expand Down Expand Up @@ -586,7 +591,7 @@ func (r *imageResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}
// Update existing image
updatedImage, err := r.client.DefaultAPI.UpdateImage(ctx, projectId, region, imageId).UpdateImagePayload(*payload).Execute()
updatedImage, err := r.client.UpdateImage(ctx, projectId, region, imageId).UpdateImagePayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating image", fmt.Sprintf("Calling API: %v", err))
return
Expand Down Expand Up @@ -627,7 +632,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,
ctx = core.InitProviderContext(ctx)

// Delete existing image
err := r.client.DefaultAPI.DeleteImage(ctx, projectId, region, imageId).Execute()
err := r.client.DeleteImage(ctx, projectId, region, imageId).Execute()
if err != nil {
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound {
Expand All @@ -640,7 +645,7 @@ func (r *imageResource) Delete(ctx context.Context, req resource.DeleteRequest,

ctx = core.LogResponse(ctx)

_, err = wait.DeleteImageWaitHandler(ctx, r.client.DefaultAPI, projectId, region, imageId).WaitWithContext(ctx)
_, err = wait.DeleteImageWaitHandler(ctx, r.client, projectId, region, imageId).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting image", fmt.Sprintf("image deletion waiting: %v", err))
return
Expand Down
19 changes: 12 additions & 7 deletions stackit/internal/services/iaas/imagev2/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
Expand Down Expand Up @@ -109,13 +111,17 @@ var checksumTypes = map[string]attr.Type{
}

// NewImageV2DataSource is a helper function to simplify the provider implementation.
func NewImageV2DataSource() datasource.DataSource {
return &imageDataV2Source{}
func NewImageV2DataSource(clientFactory clientutils.ClientFactory) datasource.DataSource {
return &imageDataV2Source{
clientFactory: clientFactory,
}
}

// imageDataV2Source is the data source implementation.
type imageDataV2Source struct {
client *iaas.APIClient
clientFactory clientutils.ClientFactory

client iaas.DefaultAPI
providerData core.ProviderData
}

Expand All @@ -136,12 +142,11 @@ func (d *imageDataV2Source) Configure(ctx context.Context, req datasource.Config
return
}

apiClient := iaasUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
d.client = d.clientFactory.NewIaaSV2Client(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}

d.client = apiClient
tflog.Info(ctx, "iaas client configured")
}

Expand Down Expand Up @@ -394,7 +399,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest

// Case 1: Direct lookup by image ID
if imageID != "" {
imageResp, err = d.client.DefaultAPI.GetImage(ctx, projectID, region, imageID).Execute()
imageResp, err = d.client.GetImage(ctx, projectID, region, imageID).Execute()
if err != nil {
utils.LogError(ctx, &resp.Diagnostics, err, "Reading image",
fmt.Sprintf("Image with ID %q does not exist in project %q.", imageID, projectID),
Expand All @@ -420,7 +425,7 @@ func (d *imageDataV2Source) Read(ctx context.Context, req datasource.ReadRequest
}

// Fetch all available images
imageList, err := d.client.DefaultAPI.ListImages(ctx, projectID, region).Execute()
imageList, err := d.client.ListImages(ctx, projectID, region).Execute()
if err != nil {
utils.LogError(ctx, &resp.Diagnostics, err, "List images", "Unable to fetch images", nil)
return
Expand Down
18 changes: 11 additions & 7 deletions stackit/internal/services/iaas/keypair/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/clientutils"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand All @@ -23,13 +23,17 @@ var (
)

// NewKeyPairDataSource is a helper function to simplify the provider implementation.
func NewKeyPairDataSource() datasource.DataSource {
return &keyPairDataSource{}
func NewKeyPairDataSource(clientFactory clientutils.ClientFactory) datasource.DataSource {
return &keyPairDataSource{
clientFactory: clientFactory,
}
}

// keyPairDataSource is the data source implementation.
type keyPairDataSource struct {
client *iaas.APIClient
clientFactory clientutils.ClientFactory

client iaas.DefaultAPI
}

// Metadata returns the data source type name.
Expand All @@ -43,11 +47,11 @@ func (d *keyPairDataSource) Configure(ctx context.Context, req datasource.Config
return
}

apiClient := iaasUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
d.client = d.clientFactory.NewIaaSV2Client(ctx, &providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient

tflog.Info(ctx, "iaas client configured")
}

Expand Down Expand Up @@ -98,7 +102,7 @@ func (d *keyPairDataSource) Read(ctx context.Context, req datasource.ReadRequest

ctx = tflog.SetField(ctx, "name", name)

keypairResp, err := d.client.DefaultAPI.GetKeyPair(ctx, name).Execute()
keypairResp, err := d.client.GetKeyPair(ctx, name).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
Loading
Loading