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

Extend ovirt_nic with mac address manage #487

Open
wants to merge 1 commit into
base: main
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
26 changes: 25 additions & 1 deletion internal/ovirt/resource_ovirt_nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ var nicSchema = map[string]*schema.Schema{
ForceNew: true,
ValidateDiagFunc: validateNonEmpty,
},
"mac": {
Type: schema.TypeString,
Optional: true,
Description: "Custom Mac Address for the NIC.",
ForceNew: true,
Computed: true,
ValidateDiagFunc: validateMacAddress,
},
}

func (p *provider) nicResource() *schema.Resource {
Expand All @@ -56,13 +64,28 @@ func (p *provider) nicCreate(ctx context.Context, data *schema.ResourceData, _ i
vmID := data.Get("vm_id").(string)
vnicProfileID := data.Get("vnic_profile_id").(string)
name := data.Get("name").(string)
var diags diag.Diagnostics
var err error
params := ovirtclient.CreateNICParams()
params, err = params.WithMac(data.Get("mac").(string))
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to set Mac Address.",
Detail: err.Error(),
})
}
if diags.HasError() {
return diags
}

nic, err := client.CreateNIC(
ovirtclient.VMID(vmID),
ovirtclient.VNICProfileID(vnicProfileID),
name,
nil,
params,
)

if err != nil {
return errorToDiags("create NIC", err)
}
Expand Down Expand Up @@ -136,5 +159,6 @@ func nicResourceUpdate(nic ovirtclient.NIC, data *schema.ResourceData) diag.Diag
diags = setResourceField(data, "vnic_profile_id", nic.VNICProfileID(), diags)
diags = setResourceField(data, "name", nic.Name(), diags)
diags = setResourceField(data, "vm_id", nic.VMID(), diags)
diags = setResourceField(data, "mac", nic.Mac(), diags)
return diags
}
27 changes: 27 additions & 0 deletions internal/ovirt/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ovirt

import (
"fmt"
"net"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -341,3 +342,29 @@ func validateHugePages(i interface{}, path cty.Path) diag.Diagnostics {
}
return nil
}

func validateMacAddress(i interface{}, path cty.Path) diag.Diagnostics {
mac, ok := i.(string)
if !ok {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "The mac address should be a string.",
Detail: "The provided mac address value is not a string.",
AttributePath: path,
},
}
}

if _, err := net.ParseMAC(mac); err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid mac address value.",
Detail: err.Error(),
AttributePath: path,
},
}
}
return nil
}