Object storage improvements (#84)

* Add example

* Rename bucket_name --> bucket

* Fix examples in wrong folder

* Add example

* Lint examples

* Fix examples

---------

Co-authored-by: Henrique Santos <henrique.santos@freiheit.com>
This commit is contained in:
Henrique Santos 2023-10-13 18:33:45 +01:00 committed by GitHub
parent 7334c802ad
commit 6e51bdd5bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 102 additions and 48 deletions

View file

@ -10,18 +10,25 @@ description: |-
ObjectStorage bucket data source schema.
## Example Usage
```terraform
data "stackit_objectstorage_bucket" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-name"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `bucket_name` (String) The bucket name. It must be DNS conform.
- `name` (String) The bucket name. It must be DNS conform.
- `project_id` (String) STACKIT Project ID to which the bucket is associated.
### Read-Only
- `id` (String) Terraform's internal data source identifier. It is structured as "`project_id`,`bucket_name`".
- `id` (String) Terraform's internal data source identifier. It is structured as "`project_id`,`name`".
- `url_path_style` (String)
- `url_virtual_hosted_style` (String)

View file

@ -10,7 +10,15 @@ description: |-
ObjectStorage credential data source schema.
## Example Usage
```terraform
data "stackit_objectstorage_credentials_group" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_group_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema

View file

@ -10,7 +10,14 @@ description: |-
ObjectStorage credentials group data source schema.
## Example Usage
```terraform
data "stackit_objectstorage_credentials_group" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_group_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```
<!-- schema generated by tfplugindocs -->
## Schema

View file

@ -10,18 +10,25 @@ description: |-
ObjectStorage bucket resource schema.
## Example Usage
```terraform
resource "stackit_objectstorage_bucket" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-bucket"
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `bucket_name` (String) The bucket name. It must be DNS conform.
- `name` (String) The bucket name. It must be DNS conform.
- `project_id` (String) STACKIT Project ID to which the bucket is associated.
### Read-Only
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`bucket_name`".
- `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`name`".
- `url_path_style` (String)
- `url_virtual_hosted_style` (String)

View file

@ -10,7 +10,15 @@ description: |-
ObjectStorage credential resource schema.
## Example Usage
```terraform
resource "stackit_objectstorage_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_group_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
expiration_timestamp = "2027-01-02T03:04:05Z"
}
```
<!-- schema generated by tfplugindocs -->
## Schema

View file

@ -10,7 +10,14 @@ description: |-
ObjectStorage credentials group resource schema.
## Example Usage
```terraform
resource "stackit_objectstorage_credentials_group" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-credentials-group"
}
```
<!-- schema generated by tfplugindocs -->
## Schema

View file

@ -1,4 +0,0 @@
data "stackit_objectstorage_bucket" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
bucket_name = "example-name"
}

View file

@ -0,0 +1,4 @@
data "stackit_objectstorage_bucket" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-name"
}

View file

@ -0,0 +1,5 @@
data "stackit_objectstorage_credentials_group" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_group_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credential_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}

View file

@ -1,4 +0,0 @@
resource "stackit_object_storage_bucket" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
bucket_name = "example-bucket"
}

View file

@ -0,0 +1,4 @@
resource "stackit_objectstorage_bucket" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-bucket"
}

View file

@ -0,0 +1,5 @@
resource "stackit_objectstorage_credential" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
credentials_group_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
expiration_timestamp = "2027-01-02T03:04:05Z"
}

View file

@ -1,4 +1,4 @@
resource "stackit_object_storage_credentials_group" "example" {
resource "stackit_objectstorage_credentials_group" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-credentials-group"
}

View file

