Skip to content

Commit

Permalink
Adding support for COS Static Web hosting (#4798)
Browse files Browse the repository at this point in the history
* Adding support for COS Static Web hosting

* Addressing the review comments

* Changes in the datasource documentation
  • Loading branch information
IBM-diksha committed Sep 15, 2023
1 parent a2c0516 commit a15d23a
Show file tree
Hide file tree
Showing 15 changed files with 2,311 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/ibm-cos-bucket-object/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ resource "ibm_cos_bucket_object" "object" {
force_delete = true
}
```
## COS Website Redirect


```hcl
resource "ibm_cos_bucket_object" "object" {
bucket_crn = ibm_cos_bucket.bucket.crn
bucket_location = ibm_cos_bucket.bucket.cross_region_location
key = "page1.html"
website_redirect = "/page2.html"
}
```

## Requirements

Expand Down Expand Up @@ -90,6 +101,7 @@ resource "ibm_cos_bucket_object" "object" {
| object_lock_legal_hold_status | An object lock configuration on the object, the valid states are ON/OFF. When ON prevents deletion of the object version. | `string` | false |
| object_lock_mode | Retention modes apply different levels of protection to the objects. | `string` | false |
| object_lock_legal_hold_status | An object cannot be deleted when the current time is earlier than the retainUntilDate. After this date, the object can be deleted. | `string` | false |
| website_redirect | To redirect the request for a particular object. | `string` | false |

## Outputs

Expand Down
10 changes: 10 additions & 0 deletions examples/ibm-cos-bucket-object/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ resource "ibm_cos_bucket_object" "object_object_lock" {
object_lock_legal_hold_status = "ON"
force_delete = true
}

// COS static web hosting

resource "ibm_cos_bucket_object" "object" {
bucket_crn = ibm_cos_bucket.object_lock.crn
bucket_location = ibm_cos_bucket.object_lock.cross_region_location
key = "page1.html"
website_redirect = "page2.html"
}

118 changes: 118 additions & 0 deletions examples/ibm-cos-bucket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,113 @@ resource ibm_cos_bucket_object_lock_configuration "objectlock" {
}
}
```


## COS Static Webhosting

Provides an Static web hosting configuration resource. This resource is used to configure the website to use your documents as an index for the site and to potentially display errors.It can also be used to configure more advanced options including routing rules and request redirect for your domain.

## Example usage
The following example creates an instance of IBM Cloud Object Storage, creates a bucket and adds a website configuration on the bucket.Along with the basic bucket configuration , example of redirect all requests and adding routing rules have been given below.

```terraform
# Create a bucket
resource "ibm_cos_bucket" "cos_bucket_website_configuration" {
bucket_name = var.bucket_name
resource_instance_id = ibm_resource_instance.cos_instance.id
region_location = var.regional_loc
storage_class = var.standard_storage_class
}
# Give public access to above mentioned bucket
resource "ibm_iam_access_group_policy" "policy" {
depends_on = [ibm_cos_bucket.cos_bucket_website_configuration]
access_group_id = data.ibm_iam_access_group.public_access_group.groups[0].id
roles = ["Object Reader"]
resources {
service = "cloud-object-storage"
resource_type = "bucket"
resource_instance_id = "COS instance guid"
resource = data.ibm_cos_bucket.cos_bucket_website_configuration.bucket_name
}
}
# Add basic website configuration on a COS bucket
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
error_document{
key = "error.html"
}
index_document{
suffix = "index.html"
}
}
}
# Add a request redirect website configuration on a COS bucket
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
redirect_all_requests_to{
host_name = "exampleBucketName"
protocol = "https"
}
}
}
# Add a website configuration on a COS bucket with routing rule
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
error_document{
key = "error.html"
}
index_document{
suffix = "index.html"
}
routing_rule {
condition {
key_prefix_equals = "pages/"
}
redirect {
replace_key_prefix_with = "web_pages/"
}
}
}
}
# Add a website configuration on a COS bucket with JSON routing rule
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
error_document{
key = "error.html"
}
index_document{
suffix = "index.html"
}
routing_rules = <<EOF
[{
"Condition": {
"KeyPrefixEquals": "pages/"
},
"Redirect": {
"ReplaceKeyPrefixWith": "webpages/"
}
}]
EOF
}
}
```
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Requirements
Expand Down Expand Up @@ -459,4 +566,15 @@ resource ibm_cos_bucket_object_lock_configuration "objectlock" {
| mode | Retention mode for the Object Lock configuration. | `String` | yes
| years | Retention period in terms of years after which the object can be deleted. | `int` | no
| days | Retention period in terms of days after which the object can be deleted. | `int` | no
| key | Object key name to use when a 4XX class error occurs given as error document. | `String` | no
| suffix | The home or default page of the website when static web hosting configuration is added. | `String` | Yes
| hostname | Name of the host where requests are redirected. | `String` | Yes
| protocol | Protocol to use when redirecting requests. The default is the protocol that is used in the original request. | `String` | No
| http_error_code_returned_equals | HTTP error code when the redirect is applied. | `String` | No
| key_prefix_equals | Object key name prefix when the redirect is applied. | `String` | No
| host_name | Host name to use in the redirect request. | `String` | Yes
| protocol | Protocol to use when redirecting requests. | `String` | No
| http_redirect_code | HTTP redirect code to use on the response. | `String` | No
| replace_key_with | Specific object key to use in the redirect request. | `String` | No
| replace_key_prefix_with | Object key prefix to use in the redirect request. | `String` | No
{: caption="inputs"}
100 changes: 100 additions & 0 deletions examples/ibm-cos-bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,103 @@ resource ibm_cos_bucket_object_lock_configuration "objectlock" {
}
}
}



