Skip to content
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

Object versioning and bucket replication fix #4355

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var CisInstance string
var CisResourceGroup string
var CloudShellAccountID string
var CosCRN string
var BucketCRN string
var CosName string
var Ibmid1 string
var Ibmid2 string
Expand Down Expand Up @@ -329,6 +330,11 @@ func init() {
CosCRN = ""
fmt.Println("[WARN] Set the environment variable IBM_COS_CRN with a VALID COS instance CRN for testing ibm_cos_* resources")
}
BucketCRN = os.Getenv("IBM_COS_Bucket_CRN")
if BucketCRN == "" {
BucketCRN = ""
fmt.Println("[WARN] Set the environment variable IBM_COS_Bucket_CRN with a VALID BUCKET CRN for testing ibm_cos_bucket* resources")
}

CosName = os.Getenv("IBM_COS_NAME")
if CosName == "" {
Expand Down
10 changes: 0 additions & 10 deletions ibm/service/cos/resource_ibm_cos_bucket_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,6 @@ func resourceIBMCOSBucketObjectCreate(ctx context.Context, d *schema.ResourceDat

objectKey := d.Get("key").(string)

// This check is to make sure new create does not
// overwrite objects that is not managed by Terraform
exists, err := objectExists(s3Client, bucketName, objectKey)
if err != nil {
return diag.FromErr(err)
}
if exists {
return diag.FromErr(fmt.Errorf("[ERROR] Error COS bucket (%s) object (%s) already exists", bucketName, objectKey))
}

var body io.ReadSeeker

if v, ok := d.GetOk("content"); ok {
Expand Down
132 changes: 132 additions & 0 deletions ibm/service/cos/resource_ibm_cos_bucket_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,103 @@ func TestAccIBMCOSBucketObject_basic(t *testing.T) {
})
}

func TestAccIBMCOSBucketObject_VersioningEnabled(t *testing.T) {
name := fmt.Sprintf("tf-testacc-cos-%d", acctest.RandIntRange(10, 100))
key := "plaintext.txt"
instanceCRN := acc.CosCRN
objectBody1 := "Acceptance Testing"
objectBody2 := "Acceptance Testing object 2"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheckCOS(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccIBMCOSBucketBucketObject_Versioning_Enabled(name, key, instanceCRN, objectBody1, objectBody2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("ibm_cos_bucket.testacc", "object_versioning.#", "1"),
resource.TestCheckResourceAttr("ibm_cos_bucket.testacc", "object_versioning.0.enable", "true"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "id"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_length"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_type"),
resource.TestCheckResourceAttr("ibm_cos_bucket_object.testacc_object", "content", objectBody1),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object2", "id"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object2", "content"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object2", "content_length"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object2", "content_type"),
resource.TestCheckResourceAttr("ibm_cos_bucket_object.testacc_object2", "content", objectBody2),
),
},
},
})
}

func TestAccIBMCOSBucketObject_Versioning_Enabled_Sequential_Upload_on_Existing_Bucket(t *testing.T) {
key := "plaintext.txt"
bucketCRN := acc.BucketCRN
objectBody1 := "Acceptance Testing"
objectBody2 := "Acceptance Testing object 2"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheckCOS(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccIBMCOSBucketBucketObjectUpload(bucketCRN, key, objectBody1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "id"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_length"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_type"),
resource.TestCheckResourceAttr("ibm_cos_bucket_object.testacc_object", "content", objectBody1),
),
},
{
Config: testAccIBMCOSBucketBucketObjectUpload(bucketCRN, key, objectBody2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "id"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_length"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_type"),
resource.TestCheckResourceAttr("ibm_cos_bucket_object.testacc_object", "content", objectBody2),
),
},
},
})
}

func TestAccIBMCOSBucketObject_Uploading_Multile_Objects_on_Existing_Bucket_without_Versioning(t *testing.T) {
key := "plaintext.txt"
bucketCRN := acc.BucketCRN
objectBody1 := "Acceptance Testing"
objectBody2 := "Acceptance Testing object 2"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheckCOS(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccIBMCOSBucketBucketObjectUpload(bucketCRN, key, objectBody1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "id"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_length"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_type"),
resource.TestCheckResourceAttr("ibm_cos_bucket_object.testacc_object", "content", objectBody1),
),
},
{
Config: testAccIBMCOSBucketBucketObjectUpload(bucketCRN, key, objectBody2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "id"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_length"),
resource.TestCheckResourceAttrSet("ibm_cos_bucket_object.testacc_object", "content_type"),
resource.TestCheckResourceAttr("ibm_cos_bucket_object.testacc_object", "content", objectBody2),
),
},
},
})
}

func testAccIBMCOSBucketObjectConfig_plaintext(name string, instanceCRN string, objectBody string) string {
return fmt.Sprintf(`
resource "ibm_cos_bucket" "testacc" {
Expand Down Expand Up @@ -115,3 +212,38 @@ func testAccIBMCOSBucketObjectConfig_file(name string, instanceCRN string, objec
content_file = "%[3]s"
}`, name, instanceCRN, objectFile)
}

func testAccIBMCOSBucketBucketObject_Versioning_Enabled(name string, key string, instanceCRN string, objectBody1 string, objectBody2 string) string {
return fmt.Sprintf(`
resource "ibm_cos_bucket" "testacc" {
bucket_name = "%[1]s"
resource_instance_id = "%[3]s"
region_location = "us-south"
storage_class = "standard"
object_versioning {
enable = true
}
}
resource "ibm_cos_bucket_object" "testacc_object" {
bucket_crn = ibm_cos_bucket.testacc.crn
bucket_location = ibm_cos_bucket.testacc.region_location
key = "%[2]s"
content = "%[4]s"
}
resource "ibm_cos_bucket_object" "testacc_object2" {
bucket_crn = ibm_cos_bucket.testacc.crn
bucket_location = ibm_cos_bucket.testacc.region_location
key = "%[2]s"
content = "%[5]s"
}`, name, key, instanceCRN, objectBody1, objectBody2)
}

func testAccIBMCOSBucketBucketObjectUpload(bucketCrn string, key string, objectBody1 string) string {
return fmt.Sprintf(`
resource "ibm_cos_bucket_object" "testacc_object" {
bucket_crn = "%[1]s"
bucket_location = "us-south"
key = "%[2]s"
content = "%[3]s"
}`, bucketCrn, key, objectBody1)
}
7 changes: 3 additions & 4 deletions ibm/service/cos/resource_ibm_cos_replication_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ func ResourceIBMCOSBucketReplicationConfiguration() *schema.Resource {
Description: "Indicates whether to replicate delete markers. It should be either Enable or Disable",
},
"destination_bucket_crn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ValidateRegexps(`^crn:.+:.+:.+:.+:.+:a\/[0-9a-f]{32}:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}:bucket:[a-z-A-Z]*[0-9]*$`),
Description: "The Cloud Resource Name (CRN) of the bucket where you want COS to store the results",
Type: schema.TypeString,
Required: true,
Description: "The Cloud Resource Name (CRN) of the bucket where you want COS to store the results",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is validateFunc removed? can't we have crn for regex?

},
},
},
Expand Down