From 50982cccaa34a2427acf9fba2072c394d37777cf Mon Sep 17 00:00:00 2001 From: Pablo Mazzini Date: Fri, 9 Nov 2018 15:10:23 +0000 Subject: address comments --- dhcpv4/ztp/ztp.go | 29 +++++++---------------------- dhcpv4/ztp/ztp_test.go | 4 +--- 2 files changed, 8 insertions(+), 25 deletions(-) (limited to 'dhcpv4/ztp') diff --git a/dhcpv4/ztp/ztp.go b/dhcpv4/ztp/ztp.go index 3a66c4e..b899425 100644 --- a/dhcpv4/ztp/ztp.go +++ b/dhcpv4/ztp/ztp.go @@ -20,14 +20,6 @@ var errVendorOptionMalformed = errors.New("malformed vendor option") // ParseVendorData will try to parse dhcp4 options looking for more // specific vendor data (like model, serial number, etc). func ParseVendorData(packet *dhcpv4.DHCPv4) (*VendorData, error) { - return parseV4VendorClass(packet); -} - -// parseV4Opt60 will attempt to look at the Vendor Class option (Option 60) on -// DHCPv4. The option is formatted as a string with the content being specific -// for the vendor, usually using a delimitator to separate the values. -// See: https://tools.ietf.org/html/rfc1533#section-9.11 -func parseV4VendorClass(packet *dhcpv4.DHCPv4) (*VendorData, error) { opt := packet.GetOneOption(dhcpv4.OptionClassIdentifier) if opt == nil { return nil, nil @@ -66,27 +58,20 @@ func parseV4VendorClass(packet *dhcpv4.DHCPv4) (*VendorData, error) { // Juniper-qfx10008 - (serial in hostname option) // Juniper-qfx10002-361-DN817 -- (model has a dash in it!) case strings.HasPrefix(vc, "Juniper-"): - // strip of the prefix - vc := vc[len("Juniper-"):] - vd.VendorName = "Juniper" - - sepIdx := strings.LastIndex(vc, "-") - if sepIdx == -1 { - // No separator was found. Attempt serial number from the hostname + p := strings.Split(vc, "-") + if len(p) < 3 { + vd.Model = p[1] if opt := packet.GetOneOption(dhcpv4.OptionHostName); opt != nil { vd.Serial = opt.(*dhcpv4.OptHostName).HostName } else { - return nil, errVendorOptionMalformed + return nil, errors.New("host name option is missing") } } else { - if len(vc) == sepIdx+1 { - return nil, errVendorOptionMalformed - } - vd.Serial = vc[sepIdx+1:] - vc = vc[:sepIdx] + vd.Model = strings.Join(p[1:len(p)-1], "-") + vd.Serial = p[len(p)-1] } - vd.Model = vc + vd.VendorName = p[0] return vd, nil } diff --git a/dhcpv4/ztp/ztp_test.go b/dhcpv4/ztp/ztp_test.go index 3fdb951..15a9d26 100644 --- a/dhcpv4/ztp/ztp_test.go +++ b/dhcpv4/ztp/ztp_test.go @@ -39,8 +39,6 @@ func TestParseV4VendorClass(t *testing.T) { want: &VendorData{VendorName: "Juniper", Model: "qfx10008", Serial: "DE123"}, }, {name: "juniperNoSerial", vc: "Juniper-qfx10008", fail: true}, - {name: "juniperInvalid", vc: "Juniper-", fail: true}, - {name: "juniperInvalid2", vc: "Juniper-qfx99999-", fail: true}, { name: "zpe", vc: "ZPESystems:NSC:001234567", @@ -65,7 +63,7 @@ func TestParseV4VendorClass(t *testing.T) { }) } - vd, err := parseV4VendorClass(packet) + vd, err := ParseVendorData(packet) if tc.fail { require.Error(t, err) } else { -- cgit v1.2.3