From 5409ee34a2afbdb465390768e245d9d150c4d09b Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Sun, 30 Sep 2018 16:05:47 +1000 Subject: [PATCH] SDKv21.1: switching to use the TargetRegion field --- azurerm/data_source_shared_image_version.go | 56 ++++++++----- azurerm/resource_arm_shared_image_version.go | 83 ++++++++++++------- .../resource_arm_shared_image_version_test.go | 21 ++++- 3 files changed, 107 insertions(+), 53 deletions(-) diff --git a/azurerm/data_source_shared_image_version.go b/azurerm/data_source_shared_image_version.go index 4d79ae9e8ac7..3f4df312bea5 100644 --- a/azurerm/data_source_shared_image_version.go +++ b/azurerm/data_source_shared_image_version.go @@ -42,11 +42,24 @@ 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, + StateFunc: azureRMNormalizeLocation, + DiffSuppressFunc: azureRMSuppressLocationDiff, + }, + + "regional_replica_count": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, }, "exclude_from_latest": { @@ -90,19 +103,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 +124,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..77446a07b52e 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 expandSharedImageVersionTargetRegions(d *schema.ResourceData) *[]compute.TargetRegion { + vs := d.Get("target_region").([]interface{}) + results := make([]compute.TargetRegion, 0) + + for _, v := range vs { + input := v.(map[string]interface{}) -func expandSharedImageVersionRegions(d *schema.ResourceData) *[]string { - vs := d.Get("regions").(*schema.Set) - output := make([]string, 0) + name := input["name"].(string) + regionalReplicaCount := input["regional_replica_count"].(int) - for _, v := range vs.List() { - output = append(output, v.(string)) + 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) }