Skip to content

Commit

Permalink
Adding new resource ibm_cos_lifecycle_configuration to manage lifecyc… (
Browse files Browse the repository at this point in the history
IBM-Cloud#5619)

* Adding new resource ibm_cos_lifecycle_configuration to manage lifecycle policy for bucket

* Adding the data source changes

* Data source changes

* Adding the import documentation

* Addressing the review comments

* Updating the resource to use context

* Changes to add multiple abort_incomplete_multipart_upload
  • Loading branch information
IBM-diksha authored Sep 24, 2024
1 parent 924fe87 commit a7de784
Show file tree
Hide file tree
Showing 11 changed files with 2,621 additions and 12 deletions.
41 changes: 41 additions & 0 deletions examples/ibm-cos-bucket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,36 @@ resource ibm_cos_bucket_website_configuration "website_configuration" {
}
}
```
## ibm_cos_bucket_lifecycle_configuration

Provides an independent resource to manage the lifecycle configuration for a bucket.For more information please refer to [`ibm_cos_bucket_lifecycle_configuration`](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/ibm_cos_bucket_lifecycle_configuration)

## Example usage

```terraform
resource "ibm_cos_bucket" "cos_bucket" {
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
}
resource "ibm_cos_bucket_lifecycle_configuration" "lifecycle" {
bucket_crn = ibm_cos_bucket.cos_bucket.crn
bucket_location = ibm_cos_bucket.cos_bucket.region_location
lifecycle_rule {
expiration{
days = 1
}
filter {
prefix = "foo"
}
rule_id = "id"
status = "enable"
}
}
```
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Requirements
Expand Down Expand Up @@ -582,4 +612,15 @@ resource ibm_cos_bucket_website_configuration "website_configuration" {
| 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
| days | Days after which the lifecycle rule expiration will be applied on the object. | `int` | No
| date | Date after which the lifecycle rule expiration will be applied on the object. | `int` | No
| expire_object_delete_marker | Indicates whether ibm will remove a delete marker with no noncurrent versions. | `bool` | No
| days | Days after which the lifecycle rule transition will be applied on the object. | `int` | No
| date | Date after which the lifecycle rule transition will be applied on the object. | `int` | No
| storage_class | Class of storage used to store the object. | `string` | No
| noncurrent_days | Number of days an object is noncurrent before lifecycle action is performed. | `int` | No
| days_after_initiatiob | Number of days after which incomplete multipart uploads are aborted. | `int` | No
| id | Unique identifier for lifecycle rule. | `int` | Yes
| status | Whether the rule is currently being applied. | `int` | Yes

{: caption="inputs"}
149 changes: 146 additions & 3 deletions examples/ibm-cos-bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ resource "ibm_kms_key" "key" {
force_delete = true
}

resource "ibm_cos_bucket" "hpcs-enabled" {
resource "ibm_cos_bucket" "hpcs-enable" {
depends_on = [ibm_iam_authorization_policy.policy2]
bucket_name = var.bucket_name
resource_instance_id = ibm_resource_instance.cos_instance.id
Expand All @@ -307,7 +307,7 @@ resource "ibm_cos_bucket" "hpcs-enabled" {
}

//HPCS - UKO plan
resource "ibm_cos_bucket" "hpcs-uko-enabled" {
resource "ibm_cos_bucket" "hpcs-uko-enable" {
depends_on = [ibm_iam_authorization_policy.policy2]
bucket_name = var.bucket_name
resource_instance_id = ibm_resource_instance.cos_instance.id
Expand Down Expand Up @@ -386,7 +386,7 @@ resource ibm_cos_bucket_object_lock_configuration "objectlock" {
bucket_crn = ibm_cos_bucket.bucket.crn
bucket_location = var.regional_loc
object_lock_configuration{
object_lock_enabled = "Enabled"
object_lock_enable = "Enabled"
object_lock_rule{
default_retention{
mode = "COMPLIANCE"
Expand Down Expand Up @@ -500,3 +500,146 @@ resource ibm_cos_bucket_website_configuration "website_configuration" {
EOF
}
}


#COS Lifecycle Configuration

# Adding lifecycle configuration with expiration and prefix filter.

resource "ibm_cos_bucket" "cos_bucket_lifecycle_expiration" {
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

}
resource "ibm_cos_bucket_lifecycle_configuration" "lifecycle" {
bucket_crn = ibm_cos_bucket.cos_bucket.crn
bucket_location = ibm_cos_bucket.cos_bucket.region_location
lifecycle_rule {
expiration{
days = 1
}
filter {
prefix = "foo"
}
rule_id = "id"
status = "enable"

}
}



# Adding lifecycle configuration with transition.

resource "ibm_cos_bucket" "cos_bucket_transition" {
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

}
resource "ibm_cos_bucket_lifecycle_configuration" "lifecycle_transition" {
bucket_crn = ibm_cos_bucket.cos_bucket.crn
bucket_location = ibm_cos_bucket.cos_bucket.region_location
lifecycle_rule {
transition{
days = 1
storage_class = "GLACIER"
}
filter {
prefix = ""
}
rule_id = "id"
status = "enable"

}
}


# Adding lifecycle configuration with abort incomplete multipart upload.

resource "ibm_cos_bucket" "cos_bucket_abort_incomplete" {
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

}
resource "ibm_cos_bucket_lifecycle_configuration" "lifecycle_abort_incomplete" {
bucket_crn = ibm_cos_bucket.cos_bucket.crn
bucket_location = ibm_cos_bucket.cos_bucket.region_location
lifecycle_rule {
abort_incomplete_multipart_upload{
days_after_initiation = 1
}
filter {
prefix = ""
}
rule_id = "id"
status = "enable"

}
}


# Adding lifecycle configuration with non current version expiration.

resource "ibm_cos_bucket" "cos_bucket_lifecycle_version_expiration" {
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

}
resource "ibm_cos_bucket_lifecycle_configuration" "lifecycle_new" {
bucket_crn = ibm_cos_bucket.cos_bucket.crn
bucket_location = ibm_cos_bucket.cos_bucket.region_location
lifecycle_rule {
noncurrent_version_expiration{
noncurrent_days = "1"
}
filter {
prefix = ""
}
rule_id = "id"
status = "enable"

}
}

# Adding lifecycle configuration with multiple rules

resource "ibm_cos_bucket" "cos_bucket_lifecycle" {
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
}

resource "ibm_cos_bucket_lifecycle_configuration" "lifecycle_config" {
bucket_crn = ibm_cos_bucket.cos_bucket.crn
bucket_location = ibm_cos_bucket.cos_bucket.region_location
lifecycle_rule {
expiration{
days = 1
}
filter {
prefix = "foo"
}
rule_id = "id"
status = "enable"
}
lifecycle_rule {
expiration{
days = 2
}
filter {
prefix = "bar"
}
rule_id = "id2"
status = "enable"
}
}

105 changes: 105 additions & 0 deletions ibm/flex/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,111 @@ func ReplicationRuleGet(in *s3.ReplicationConfiguration) []map[string]interface{
return rules
}

func flattenLifecycleExpiration(expiration *s3.LifecycleExpiration) []interface{} {
if expiration == nil {
return []interface{}{}
}
m := make(map[string]interface{})
if expiration.Date != nil {
m["date"] = expiration.Date.Format(time.RFC3339)
}
if expiration.Days != nil {
m["days"] = int(aws.Int64Value(expiration.Days))
}
if expiration.ExpiredObjectDeleteMarker != nil {
m["expired_object_delete_marker"] = aws.Bool(*expiration.ExpiredObjectDeleteMarker)
}
return []interface{}{m}
}

func flattenNoncurrentVersionExpiration(expiration *s3.NoncurrentVersionExpiration) []interface{} {
if expiration == nil {
return []interface{}{}
}
m := make(map[string]interface{})
if expiration.NoncurrentDays != nil {
m["noncurrent_days"] = int(aws.Int64Value(expiration.NoncurrentDays))
}
return []interface{}{m}
}
func flattenTransitions(transitions []*s3.Transition) []interface{} {
if len(transitions) == 0 {
return []interface{}{}
}
var results []interface{}
for _, transition := range transitions {
m := make(map[string]interface{})
if transition.StorageClass != nil {
m["storage_class"] = transition.StorageClass
}
if transition.Date != nil {
m["date"] = transition.Date.Format(time.RFC3339)
}
if transition.Days != nil {
m["days"] = int(aws.Int64Value(transition.Days))
}
results = append(results, m)
}
return results
}

func flattenLifecycleRuleFilter(filter *s3.LifecycleRuleFilter) []interface{} {
if filter == nil {
return []interface{}{}
}
m := make(map[string]interface{})
if filter.Prefix != nil {
m["prefix"] = aws.String(*filter.Prefix)
}
return []interface{}{m}
}

func flattenAbortIncompleteMultipartUpload(abortIncompleteMultipartUploadInput *s3.AbortIncompleteMultipartUpload) []interface{} {
if abortIncompleteMultipartUploadInput == nil {
return []interface{}{}
}
abortIncompleteMultipartUploadMap := make(map[string]interface{})
if abortIncompleteMultipartUploadInput.DaysAfterInitiation != nil {
abortIncompleteMultipartUploadMap["days_after_initiation"] = int(aws.Int64Value(abortIncompleteMultipartUploadInput.DaysAfterInitiation))
}
return []interface{}{abortIncompleteMultipartUploadMap}
}

func LifecylceRuleGet(lifecycleRuleInput []*s3.LifecycleRule) []map[string]interface{} {
rules := make([]map[string]interface{}, 0, len(lifecycleRuleInput))
if lifecycleRuleInput != nil {
for _, lifecyclerule := range lifecycleRuleInput {
lifecycleRuleConfig := make(map[string]interface{})
if lifecyclerule.Status != nil {
if *lifecyclerule.Status == "Enabled" {
lifecycleRuleConfig["status"] = "enable"
} else {
lifecycleRuleConfig["status"] = "disable"
}
}
if lifecyclerule.ID != nil {
lifecycleRuleConfig["rule_id"] = *lifecyclerule.ID
}
if lifecyclerule.Expiration != nil {
lifecycleRuleConfig["expiration"] = flattenLifecycleExpiration(lifecyclerule.Expiration)
}
if lifecyclerule.Transitions != nil {
lifecycleRuleConfig["transition"] = flattenTransitions(lifecyclerule.Transitions)
}
if lifecyclerule.AbortIncompleteMultipartUpload != nil {
lifecycleRuleConfig["abort_incomplete_multipart_upload"] = flattenAbortIncompleteMultipartUpload(lifecyclerule.AbortIncompleteMultipartUpload)
}
if lifecyclerule.NoncurrentVersionExpiration != nil {
lifecycleRuleConfig["noncurrent_version_expiration"] = flattenNoncurrentVersionExpiration(lifecyclerule.NoncurrentVersionExpiration)
}
if lifecyclerule.Filter != nil {
lifecycleRuleConfig["filter"] = flattenLifecycleRuleFilter(lifecyclerule.Filter)
}
rules = append(rules, lifecycleRuleConfig)
}
}
return rules
}
func ObjectLockConfigurationGet(in *s3.ObjectLockConfiguration) []map[string]interface{} {
configuration := make([]map[string]interface{}, 0, 1)
if in != nil {
Expand Down
1 change: 1 addition & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ func Provider() *schema.Provider {
"ibm_cos_bucket_object": cos.ResourceIBMCOSBucketObject(),
"ibm_cos_bucket_object_lock_configuration": cos.ResourceIBMCOSBucketObjectlock(),
"ibm_cos_bucket_website_configuration": cos.ResourceIBMCOSBucketWebsiteConfiguration(),
"ibm_cos_bucket_lifecycle_configuration": cos.ResourceIBMCOSBucketLifecycleConfiguration(),
"ibm_dns_domain": classicinfrastructure.ResourceIBMDNSDomain(),
"ibm_dns_domain_registration_nameservers": classicinfrastructure.ResourceIBMDNSDomainRegistrationNameservers(),
"ibm_dns_secondary": classicinfrastructure.ResourceIBMDNSSecondary(),
Expand Down
Loading

0 comments on commit a7de784

Please sign in to comment.