Skip to content

Commit

Permalink
Implement port deletion
Browse files Browse the repository at this point in the history
Plus some renaming

Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Oct 7, 2019
1 parent 3c7f3af commit b70e64b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
11 changes: 9 additions & 2 deletions megaport/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ const (
UserAgent = "megaport-api-go-client/" + Version
)

var (
ErrNotFound = fmt.Errorf("megaport-api: not found")
)

type Client struct {
c *http.Client
BaseURL string
Token string
UserAgent string

Ports *PortsService
Port *PortService
}

func NewClient(baseURL string) *Client {
c := &Client{c: &http.Client{}, BaseURL: baseURL}
c.Ports = NewPortsService(c)
c.Port = NewPortService(c)
return c
}

Expand Down Expand Up @@ -171,6 +175,9 @@ func (c *Client) do(req *http.Request, data interface{}) error {
return err
}
r := megaportResponse{}
if resp.StatusCode == http.StatusNotFound {
return ErrNotFound
}
if resp.StatusCode != http.StatusOK {
r.Data = map[string]interface{}{}
if err = parseResponseBody(resp, &r); err != nil {
Expand Down
52 changes: 31 additions & 21 deletions megaport/api/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@ const (
// mcr1: virtual = true , type = MEGAPORT
// mcr2: virtual = false, type = MCR2

type PortsService struct {
type PortService struct {
c *Client
}

func NewPortsService(c *Client) *PortsService {
return &PortsService{c}
func NewPortService(c *Client) *PortService {
return &PortService{c}
}

func (p *PortsService) Create(name string, locationId, speed, term uint64, validate bool) (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,
},
})
func (p *PortService) Create(name string, locationId, speed, term uint64, validate bool) (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
}
Expand All @@ -66,7 +64,7 @@ func (p *PortsService) Create(name string, locationId, speed, term uint64, valid
return d[0]["technicalServiceUid"].(string), nil
}

func (p *PortsService) Get(uid string) (*Product, error) {
func (p *PortService) Get(uid string) (*Product, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/product/%s", p.c.BaseURL, uid), nil)
if err != nil {
return nil, err
Expand All @@ -78,8 +76,20 @@ func (p *PortsService) Get(uid string) (*Product, error) {
return data, nil
}

func (p *PortsService) Update() {}
func (p *PortsService) Delete() {}
func (p *PortService) Update() error {
return nil
}

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

type portOrder struct {
CreateDate uint64 `json:"createDate,omitempty"` // TODO: need to fill in? :o
Expand Down
16 changes: 12 additions & 4 deletions megaport/resource_megaport_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/hashicorp/terraform/helper/schema"
"github.com/utilitywarehouse/terraform-provider-megaport/megaport/api"
)

func resourceMegaportPort() *schema.Resource {
Expand Down Expand Up @@ -121,10 +122,11 @@ func resourceMegaportPort() *schema.Resource {

func resourceMegaportPortRead(d *schema.ResourceData, m interface{}) error {
cfg := m.(*Config)
log.Printf("!!! READ")
p, err := cfg.Client.Ports.Get(d.Id())
p, err := cfg.Client.Port.Get(d.Id())
if err != nil {
return err
log.Printf("resourceMegaportPortRead: %v", err)
d.SetId("")
return nil
}
d.Set("location_id", p.LocationId)
d.Set("name", p.ProductName)
Expand Down Expand Up @@ -153,7 +155,7 @@ 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.Ports.Create(d.Get("name").(string),
uid, err := cfg.Client.Port.Create(d.Get("name").(string),
uint64(d.Get("location_id").(int)), uint64(d.Get("speed").(int)),
uint64(d.Get("term").(int)), true)
if err != nil {
Expand All @@ -170,6 +172,12 @@ func resourceMegaportPortUpdate(d *schema.ResourceData, m interface{}) error {

func resourceMegaportPortDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("!!! DELETE")
cfg := m.(*Config)
err := cfg.Client.Port.Delete(d.Id())
if err != api.ErrNotFound {
return err
}
log.Printf("resourceMegaportPortDelete: resource not found, deleting anyway")
return nil
}

Expand Down

0 comments on commit b70e64b

Please sign in to comment.