Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Add Interface(s) gomaasapi wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
onwsk8r committed Feb 18, 2020
1 parent fa753b1 commit 30dcc3a
Show file tree
Hide file tree
Showing 7 changed files with 787 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package api

import (
"github.com/roblox/terraform-provider-maas/pkg/api/params"
"github.com/roblox/terraform-provider-maas/pkg/maas/entity"
)

type Interface interface {
Delete(systemID string, id int) error
Get(systemID string, id int) (*entity.Interface, error)
AddTag(systemID string, id int, tag string) (*entity.Interface, error)
Disconnect(systemID string, id int) (*entity.Interface, error)
LinkSubnet(systemID string, id int, params *params.InterfaceLinkSubnet) (*entity.Interface, error)
RemoveTag(systemID string, id int, tag string) (*entity.Interface, error)
SetDefaultGateway(systemID string, id, linkID int) (*entity.Interface, error)
UnlinkSubnet(systemID string, id, linkID int) (*entity.Interface, error)
Put(systemID string, id int, params interface{}) (*entity.Interface, error)
}
14 changes: 14 additions & 0 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package api

import (
"github.com/roblox/terraform-provider-maas/pkg/api/params"
"github.com/roblox/terraform-provider-maas/pkg/maas/entity"
)

type Interfaces interface {
Get(systemID string) ([]entity.Interface, error)
CreateBond(systemID string, params *params.InterfaceBond) (*entity.Interface, error)
CreateBridge(systemID string, params *params.InterfaceBridge) (*entity.Interface, error)
CreatePhysical(systemID string, params *params.InterfacePhysical) (*entity.Interface, error)
CreateVLAN(systemID string, params *params.InterfaceVLAN) (*entity.Interface, error)
}
60 changes: 60 additions & 0 deletions pkg/api/params/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package params

import "net"

// InterfaceBond is the parameters for the Interfaces create_bond POST operation.
type InterfaceBond struct {
InterfacePhysical
Parents []int `json:"parents,omitempty"`
BondMode string `json:"bond_mode,omitempty"`
BondMiimon int `json:"bond_miimon,omitempty"`
BondDownDelay int `json:"bond_down_delay,omitempty"`
BondUpDelay int `json:"bond_up_delay,omitempty"`
BondLACPRate string `json:"bond_lacp_rate,omitempty"`
BondXMitHashPolicy string `json:"bond_x_mit_hash_policy,omitempty"`
BondNumberGratARP int `json:"bond_number_grat_arp,omitempty"`
}

// InterfaceBridge is the parameters for the Interfaces create_bridge POST operation.
type InterfaceBridge struct {
InterfacePhysical
Parent int `json:"parent,omitempty"`
BridgeSTP bool `json:"bridge_stp,omitempty"`
BridgeFD int `json:"bridge_fd,omitempty"`
}

// InterfacePhysical is the parameters for the Interfaces create_physical POST operation.
type InterfacePhysical struct {
Name string `json:"name,omitempty"`
MACAddress string `json:"mac_address,omitempty"`
Tags []string `json:"tags,omitempty"`
VLAN string `json:"vlan,omitempty"`
MTU int `json:"mtu,omitempty"`
AcceptRA bool `json:"accept_ra,omitempty"`
Autoconf bool `json:"autoconf,omitempty"`
}

// InterfaceVLAN is the parameters for the Interfaces create_vlan POST operation.
type InterfaceVLAN struct {
Tags []string `json:"tags,omitempty"`
VLAN string `json:"vlan,omitempty"`
Parent int `json:"parent,omitempty"`
MTU int `json:"mtu,omitempty"`
AcceptRA bool `json:"accept_ra,omitempty"`
Autoconf bool `json:"autoconf,omitempty"`
}

