mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-26 08:26:26 -06:00
provider/google: Content field for bucket objects
This commit is contained in:
parent
aa05e8262a
commit
b8c66dc5e5
@ -1,7 +1,9 @@
|
|||||||
package google
|
package google
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -23,26 +25,39 @@ func resourceStorageBucketObject() *schema.Resource {
|
|||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"name": &schema.Schema{
|
"name": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"source": &schema.Schema{
|
"source": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Optional: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
|
ConflictsWith: []string{"content"},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"content": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: true,
|
||||||
|
ConflictsWith: []string{"source"},
|
||||||
|
},
|
||||||
|
|
||||||
"predefined_acl": &schema.Schema{
|
"predefined_acl": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Deprecated: "Please use resource \"storage_object_acl.predefined_acl\" instead.",
|
Deprecated: "Please use resource \"storage_object_acl.predefined_acl\" instead.",
|
||||||
Optional: true,
|
Optional: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"md5hash": &schema.Schema{
|
"md5hash": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"crc32c": &schema.Schema{
|
"crc32c": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
@ -60,11 +75,18 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})
|
|||||||
|
|
||||||
bucket := d.Get("bucket").(string)
|
bucket := d.Get("bucket").(string)
|
||||||
name := d.Get("name").(string)
|
name := d.Get("name").(string)
|
||||||
source := d.Get("source").(string)
|
var media io.Reader
|
||||||
|
|
||||||
file, err := os.Open(source)
|
if v, ok := d.GetOk("source"); ok {
|
||||||
if err != nil {
|
err := error(nil)
|
||||||
return fmt.Errorf("Error opening %s: %s", source, err)
|
media, err = os.Open(v.(string))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if v, ok := d.GetOk("content"); ok {
|
||||||
|
media = bytes.NewReader([]byte(v.(string)))
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("Error, either \"content\" or \"string\" must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
objectsService := storage.NewObjectsService(config.clientStorage)
|
objectsService := storage.NewObjectsService(config.clientStorage)
|
||||||
@ -72,15 +94,15 @@ func resourceStorageBucketObjectCreate(d *schema.ResourceData, meta interface{})
|
|||||||
|
|
||||||
insertCall := objectsService.Insert(bucket, object)
|
insertCall := objectsService.Insert(bucket, object)
|
||||||
insertCall.Name(name)
|
insertCall.Name(name)
|
||||||
insertCall.Media(file)
|
insertCall.Media(media)
|
||||||
if v, ok := d.GetOk("predefined_acl"); ok {
|
if v, ok := d.GetOk("predefined_acl"); ok {
|
||||||
insertCall.PredefinedAcl(v.(string))
|
insertCall.PredefinedAcl(v.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = insertCall.Do()
|
_, err := insertCall.Do()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error uploading contents of object %s from %s: %s", name, source, err)
|
return fmt.Errorf("Error uploading object %s: %s", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return resourceStorageBucketObjectRead(d, meta)
|
return resourceStorageBucketObjectRead(d, meta)
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
var tf, err = ioutil.TempFile("", "tf-gce-test")
|
var tf, err = ioutil.TempFile("", "tf-gce-test")
|
||||||
var bucketName = "tf-gce-bucket-test"
|
var bucketName = "tf-gce-bucket-test"
|
||||||
var objectName = "tf-gce-test"
|
var objectName = "tf-gce-test"
|
||||||
|
var content = "now this is content!"
|
||||||
|
|
||||||
func TestAccGoogleStorageObject_basic(t *testing.T) {
|
func TestAccGoogleStorageObject_basic(t *testing.T) {
|
||||||
data := []byte("data data data")
|
data := []byte("data data data")
|
||||||
@ -42,6 +43,31 @@ func TestAccGoogleStorageObject_basic(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAccGoogleStorageObject_content(t *testing.T) {
|
||||||
|
data := []byte(content)
|
||||||
|
h := md5.New()
|
||||||
|
h.Write(data)
|
||||||
|
data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||||
|
|
||||||
|
ioutil.WriteFile(tf.Name(), data, 0644)
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
testAccPreCheck(t)
|
||||||
|
},
|
||||||
|
Providers: testAccProviders,
|
||||||
|
CheckDestroy: testAccGoogleStorageObjectDestroy,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
resource.TestStep{
|
||||||
|
Config: testGoogleStorageBucketsObjectContent,
|
||||||
|
Check: testAccCheckGoogleStorageObject(bucketName, objectName, data_md5),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func testAccCheckGoogleStorageObject(bucket, object, md5 string) resource.TestCheckFunc {
|
func testAccCheckGoogleStorageObject(bucket, object, md5 string) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
config := testAccProvider.Meta().(*Config)
|
config := testAccProvider.Meta().(*Config)
|
||||||
@ -87,6 +113,19 @@ func testAccGoogleStorageObjectDestroy(s *terraform.State) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var testGoogleStorageBucketsObjectContent = fmt.Sprintf(`
|
||||||
|
resource "google_storage_bucket" "bucket" {
|
||||||
|
name = "%s"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_storage_bucket_object" "object" {
|
||||||
|
name = "%s"
|
||||||
|
bucket = "${google_storage_bucket.bucket.name}"
|
||||||
|
content = "%s"
|
||||||
|
predefined_acl = "projectPrivate"
|
||||||
|
}
|
||||||
|
`, bucketName, objectName, content)
|
||||||
|
|
||||||
var testGoogleStorageBucketsObjectBasic = fmt.Sprintf(`
|
var testGoogleStorageBucketsObjectBasic = fmt.Sprintf(`
|
||||||
resource "google_storage_bucket" "bucket" {
|
resource "google_storage_bucket" "bucket" {
|
||||||
name = "%s"
|
name = "%s"
|
||||||
|
@ -29,8 +29,15 @@ resource "google_storage_bucket_object" "picture" {
|
|||||||
The following arguments are supported:
|
The following arguments are supported:
|
||||||
|
|
||||||
* `name` - (Required) The name of the object.
|
* `name` - (Required) The name of the object.
|
||||||
|
|
||||||
* `bucket` - (Required) The name of the containing bucket.
|
* `bucket` - (Required) The name of the containing bucket.
|
||||||
* `source` - (Required) A path to the data you want to upload.
|
|
||||||
|
* `source` - (Optional) A path to the data you want to upload. Must be defined
|
||||||
|
if `content` is not.
|
||||||
|
|
||||||
|
* `content` - (Optional) Data as `string` to be uploaded. Must be defined if
|
||||||
|
`source` is not.
|
||||||
|
|
||||||
* `predefined_acl` - (Optional, Deprecated) The [canned GCS ACL](https://cloud.google.com/storage/docs/access-control#predefined-acl) apply. Please switch
|
* `predefined_acl` - (Optional, Deprecated) The [canned GCS ACL](https://cloud.google.com/storage/docs/access-control#predefined-acl) apply. Please switch
|
||||||
to `google_storage_object_acl.predefined_acl`.
|
to `google_storage_object_acl.predefined_acl`.
|
||||||
|
|
||||||
@ -39,4 +46,5 @@ to `google_storage_object_acl.predefined_acl`.
|
|||||||
The following attributes are exported:
|
The following attributes are exported:
|
||||||
|
|
||||||
* `md5hash` - (Computed) Base 64 MD5 hash of the uploaded data.
|
* `md5hash` - (Computed) Base 64 MD5 hash of the uploaded data.
|
||||||
|
|
||||||
* `crc32c` - (Computed) Base 64 CRC32 hash of the uploaded data.
|
* `crc32c` - (Computed) Base 64 CRC32 hash of the uploaded data.
|
||||||
|
Loading…
Reference in New Issue
Block a user