Azure blob contents can be copied from an existing blob (#8126)

- adds "source_uri" field

- "source_uri" expects the URI to an existing blob that you have access
  to
- it can be in a different storage account, or in the Azure File service

- the docs have been updated to reflect the change

Signed-off-by: Dan Wendorf <dwendorf@pivotal.io>
This commit is contained in:
Dave Walter 2016-08-14 03:14:42 -07:00 committed by Paul Stack
parent 598b940e1c
commit ec42a98cb2
3 changed files with 130 additions and 29 deletions

View File

@ -46,7 +46,7 @@ func resourceArmStorageBlob() *schema.Resource {
},
"type": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
ValidateFunc: validateArmStorageBlobType,
},
@ -61,6 +61,13 @@ func resourceArmStorageBlob() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source_uri"},
},
"source_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source"},
},
"url": {
Type: schema.TypeString,
@ -144,8 +151,14 @@ func resourceArmStorageBlobCreate(d *schema.ResourceData, meta interface{}) erro
name := d.Get("name").(string)
blobType := d.Get("type").(string)
cont := d.Get("storage_container_name").(string)
sourceUri := d.Get("source_uri").(string)
log.Printf("[INFO] Creating blob %q in storage account %q", name, storageAccountName)
if sourceUri != "" {
if err := blobClient.CopyBlob(cont, name, sourceUri); err != nil {
return fmt.Errorf("Error creating storage blob on Azure: %s", err)
}
} else {
switch strings.ToLower(blobType) {
case "block":
if err := blobClient.CreateBlockBlob(cont, name); err != nil {
@ -175,6 +188,7 @@ func resourceArmStorageBlobCreate(d *schema.ResourceData, meta interface{}) erro
}
}
}
}
d.SetId(name)
return resourceArmStorageBlobRead(d, meta)

View File

@ -257,6 +257,41 @@ func TestAccAzureRMStorageBlobPage_source(t *testing.T) {
})
}
func TestAccAzureRMStorageBlob_source_uri(t *testing.T) {
ri := acctest.RandInt()
rs1 := strings.ToLower(acctest.RandString(11))
sourceBlob, err := ioutil.TempFile("", "")
if err != nil {
t.Fatalf("Failed to create local source blob file")
}
_, err = io.CopyN(sourceBlob, rand.Reader, 25*1024*1024)
if err != nil {
t.Fatalf("Failed to write random test to source blob")
}
err = sourceBlob.Close()
if err != nil {
t.Fatalf("Failed to close source blob")
}
config := fmt.Sprintf(testAccAzureRMStorageBlob_source_uri, ri, rs1, sourceBlob.Name())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageBlobDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageBlobMatchesFile("azurerm_storage_blob.destination", storage.BlobTypeBlock, sourceBlob.Name()),
),
},
},
})
}
func testCheckAzureRMStorageBlobExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
@ -500,3 +535,51 @@ resource "azurerm_storage_blob" "source" {
attempts = 3
}
`
var testAccAzureRMStorageBlob_source_uri = `
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "westus"
}
resource "azurerm_storage_account" "source" {
name = "acctestacc%s"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "westus"
account_type = "Standard_LRS"
tags {
environment = "staging"
}
}
resource "azurerm_storage_container" "source" {
name = "source"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.source.name}"
container_access_type = "blob"
}
resource "azurerm_storage_blob" "source" {
name = "source.vhd"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.source.name}"
storage_container_name = "${azurerm_storage_container.source.name}"
type = "block"
source = "%s"
parallelism = 4
attempts = 2
}
resource "azurerm_storage_blob" "destination" {
name = "destination.vhd"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_name = "${azurerm_storage_account.source.name}"
storage_container_name = "${azurerm_storage_container.source.name}"
source_uri = "${azurerm_storage_blob.source.url}"
}
`

View File

@ -58,11 +58,15 @@ The following arguments are supported:
* `storage_container_name` - (Required) The name of the storage container in which this blob should be created.
* `type` - (Required) The type of the storage blob to be created. One of either `block` or `page`.
* `type` - (Optional) The type of the storage blob to be created. One of either `block` or `page`. When not copying from an existing blob,
this becomes required.
* `size` - (Optional) Used only for `page` blobs to specify the size in bytes of the blob to be created. Must be a multiple of 512. Defaults to 0.
* `source` - (Optional) An absolute path to a file on the local system
* `source` - (Optional) An absolute path to a file on the local system. Cannot be defined if `source_uri` is defined.
* `source_uri` - (Optional) The URI of an existing blob, or a file in the Azure File service, to use as the source contents
for the blob to be created. Changing this forces a new resource to be created. Cannot be defined if `source` is defined.
* `parallelism` - (Optional) The number of workers per CPU core to run for concurrent uploads. Defaults to `8`.