// InterfaceLinkSubnet is used with Interface.LinkSubnet().
// Mode must be one of (AUTO, DHCP, STATIC, LINK_UP). IPAddress is ignored
// unless mode is STATIC, and will be set automatically if empty. Force
// allows LINK_UP to be set when other links exist, allows links between
// different VLANs, and deletes all other links on the interface.
// DefaultGateway is ignored unless Mode is AUTO or STATIC.
// Note: You can parse an IP address into a net.IP via net.ParseIP(string).
type InterfaceLinkSubnet struct {
Mode string `json:"mode,omitempty"`
Subnet int `json:"subnet,omitempty"`
IPAddress net.IP `json:"ip_address,omitempty"`
Force bool `json:"force,omitempty"`
DefaultGateway net.IP `json:"default_gateway,omitempty"`
}
143 changes: 143 additions & 0 deletions pkg/gmaw/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package gmaw

import (
"encoding/json"
"net/url"
"strconv"

"github.com/juju/gomaasapi"
"github.com/roblox/terraform-provider-maas/pkg/api/params"
"github.com/roblox/terraform-provider-maas/pkg/maas"
"github.com/roblox/terraform-provider-maas/pkg/maas/entity"
)

// Interface provides methods for the Interface operations in the MaaS API.
// This type should be instantiated via NewInterface(). It fulfills the
// api.Interface interface.
type Interface struct {
c Client
}

// NewInterface configures a new Interface.
func NewInterface(client *gomaasapi.MAASObject) *Interface {
c := client.GetSubObject("nodes")
return &Interface{c: Client{&c}}
}

// client returns a Client with the MAASObject that correlates to the correct endpoint.
func (i *Interface) client(systemID string, id int) Client {
return i.c.GetSubObject(systemID).
GetSubObject("interfaces").
GetSubObject(strconv.Itoa(id))
}

// Delete the selected interface.
// This function returns an error if the gomaasapi returns an error.
func (i *Interface) Delete(systemID string, id int) error {
return i.client(systemID, id).Delete()
}

// Get information about the interface with <id> on <systemID>.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) Get(systemID string, id int) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
err = i.client(systemID, id).Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// AddTag adds an additional tag to the interface.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) AddTag(systemID string, id int, tag string) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
qsp := url.Values{}
qsp.Add("tag", tag)
err = i.client(systemID, id).Post("add_tag", qsp, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// Disconnect the interface with <id> on <systemID>.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) Disconnect(systemID string, id int) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
err = i.client(systemID, id).Post("disconnect", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// LinkSubnet links the interface with <id> on <systemID> as described in <params>.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) LinkSubnet(systemID string, id int, p *params.InterfaceLinkSubnet) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
qsp := maas.ToQSP(p)
err = i.client(systemID, id).Post("link_subnet", qsp, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// RemoveTag removes the <tag> tag from the interface
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) RemoveTag(systemID string, id int, tag string) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
qsp := url.Values{}
qsp.Add("tag", tag)
err = i.client(systemID, id).Post("remove_tag", qsp, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// SetDefaultGateway sets interface <id> to be the default gateway for <systemID>.
// If this interface has more than one subnet with a gateway IP in the same
// IP address family then specifying the ID of the link on this interface is
// required. Set the linkID to 0 to omit this parameter.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) SetDefaultGateway(systemID string, id, linkID int) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
qsp := url.Values{}
if linkID > 0 {
qsp.Add("link_id", strconv.Itoa(linkID))
}
err = i.client(systemID, id).Post("set_default_gateway", qsp, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// Unlink subnet removes the link between interface <id> and link <linkID>.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) UnlinkSubnet(systemID string, id, linkID int) (ifc *entity.Interface, err error) {
ifc = new(entity.Interface)
qsp := url.Values{}
qsp.Add("id", strconv.Itoa(linkID))
err = i.client(systemID, id).Post("unlink_subnet", qsp, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}

// Put updates the interface configuration with <params>.
// The params argument is one of params.Interface{Bond,Bridge,Physical,VLAN},
// depending on the type of interface being updated.
// This function returns an error if the gomaasapi returns an error or if
// the response cannot be decoded.
func (i *Interface) Put(systemID string, id int, p interface{}) (ifc *entity.Interface, err error) {
qsp := maas.ToQSP(p)
ifc = new(entity.Interface)
err = i.client(systemID, id).Put(qsp, func(data []byte) error {
return json.Unmarshal(data, ifc)
})
return
}
Loading

0 comments on commit 30dcc3a

Please sign in to comment.