Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Oct 30, 2024
1 parent e19c424 commit 570ce45
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
19 changes: 19 additions & 0 deletions examples/sos-bucket-policy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SOS Bucket Policies

This example demonstrates how to manage Exoscale [SOS Bucket Policies](https://community.exoscale.com/documentation/storage/bucketpolicy/).

```console
terraform init
terraform apply \
-var exoscale_api_key=$EXOSCALE_API_KEY \
-var exoscale_api_secret=$EXOSCALE_API_SECRET

...

Outputs:

my_object_uri = "https://sos-ch-gva-2.exo.io/my-bucket-2da17217-8ef3-254d-429e-08bced1109a5/my-object.txt"

$ wget -qO- https://sos-ch-gva-2.exo.io/my-bucket-2da17217-8ef3-254d-429e-08bced1109a5/my-object.txt
Hello World!
```
9 changes: 9 additions & 0 deletions examples/sos-bucket-policy/bucket_policy.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Version": "exoscale",
"default-service-strategy": "allow",
"services": {
"sos": {
"type": "allow"
}
}
}
32 changes: 32 additions & 0 deletions examples/sos-bucket-policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Providers
# -> providers.tf

# Customizable parameters
locals {
my_zone = "ch-gva-2"
my_bucket = "my-bucket"
}

# Sample random UUID
resource "random_uuid" "my_uuid" {
}

# SOS bucket
resource "aws_s3_bucket" "my_bucket" {
bucket = "${local.my_bucket}-${resource.random_uuid.my_uuid.result}"
}

resource "exoscale_sos_bucket_policy" "my_policy" {
bucket = "${local.my_bucket}-${resource.random_uuid.my_uuid.result}"
policy = templatefile("${path.module}/bucket_policy.json.tpl", {})
zone = local.my_zone
}

# Outputs
output "my_object_uri" {
value = format(
"https://sos-%s.exo.io/%s",
aws_s3_bucket.my_bucket.region,
aws_s3_bucket.my_bucket.bucket,
)
}
27 changes: 27 additions & 0 deletions examples/sos-bucket-policy/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
exoscale = {
source = "exoscale/exoscale"
}
}
}

variable "exoscale_api_key" { type = string }
variable "exoscale_api_secret" { type = string }
provider "aws" {
access_key = var.exoscale_api_key
secret_key = var.exoscale_api_secret

region = local.my_zone
endpoints {
s3 = "https://sos-${local.my_zone}.exo.io"
}

# Disable AWS-specific features
skip_credentials_validation = true
skip_region_validation = true
skip_requesting_account_id = true
}
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/database"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/iam"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/nlb_service"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/sos_bucket_policy"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/zones"
)

Expand Down Expand Up @@ -205,6 +206,7 @@ func (p *ExoscaleProvider) Resources(ctx context.Context) []func() resource.Reso
iam.NewResourceAPIKey,
block_storage.NewResourceVolume,
block_storage.NewResourceSnapshot,
sos_bucket_policy.NewResourceSOSBucketPolicy,
}
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/resources/sos_bucket_policy/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (r *ResourceSOSBucketPolicy) Schema(ctx context.Context, req resource.Schem
},
},
},
Blocks: map[string]schema.Block{
"timeouts": timeouts.BlockAll(ctx),
},
}
}

Expand Down Expand Up @@ -130,7 +133,7 @@ func (r *ResourceSOSBucketPolicy) Create(ctx context.Context, req resource.Creat
_, err = sosClient.PutBucketPolicy(ctx, &s3.PutBucketPolicyInput{
Bucket: plan.Bucket.ValueStringPointer(),
Policy: plan.Policy.ValueStringPointer(),
}, nil)
})
if err != nil {
resp.Diagnostics.AddError(
"failed to put bucket policy",
Expand Down Expand Up @@ -177,7 +180,7 @@ func (r *ResourceSOSBucketPolicy) Read(ctx context.Context, req resource.ReadReq

policy, err := sosClient.GetBucketPolicy(ctx, &s3.GetBucketPolicyInput{
Bucket: state.Bucket.ValueStringPointer(),
}, nil)
})
if err != nil {
resp.Diagnostics.AddError(
"failed to get bucket policy",
Expand Down Expand Up @@ -230,7 +233,7 @@ func (r *ResourceSOSBucketPolicy) Update(ctx context.Context, req resource.Updat
_, err = sosClient.PutBucketPolicy(ctx, &s3.PutBucketPolicyInput{
Bucket: plan.Bucket.ValueStringPointer(),
Policy: plan.Policy.ValueStringPointer(),
}, nil)
})
if err != nil {
resp.Diagnostics.AddError(
"failed to put bucket policy",
Expand Down Expand Up @@ -280,7 +283,7 @@ func (r *ResourceSOSBucketPolicy) Delete(ctx context.Context, req resource.Delet

_, err = sosClient.DeleteBucketPolicy(ctx, &s3.DeleteBucketPolicyInput{
Bucket: state.Bucket.ValueStringPointer(),
}, nil)
})
if err != nil {
resp.Diagnostics.AddError(
"failed to put bucket policy",
Expand Down

0 comments on commit 570ce45

Please sign in to comment.