Skip to content

Commit

Permalink
Refactor VXC creation in the api
Browse files Browse the repository at this point in the history
This is a proof of concept for new design heavily based on `aws-sdk-go`
that will hopefully bring some consistency to the exposed API.

Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Oct 11, 2019
1 parent e35e596 commit 5369be8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 58 deletions.
98 changes: 50 additions & 48 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,84 @@ import (
"net/http"
)

type vxcOrder struct {
ProductUid string `json:"productUid"`
AssociatedVxcs []vxcOrderAssociatedVxcs `json:"associatedVxcs"`
type VxcService struct {
c *Client
}

type vxcOrderAssociatedVxcs struct {
ProductName string `json:"productName"`
RateLimit uint64 `json:"rateLimit"`
CostCentre string `json:"costCentre"`
AEnd *vxcOrderEnd `json:"aEnd,omitempty"`
BEnd *vxcOrderEnd `json:"bEnd"`
func NewVxcService(c *Client) *VxcService {
return &VxcService{c}
}

type vxcOrderEnd struct {
ProductUid string `json:"productUid"`
VLan uint64 `json:"vlan,omitempty"`
type VxcCreateInput struct {
InvoiceReference string
Name string
ProductUidA string
ProductUidB string
RateLimit uint64
VlanA uint64
VlanB uint64
}

type vxcOrderUpdate struct {
AEndVlan uint64 `json:"aEndVlan"`
BEndVlan uint64 `json:"bEndVlan,omitempty"`
CostCentre string `json:"costCentre"`
Name string `json:"name"`
RateLimit uint64 `json:"rateLimit"`
func (v *VxcCreateInput) toPayload() []*vxcCreatePayload {
ret := &vxcCreatePayload{
ProductUid: v.ProductUidA,
AssociatedVxcs: []vxcCreatePayloadAssociatedVxc{{
ProductName: v.Name,
RateLimit: v.RateLimit,
CostCentre: v.InvoiceReference,
AEnd: &vxcCreatePayloadAssociatedVxcEnd{Vlan: v.VlanA},
BEnd: &vxcCreatePayloadAssociatedVxcEnd{ProductUid: v.ProductUidB, Vlan: v.VlanB},
}},
}
return []*vxcCreatePayload{ret}
}

type VxcService struct {
c *Client
type VxcCreateOutput struct {
ProductUid string
}

func NewVxcService(c *Client) *VxcService {
return &VxcService{c}
type vxcCreatePayload struct {
ProductUid string `json:"productUid"`
AssociatedVxcs []vxcCreatePayloadAssociatedVxc `json:"associatedVxcs"`
}

func (p *VxcService) Create(productAUid, productBUid, name, invoiceReference string, vlanA, vlanB, rateLimit uint64) (string, error) {
order := []vxcOrder{vxcOrder{
ProductUid: productAUid,
AssociatedVxcs: []vxcOrderAssociatedVxcs{
vxcOrderAssociatedVxcs{
ProductName: name,
CostCentre: invoiceReference,
RateLimit: rateLimit,
BEnd: &vxcOrderEnd{
ProductUid: productBUid,
},
},
},
}}
if vlanA != 0 {
order[0].AssociatedVxcs[0].AEnd = &vxcOrderEnd{VLan: vlanB}
}
if vlanB != 0 {
order[0].AssociatedVxcs[0].BEnd.VLan = vlanB
}
type vxcCreatePayloadAssociatedVxc struct {
ProductName string `json:"productName"`
RateLimit uint64 `json:"rateLimit"`
CostCentre string `json:"costCentre"`
AEnd *vxcCreatePayloadAssociatedVxcEnd `json:"aEnd"`
BEnd *vxcCreatePayloadAssociatedVxcEnd `json:"bEnd"`
}

type vxcCreatePayloadAssociatedVxcEnd struct {
ProductUid string `json:"productUid,omitempty"`
Vlan uint64 `json:"vlan"`
}

func (p *VxcService) Create(v VxcCreateInput) (*VxcCreateOutput, error) {
order := v.toPayload()
payload, err := json.Marshal(order)
if err != nil {
return "", err
return nil, err
}
b := bytes.NewReader(payload)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/validate", p.c.BaseURL), b)
if err != nil {
return "", err
return nil, err
}
if err := p.c.do(req, nil); err != nil {
return "", err
return nil, err
}
b.Seek(0, 0) // TODO: error handling ?
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/buy", p.c.BaseURL), b)
if err != nil {
return "", err
return nil, err
}
d := []map[string]interface{}{}
if err := p.c.do(req, &d); err != nil {
return "", err
return nil, err
}
return d[0]["vxcJTechnicalServiceUid"].(string), nil
return &VxcCreateOutput{ProductUid: d[0]["vxcJTechnicalServiceUid"].(string)}, nil
}

func (p *VxcService) Get(uid string) (*ProductAssociatedVxc, error) {
Expand Down
20 changes: 10 additions & 10 deletions megaport/resource_megaport_private_vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ func resourceMegaportPrivateVxcCreate(d *schema.ResourceData, m interface{}) err
cfg := m.(*Config)
a := d.Get("a_end").([]interface{})[0].(map[string]interface{})
b := d.Get("b_end").([]interface{})[0].(map[string]interface{})
uid, err := cfg.Client.Vxc.Create(
a["product_uid"].(string),
b["product_uid"].(string),
d.Get("name").(string),
d.Get("invoice_reference").(string),
uint64(a["vlan"].(int)),
uint64(b["vlan"].(int)),
uint64(d.Get("rate_limit").(int)),
)
o, err := cfg.Client.Vxc.Create(api.VxcCreateInput{
ProductUidA: a["product_uid"].(string),
ProductUidB: b["product_uid"].(string),
Name: d.Get("name").(string),
InvoiceReference: d.Get("invoice_reference").(string),
VlanA: uint64(a["vlan"].(int)),
VlanB: uint64(b["vlan"].(int)),
RateLimit: uint64(d.Get("rate_limit").(int)),
})
if err != nil {
return err
}
d.SetId(uid)
d.SetId(o.ProductUid)
return resourceMegaportPrivateVxcRead(d, m)
}

Expand Down

0 comments on commit 5369be8

Please sign in to comment.