Skip to content

Commit

Permalink
SDKv21.1: switching to use the TargetRegion field
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Sep 30, 2018
1 parent 913d219 commit 5409ee3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 53 deletions.
56 changes: 36 additions & 20 deletions azurerm/data_source_shared_image_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
83 changes: 54 additions & 29 deletions azurerm/resource_arm_shared_image_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -84,15 +96,15 @@ 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{
Location: utils.String(location),
GalleryImageVersionProperties: &compute.GalleryImageVersionProperties{
PublishingProfile: &compute.GalleryImageVersionPublishingProfile{
ExcludeFromLatest: utils.Bool(excludeFromLatest),
Regions: regions,
TargetRegions: targetRegions,
Source: &compute.GalleryArtifactSource{
ManagedImage: &compute.ManagedArtifact{
ID: utils.String(managedImageId),
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
21 changes: 17 additions & 4 deletions azurerm/resource_arm_shared_image_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ 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"),
),
},
{
Config: testAccAzureRMSharedImageVersion_imageVersionUpdated(ri, location, altLocation, userName, password, hostName),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSharedImageVersionExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "managed_image_id"),
resource.TestCheckResourceAttr(resourceName, "regions.#", "2"),
resource.TestCheckResourceAttr(resourceName, "target_region.#", "2"),
),
},
{
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

0 comments on commit 5409ee3

Please sign in to comment.