Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #96 from terraform-providers/object-storage-updates
Browse files Browse the repository at this point in the history
Object storage updates
  • Loading branch information
mbfrahry authored Jan 11, 2018
2 parents aa09988 + dcd7370 commit 26c8387
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 66 deletions.
21 changes: 15 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
## 1.0.2 (Unreleased)

BUG FIXES:

IMPROVEMENTS:

* r/opc_storage_container: Add `quota_bytes`, `quota_count` and `metadata` attributes

* r/opc_storage_object: Add `metadata` attributes

## 1.0.1 (December 20, 2017)

BUG FIXES
Expand All @@ -7,7 +16,7 @@ BUG FIXES

## 1.0.0 (December 20, 2017)

NEW RESOURCE:
NEW RESOURCE:

* r/opc_compute_orchestrated_instance [#92]

Expand All @@ -25,11 +34,11 @@ IMPROVEMENTS:

## 0.1.3 (September 15, 2017)

FEATURES:
FEATURES:

* **New Resource:** `opc_storage_object` ([#55](https://github.com/terraform-providers/terraform-provider-opc/issues/55))

BUG FIXES:
BUG FIXES:

* r/ip_network: Allow changing the name of an IP Network ([#73](https://github.com/terraform-providers/terraform-provider-opc/issues/73))
* r/opc_compute_image_list_entry: Fix resource imports ([#66](https://github.com/terraform-providers/terraform-provider-opc/issues/66))
Expand All @@ -39,7 +48,7 @@ BUG FIXES:
* r/storage_volume_snapshot: Increase timeout for larger snapshots ([#79](https://github.com/terraform-providers/terraform-provider-opc/issues/79))
* r/storage_volume: Remove validation around storage_type ([#80](https://github.com/terraform-providers/terraform-provider-opc/issues/80))

NOTES:
NOTES:

* Various doc fixes/updates

Expand All @@ -51,7 +60,7 @@ FEATURES:
* **New Datasource:** `opc_compute_storage_volume_snapshot` ([#46](https://github.com/terraform-providers/terraform-provider-opc/issues/46))
* **New Resource:** `opc_compute_storage_container` ([#23](https://github.com/terraform-providers/terraform-provider-opc/issues/23))
* Add timeout configuration: ([#41](https://github.com/terraform-providers/terraform-provider-opc/issues/41))

BUG FIXES:
* `opc_storage_volume_snapshot`: Fix crash on import ([#10](https://github.com/terraform-providers/terraform-provider-opc/issues/10))
* `opc_compute_storage_volume`: bootable volumes can be added without an image list/image list entry ([#19](https://github.com/terraform-providers/terraform-provider-opc/issues/19))
Expand All @@ -61,7 +70,7 @@ BUG FIXES:

NOTES:

* Bumping the provider version to get around provider caching issues - still same functionality
* Bumping the provider version to get around provider caching issues - still same functionality

## 0.1.0 (June 20, 2017)

Expand Down
3 changes: 1 addition & 2 deletions opc/import_storage_container_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package opc

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -12,7 +11,7 @@ func TestAccOPCStorageContainer_importBasic(t *testing.T) {
resourceName := "opc_storage_container.test"

rInt := acctest.RandInt()
config := fmt.Sprintf(testAccOPCStorageContainerBasic, rInt)
config := testAccOPCStorageContainerBasic(rInt)

resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down
82 changes: 82 additions & 0 deletions opc/resource_storage_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ func resourceOPCStorageContainer() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"quota_bytes": {
Type: schema.TypeInt,
Optional: true,
},
"quota_count": {
Type: schema.TypeInt,
Optional: true,
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
},
// "georeplication_policy": {
// Type: schema.TypeList,
// Optional: true,
// Computed: true,
// Elem: &schema.Schema{Type: schema.TypeString},
// },
},
}
}
Expand Down Expand Up @@ -93,6 +111,20 @@ func resourceOPCStorageContainerCreate(d *schema.ResourceData, meta interface{})
if maxAge, ok := d.GetOk("max_age"); ok {
input.MaxAge = maxAge.(int)
}
if quotaBytes, ok := d.GetOk("quota_bytes"); ok {
input.QuotaBytes = quotaBytes.(int)
}
if quotaCount, ok := d.GetOk("quota_count"); ok {
input.QuotaCount = quotaCount.(int)
}

if v, ok := d.GetOk("metadata"); ok {
metadata := make(map[string]string)
for name, value := range v.(map[string]interface{}) {
metadata[name] = value.(string)
}
input.CustomMetadata = metadata
}

info, err := client.CreateContainer(&input)
if err != nil {
Expand Down Expand Up @@ -134,6 +166,10 @@ func resourceOPCStorageContainerRead(d *schema.ResourceData, meta interface{}) e
d.Set("primary_key", result.PrimaryKey)
d.Set("secondary_key", result.SecondaryKey)
d.Set("max_age", result.MaxAge)
d.Set("quota_bytes", result.QuotaBytes)
d.Set("quota_count", result.QuotaCount)
d.Set("metadata", result.CustomMetadata)

if err := setStringList(d, "read_acls", result.ReadACLs); err != nil {
return err
}
Expand Down Expand Up @@ -197,6 +233,33 @@ func resourceOPCStorageContainerUpdate(d *schema.ResourceData, meta interface{})
if maxAge, ok := d.GetOk("max_age"); ok {
input.MaxAge = maxAge.(int)
}
if quotaBytes, ok := d.GetOk("quota_bytes"); ok {
input.QuotaBytes = quotaBytes.(int)
}
if quotaCount, ok := d.GetOk("quota_count"); ok {
input.QuotaCount = quotaCount.(int)
}

// Create list of metadata headers to be removed
old, new := d.GetChange("metadata")
newHeaders := getKeys(new.(map[string]interface{}))
oldHeaders := getKeys(old.(map[string]interface{}))
removeHeaders := []string{}
for i := range oldHeaders {
if !contains(newHeaders, oldHeaders[i]) {
removeHeaders = append(removeHeaders, oldHeaders[i])
}
}
input.RemoveCustomMetadata = removeHeaders

// metadata headers to add or update
if v, ok := d.GetOk("metadata"); ok {
metadata := make(map[string]string)
for name, value := range v.(map[string]interface{}) {
metadata[name] = value.(string)
}
input.CustomMetadata = metadata
}

info, err := client.UpdateContainer(&input)
if err != nil {
Expand All @@ -207,3 +270,22 @@ func resourceOPCStorageContainerUpdate(d *schema.ResourceData, meta interface{})

return resourceOPCStorageContainerRead(d, meta)
}

// get keys from a map
func getKeys(m map[string]interface{}) []string {
keys := []string{}
for k, _ := range m {
keys = append(keys, k)
}
return keys
}

// check if value is in slice
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
40 changes: 33 additions & 7 deletions opc/resource_storage_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestAccOPCStorageContainer_Basic(t *testing.T) {
containerResourceName := "opc_storage_container.test"
ri := acctest.RandInt()
config := fmt.Sprintf(testAccOPCStorageContainerBasic, ri)
config := testAccOPCStorageContainerBasic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -35,8 +35,8 @@ func TestAccOPCStorageContainer_Basic(t *testing.T) {
func TestAccOPCStorageContainer_Updated(t *testing.T) {
containerResourceName := "opc_storage_container.test"
ri := acctest.RandInt()
config := fmt.Sprintf(testAccOPCStorageContainerBasic, ri)
config2 := fmt.Sprintf(testAccOPCStorageContainerUpdated, ri)
config := testAccOPCStorageContainerBasic(ri)
config2 := testAccOPCStorageContainerUpdated(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -53,6 +53,11 @@ func TestAccOPCStorageContainer_Updated(t *testing.T) {
resource.TestCheckResourceAttr(containerResourceName, "allowed_origins.0", "origin-1"),
resource.TestCheckResourceAttr(containerResourceName, "exposed_headers.#", "1"),
resource.TestCheckResourceAttr(containerResourceName, "exposed_headers.0", "exposed-header-1"),
resource.TestCheckResourceAttr(containerResourceName, "quota_bytes", "1000000000"),
resource.TestCheckResourceAttr(containerResourceName, "quota_count", "1000"),
resource.TestCheckResourceAttr(containerResourceName, "metadata.%", "2"),
resource.TestCheckResourceAttr(containerResourceName, "metadata.Foo", "bar"),
resource.TestCheckResourceAttr(containerResourceName, "metadata.Abc-Def", "xyz"),
),
},
{
Expand All @@ -66,6 +71,11 @@ func TestAccOPCStorageContainer_Updated(t *testing.T) {
resource.TestCheckResourceAttr(containerResourceName, "allowed_origins.1", "origin-2"),
resource.TestCheckResourceAttr(containerResourceName, "exposed_headers.#", "2"),
resource.TestCheckResourceAttr(containerResourceName, "exposed_headers.1", "exposed-header-2"),
resource.TestCheckResourceAttr(containerResourceName, "quota_bytes", "2000000000"),
resource.TestCheckResourceAttr(containerResourceName, "quota_count", "2000"),
resource.TestCheckResourceAttr(containerResourceName, "metadata.%", "2"),
resource.TestCheckResourceAttr(containerResourceName, "metadata.Bar", "foo"),
resource.TestCheckResourceAttr(containerResourceName, "metadata.Abc-Def", "xyz"),
),
},
},
Expand Down Expand Up @@ -110,23 +120,39 @@ func testAccCheckStorageContainerDestroy(s *terraform.State) error {
return nil
}

const testAccOPCStorageContainerBasic = `
func testAccOPCStorageContainerBasic(rInt int) string {
return fmt.Sprintf(`
resource "opc_storage_container" "test" {
name = "acc-storage-container-%d"
max_age = 50
quota_bytes = 1000000000
quota_count = 1000
primary_key = "test-key"
allowed_origins = ["origin-1"]
exposed_headers = ["exposed-header-1"]
metadata {
"Foo" = "bar",
"Abc-Def" = "xyz"
}
}
`, rInt)
}
`

const testAccOPCStorageContainerUpdated = `
func testAccOPCStorageContainerUpdated(rInt int) string {
return fmt.Sprintf(`
resource "opc_storage_container" "test" {
name = "acc-storage-container-%d"
max_age = 60
quota_bytes = 2000000000
quota_count = 2000
primary_key = "test-key-updated"
secondary_key = "test-key"
allowed_origins = ["origin-1", "origin-2"]
exposed_headers = ["exposed-header-1", "exposed-header-2"]
metadata {
"Bar" = "foo",
"Abc-Def" = "xyz"
}
}
`, rInt)
}
`
18 changes: 17 additions & 1 deletion opc/resource_storage_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func resourceOPCStorageObject() *schema.Resource {
Optional: true,
Computed: true,
ForceNew: true,
Description: "Specify the number of seconds after which the system deletes the object",
Description: "The date and time in UNIX Epoch time stamp format when the system removes the object",
},
"etag": {
Type: schema.TypeString,
Expand All @@ -95,6 +95,13 @@ func resourceOPCStorageObject() *schema.Resource {
ForceNew: true,
Description: "MD5 checksum value of the request body. Unquoted. Strongly Recommended",
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The object metadata",
},
"transfer_encoding": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -201,6 +208,14 @@ func resourceOPCStorageObjectCreate(d *schema.ResourceData, meta interface{}) er
input.ETag = v.(string)
}

if v, ok := d.GetOk("metadata"); ok {
metadata := make(map[string]string)
for name, value := range v.(map[string]interface{}) {
metadata[name] = value.(string)
}
input.ObjectMetadata = metadata
}

if v, ok := d.GetOk("transfer_encoding"); ok {
input.TransferEncoding = v.(string)
}
Expand Down Expand Up @@ -245,6 +260,7 @@ func resourceOPCStorageObjectRead(d *schema.ResourceData, meta interface{}) erro
d.Set("last_modified", result.LastModified)
d.Set("delete_at", result.DeleteAt)
d.Set("object_manifest", result.ObjectManifest)
d.Set("metadata", result.ObjectMetadata)
d.Set("timestamp", result.Timestamp)
d.Set("transaction_id", result.TransactionID)

Expand Down
47 changes: 47 additions & 0 deletions opc/resource_storage_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ func TestAccOPCStorageObject_fileSource(t *testing.T) {
})
}

func TestAccOPCStorageObject_objectMetadata(t *testing.T) {
resName := "opc_storage_object.test"
rInt := acctest.RandInt()

body := _SourceInput

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckStorageObjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccOPCStorageObject_objectMetadata(rInt, body),
Check: resource.ComposeTestCheckFunc(
testAccCheckStorageObjectExists,
resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("test-acc-%d", rInt)),
resource.TestCheckResourceAttr(resName, "container", fmt.Sprintf("acc-test-%d", rInt)),
resource.TestCheckResourceAttr(resName, "metadata.%", "2"),
resource.TestCheckResourceAttr(resName, "metadata.Foo", "bar"),
resource.TestCheckResourceAttr(resName, "metadata.Abc-Def", "xyz"),
),
},
},
})
}

func testAccCheckStorageObjectExists(s *terraform.State) error {
client := testAccProvider.Meta().(*OPCClient).storageClient.Objects()

Expand Down Expand Up @@ -146,6 +172,27 @@ resource "opc_storage_container" "foo" {
}`, rInt)
}

func testAccOPCStorageObject_objectMetadata(rInt int, body string) string {
return fmt.Sprintf(`
%s
resource "opc_storage_object" "test" {
name = "test-acc-%d"
container = "${opc_storage_container.foo.name}"
metadata {
"Foo" = "bar",
"Abc-Def" = "xyz"
}
content_type = "text/plain;charset=UTF-8"
content = <<EOF
%s
EOF
}`,
testAccOPCStorageObject_testContainer(rInt),
rInt,
body)
}

func testAccOPCStorageObject_contentSource(rInt int, body string) string {
return fmt.Sprintf(`
%s
Expand Down
Loading

0 comments on commit 26c8387

Please sign in to comment.