@ -75,8 +75,8 @@ func (r *bucketDataSource) Configure(ctx context.Context, req datasource.Configu
func (r *bucketDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{
"main": "ObjectStorage bucket data source schema.",
"id": "Terraform's internal data source identifier. It is structured as \"`project_id`,`bucket_name`\".",
"bucket_name": "The bucket name. It must be DNS conform.",
"id": "Terraform's internal data source identifier. It is structured as \"`project_id`,`name`\".",
"name": "The bucket name. It must be DNS conform.",
"project_id": "STACKIT Project ID to which the bucket is associated.",
"url_path_style": "URL in path style.",
"url_virtual_hosted_style": "URL in virtual hosted style.",
@ -89,8 +89,8 @@ func (r *bucketDataSource) Schema(_ context.Context, _ datasource.SchemaRequest,
Description: descriptions["id"],
Computed: true,
},
"bucket_name": schema.StringAttribute{
Description: descriptions["bucket_name"],
"name": schema.StringAttribute{
Description: descriptions["name"],
Required: true,
Validators: []validator.String{
validate.NoSeparator(),
@ -123,9 +123,9 @@ func (r *bucketDataSource) Read(ctx context.Context, req datasource.ReadRequest,
return
}
projectId := model.ProjectId.ValueString()
bucketName := model.BucketName.ValueString()
bucketName := model.Name.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "bucket_name", bucketName)
ctx = tflog.SetField(ctx, "name", bucketName)
bucketResp, err := r.client.GetBucket(ctx, projectId, bucketName).Execute()
if err != nil {

View file

@ -31,7 +31,7 @@ var (
type Model struct {
Id types.String `tfsdk:"id"` // needed by TF
BucketName types.String `tfsdk:"bucket_name"`
Name types.String `tfsdk:"name"`
ProjectId types.String `tfsdk:"project_id"`
URLPathStyle types.String `tfsdk:"url_path_style"`
URLVirtualHostedStyle types.String `tfsdk:"url_virtual_hosted_style"`
@ -92,8 +92,8 @@ func (r *bucketResource) Configure(ctx context.Context, req resource.ConfigureRe
func (r *bucketResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{
"main": "ObjectStorage bucket resource schema.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`bucket_name`\".",
"bucket_name": "The bucket name. It must be DNS conform.",
"id": "Terraform's internal resource identifier. It is structured as \"`project_id`,`name`\".",
"name": "The bucket name. It must be DNS conform.",
"project_id": "STACKIT Project ID to which the bucket is associated.",
"url_path_style": "URL in path style.",
"url_virtual_hosted_style": "URL in virtual hosted style.",
@ -109,8 +109,8 @@ func (r *bucketResource) Schema(_ context.Context, _ resource.SchemaRequest, res
stringplanmodifier.UseStateForUnknown(),
},
},
"bucket_name": schema.StringAttribute{
Description: descriptions["bucket_name"],
"name": schema.StringAttribute{
Description: descriptions["name"],
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
@ -151,9 +151,9 @@ func (r *bucketResource) Create(ctx context.Context, req resource.CreateRequest,
return
}
projectId := model.ProjectId.ValueString()
bucketName := model.BucketName.ValueString()
bucketName := model.Name.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "bucket_name", bucketName)
ctx = tflog.SetField(ctx, "name", bucketName)
// Handle project init
err := enableProject(ctx, &model, r.client)
@ -203,9 +203,9 @@ func (r *bucketResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}
projectId := model.ProjectId.ValueString()
bucketName := model.BucketName.ValueString()
bucketName := model.Name.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "bucket_name", bucketName)
ctx = tflog.SetField(ctx, "name", bucketName)
bucketResp, err := r.client.GetBucket(ctx, projectId, bucketName).Execute()
if err != nil {
@ -244,9 +244,9 @@ func (r *bucketResource) Delete(ctx context.Context, req resource.DeleteRequest,
return
}
projectId := model.ProjectId.ValueString()
bucketName := model.BucketName.ValueString()
bucketName := model.Name.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "bucket_name", bucketName)
ctx = tflog.SetField(ctx, "name", bucketName)
// Delete existing bucket
_, err := r.client.DeleteBucket(ctx, projectId, bucketName).Execute()
@ -262,19 +262,19 @@ func (r *bucketResource) Delete(ctx context.Context, req resource.DeleteRequest,
}
// ImportState imports a resource into the Terraform state on success.
// The expected format of the resource import identifier is: project_id,bucket_name
// The expected format of the resource import identifier is: project_id,name
func (r *bucketResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing bucket",
fmt.Sprintf("Expected import identifier with format [project_id],[bucket_name], got %q", req.ID),
fmt.Sprintf("Expected import identifier with format [project_id],[name], got %q", req.ID),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("bucket_name"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[1])...)
tflog.Info(ctx, "ObjectStorage bucket state imported")
}
@ -292,7 +292,7 @@ func mapFields(bucketResp *objectstorage.GetBucketResponse, model *Model) error
idParts := []string{
model.ProjectId.ValueString(),
model.BucketName.ValueString(),
model.Name.ValueString(),
}
model.Id = types.StringValue(
strings.Join(idParts, core.Separator),

View file

@ -39,7 +39,7 @@ func TestMapFields(t *testing.T) {
},
Model{
Id: types.StringValue("pid,bname"),
BucketName: types.StringValue("bname"),
Name: types.StringValue("bname"),
ProjectId: types.StringValue("pid"),
URLPathStyle: types.StringNull(),
URLVirtualHostedStyle: types.StringNull(),
@ -56,7 +56,7 @@ func TestMapFields(t *testing.T) {
},
Model{
Id: types.StringValue("pid,bname"),
BucketName: types.StringValue("bname"),
Name: types.StringValue("bname"),
ProjectId: types.StringValue("pid"),
URLPathStyle: types.StringValue("url/path/style"),
URLVirtualHostedStyle: types.StringValue("url/virtual/hosted/style"),
@ -73,7 +73,7 @@ func TestMapFields(t *testing.T) {
},
Model{
Id: types.StringValue("pid,bname"),
BucketName: types.StringValue("bname"),
Name: types.StringValue("bname"),
ProjectId: types.StringValue("pid"),
URLPathStyle: types.StringValue(""),
URLVirtualHostedStyle: types.StringValue(""),
@ -96,8 +96,8 @@ func TestMapFields(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
model := &Model{
ProjectId: tt.expected.ProjectId,
BucketName: tt.expected.BucketName,
ProjectId: tt.expected.ProjectId,
Name: tt.expected.Name,
}
err := mapFields(tt.input, model)
if !tt.isValid && err == nil {

View file

@ -20,8 +20,8 @@ import (
// Bucket resource data
var bucketResource = map[string]string{
"project_id": testutil.ProjectId,
"bucket_name": fmt.Sprintf("acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)),
"project_id": testutil.ProjectId,
"name": fmt.Sprintf("acc-test-%s", acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)),
}
// Credentials group resource data
@ -41,7 +41,7 @@ func resourceConfig() string {
resource "stackit_objectstorage_bucket" "bucket" {
project_id = "%s"
bucket_name = "%s"
name = "%s"
}
resource "stackit_objectstorage_credentials_group" "credentials_group" {
@ -57,7 +57,7 @@ func resourceConfig() string {
`,
testutil.ObjectStorageProviderConfig(),
bucketResource["project_id"],
bucketResource["bucket_name"],
bucketResource["name"],
credentialsGroupResource["project_id"],
credentialsGroupResource["name"],
credentialResource["expiration_timestamp"],
@ -76,7 +76,7 @@ func TestAccObjectStorageResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
// Bucket data
resource.TestCheckResourceAttr("stackit_objectstorage_bucket.bucket", "project_id", bucketResource["project_id"]),
resource.TestCheckResourceAttr("stackit_objectstorage_bucket.bucket", "bucket_name", bucketResource["bucket_name"]),
resource.TestCheckResourceAttr("stackit_objectstorage_bucket.bucket", "name", bucketResource["name"]),
resource.TestCheckResourceAttrSet("stackit_objectstorage_bucket.bucket", "url_path_style"),
resource.TestCheckResourceAttrSet("stackit_objectstorage_bucket.bucket", "url_virtual_hosted_style"),
@ -109,7 +109,7 @@ func TestAccObjectStorageResource(t *testing.T) {
data "stackit_objectstorage_bucket" "bucket" {
project_id = stackit_objectstorage_bucket.bucket.project_id
bucket_name = stackit_objectstorage_bucket.bucket.bucket_name
name = stackit_objectstorage_bucket.bucket.name
}
data "stackit_objectstorage_credentials_group" "credentials_group" {
@ -128,8 +128,8 @@ func TestAccObjectStorageResource(t *testing.T) {
// Bucket data
resource.TestCheckResourceAttr("data.stackit_objectstorage_bucket.bucket", "project_id", bucketResource["project_id"]),
resource.TestCheckResourceAttrPair(
"stackit_objectstorage_bucket.bucket", "bucket_name",
"data.stackit_objectstorage_bucket.bucket", "bucket_name",
"stackit_objectstorage_bucket.bucket", "name",
"data.stackit_objectstorage_bucket.bucket", "name",
),
resource.TestCheckResourceAttrPair(
"stackit_objectstorage_bucket.bucket", "url_path_style",
@ -244,7 +244,7 @@ func testAccCheckObjectStorageDestroy(s *terraform.State) error {
if rs.Type != "stackit_objectstorage_bucket" {
continue
}
// bucket terraform ID: "[project_id],[bucket_name]"
// bucket terraform ID: "[project_id],[name]"
bucketName := strings.Split(rs.Primary.ID, core.Separator)[1]
bucketsToDestroy = append(bucketsToDestroy, bucketName)
}