#COS static webhosting


# Create a bucket
resource "ibm_cos_bucket" "cos_bucket_website_configuration" {
bucket_name = var.bucket_name
resource_instance_id = ibm_resource_instance.cos_instance.id
region_location = var.regional_loc
storage_class = var.standard_storage_class

}
# Give public access to above mentioned bucket
resource "ibm_iam_access_group_policy" "policy" {
depends_on = [ibm_cos_bucket.cos_bucket_website_configuration]
access_group_id = data.ibm_iam_access_group.public_access_group.groups[0].id
roles = ["Object Reader"]

resources {
service = "cloud-object-storage"
resource_type = "bucket"
resource_instance_id = "COS instance guid"
resource = data.ibm_cos_bucket.cos_bucket_website_configuration.bucket_name
}
}

# Add basic website configuration on a COS bucket
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
error_document{
key = "error.html"
}
index_document{
suffix = "index.html"
}
}
}

# Add a request redirect website configuration on a COS bucket
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
redirect_all_requests_to{
host_name = "exampleBucketName"
protocol = "https"
}
}
}


# Add a website configuration on a COS bucket with routing rule
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
error_document{
key = "error.html"
}
index_document{
suffix = "index.html"
}
routing_rule {
condition {
key_prefix_equals = "pages/"
}
redirect {
replace_key_prefix_with = "web_pages/"
}
}
}
}

# Add a website configuration on a COS bucket with JSON routing rule
resource ibm_cos_bucket_website_configuration "website_configuration" {
bucket_crn = "bucket_crn"
bucket_location = data.ibm_cos_bucket.cos_bucket_website_configuration.regional_location
website_configuration {
error_document{
key = "error.html"
}
index_document{
suffix = "index.html"
}
routing_rules = <<EOF
[{
"Condition": {
"KeyPrefixEquals": "pages/"
},
"Redirect": {
"ReplaceKeyPrefixWith": "webpages/"
}
}]
EOF
}
}

0 comments on commit a15d23a

Please sign in to comment.