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

Commit

Permalink
Flatten internal sdk structure
Browse files Browse the repository at this point in the history
Undo the split to individual 'services'. It complicates the SDK
unnecessarily without provindg any obvious benefits.

Signed-off-by: Dimitrios Karagiannis <dhkarag@gmail.com>
  • Loading branch information
alkar committed Oct 18, 2019
1 parent 76a56f3 commit dcdc4bb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
2 changes: 0 additions & 2 deletions megaport/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ type Client struct {
UserAgent string

Port *PortService
Vxc *VxcService
}

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

Expand Down
53 changes: 22 additions & 31 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,75 @@ import (
"bytes"
"encoding/json"
"fmt"
// "log"
"net/http"
)

type VxcService struct {
c *Client
}

func NewVxcService(c *Client) *VxcService {
return &VxcService{c}
}

type networkDesignInput interface {
toPayload() ([]byte, error)
}

func (p *VxcService) create(v networkDesignInput) (*string, error) {
func (c *Client) create(v networkDesignInput) (*string, error) {
payload, err := v.toPayload()
if err != nil {
return nil, err
}
b := bytes.NewReader(payload)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/validate", p.c.BaseURL), b)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/validate", c.BaseURL), b)
if err != nil {
return nil, err
}
if err := p.c.do(req, nil); err != nil {
if err := c.do(req, nil); err != nil {
return nil, err
}
if _, err := b.Seek(0, 0); err != nil {
return nil, err
}
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/buy", p.c.BaseURL), b)
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/networkdesign/buy", c.BaseURL), b)
if err != nil {
return nil, err
}
d := []map[string]interface{}{}
if err := p.c.do(req, &d); err != nil {
if err := c.do(req, &d); err != nil {
return nil, err
}
uid := d[0]["vxcJTechnicalServiceUid"].(string)
return &uid, nil
}

func (p *VxcService) get(uid string) (*ProductAssociatedVxc, error) { // TODO: change name
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/product/%s", p.c.BaseURL, uid), nil)
func (c *Client) get(uid string) (*ProductAssociatedVxc, error) { // TODO: change name
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/product/%s", c.BaseURL, uid), nil)
if err != nil {
return nil, err
}
data := &ProductAssociatedVxc{}
if err := p.c.do(req, &data); err != nil {
if err := c.do(req, &data); err != nil {
return nil, err
}
return data, nil
}

func (p *VxcService) update(uid string, v networkDesignInput) error {
func (c *Client) update(uid string, v networkDesignInput) error {
payload, err := v.toPayload()
if err != nil {
return err
}
b := bytes.NewReader(payload)
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/v2/product/vxc/%s", p.c.BaseURL, uid), b)
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/v2/product/vxc/%s", c.BaseURL, uid), b)
if err != nil {
return err
}
if err := p.c.do(req, nil); err != nil {
if err := c.do(req, nil); err != nil {
return err
}
return nil
}

func (p *VxcService) delete(uid string) error {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v2/product/%s/action/CANCEL_NOW", p.c.BaseURL, uid), nil)
func (c *Client) delete(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 := p.c.do(req, nil); err != nil {
if err := c.do(req, nil); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -171,18 +162,18 @@ func (v *PrivateVxcUpdateInput) toPayload() ([]byte, error) {
return json.Marshal(payload)
}

func (p *VxcService) CreatePrivateVxc(v *PrivateVxcCreateInput) (*string, error) {
return p.create(v)
func (c *Client) CreatePrivateVxc(v *PrivateVxcCreateInput) (*string, error) {
return c.create(v)
}

func (p *VxcService) GetPrivateVxc(uid string) (*ProductAssociatedVxc, error) {
return p.get(uid)
func (c *Client) GetPrivateVxc(uid string) (*ProductAssociatedVxc, error) {
return c.get(uid)
}

func (p *VxcService) UpdatePrivateVxc(v *PrivateVxcUpdateInput) error {
return p.update(*v.ProductUid, v)
func (c *Client) UpdatePrivateVxc(v *PrivateVxcUpdateInput) error {
return c.update(*v.ProductUid, v)
}

func (p *VxcService) DeletePrivateVxc(uid string) error {
return p.delete(uid)
func (c *Client) DeletePrivateVxc(uid string) error {
return c.delete(uid)
}
8 changes: 4 additions & 4 deletions megaport/resource_megaport_private_vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func resourceMegaportPrivateVxc() *schema.Resource {

func resourceMegaportPrivateVxcRead(d *schema.ResourceData, m interface{}) error {
cfg := m.(*Config)
p, err := cfg.Client.Vxc.GetPrivateVxc(d.Id())
p, err := cfg.Client.GetPrivateVxc(d.Id())
if err != nil {
log.Printf("resourceMegaportPrivateVxcRead: %v", err)
d.SetId("")
Expand All @@ -71,7 +71,7 @@ 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.CreatePrivateVxc(&api.PrivateVxcCreateInput{
uid, err := cfg.Client.CreatePrivateVxc(&api.PrivateVxcCreateInput{
ProductUidA: api.String(a["product_uid"]),
ProductUidB: api.String(b["product_uid"]),
Name: api.String(d.Get("name")),
Expand All @@ -98,7 +98,7 @@ func resourceMegaportPrivateVxcUpdate(d *schema.ResourceData, m interface{}) err
//if
log.Printf(">>1 %#v", a)
log.Printf(">>2 %#v", a["vlan"])
if err := cfg.Client.Vxc.UpdatePrivateVxc(&api.PrivateVxcUpdateInput{
if err := cfg.Client.UpdatePrivateVxc(&api.PrivateVxcUpdateInput{
InvoiceReference: api.String(d.Get("invoice_reference")),
Name: api.String(d.Get("name")),
ProductUid: api.String(d.Id()),
Expand All @@ -113,7 +113,7 @@ func resourceMegaportPrivateVxcUpdate(d *schema.ResourceData, m interface{}) err

func resourceMegaportPrivateVxcDelete(d *schema.ResourceData, m interface{}) error {
cfg := m.(*Config)
err := cfg.Client.Vxc.DeletePrivateVxc(d.Id())
err := cfg.Client.DeletePrivateVxc(d.Id())
if err != nil && err != api.ErrNotFound {
return err
}
Expand Down

0 comments on commit dcdc4bb

Please sign in to comment.