Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support attaching interfaces to devices #176

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions maas/resource_maas_network_interface_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,23 @@ func resourceNetworkInterfaceLinkCreate(ctx context.Context, d *schema.ResourceD
client := meta.(*client.Client)

// Create network interface link
machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := getNetworkInterface(client, machine.SystemID, d.Get("network_interface").(string))
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := getNetworkInterface(client, systemID, d.Get("network_interface").(string))
if err != nil {
return diag.FromErr(err)
}
subnet, err := getSubnet(client, d.Get("subnet").(string))
if err != nil {
return diag.FromErr(err)
}
link, err := createNetworkInterfaceLink(client, machine.SystemID, networkInterface, getNetworkInterfaceLinkParams(d, subnet.ID))
link, err := createNetworkInterfaceLink(client, systemID, networkInterface, getNetworkInterfaceLinkParams(d, subnet.ID))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -100,17 +104,21 @@ func resourceNetworkInterfaceLinkRead(ctx context.Context, d *schema.ResourceDat
if err != nil {
return diag.FromErr(err)
}
machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := getNetworkInterface(client, machine.SystemID, d.Get("network_interface").(string))
networkInterface, err := getNetworkInterface(client, systemID, d.Get("network_interface").(string))
if err != nil {
return diag.FromErr(err)
}

// Get the network interface link
link, err := getNetworkInterfaceLink(client, machine.SystemID, networkInterface.ID, linkID)
link, err := getNetworkInterfaceLink(client, systemID, networkInterface.ID, linkID)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -131,21 +139,25 @@ func resourceNetworkInterfaceLinkUpdate(ctx context.Context, d *schema.ResourceD
if err != nil {
return diag.FromErr(err)
}
machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := getNetworkInterface(client, machine.SystemID, d.Get("network_interface").(string))
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := getNetworkInterface(client, systemID, d.Get("network_interface").(string))
if err != nil {
return diag.FromErr(err)
}

// Run update operation
if _, err := client.Machine.ClearDefaultGateways(machine.SystemID); err != nil {
if _, err := client.Machine.ClearDefaultGateways(systemID); err != nil {
return diag.FromErr(err)
}
if d.Get("default_gateway").(bool) {
if _, err := client.NetworkInterface.SetDefaultGateway(machine.SystemID, networkInterface.ID, linkID); err != nil {
if _, err := client.NetworkInterface.SetDefaultGateway(systemID, networkInterface.ID, linkID); err != nil {
return diag.FromErr(err)
}
}
Expand All @@ -161,17 +173,21 @@ func resourceNetworkInterfaceLinkDelete(ctx context.Context, d *schema.ResourceD
if err != nil {
return diag.FromErr(err)
}
machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := getNetworkInterface(client, machine.SystemID, d.Get("network_interface").(string))
networkInterface, err := getNetworkInterface(client, systemID, d.Get("network_interface").(string))
if err != nil {
return diag.FromErr(err)
}

// Delete the network interface link
if err := deleteNetworkInterfaceLink(client, machine.SystemID, networkInterface.ID, linkID); err != nil {
if err := deleteNetworkInterfaceLink(client, systemID, networkInterface.ID, linkID); err != nil {
return diag.FromErr(err)
}

Expand Down
46 changes: 34 additions & 12 deletions maas/resource_maas_network_interface_physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ func resourceMaasNetworkInterfacePhysical() *schema.Resource {
return nil, fmt.Errorf("unexpected format of ID (%q), expected MACHINE/NETWORK_INTERFACE", d.Id())
}
client := meta.(*client.Client)
machine, err := getMachine(client, idParts[0])
machineOrDevice, err := getMachineOrDevice(client, idParts[0])
if err != nil {
return nil, err
}
n, err := getNetworkInterfacePhysical(client, machine.SystemID, idParts[1])
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return nil, err
}
n, err := getNetworkInterfacePhysical(client, systemID, idParts[1])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,21 +91,26 @@ func resourceMaasNetworkInterfacePhysical() *schema.Resource {
func resourceNetworkInterfacePhysicalCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := findNetworkInterfacePhysical(client, machine.SystemID, d.Get("mac_address").(string))
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := findNetworkInterfacePhysical(client, systemID, d.Get("mac_address").(string))
if err != nil {
return diag.FromErr(err)
}
if networkInterface == nil {
networkInterface, err = client.NetworkInterfaces.CreatePhysical(machine.SystemID, getNetworkInterfacePhysicalParams(d))
networkInterface, err = client.NetworkInterfaces.CreatePhysical(systemID, getNetworkInterfacePhysicalParams(d))
} else {
networkInterface, err = client.NetworkInterface.Update(machine.SystemID, networkInterface.ID, getNetworkInterfaceUpdateParams(d))
networkInterface, err = client.NetworkInterface.Update(systemID, networkInterface.ID, getNetworkInterfaceUpdateParams(d))
}
if err != nil {
return diag.FromErr(err)

}
d.SetId(strconv.Itoa(networkInterface.ID))

Expand All @@ -122,15 +131,19 @@ func resourceNetworkInterfacePhysicalCreate(ctx context.Context, d *schema.Resou
func resourceNetworkInterfacePhysicalRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := client.NetworkInterface.Get(machine.SystemID, id)
networkInterface, err := client.NetworkInterface.Get(systemID, id)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -152,15 +165,20 @@ func resourceNetworkInterfacePhysicalRead(ctx context.Context, d *schema.Resourc
func resourceNetworkInterfacePhysicalUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.FromErr(err)
}
networkInterface, err := client.NetworkInterface.Update(machine.SystemID, id, getNetworkInterfaceUpdateParams(d))

networkInterface, err := client.NetworkInterface.Update(systemID, id, getNetworkInterfaceUpdateParams(d))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -182,15 +200,19 @@ func resourceNetworkInterfacePhysicalUpdate(ctx context.Context, d *schema.Resou
func resourceNetworkInterfacePhysicalDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*client.Client)

machine, err := getMachine(client, d.Get("machine").(string))
machineOrDevice, err := getMachineOrDevice(client, d.Get("machine").(string))
if err != nil {
return diag.FromErr(err)
}
systemID, err := getSystemID(machineOrDevice)
if err != nil {
return diag.FromErr(err)
}
id, err := strconv.Atoi(d.Id())
if err != nil {
return diag.FromErr(err)
}
if err := client.NetworkInterface.Delete(machine.SystemID, id); err != nil {
if err := client.NetworkInterface.Delete(systemID, id); err != nil {
return diag.FromErr(err)
}

Expand Down
25 changes: 25 additions & 0 deletions maas/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,28 @@ func setTerraformState(d *schema.ResourceData, tfState map[string]interface{}) e
}
return nil
}

func getMachineOrDevice(client *client.Client, identifier string) (interface{}, error) {
machine, err := getMachine(client, identifier)
if err == nil {
return machine, nil
}

device, err := getDevice(client, identifier)
if err == nil {
return device, nil
}

return nil, err
}

func getSystemID(machineOrDevice interface{}) (string, error) {
switch v := machineOrDevice.(type) {
case *entity.Machine:
return v.SystemID, nil
case *entity.Device:
return v.SystemID, nil
default:
return "", fmt.Errorf("unexpected type %T for machineOrDevice", machineOrDevice)
}
}
Loading