Skip to content

Commit 32b429a

Browse files
Flatten datasource schema
1 parent eaddee9 commit 32b429a

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

octopusdeploy_framework/datasource_space_default_retention_policies.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package octopusdeploy_framework
22

33
import (
44
"context"
5-
"time"
65

76
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/retention"
87
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
98
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
10-
"github.com/hashicorp/terraform-plugin-framework/attr"
119
"github.com/hashicorp/terraform-plugin-framework/datasource"
1210
"github.com/hashicorp/terraform-plugin-framework/types"
1311
)
@@ -49,23 +47,18 @@ func (s *spaceDefaultRetentionPoliciesDataSource) Read(ctx context.Context, req
4947
return
5048
}
5149

52-
util.DatasourceResultCount(ctx, "space_default_retention_policy", 1)
53-
retentionPolicy, diag := types.ObjectValue(schemas.GetSpaceDefaultRetentionPolicyAttributes(), map[string]attr.Value{
54-
"id": types.StringValue(existingPolicy.GetID()),
55-
"space_id": types.StringValue(existingPolicy.GetSpaceID()),
56-
"retention_type": types.StringValue(string(existingPolicy.RetentionType)),
57-
"strategy": types.StringValue(string(existingPolicy.Strategy)),
58-
"quantity_to_keep": types.Int64Value(int64(existingPolicy.QuantityToKeep)),
59-
"unit": types.StringValue(string(existingPolicy.Unit)),
60-
})
61-
data.RetentionPolicy = retentionPolicy
62-
data.ID = types.StringValue("Space Default Retention Policy " + time.Now().UTC().String())
63-
resp.Diagnostics.Append(diag...)
64-
50+
updateRetentionPolicyDatasourceModelFromResource(&data, existingPolicy)
6551
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
6652
}
6753

6854
// Schema implements datasource.DataSource.
6955
func (s *spaceDefaultRetentionPoliciesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
7056
resp.Schema = schemas.SpaceDefaultRetentionPolicySchema{}.GetDatasourceSchema()
7157
}
58+
59+
func updateRetentionPolicyDatasourceModelFromResource(data *schemas.SpaceDefaultRetentionPoliciesDataSourceModel, resource *retention.SpaceDefaultRetentionPolicyResource) {
60+
data.ID = types.StringValue(resource.GetID())
61+
data.Strategy = types.StringValue(resource.Strategy)
62+
data.QuantityToKeep = util.Ternary(resource.QuantityToKeep == 0, types.Int64Null(), types.Int64Value(int64(resource.QuantityToKeep)))
63+
data.Unit = util.Ternary(resource.Unit == "", types.StringNull(), types.StringValue(resource.Unit))
64+
}

octopusdeploy_framework/datasource_space_default_retention_policies_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ func TestAccDataSourceSpaceDefaultRetentionPolicy(t *testing.T) {
2121
{
2222
Config: testAccDataSourceSpaceDefaultRetentionPolicyConfig(spaceName, retentionType),
2323
Check: resource.ComposeTestCheckFunc(
24-
resource.TestCheckResourceAttrSet(resourceName, "retention_policy.id"),
25-
resource.TestCheckResourceAttrSet(resourceName, "retention_policy.space_id"),
26-
resource.TestCheckResourceAttr(resourceName, "retention_policy.retention_type", retentionType),
27-
resource.TestCheckResourceAttrSet(resourceName, "retention_policy.strategy"),
24+
resource.TestCheckResourceAttrSet(resourceName, "id"),
25+
resource.TestCheckResourceAttrSet(resourceName, "space_id"),
26+
resource.TestCheckResourceAttr(resourceName, "retention_type", retentionType),
27+
resource.TestCheckResourceAttrSet(resourceName, "strategy"),
2828
),
2929
},
3030
},

octopusdeploy_framework/schemas/space_default_retention_policy.go

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
55
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
66
"github.com/hashicorp/terraform-plugin-framework/attr"
7-
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
87
ds "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
98
rs "github.com/hashicorp/terraform-plugin-framework/resource/schema"
109
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -34,20 +33,10 @@ func (s SpaceDefaultRetentionPolicySchema) GetDatasourceSchema() ds.Schema {
3433
"retention_type": util.ResourceString().Description("The type of retention policy.").Required().Validators(stringvalidator.OneOf("LifecycleRelease", "LifecycleTentacle")).Build(),
3534

3635
// response
37-
"id": GetIdDatasourceSchema(true),
38-
},
39-
Blocks: map[string]ds.Block{
40-
"retention_policy": schema.SingleNestedBlock{
41-
Description: "The default retention policy for the space.",
42-
Attributes: map[string]ds.Attribute{
43-
"id": util.ResourceString().Description("The ID of the retention policy.").Computed().Build(),
44-
"space_id": util.ResourceString().Description("The ID of the space.").Computed().Build(),
45-
"retention_type": util.ResourceString().Description("The type of retention policy.").Computed().Build(),
46-
"strategy": util.ResourceString().Description("The strategy for the retention policy.").Computed().Build(),
47-
"quantity_to_keep": util.ResourceInt64().Description("The quantity of items to keep.").Computed().Optional().Build(),
48-
"unit": util.ResourceString().Description("The unit of time for the retention policy.").Computed().Optional().Build(),
49-
},
50-
},
36+
"id": util.ResourceString().Description("The ID of the retention policy.").Computed().Build(),
37+
"strategy": util.ResourceString().Description("The strategy for the retention policy.").Computed().Build(),
38+
"quantity_to_keep": util.ResourceInt64().Description("The quantity of items to keep.").Computed().Optional().Build(),
39+
"unit": util.ResourceString().Description("The unit of time for the retention policy.").Computed().Optional().Build(),
5140
},
5241
}
5342
}
@@ -57,8 +46,10 @@ func (s SpaceDefaultRetentionPolicySchema) GetResourceSchema() rs.Schema {
5746
}
5847

5948
type SpaceDefaultRetentionPoliciesDataSourceModel struct {
60-
ID types.String `tfsdk:"id"`
61-
SpaceID types.String `tfsdk:"space_id"`
62-
RetentionType types.String `tfsdk:"retention_type"`
63-
RetentionPolicy types.Object `tfsdk:"retention_policy"`
49+
ID types.String `tfsdk:"id"`
50+
SpaceID types.String `tfsdk:"space_id"`
51+
RetentionType types.String `tfsdk:"retention_type"`
52+
Strategy types.String `tfsdk:"strategy"`
53+
QuantityToKeep types.Int64 `tfsdk:"quantity_to_keep"`
54+
Unit types.String `tfsdk:"unit"`
6455
}

0 commit comments

Comments
 (0)