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

Commit

Permalink
Rename Interface to NetworkInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
onwsk8r committed Mar 24, 2020
1 parent 8784bc2 commit 4ffc110
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,52 @@ import (
"github.com/roblox/terraform-provider-maas/pkg/maas/entity"
)

// Interface contains methods for connecting maas_interfaces to MaaS Interfaces.
type Interface struct {
// NetworkInterface contains methods for connecting maas_interfaces to MaaS Interfaces.
type NetworkInterface struct {
mo *gomaasapi.MAASObject
}

// NewInterface creates a new Interface.
// NewNetworkInterface creates a new NetworkInterface.
// The parameter should be the metadata passed to the Terraform CRUD functions,
// which should be a *gomaasapi.MAASObject. This function will cast the interface
// received by the Terraform functions to the correct type and store it in the
// Interface. If the type does not convert, a nil pointer will be stored.
func NewInterface(m interface{}) *Interface {
// NetworkInterface. If the type does not convert, a nil pointer will be stored.
func NewNetworkInterface(m interface{}) *NetworkInterface {
mo := m.(*gomaasapi.MAASObject)
return &Interface{
return &NetworkInterface{
mo: mo,
}
}

// Create a new Interface in MaaS.
// Create a new NetworkInterface in MaaS.
// The sch parameter should be a tfschema type that can be used to create an
// Interface in MaaS. The only compatible type at this time is InterfacePhysical.
// Interface in MaaS. The only compatible type at this time is NetworkInterfacePhysical.
// This method will set the InterfaceID of the type, and expects any attributes
// required to create the Interface to be preset.
// This function will return an error if the MaaS API client returns an error.
func (i *Interface) Create(sch interface{}) error {
var res *entity.Interface
func (i *NetworkInterface) Create(sch interface{}) error {
var res *entity.NetworkInterface
var err error
switch tmpl := sch.(type) { // nolint: gocritic
case *tfschema.InterfacePhysical:
case *tfschema.NetworkInterfacePhysical:
params := tmpl.Params()
res, err = gmaw.NewInterfaces(i.mo).CreatePhysical(tmpl.SystemID, params)
res, err = gmaw.NewNetworkInterfaces(i.mo).CreatePhysical(tmpl.SystemID, params)
if err != nil {
tmpl.InterfaceID = res.ID
}
}
return err
}

// ReadTo updates a tfschema representation of an Interface to the current state in MaaS.
// ReadTo updates a tfschema representation of an NetworkInterface to the current state in MaaS.
// The sch parameter should be a tfschema type that represents an Interface in MaaS. This
// function will return an error if the MaaS API client returns an error.
func (i *Interface) ReadTo(sch interface{}) error {
var res *entity.Interface
func (i *NetworkInterface) ReadTo(sch interface{}) error {
var res *entity.NetworkInterface
var err error
switch tmpl := sch.(type) { // nolint: gocritic
case *tfschema.InterfacePhysical:
if res, err = gmaw.NewInterface(i.mo).Get(tmpl.SystemID, tmpl.InterfaceID); err != nil {
case *tfschema.NetworkInterfacePhysical:
if res, err = gmaw.NewNetworkInterface(i.mo).Get(tmpl.SystemID, tmpl.InterfaceID); err != nil {
return err
}
tmpl.Name = res.Name
Expand All @@ -71,12 +71,12 @@ func (i *Interface) ReadTo(sch interface{}) error {
// UpdateFrom updates the MaaS resource represented by sch.
// The sch parameter should be a tfschema type that represents an Interface in MaaS. This
// function will return an error if the MaaS API client returns an error.
func (i *Interface) UpdateFrom(sch interface{}) error {
var res *entity.Interface
ifc := gmaw.NewInterface(i.mo)
func (i *NetworkInterface) UpdateFrom(sch interface{}) error {
var res *entity.NetworkInterface
ifc := gmaw.NewNetworkInterface(i.mo)
var err error
switch tmpl := sch.(type) { // nolint: gocritic
case *tfschema.InterfacePhysical:
case *tfschema.NetworkInterfacePhysical:
res, err = ifc.Get(tmpl.SystemID, tmpl.InterfaceID)
if err == nil && !(tmpl.Name == res.Name && tmpl.MACAddress == res.MACAddress &&
reflect.DeepEqual(tmpl.Tags, res.Tags) && tmpl.VLAN == res.VLAN.Name &&
Expand All @@ -90,33 +90,33 @@ func (i *Interface) UpdateFrom(sch interface{}) error {
// Delete an Interface in MaaS.
// The sch parameter should be a tfschema type that represents an Interface in MaaS. This
// function will return an error if the MaaS API client returns an error.
func (i *Interface) Delete(sch interface{}) (err error) {
func (i *NetworkInterface) Delete(sch interface{}) (err error) {
switch tmpl := sch.(type) { // nolint: gocritic
case *tfschema.InterfacePhysical:
err = gmaw.NewInterface(i.mo).Delete(tmpl.SystemID, tmpl.InterfaceID)
case *tfschema.NetworkInterfacePhysical:
err = gmaw.NewNetworkInterface(i.mo).Delete(tmpl.SystemID, tmpl.InterfaceID)
}
return
}

// LinkSubnet creates a link between an interface and a subnet.
// This function will return an error if the MaaS API client returns an error.
func (i *Interface) LinkSubnet(sch *tfschema.InterfaceLink) (err error) {
_, err = gmaw.NewInterface(i.mo).LinkSubnet(sch.SystemID, sch.InterfaceID, sch.Params())
func (i *NetworkInterface) LinkSubnet(sch *tfschema.NetworkInterfaceLink) (err error) {
_, err = gmaw.NewNetworkInterface(i.mo).LinkSubnet(sch.SystemID, sch.InterfaceID, sch.Params())
return
}

// UnlinkSubnet removes the link between an interface and a subnet.
// This function will return an error if the MaaS API client returns an error.
func (i *Interface) UnlinkSubnet(sch *tfschema.InterfaceLink) (err error) {
_, err = gmaw.NewInterface(i.mo).UnlinkSubnet(sch.SystemID, sch.InterfaceID, sch.SubnetID)
func (i *NetworkInterface) UnlinkSubnet(sch *tfschema.NetworkInterfaceLink) (err error) {
_, err = gmaw.NewNetworkInterface(i.mo).UnlinkSubnet(sch.SystemID, sch.InterfaceID, sch.SubnetID)
return
}

// ReadLink synchronizes the InterfaceLink with the current MaaS state.
// ReadLink synchronizes the NetworkInterfaceLink with the current MaaS state.
// This function will return an error if the MaaS API client returns an error, or
// if the link cannot be found in MaaS.
func (i *Interface) ReadLink(sch *tfschema.InterfaceLink) error {
res, err := gmaw.NewInterface(i.mo).Get(sch.SystemID, sch.InterfaceID)
func (i *NetworkInterface) ReadLink(sch *tfschema.NetworkInterfaceLink) error {
res, err := gmaw.NewNetworkInterface(i.mo).Get(sch.SystemID, sch.InterfaceID)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func Provider() terraform.ResourceProvider {
},
ResourcesMap: map[string]*schema.Resource{
"maas_instance": resourceInstance(),
"maas_interface_physical": ResourceInterfacePhysical(),
"maas_interface_link": ResourceInterfaceLink(),
"maas_interface_physical": ResourceNetworkInterfacePhysical(),
"maas_interface_link": ResourceNetworkInterfaceLink(),
"maas_server": ResourceServer(),
},
DataSourcesMap: map[string]*schema.Resource{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/roblox/terraform-provider-maas/internal/tfschema"
)

func ResourceInterfaceLink() *schema.Resource {
func ResourceNetworkInterfaceLink() *schema.Resource {
return &schema.Resource{
Create: resourceInterfaceLinkCreate,
Read: resourceInterfaceLinkRead,
Delete: resourceInterfaceLinkDelete,
Create: resourceNetworkInterfaceLinkCreate,
Read: resourceNetworkInterfaceLinkRead,
Delete: resourceNetworkInterfaceLinkDelete,

Schema: map[string]*schema.Schema{
"system_id": &schema.Schema{
Expand Down Expand Up @@ -80,9 +80,9 @@ func ResourceInterfaceLink() *schema.Resource {
}
}

func resourceInterfaceLinkCreate(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfaceLink(d)
func resourceNetworkInterfaceLinkCreate(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfaceLink(d)
if err = ifc.LinkSubnet(sch); err != nil {
return
}
Expand All @@ -92,18 +92,18 @@ func resourceInterfaceLinkCreate(d *schema.ResourceData, m interface{}) (err err
return
}

func resourceInterfaceLinkRead(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfaceLink(d)
func resourceNetworkInterfaceLinkRead(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfaceLink(d)
if err = ifc.ReadLink(sch); err == nil {
err = sch.UpdateResource(d)
}
return err
}

func resourceInterfaceLinkDelete(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfaceLink(d)
func resourceNetworkInterfaceLinkDelete(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfaceLink(d)
if err = ifc.UnlinkSubnet(sch); err == nil {
d.SetId("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"github.com/roblox/terraform-provider-maas/internal/tfschema"
)

func ResourceInterfacePhysical() *schema.Resource {
func ResourceNetworkInterfacePhysical() *schema.Resource {
return &schema.Resource{
Create: resourceInterfacePhysicalCreate,
Read: resourceInterfacePhysicalRead,
Update: resourceInterfacePhysicalUpdate,
Delete: resourceInterfacePhysicalDelete,
Create: resourceNetworkInterfacePhysicalCreate,
Read: resourceNetworkInterfacePhysicalRead,
Update: resourceNetworkInterfacePhysicalUpdate,
Delete: resourceNetworkInterfacePhysicalDelete,

Schema: map[string]*schema.Schema{
"system_id": &schema.Schema{
Expand Down Expand Up @@ -63,39 +63,39 @@ func ResourceInterfacePhysical() *schema.Resource {
}
}

func resourceInterfacePhysicalCreate(d *schema.ResourceData, m interface{}) error {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfacePhysical(d)
func resourceNetworkInterfacePhysicalCreate(d *schema.ResourceData, m interface{}) error {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfacePhysical(d)
if err := ifc.Create(sch); err != nil {
return err
}
if id, err := sch.GetID(); err == nil {
d.SetId(id)
}
return resourceInterfacePhysicalRead(d, m)
return resourceNetworkInterfacePhysicalRead(d, m)
}

func resourceInterfacePhysicalRead(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfacePhysical(d)
func resourceNetworkInterfacePhysicalRead(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfacePhysical(d)
if err = ifc.ReadTo(sch); err == nil {
err = sch.UpdateResource(d)
}
return err
}

func resourceInterfacePhysicalUpdate(d *schema.ResourceData, m interface{}) error {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfacePhysical(d)
func resourceNetworkInterfacePhysicalUpdate(d *schema.ResourceData, m interface{}) error {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfacePhysical(d)
if err := ifc.UpdateFrom(sch); err != nil {
return err
}
return resourceInterfacePhysicalRead(d, m)
return resourceNetworkInterfacePhysicalRead(d, m)
}

func resourceInterfacePhysicalDelete(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewInterface(m)
sch := tfschema.NewInterfacePhysical(d)
func resourceNetworkInterfacePhysicalDelete(d *schema.ResourceData, m interface{}) (err error) {
ifc := bridge.NewNetworkInterface(m)
sch := tfschema.NewNetworkInterfacePhysical(d)
if err = ifc.Delete(sch); err == nil {
d.SetId("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/roblox/terraform-provider-maas/pkg/api/params"
)

// InterfacePhysical represents a maas_interface_physical
type InterfacePhysical struct {
// NetworkInterfacePhysical represents a maas_interface_physical
type NetworkInterfacePhysical struct {
InterfaceID int
SystemID string
Name string
Expand All @@ -22,9 +22,9 @@ type InterfacePhysical struct {
Autoconf bool
}

// NewInterfacePhysical creates an InterfacePhysical from the Terraform state.
func NewInterfacePhysical(d *schema.ResourceData) *InterfacePhysical {
var i InterfacePhysical
// NewNetworkInterfacePhysical creates an NetworkInterfacePhysical from the Terraform state.
func NewNetworkInterfacePhysical(d *schema.ResourceData) *NetworkInterfacePhysical {
var i NetworkInterfacePhysical
if i.SystemID = d.Get("system_id").(string); i.SystemID == "" {
id := d.Id()
idx := strings.Index(id, ":")
Expand All @@ -47,8 +47,8 @@ func NewInterfacePhysical(d *schema.ResourceData) *InterfacePhysical {
}

// Params returns a type that can be used to create and update a MaaS Interface.
func (i *InterfacePhysical) Params() *params.InterfacePhysical {
return &params.InterfacePhysical{
func (i *NetworkInterfacePhysical) Params() *params.NetworkInterfacePhysical {
return &params.NetworkInterfacePhysical{
Name: i.Name,
MACAddress: i.MACAddress,
Tags: i.Tags,
Expand All @@ -60,7 +60,7 @@ func (i *InterfacePhysical) Params() *params.InterfacePhysical {
}

// UpdateResource updates the Terraform state to reflect the state of the struct.
func (i *InterfacePhysical) UpdateResource(d *schema.ResourceData) (err error) {
func (i *NetworkInterfacePhysical) UpdateResource(d *schema.ResourceData) (err error) {
if err = d.Set("name", i.Name); err != nil {
return
}
Expand All @@ -81,7 +81,7 @@ func (i *InterfacePhysical) UpdateResource(d *schema.ResourceData) (err error) {
}

// GetID returns "<SystemID>:<InterfaceID>" to be used as the Terraform resource ID.
func (i *InterfacePhysical) GetID() (string, error) {
func (i *NetworkInterfacePhysical) GetID() (string, error) {
if i.SystemID == "" {
return "", fmt.Errorf("SystemID is empty")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/roblox/terraform-provider-maas/pkg/api/params"
)

// InterfaceLink represents a maas_interface_link
type InterfaceLink struct {
// NetworkInterfaceLink represents a maas_interface_link
type NetworkInterfaceLink struct {
SystemID string
InterfaceID int
SubnetID int
Expand All @@ -21,9 +21,9 @@ type InterfaceLink struct {
DefaultGateway net.IP
}

// NewInterfaceLink creates an InterfaceLink from the Terraform state.
func NewInterfaceLink(d *schema.ResourceData) *InterfaceLink {
var i InterfaceLink
// NewNetworkInterfaceLink creates an NetworkInterfaceLink from the Terraform state.
func NewNetworkInterfaceLink(d *schema.ResourceData) *NetworkInterfaceLink {
var i NetworkInterfaceLink
if i.SystemID = d.Get("system_id").(string); i.SystemID == "" {
id := strings.Split(d.Id(), ":")
i.SystemID = id[0]
Expand All @@ -41,8 +41,8 @@ func NewInterfaceLink(d *schema.ResourceData) *InterfaceLink {
}

// Params returns a type that can be used to create and delete a MaaS Interface.
func (i *InterfaceLink) Params() *params.InterfaceLink {
return &params.InterfaceLink{
func (i *NetworkInterfaceLink) Params() *params.NetworkInterfaceLink {
return &params.NetworkInterfaceLink{
Subnet: i.SubnetID,
Mode: i.Mode,
IPAddress: i.IPAddress,
Expand All @@ -52,7 +52,7 @@ func (i *InterfaceLink) Params() *params.InterfaceLink {
}

// GetID returns "<SystemID>:<InterfaceID>:<SubnetID>" to be used as the Terraform resource ID.
func (i *InterfaceLink) GetID() (string, error) {
func (i *NetworkInterfaceLink) GetID() (string, error) {
if i.SystemID == "" {
return "", fmt.Errorf("SystemID is empty")
}
Expand All @@ -66,7 +66,7 @@ func (i *InterfaceLink) GetID() (string, error) {
}

// UpdateResource updates the Terraform state to reflect the state of the struct.
func (i *InterfaceLink) UpdateResource(d *schema.ResourceData) (err error) {
func (i *NetworkInterfaceLink) UpdateResource(d *schema.ResourceData) (err error) {
if err = d.Set("mode", i.Mode); err != nil {
return
}
Expand Down
18 changes: 0 additions & 18 deletions pkg/api/interface.go

This file was deleted.

Loading

0 comments on commit 4ffc110

Please sign in to comment.