Skip to content

Commit

Permalink
Extract some login into a flattener
Browse files Browse the repository at this point in the history
This can be reused for at least the port resource.

Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Oct 10, 2019
1 parent 0cd0a44 commit 71d6fdb
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions megaport/resource_megaport_private_vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,6 @@ import (
"github.com/utilitywarehouse/terraform-provider-megaport/megaport/api"
)

var (
vxcEndResourceElem = &schema.Resource{
Schema: map[string]*schema.Schema{
"product_uid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vlan": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
}
)

func resourceMegaportPrivateVxc() *schema.Resource {
return &schema.Resource{
Create: resourceMegaportPrivateVxcCreate,
Expand All @@ -48,13 +31,13 @@ func resourceMegaportPrivateVxc() *schema.Resource {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: vxcEndResourceElem,
Elem: resourceMegaportVxcEndElem(),
},
"b_end": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: vxcEndResourceElem,
Elem: resourceMegaportVxcEndElem(),
},
"invoice_reference": {
Type: schema.TypeString,
Expand All @@ -74,14 +57,8 @@ func resourceMegaportPrivateVxcRead(d *schema.ResourceData, m interface{}) error
}
d.Set("name", p.ProductName)
d.Set("rate_limit", p.RateLimit)
d.Set("a_end", []interface{}{map[string]interface{}{
"product_uid": p.AEnd.ProductUid,
"vlan": int(p.AEnd.Vlan),
}})
d.Set("b_end", []interface{}{map[string]interface{}{
"product_uid": p.BEnd.ProductUid,
"vlan": int(p.BEnd.Vlan),
}})
d.Set("a_end", flattenVxcEnd(p.AEnd))
d.Set("b_end", flattenVxcEnd(p.BEnd))
//d.Set("invoice_reference", p.) // TODO: is this even exported?
return nil
}
Expand Down Expand Up @@ -124,3 +101,27 @@ func resourceMegaportPrivateVxcDelete(d *schema.ResourceData, m interface{}) err
func resourceMegaportPrivateVxcImportState(*schema.ResourceData, interface{}) ([]*schema.ResourceData, error) {
return nil, nil
}

func resourceMegaportVxcEndElem() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"product_uid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vlan": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
},
}
}

func flattenVxcEnd(v api.ProductAssociatedVxcEnd) []interface{} {
return []interface{}{map[string]interface{}{
"product_uid": v.ProductUid,
"vlan": int(v.Vlan),
}}
}

0 comments on commit 71d6fdb

Please sign in to comment.