-
Notifications
You must be signed in to change notification settings - Fork 75
feat(objectstorage): add default retention for buckets #1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_objectstorage_default_retention Data Source - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| ObjectStorage default-retention resource schema. Must have a region specified in the provider configuration. | ||
| --- | ||
|
|
||
| # stackit_objectstorage_default_retention (Data Source) | ||
|
|
||
| ObjectStorage default-retention resource schema. Must have a `region` specified in the provider configuration. | ||
|
|
||
|
|
||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `bucket_name` (String) The associated bucket's name. It must be DNS conform. | ||
| - `days` (Number) The number retention period in days. | ||
| - `mode` (String) The retention mode for default retention on a bucket. | ||
| - `project_id` (String) STACKIT Project ID to which the default-retention is associated. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) The resource region. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`,`bucket_name`". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_objectstorage_default_retention Resource - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| ObjectStorage default-retention resource schema. Must have a region specified in the provider configuration. | ||
| --- | ||
|
|
||
| # stackit_objectstorage_default_retention (Resource) | ||
|
|
||
| ObjectStorage default-retention resource schema. Must have a `region` specified in the provider configuration. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```terraform | ||
| resource "stackit_objectstorage_compliance_lock" "example_bucket" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } | ||
|
|
||
| resource "stackit_objectstorage_default_retention" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| bucket_name = stackit_objectstorage_compliance_lock.example_bucket.name | ||
| days = var.retention_days | ||
| mode = var.retention_mode | ||
| } | ||
| ``` | ||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `bucket_name` (String) The associated bucket's name. It must be DNS conform. | ||
| - `days` (Number) The number retention period in days. | ||
| - `mode` (String) The retention mode for default retention on a bucket. | ||
| - `project_id` (String) STACKIT Project ID to which the default-retention is associated. | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) The resource region. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`,`bucket_name`". | ||
|
|
||
| ## Import | ||
|
|
||
| Import is supported using the following syntax: | ||
|
|
||
| In Terraform v1.5.0 and later, the [` + "`" + `import` + "`" + ` block](https://developer.hashicorp.com/terraform/language/import) can be used with the ` + "`" + `id` + "`" + ` attribute, for example: | ||
|
|
||
| ```terraform | ||
| # Only use the import statement, if you want to import an existing objectstorage default retention | ||
| import { | ||
| to = stackit_objectstorage_default_retention.import-example | ||
| id = "${var.project_id},${var.region},${var.bucket_name}" | ||
| } | ||
| ``` | ||
5 changes: 5 additions & 0 deletions
5
examples/resources/stackit_objectstorage_default_retention/import-by-string-id.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Only use the import statement, if you want to import an existing objectstorage default retention | ||
| import { | ||
| to = stackit_objectstorage_default_retention.import-example | ||
| id = "${var.project_id},${var.region},${var.bucket_name}" | ||
| } |
10 changes: 10 additions & 0 deletions
10
examples/resources/stackit_objectstorage_default_retention/resource.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| resource "stackit_objectstorage_compliance_lock" "example_bucket" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } | ||
|
|
||
| resource "stackit_objectstorage_default_retention" "example" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| bucket_name = stackit_objectstorage_compliance_lock.example_bucket.name | ||
| days = var.retention_days | ||
| mode = var.retention_mode | ||
| } |
145 changes: 145 additions & 0 deletions
145
stackit/internal/services/objectstorage/default-retention/datasource.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package objectstorage | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||
| sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api" | ||
|
|
||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||
| objectstorageUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/objectstorage/utils" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||
| ) | ||
|
|
||
| var ( | ||
| _ datasource.DataSourceWithConfigure = &defaultRetentionDataSource{} | ||
| ) | ||
|
|
||
| func NewDefaultRetentionDataSource() datasource.DataSource { | ||
| return &defaultRetentionDataSource{} | ||
| } | ||
|
|
||
| type defaultRetentionDataSource struct { | ||
| client *objectstorage.APIClient | ||
| providerData core.ProviderData | ||
| } | ||
|
|
||
| func (r *defaultRetentionDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
| var ok bool | ||
| r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| apiClient := objectstorageUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| r.client = apiClient | ||
| tflog.Info(ctx, "Bucket Default Retention configured") | ||
| } | ||
|
|
||
| func (d *defaultRetentionDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { // nolint:gocritic | ||
| resp.Schema = schema.Schema{ | ||
| Description: descriptions["main"], | ||
| Attributes: map[string]schema.Attribute{ | ||
| "id": schema.StringAttribute{ | ||
| Description: descriptions["id"], | ||
| Computed: true, | ||
| }, | ||
| "bucket_name": schema.StringAttribute{ | ||
| Description: descriptions["bucket_name"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "project_id": schema.StringAttribute{ | ||
| Description: descriptions["project_id"], | ||
| Required: true, | ||
| Validators: []validator.String{ | ||
| validate.UUID(), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "days": schema.Int32Attribute{ | ||
| Required: true, | ||
| Description: descriptions["days"], | ||
| }, | ||
| "mode": schema.StringAttribute{ | ||
| Required: true, | ||
| Description: descriptions["mode"], | ||
| Validators: []validator.String{ | ||
| stringvalidator.OneOf(sdkUtils.EnumSliceToStringSlice(objectstorage.AllowedRetentionModeEnumValues)...), | ||
| validate.NoSeparator(), | ||
| }, | ||
| }, | ||
| "region": schema.StringAttribute{ | ||
| Optional: true, | ||
| // must be computed to allow for storing the override value from the provider | ||
| Computed: true, | ||
| Description: descriptions["region"], | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (d *defaultRetentionDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
| resp.TypeName = req.ProviderTypeName + "_objectstorage_default_retention" | ||
| } | ||
|
|
||
| func (d *defaultRetentionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic | ||
| var model model | ||
| diags := req.Config.Get(ctx, &model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| ctx = core.InitProviderContext(ctx) | ||
|
|
||
| projectId := model.ProjectId.ValueString() | ||
| bucketName := model.BucketName.ValueString() | ||
| region := d.providerData.GetRegionWithOverride(model.Region) | ||
|
|
||
| ctx = tflog.SetField(ctx, "project_id", projectId) | ||
| ctx = tflog.SetField(ctx, "bucket_name", bucketName) | ||
| ctx = tflog.SetField(ctx, "region", region) | ||
|
|
||
| // Read default-retention | ||
| result, err := d.client.DefaultAPI.GetDefaultRetention(ctx, projectId, region, bucketName).Execute() | ||
| if err != nil { | ||
| var oapiErr *oapierror.GenericOpenAPIError | ||
| if errors.As(err, &oapiErr) && oapiErr.StatusCode == http.StatusNotFound { | ||
| resp.State.RemoveResource(ctx) | ||
| return | ||
| } | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading default-retention", fmt.Sprintf("Calling API: %v", err)) | ||
| return | ||
| } | ||
|
|
||
| ctx = core.LogResponse(ctx) | ||
|
|
||
| err = mapFields(result, &model, region) | ||
| if err != nil { | ||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading default-retention", fmt.Sprintf("Processing API payload: %v", err)) | ||
| return | ||
| } | ||
|
|
||
| // Set refreshed state | ||
| diags = resp.State.Set(ctx, model) | ||
| resp.Diagnostics.Append(diags...) | ||
| if resp.Diagnostics.HasError() { | ||
| return | ||
| } | ||
| tflog.Info(ctx, "ObjectStorage default-retention read") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.