Skip to content
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
40 changes: 18 additions & 22 deletions stackit/internal/services/cdn/customdomain/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

cdnSdk "github.com/stackitcloud/stackit-sdk-go/services/cdn/v1api"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
cdnUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/cdn/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
Expand All @@ -27,11 +27,11 @@ var (
)

var certificateDataSourceTypes = map[string]attr.Type{
"version": types.Int64Type,
"version": types.Int32Type,
}

type customDomainDataSource struct {
client *cdn.APIClient
client *cdnSdk.APIClient
}

func NewCustomDomainDataSource() datasource.DataSource {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (r *customDomainDataSource) Schema(_ context.Context, _ datasource.SchemaRe
Description: certificateSchemaDescriptions["main"],
Optional: true,
Attributes: map[string]schema.Attribute{
"version": schema.Int64Attribute{
"version": schema.Int32Attribute{
Description: certificateSchemaDescriptions["version"],
Computed: true,
},
Expand All @@ -133,7 +133,7 @@ func (r *customDomainDataSource) Read(ctx context.Context, req datasource.ReadRe
name := model.Name.ValueString()
ctx = tflog.SetField(ctx, "name", name)

customDomainResp, err := r.client.GetCustomDomain(ctx, projectId, distributionId, name).Execute()
customDomainResp, err := r.client.DefaultAPI.GetCustomDomain(ctx, projectId, distributionId, name).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down Expand Up @@ -165,23 +165,19 @@ func (r *customDomainDataSource) Read(ctx context.Context, req datasource.ReadRe
tflog.Info(ctx, "CDN custom domain read")
}

func mapCustomDomainDataSourceFields(customDomainResponse *cdn.GetCustomDomainResponse, model *customDomainDataSourceModel, projectId, distributionId string) error {
func mapCustomDomainDataSourceFields(customDomainResponse *cdnSdk.GetCustomDomainResponse, model *customDomainDataSourceModel, projectId, distributionId string) error {
if customDomainResponse == nil {
return fmt.Errorf("response input is nil")
}
if model == nil {
return fmt.Errorf("model input is nil")
}

if customDomainResponse.CustomDomain == nil {
return fmt.Errorf("CustomDomain is missing in response")
if customDomainResponse.CustomDomain.Name == "" {
return fmt.Errorf("name is empty in response")
}

if customDomainResponse.CustomDomain.Name == nil {
return fmt.Errorf("name is missing in response")
}
if customDomainResponse.CustomDomain.Status == nil {
return fmt.Errorf("status missing in response")
if customDomainResponse.CustomDomain.Status == "" {
return fmt.Errorf("status is empty in response")
}

normalizedCert, err := normalizeCertificate(customDomainResponse.Certificate)
Expand All @@ -194,9 +190,9 @@ func mapCustomDomainDataSourceFields(customDomainResponse *cdn.GetCustomDomainRe
model.Certificate = types.ObjectNull(certificateDataSourceTypes)
} else {
// For custom certificates, we only care about the version.
version := types.Int64Null()
version := types.Int32Null()
if normalizedCert.Version != nil {
version = types.Int64Value(*normalizedCert.Version)
version = types.Int32Value(*normalizedCert.Version)
}

certificateObj, diags := types.ObjectValue(certificateDataSourceTypes, map[string]attr.Value{
Expand All @@ -208,16 +204,16 @@ func mapCustomDomainDataSourceFields(customDomainResponse *cdn.GetCustomDomainRe
model.Certificate = certificateObj
}

model.ID = types.StringValue(fmt.Sprintf("%s,%s,%s", projectId, distributionId, *customDomainResponse.CustomDomain.Name))
model.Status = types.StringValue(string(*customDomainResponse.CustomDomain.Status))
model.ID = types.StringValue(fmt.Sprintf("%s,%s,%s", projectId, distributionId, customDomainResponse.CustomDomain.Name))
model.Status = types.StringValue(string(customDomainResponse.CustomDomain.Status))

customDomainErrors := []attr.Value{}
if customDomainResponse.CustomDomain.Errors != nil {
for _, e := range *customDomainResponse.CustomDomain.Errors {
if e.En == nil {
for _, e := range customDomainResponse.CustomDomain.Errors {
if e.En == "" {
return fmt.Errorf("error description missing")
}
customDomainErrors = append(customDomainErrors, types.StringValue(*e.En))
customDomainErrors = append(customDomainErrors, types.StringValue(e.En))
}
}
modelErrors, diags := types.ListValue(types.StringType, customDomainErrors)
Expand All @@ -229,7 +225,7 @@ func mapCustomDomainDataSourceFields(customDomainResponse *cdn.GetCustomDomainRe
// Also map the fields back to the model from the config
model.ProjectId = types.StringValue(projectId)
model.DistributionId = types.StringValue(distributionId)
model.Name = types.StringValue(*customDomainResponse.CustomDomain.Name)
model.Name = types.StringValue(customDomainResponse.CustomDomain.Name)

return nil
}
50 changes: 25 additions & 25 deletions stackit/internal/services/cdn/customdomain/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
cdnSdk "github.com/stackitcloud/stackit-sdk-go/services/cdn/v1api"
)

func TestMapDataSourceFields(t *testing.T) {
emtpyErrorsList := types.ListValueMust(types.StringType, []attr.Value{})

// Expected certificate object when a custom certificate is returned
certAttributes := map[string]attr.Value{
"version": types.Int64Value(3),
"version": types.Int32Value(3),
}
certificateObj, _ := types.ObjectValue(certificateDataSourceTypes, certAttributes)

Expand All @@ -37,30 +37,30 @@ func TestMapDataSourceFields(t *testing.T) {

// API response fixtures for custom and managed certificates
customType := "custom"
customVersion := int64(3)
getRespCustom := cdn.GetCustomDomainResponseGetCertificateAttributeType(&cdn.GetCustomDomainResponseCertificate{
GetCustomDomainCustomCertificate: &cdn.GetCustomDomainCustomCertificate{
Type: &customType,
Version: &customVersion,
customVersion := int32(3)
getRespCustom := cdnSdk.GetCustomDomainResponseCertificate{
GetCustomDomainCustomCertificate: &cdnSdk.GetCustomDomainCustomCertificate{
Type: customType,
Version: customVersion,
},
})
}

managedType := "managed"
getRespManaged := cdn.GetCustomDomainResponseGetCertificateAttributeType(&cdn.GetCustomDomainResponseCertificate{
GetCustomDomainManagedCertificate: &cdn.GetCustomDomainManagedCertificate{
Type: &managedType,
getRespManaged := cdnSdk.GetCustomDomainResponseCertificate{
GetCustomDomainManagedCertificate: &cdnSdk.GetCustomDomainManagedCertificate{
Type: managedType,
},
})
}

// Helper to create API response fixtures
customDomainFixture := func(mods ...func(*cdn.GetCustomDomainResponse)) *cdn.GetCustomDomainResponse {
distribution := &cdn.CustomDomain{
Errors: &[]cdn.StatusError{},
Name: new("https://testdomain.com"),
Status: cdn.DOMAINSTATUS_ACTIVE.Ptr(),
customDomainFixture := func(mods ...func(*cdnSdk.GetCustomDomainResponse)) *cdnSdk.GetCustomDomainResponse {
distribution := &cdnSdk.CustomDomain{
Errors: []cdnSdk.StatusError{},
Name: "https://testdomain.com",
Status: cdnSdk.DOMAINSTATUS_ACTIVE,
}
customDomainResponse := &cdn.GetCustomDomainResponse{
CustomDomain: distribution,
customDomainResponse := &cdnSdk.GetCustomDomainResponse{
CustomDomain: *distribution,
Certificate: getRespCustom,
}

Expand All @@ -72,7 +72,7 @@ func TestMapDataSourceFields(t *testing.T) {

// Test cases
tests := map[string]struct {
Input *cdn.GetCustomDomainResponse
Input *cdnSdk.GetCustomDomainResponse
Expected *customDomainDataSourceModel
IsValid bool
}{
Expand All @@ -87,7 +87,7 @@ func TestMapDataSourceFields(t *testing.T) {
Expected: expectedModel(func(m *customDomainDataSourceModel) {
m.Certificate = types.ObjectNull(certificateDataSourceTypes)
}),
Input: customDomainFixture(func(gcdr *cdn.GetCustomDomainResponse) {
Input: customDomainFixture(func(gcdr *cdnSdk.GetCustomDomainResponse) {
gcdr.Certificate = getRespManaged
}),
IsValid: true,
Expand All @@ -97,8 +97,8 @@ func TestMapDataSourceFields(t *testing.T) {
m.Status = types.StringValue("ERROR")
m.Certificate = certificateObj
}),
Input: customDomainFixture(func(d *cdn.GetCustomDomainResponse) {
d.CustomDomain.Status = cdn.DOMAINSTATUS_ERROR.Ptr()
Input: customDomainFixture(func(d *cdnSdk.GetCustomDomainResponse) {
d.CustomDomain.Status = cdnSdk.DOMAINSTATUS_ERROR
}),
IsValid: true,
},
Expand All @@ -109,8 +109,8 @@ func TestMapDataSourceFields(t *testing.T) {
},
"sad_path_name_missing": {
Expected: expectedModel(),
Input: customDomainFixture(func(d *cdn.GetCustomDomainResponse) {
d.CustomDomain.Name = nil
Input: customDomainFixture(func(d *cdnSdk.GetCustomDomainResponse) {
d.CustomDomain.Name = ""
}),
IsValid: false,
},
Expand Down
Loading
Loading