diff --git a/azurerm/data_source_shared_image_gallery_test.go b/azurerm/data_source_shared_image_gallery_test.go index 3c0a62e54f99..b6823a5326f7 100644 --- a/azurerm/data_source_shared_image_gallery_test.go +++ b/azurerm/data_source_shared_image_gallery_test.go @@ -41,7 +41,7 @@ func TestAccDataSourceAzureRMSharedImageGallery_complete(t *testing.T) { { Config: testAccDataSourceSharedImageGallery_complete(rInt, location), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(dataSourceName, "description", "hello, world"), + resource.TestCheckResourceAttr(dataSourceName, "description", "Shared images and things."), resource.TestCheckResourceAttr(dataSourceName, "tags.%", "2"), resource.TestCheckResourceAttr(dataSourceName, "tags.Hello", "There"), resource.TestCheckResourceAttr(dataSourceName, "tags.World", "Example"), diff --git a/azurerm/data_source_shared_image_version.go b/azurerm/data_source_shared_image_version.go index 4d79ae9e8ac7..4469986297c6 100644 --- a/azurerm/data_source_shared_image_version.go +++ b/azurerm/data_source_shared_image_version.go @@ -42,11 +42,22 @@ func dataSourceArmSharedImageVersion() *schema.Resource { Computed: true, }, - "regions": { - Type: schema.TypeSet, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - StateFunc: azureRMNormalizeLocation, + "target_region": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + + "regional_replica_count": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, }, "exclude_from_latest": { @@ -90,19 +101,14 @@ func dataSourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{ } if props := resp.GalleryImageVersionProperties; props != nil { - // `targetRegions` is returned in the API Response but isn't exposed there. - // TODO: replace this once this fields exposed - // BUG: https://github.com/Azure/azure-sdk-for-go/issues/2855 - if status := props.ReplicationStatus; status != nil { - flattenedRegions := flattenSharedImageVersionDataSourceRegions(status.Summary) - if err := d.Set("regions", flattenedRegions); err != nil { - return fmt.Errorf("Error flattening `regions`: %+v", err) - } - } - if profile := props.PublishingProfile; profile != nil { d.Set("exclude_from_latest", profile.ExcludeFromLatest) + flattenedRegions := flattenSharedImageVersionDataSourceTargetRegions(profile.TargetRegions) + if err := d.Set("target_region", flattenedRegions); err != nil { + return fmt.Errorf("Error flattening `target_region`: %+v", err) + } + if source := profile.Source; source != nil { if image := source.ManagedImage; image != nil { d.Set("managed_image_id", image.ID) @@ -116,16 +122,24 @@ func dataSourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{ return nil } -func flattenSharedImageVersionDataSourceRegions(input *[]compute.RegionalReplicationStatus) []interface{} { - output := make([]interface{}, 0) +func flattenSharedImageVersionDataSourceTargetRegions(input *[]compute.TargetRegion) []interface{} { + results := make([]interface{}, 0) if input != nil { for _, v := range *input { - if v.Region != nil { - output = append(output, azureRMNormalizeLocation(*v.Region)) + output := make(map[string]interface{}, 0) + + if v.Name != nil { + output["name"] = azureRMNormalizeLocation(*v.Name) } + + if v.RegionalReplicaCount != nil { + output["regional_replica_count"] = int(*v.RegionalReplicaCount) + } + + results = append(results, output) } } - return output + return results } diff --git a/azurerm/resource_arm_shared_image_version.go b/azurerm/resource_arm_shared_image_version.go index b6b2bc1120ab..441c2c66cb87 100644 --- a/azurerm/resource_arm_shared_image_version.go +++ b/azurerm/resource_arm_shared_image_version.go @@ -53,12 +53,24 @@ func resourceArmSharedImageVersion() *schema.Resource { ForceNew: true, }, - "regions": { - Type: schema.TypeSet, - Required: true, - Elem: &schema.Schema{Type: schema.TypeString}, - StateFunc: azureRMNormalizeLocation, - DiffSuppressFunc: azureRMSuppressLocationDiff, + "target_region": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + StateFunc: azureRMNormalizeLocation, + DiffSuppressFunc: azureRMSuppressLocationDiff, + }, + + "regional_replica_count": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, }, "exclude_from_latest": { @@ -84,7 +96,7 @@ func resourceArmSharedImageVersionCreateUpdate(d *schema.ResourceData, meta inte managedImageId := d.Get("managed_image_id").(string) excludeFromLatest := d.Get("exclude_from_latest").(bool) - regions := expandSharedImageVersionRegions(d) + targetRegions := expandSharedImageVersionTargetRegions(d) tags := d.Get("tags").(map[string]interface{}) version := compute.GalleryImageVersion{ @@ -92,7 +104,7 @@ func resourceArmSharedImageVersionCreateUpdate(d *schema.ResourceData, meta inte GalleryImageVersionProperties: &compute.GalleryImageVersionProperties{ PublishingProfile: &compute.GalleryImageVersionPublishingProfile{ ExcludeFromLatest: utils.Bool(excludeFromLatest), - Regions: regions, + TargetRegions: targetRegions, Source: &compute.GalleryArtifactSource{ ManagedImage: &compute.ManagedArtifact{ ID: utils.String(managedImageId), @@ -113,6 +125,8 @@ func resourceArmSharedImageVersionCreateUpdate(d *schema.ResourceData, meta inte return fmt.Errorf("Error waiting for the creation of Shared Image Version %q (Image %q / Gallery %q / Resource Group %q): %+v", imageVersion, imageName, galleryName, resourceGroup, err) } + // TODO: poll? + read, err := client.Get(ctx, resourceGroup, galleryName, imageName, imageVersion, "") if err != nil { return fmt.Errorf("Error retrieving Shared Image Version %q (Image %q / Gallery %q / Resource Group %q): %+v", imageVersion, imageName, galleryName, resourceGroup, err) @@ -157,19 +171,14 @@ func resourceArmSharedImageVersionRead(d *schema.ResourceData, meta interface{}) } if props := resp.GalleryImageVersionProperties; props != nil { - // `targetRegions` is returned in the API Response but isn't exposed there. - // TODO: replace this once this fields exposed - // BUG: https://github.com/Azure/azure-sdk-for-go/issues/2855 - if status := props.ReplicationStatus; status != nil { - flattenedRegions := flattenSharedImageVersionRegions(status.Summary) - if err := d.Set("regions", flattenedRegions); err != nil { - return fmt.Errorf("Error flattening `regions`: %+v", err) - } - } - if profile := props.PublishingProfile; profile != nil { d.Set("exclude_from_latest", profile.ExcludeFromLatest) + flattenedRegions := flattenSharedImageVersionTargetRegions(profile.TargetRegions) + if err := d.Set("target_region", flattenedRegions); err != nil { + return fmt.Errorf("Error flattening `target_region`: %+v", err) + } + if source := profile.Source; source != nil { if image := source.ManagedImage; image != nil { d.Set("managed_image_id", image.ID) @@ -215,28 +224,44 @@ func resourceArmSharedImageVersionDelete(d *schema.ResourceData, meta interface{ return nil } - -func expandSharedImageVersionRegions(d *schema.ResourceData) *[]string { - vs := d.Get("regions").(*schema.Set) - output := make([]string, 0) +func expandSharedImageVersionTargetRegions(d *schema.ResourceData) *[]compute.TargetRegion { + vs := d.Get("target_region").(*schema.Set) + results := make([]compute.TargetRegion, 0) for _, v := range vs.List() { - output = append(output, v.(string)) + input := v.(map[string]interface{}) + + name := input["name"].(string) + regionalReplicaCount := input["regional_replica_count"].(int) + + output := compute.TargetRegion{ + Name: utils.String(name), + RegionalReplicaCount: utils.Int32(int32(regionalReplicaCount)), + } + results = append(results, output) } - return &output + return &results } -func flattenSharedImageVersionRegions(input *[]compute.RegionalReplicationStatus) []interface{} { - output := make([]interface{}, 0) +func flattenSharedImageVersionTargetRegions(input *[]compute.TargetRegion) []interface{} { + results := make([]interface{}, 0) if input != nil { for _, v := range *input { - if v.Region != nil { - output = append(output, azureRMNormalizeLocation(*v.Region)) + output := make(map[string]interface{}, 0) + + if v.Name != nil { + output["name"] = azureRMNormalizeLocation(*v.Name) + } + + if v.RegionalReplicaCount != nil { + output["regional_replica_count"] = int(*v.RegionalReplicaCount) } + + results = append(results, output) } } - return output + return results } diff --git a/azurerm/resource_arm_shared_image_version_test.go b/azurerm/resource_arm_shared_image_version_test.go index 69d353415e3a..7f7945d75131 100644 --- a/azurerm/resource_arm_shared_image_version_test.go +++ b/azurerm/resource_arm_shared_image_version_test.go @@ -42,7 +42,7 @@ func TestAccAzureRMSharedImageVersion_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testCheckAzureRMSharedImageVersionExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "managed_image_id"), - resource.TestCheckResourceAttr(resourceName, "regions.#", "1"), + resource.TestCheckResourceAttr(resourceName, "target_region.#", "1"), ), }, { @@ -50,7 +50,7 @@ func TestAccAzureRMSharedImageVersion_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testCheckAzureRMSharedImageVersionExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "managed_image_id"), - resource.TestCheckResourceAttr(resourceName, "regions.#", "2"), + resource.TestCheckResourceAttr(resourceName, "target_region.#", "2"), ), }, { @@ -166,7 +166,11 @@ resource "azurerm_shared_image_version" "test" { resource_group_name = "${azurerm_resource_group.test.name}" location = "${azurerm_resource_group.test.location}" managed_image_id = "${azurerm_image.test.id}" - regions = ["${azurerm_resource_group.test.location}"] + + target_region { + name = "${azurerm_resource_group.test.location}" + regional_replica_count = 1 + } } `, template) } @@ -183,7 +187,16 @@ resource "azurerm_shared_image_version" "test" { resource_group_name = "${azurerm_resource_group.test.name}" location = "${azurerm_resource_group.test.location}" managed_image_id = "${azurerm_image.test.id}" - regions = ["${azurerm_resource_group.test.location}", "%s"] + + target_region { + name = "${azurerm_resource_group.test.location}" + regional_replica_count = 1 + } + + target_region { + name = "%s" + regional_replica_count = 2 + } } `, template, altLocation) }