Skip to content

Commit

Permalink
Refactoring: moving gallery_image_reference out to a shared function
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Oct 11, 2018
1 parent 1d17f17 commit c307dd5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 79 deletions.
80 changes: 80 additions & 0 deletions azurerm/helpers/azure/devtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,83 @@ func ExpandDevTestLabVirtualMachineNatRules(input *schema.Set) []dtl.InboundNatR

return rules
}

func ExpandDevTestLabVirtualMachineGalleryImageReference(input []interface{}, osType string) *dtl.GalleryImageReference {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})
offer := v["offer"].(string)
publisher := v["publisher"].(string)
sku := v["sku"].(string)
version := v["version"].(string)

return &dtl.GalleryImageReference{
Offer: utils.String(offer),
OsType: utils.String(osType),
Publisher: utils.String(publisher),
Sku: utils.String(sku),
Version: utils.String(version),
}
}

func SchemaDevTestVirtualMachineGalleryImageReference() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"offer": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"publisher": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sku": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
}
}

func FlattenDevTestVirtualMachineGalleryImage(input *dtl.GalleryImageReference) []interface{} {
results := make([]interface{}, 0)

if input != nil {
output := make(map[string]interface{}, 0)

if input.Offer != nil {
output["offer"] = *input.Offer
}

if input.Publisher != nil {
output["publisher"] = *input.Publisher
}

if input.Sku != nil {
output["sku"] = *input.Sku
}

if input.Version != nil {
output["version"] = *input.Version
}

results = append(results, output)
}

return results
}
82 changes: 3 additions & 79 deletions azurerm/resource_arm_dev_test_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,7 @@ func resourceArmDevTestVirtualMachine() *schema.Resource {
ForceNew: true,
},

"gallery_image_reference": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"offer": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"publisher": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"sku": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
},
},
"gallery_image_reference": azure.SchemaDevTestVirtualMachineGalleryImageReference(),

"inbound_nat_rule": azure.SchemaDevTestVirtualMachineInboundNatRule(),

Expand Down Expand Up @@ -194,7 +166,7 @@ func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta i
username := d.Get("username").(string)

galleryImageReferenceRaw := d.Get("gallery_image_reference").([]interface{})
galleryImageReference := expandDevTestLabVirtualMachineGalleryImageReference(galleryImageReferenceRaw, osType)
galleryImageReference := azure.ExpandDevTestLabVirtualMachineGalleryImageReference(galleryImageReferenceRaw, osType)

natRulesRaw := d.Get("inbound_nat_rule").(*schema.Set)
natRules := azure.ExpandDevTestLabVirtualMachineNatRules(natRulesRaw)
Expand Down Expand Up @@ -295,7 +267,7 @@ func resourceArmDevTestVirtualMachineRead(d *schema.ResourceData, meta interface
d.Set("storage_type", props.StorageType)
d.Set("username", props.UserName)

flattenedImage := flattenDevTestVirtualMachineGalleryImage(props.GalleryImageReference)
flattenedImage := azure.FlattenDevTestVirtualMachineGalleryImage(props.GalleryImageReference)
if err := d.Set("gallery_image_reference", flattenedImage); err != nil {
return fmt.Errorf("Error flattening `gallery_image_reference`: %+v", err)
}
Expand Down Expand Up @@ -345,51 +317,3 @@ func resourceArmDevTestVirtualMachineDelete(d *schema.ResourceData, meta interfa

return err
}

func expandDevTestLabVirtualMachineGalleryImageReference(input []interface{}, osType string) *dtl.GalleryImageReference {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})
offer := v["offer"].(string)
publisher := v["publisher"].(string)
sku := v["sku"].(string)
version := v["version"].(string)

return &dtl.GalleryImageReference{
Offer: utils.String(offer),
OsType: utils.String(osType),
Publisher: utils.String(publisher),
Sku: utils.String(sku),
Version: utils.String(version),
}
}

func flattenDevTestVirtualMachineGalleryImage(input *dtl.GalleryImageReference) []interface{} {
results := make([]interface{}, 0)

if input != nil {
output := make(map[string]interface{}, 0)

if input.Offer != nil {
output["offer"] = *input.Offer
}

if input.Publisher != nil {
output["publisher"] = *input.Publisher
}

if input.Sku != nil {
output["sku"] = *input.Sku
}

if input.Version != nil {
output["version"] = *input.Version
}

results = append(results, output)
}

return results
}

0 comments on commit c307dd5

Please sign in to comment.