Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Commit

Permalink
Merge branch 'port-refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
alkar committed Oct 25, 2019
2 parents a10dff0 + 3c71cd5 commit 7932c44
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 76 deletions.
5 changes: 5 additions & 0 deletions megaport/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ func Uint64FromInt(v interface{}) *uint64 {
r := uint64(v.(int))
return &r
}

func Bool(v interface{}) *bool {
r := v.(bool)
return &r
}
132 changes: 69 additions & 63 deletions megaport/api/port.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package api

import (
"bytes"
"encoding/json"
"fmt"
// "log"
"net/http"
)

Expand All @@ -19,84 +17,92 @@ const (
// mcr1: virtual = true , type = MEGAPORT
// mcr2: virtual = false, type = MCR2

type portOrder struct {
CreateDate uint64 `json:"createDate,omitempty"` // TODO: need to fill in? :o
LagPortCount uint64 `json:"lagPortCount,omitempty"` // TODO: Required: the number of ports in this LAG order (https://dev.megaport.com/#standard-api-orders-validate-lag-order)
LocationId uint64 `json:"locationId"`
LocationUid string `json:"locationUid,omitempty"` // TODO: null in example, is it a string? https://dev.megaport.com/#standard-api-orders-validate-port-order
Market string `json:"market,omitempty"` // TODO: what is this ???
PortSpeed uint64 `json:"portSpeed"` // TODO: validate 1000, 10000, 100000
ProductName string `json:"productName"`
ProductType string `json:"productType"` // TODO: "MEGAPORT"?
Term uint64 `json:"term"`
Virtual bool `json:"virtual"` // TODO: False for port, true for MCR1.0 (https://dev.megaport.com/#standard-api-orders-validate-port-order)
type portCreatePayload struct {
CreateDate *uint64 `json:"createDate,omitempty"` // TODO: need to fill in? :o
LagPortCount *uint64 `json:"lagPortCount,omitempty"` // TODO: Required: the number of ports in this LAG order (https://dev.megaport.com/#standard-api-orders-validate-lag-order)
LocationId *uint64 `json:"locationId"`
LocationUid *string `json:"locationUid,omitempty"` // TODO: null in example, is it a string? https://dev.megaport.com/#standard-api-orders-validate-port-order
Market *string `json:"market,omitempty"` // TODO: what is this ???
PortSpeed *uint64 `json:"portSpeed"` // TODO: validate 1000, 10000, 100000
ProductName *string `json:"productName"`
ProductType *string `json:"productType"` // TODO: "MEGAPORT"?
Term *uint64 `json:"term"`
Virtual *bool `json:"virtual"` // TODO: False for port, true for MCR1.0 (https://dev.megaport.com/#standard-api-orders-validate-port-order)
}

type portOrderConfig struct {
McrASN uint64 `json:"mcrAsn,omitempty"`
type portUpdatePayload struct {
Name *string `json:"name,omitempty"`
CostCentre *string `json:"costCentre,omitempty"`
MarketplaceVisibility *bool `json:"marketplaceVisibility,omitempty"`
// RateLimit *uint64 `json:"rateLimit,omitempty"` // Only applicable to MCR. Must be one of 100, 500, 1000, 2000, 3000, 4000, 5000
}

func (c *Client) CreatePort(name string, locationId, speed, term uint64) (string, error) {
payload, err := json.Marshal([]portOrder{portOrder{
// CreateDate uint64 // TODO: need to fill in? :o
//LagPortCount uint64 // TODO: Required: the number of ports in this LAG order (https://dev.megaport.com/#standard-api-orders-validate-lag-order)
LocationId: locationId,
// LocationUid string // TODO: null in example, is it a string? https://dev.megaport.com/#standard-api-orders-validate-port-order
// Market : l.Market,
PortSpeed: speed,
ProductName: name,
ProductType: ProductTypePort,
Term: term,
Virtual: false,
}})
if err != nil {
return "", err
}
b := bytes.NewReader(payload)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/validate", c.BaseURL), b)
if err != nil {
return "", err
}
if err := c.do(req, nil); err != nil {
return "", err
}
b.Seek(0, 0) // TODO: error handling ?
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/buy", c.BaseURL), b)
if err != nil {
return "", err
}
d := []map[string]interface{}{}
if err := c.do(req, &d); err != nil {
return "", err
type PortCreateInput struct {
LocationId *uint64
Name *string
Speed *uint64
Term *uint64
}

func (v *PortCreateInput) productType() string {
return ProductTypePort
}

func (v *PortCreateInput) toPayload() ([]byte, error) {
payload := []*portCreatePayload{{
LocationId: v.LocationId,
PortSpeed: v.Speed,
ProductName: v.Name,
ProductType: String(ProductTypePort), // TODO
Term: v.Term,
Virtual: Bool(false), // TODO
}}
return json.Marshal(payload)
}

type PortUpdateInput struct {
InvoiceReference *string
MarketplaceVisibility *bool
Name *string
ProductUid *string
}

func (v *PortUpdateInput) productType() string {
return ProductTypePort
}

func (v *PortUpdateInput) toPayload() ([]byte, error) {
payload := &portUpdatePayload{
Name: v.Name,
CostCentre: v.InvoiceReference,
MarketplaceVisibility: v.MarketplaceVisibility,
}
return d[0]["technicalServiceUid"].(string), nil
return json.Marshal(payload)
}

func (c *Client) GetPort(uid string) (*Product, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/product/%s", c.BaseURL, uid), nil)
func (c *Client) CreatePort(v *PortCreateInput) (*string, error) {
d, err := c.create(v)
if err != nil {
return nil, err
}
data := &Product{}
if err := c.do(req, &data); err != nil {
uid := d[0]["technicalServiceUid"].(string)
return &uid, nil
}

func (c *Client) GetPort(uid string) (*Product, error) {
d := &Product{}
if err := c.get(uid, d); err != nil {
return nil, err
}
return data, nil
return d, nil
}

func (c *Client) UpdatePort() error {
return nil
func (c *Client) UpdatePort(v *PortUpdateInput) error {
return c.update(*v.ProductUid, v)
}

func (c *Client) DeletePort(uid string) error {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/product/%s/action/CANCEL_NOW", c.BaseURL, uid), nil)
if err != nil {
return err
}
if err := c.do(req, nil); err != nil {
return err
}
return nil
return c.delete(uid)
}

func (c *Client) ListPorts() ([]*Product, error) {
Expand Down
6 changes: 4 additions & 2 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ func (c *Client) CreateCloudVxc(v *CloudVxcCreateInput) (*string, error) {

func (c *Client) GetCloudVxc(uid string) (*ProductAssociatedVxc, error) { // TODO: rename struct
d := &ProductAssociatedVxc{}
err := c.get(uid, d)
return d, err
if err := c.get(uid, d); err != nil {
return nil, err
}
return d, nil
}

func (c *Client) UpdateCloudVxc(v *CloudVxcUpdateInput) error {
Expand Down
30 changes: 19 additions & 11 deletions megaport/resource_megaport_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,33 @@ func resourceMegaportPortRead(d *schema.ResourceData, m interface{}) error {

func resourceMegaportPortCreate(d *schema.ResourceData, m interface{}) error {
cfg := m.(*Config)
log.Printf("!!! CREATE")
uid, err := cfg.Client.CreatePort(d.Get("name").(string),
uint64(d.Get("location_id").(int)), uint64(d.Get("speed").(int)),
uint64(d.Get("term").(int)))
uid, err := cfg.Client.CreatePort(&api.PortCreateInput{
Name: api.String(d.Get("name")),
LocationId: api.Uint64FromInt(d.Get("location_id")),
Speed: api.Uint64FromInt(d.Get("speed")),
Term: api.Uint64FromInt(d.Get("term")),
})
if err != nil {
return err
}
d.SetId(uid)
return nil
d.SetId(*uid)
return resourceMegaportPortRead(d, m)
}

func resourceMegaportPortUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("!!! UPDATE")
return nil
cfg := m.(*Config)
if err := cfg.Client.UpdatePort(&api.PortUpdateInput{
InvoiceReference: api.String(d.Get("invoice_reference")),
Name: api.String(d.Get("name")),
ProductUid: api.String(d.Id()),
//RateLimit: api.Uint64FromInt(d.Get("rate_limit")),
}); err != nil {
return err
}
return resourceMegaportPortRead(d, m)
}

func resourceMegaportPortDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("!!! DELETE")
cfg := m.(*Config)
err := cfg.Client.DeletePort(d.Id())
if err != nil && err != api.ErrNotFound {
Expand All @@ -119,8 +128,7 @@ func resourceMegaportPortDelete(d *schema.ResourceData, m interface{}) error {
}

func resourceMegaportPortImportState(*schema.ResourceData, interface{}) ([]*schema.ResourceData, error) {
log.Printf("!!! IMPORT")
return nil, nil
return nil, nil // TODO
}

func flattenVxc(v api.ProductAssociatedVxc) interface{} {
Expand Down

0 comments on commit 7932c44

Please sign in to comment.