Skip to content

Commit 2adf8f0

Browse files
authored
feat(objectstorage): Onboard compliance lock (#1323)
STACKITTPR-502 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 91c0726 commit 2adf8f0

File tree

12 files changed

+603
-3
lines changed

12 files changed

+603
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_objectstorage_compliance_lock Data Source - stackit"
4+
subcategory: ""
5+
description: |-
6+
ObjectStorage compliance lock resource schema. Must have a region specified in the provider configuration.
7+
---
8+
9+
# stackit_objectstorage_compliance_lock (Data Source)
10+
11+
ObjectStorage compliance lock resource schema. Must have a `region` specified in the provider configuration.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "stackit_objectstorage_compliance_lock" "example" {
17+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `project_id` (String) STACKIT Project ID to which the compliance lock is associated.
27+
28+
### Optional
29+
30+
- `region` (String) The resource region. If not defined, the provider region is used.
31+
32+
### Read-Only
33+
34+
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`".
35+
- `max_retention_days` (Number) Maximum retention period in days.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_objectstorage_compliance_lock Resource - stackit"
4+
subcategory: ""
5+
description: |-
6+
ObjectStorage compliance lock resource schema. Must have a region specified in the provider configuration. Always use only one compliance lock per project.
7+
---
8+
9+
# stackit_objectstorage_compliance_lock (Resource)
10+
11+
ObjectStorage compliance lock resource schema. Must have a `region` specified in the provider configuration. Always use only one compliance lock per project.
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "stackit_objectstorage_compliance_lock" "example" {
17+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
18+
}
19+
```
20+
21+
<!-- schema generated by tfplugindocs -->
22+
## Schema
23+
24+
### Required
25+
26+
- `project_id` (String) STACKIT Project ID to which the compliance lock is associated.
27+
28+
### Optional
29+
30+
- `region` (String) The resource region. If not defined, the provider region is used.
31+
32+
### Read-Only
33+
34+
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`".
35+
- `max_retention_days` (Number) Maximum retention period in days.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "stackit_objectstorage_compliance_lock" "example" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "stackit_objectstorage_compliance_lock" "example" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.27.1
2626
github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.8.1
2727
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.7.1
28-
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.6.1
28+
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.7.0
2929
github.com/stackitcloud/stackit-sdk-go/services/observability v0.17.0
3030
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1
3131
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.8.1 h1:qBPfWK6Xp
181181
github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.8.1/go.mod h1:b8L6f68HZce01y+eZ1o7KTRAkgpWhggpvakAEwnxnCs=
182182
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.7.1 h1:8HFqfUI35Uk5QHUr3+VO21KXstzUl5zDKFrMuVOx+BI=
183183
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.7.1/go.mod h1:an5vc+0PL6OBMqFl75uuQpVGyzWHBpoxc9a5p41fFA8=
184-
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.6.1 h1:rOf829+57quGO2x3aG4dJJFgx4ZdtWqYE+hkW1tqGSY=
185-
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.6.1/go.mod h1:RFL4h6JZvpsyFYbdJ3+eINEkletzJQTfrPdd+yPT/fU=
184+
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.7.0 h1:UxnbsKm6PQV8Gudw/EhySaEh9q1xSaTG8mzJz1EvhnE=
185+
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.7.0/go.mod h1:RFL4h6JZvpsyFYbdJ3+eINEkletzJQTfrPdd+yPT/fU=
186186
github.com/stackitcloud/stackit-sdk-go/services/observability v0.17.0 h1:LGwCvvST0fwUgZ6bOxYIfu45qqTgv421ZS07UhKjZL8=
187187
github.com/stackitcloud/stackit-sdk-go/services/observability v0.17.0/go.mod h1:9KdrXC5JS30Ay3mR0adb3vNdhca+qxiy/cPF5P4wehQ=
188188
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.26.1 h1:AO5Np67/w0AUdhb6yk+CTXMzMkGdQPudmI8ryWp94fQ=
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package compliancelock
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
11+
"github.com/hashicorp/terraform-plugin-log/tflog"
12+
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
13+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
14+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
15+
objectstorageUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/objectstorage/utils"
16+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
17+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
18+
)
19+
20+
// Ensure the implementation satisfies the expected interfaces.
21+
var (
22+
_ datasource.DataSource = &compliancelockDataSource{}
23+
)
24+
25+
// NewComplianceLockDataSource is a helper function to simplify the provider implementation.
26+
func NewComplianceLockDataSource() datasource.DataSource {
27+
return &compliancelockDataSource{}
28+
}
29+
30+
// compliancelockDataSource is the data source implementation.
31+
type compliancelockDataSource struct {
32+
client *objectstorage.APIClient
33+
providerData core.ProviderData
34+
}
35+
36+
// Metadata returns the data source type name.
37+
func (d *compliancelockDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
38+
resp.TypeName = req.ProviderTypeName + "_objectstorage_compliance_lock"
39+
}
40+
41+
// Configure adds the provider configured client to the data source.
42+
func (d *compliancelockDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
43+
var ok bool
44+
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
45+
if !ok {
46+
return
47+
}
48+
49+
apiClient := objectstorageUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
50+
if resp.Diagnostics.HasError() {
51+
return
52+
}
53+
d.client = apiClient
54+
tflog.Info(ctx, "ObjectStorage compliance lock client configured")
55+
}
56+
57+
// Schema defines the schema for the resource.
58+
func (d *compliancelockDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
59+
descriptions := map[string]string{
60+
"main": "ObjectStorage compliance lock resource schema. Must have a `region` specified in the provider configuration.",
61+
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`region`\".",
62+
"project_id": "STACKIT Project ID to which the compliance lock is associated.",
63+
"region": "The resource region. If not defined, the provider region is used.",
64+
"max_retention_days": "Maximum retention period in days.",
65+
}
66+
67+
resp.Schema = schema.Schema{
68+
Description: descriptions["main"],
69+
Attributes: map[string]schema.Attribute{
70+
"id": schema.StringAttribute{
71+
Description: descriptions["id"],
72+
Computed: true,
73+
},
74+
"project_id": schema.StringAttribute{
75+
Description: descriptions["project_id"],
76+
Required: true,
77+
Validators: []validator.String{
78+
validate.UUID(),
79+
validate.NoSeparator(),
80+
},
81+
},
82+
"max_retention_days": schema.Int64Attribute{
83+
Description: descriptions["max_retention_days"],
84+
Computed: true,
85+
},
86+
"region": schema.StringAttribute{
87+
Optional: true,
88+
// the region cannot be found automatically, so it has to be passed
89+
Description: descriptions["region"],
90+
},
91+
},
92+
}
93+
}
94+
95+
// Read refreshes the Terraform state with the latest data.
96+
func (d *compliancelockDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
97+
var model Model
98+
diags := req.Config.Get(ctx, &model)
99+
resp.Diagnostics.Append(diags...)
100+
if resp.Diagnostics.HasError() {
101+
return
102+
}
103+
104+
ctx = core.InitProviderContext(ctx)
105+
106+
projectId := model.ProjectId.ValueString()
107+
region := d.providerData.GetRegionWithOverride(model.Region)
108+
109+
ctx = tflog.SetField(ctx, "project_id", projectId)
110+
ctx = tflog.SetField(ctx, "region", region)
111+
112+
complianceResp, err := d.client.GetComplianceLock(ctx, projectId, region).Execute()
113+
if err != nil {
114+
utils.LogError(
115+
ctx,
116+
&resp.Diagnostics,
117+
err,
118+
"Reading compliance lock",
119+
fmt.Sprintf("Compliance lock does not exist in project %q.", projectId),
120+
map[int]string{
121+
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
122+
},
123+
)
124+
resp.State.RemoveResource(ctx)
125+
return
126+
}
127+
128+
ctx = core.LogResponse(ctx)
129+
130+
// Map response body to schema
131+
err = mapFields(complianceResp, &model, region)
132+
if err != nil {
133+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading compliance lock", fmt.Sprintf("Processing API payload: %v", err))
134+
return
135+
}
136+
137+
// Set refreshed state
138+
diags = resp.State.Set(ctx, model)
139+
resp.Diagnostics.Append(diags...)
140+
if resp.Diagnostics.HasError() {
141+
return
142+
}
143+
tflog.Info(ctx, "ObjectStorage compliance lock read")
144+
}

0 commit comments

Comments
 (0)