From fefd1d4444dfca540ba4bc12980105907fb6ab88 Mon Sep 17 00:00:00 2001 From: Daniel Lepage Date: Mon, 16 Dec 2024 23:30:19 -0500 Subject: [PATCH] Add method to validate and normalize an entire profile. --- .../profile/profiledefinition/validation.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/networkdevice/profile/profiledefinition/validation.go b/pkg/networkdevice/profile/profiledefinition/validation.go index 99f80769aa1f4..21e55dfb67329 100644 --- a/pkg/networkdevice/profile/profiledefinition/validation.go +++ b/pkg/networkdevice/profile/profiledefinition/validation.go @@ -47,6 +47,32 @@ const ( MetadataSymbol ) +// ValidateEnrichProfile validates a profile and normalizes it. +func ValidateEnrichProfile(profile *ProfileDefinition) []string { + NormalizeMetrics(profile.Metrics) + // migrate legacy profile.Device.Vendor to metadata field. + if profile.Device.Vendor != "" { + dev, ok := profile.Metadata["device"] + if !ok { + profile.Metadata["device"] = MetadataResourceConfig{ + Fields: make(map[string]MetadataField), + } + dev = profile.Metadata["device"] + } + _, ok = dev.Fields["vendor"] + if !ok { + dev.Fields["vendor"] = MetadataField{ + Value: profile.Device.Vendor, + } + } + profile.Device.Vendor = "" + } + errors := ValidateEnrichMetadata(profile.Metadata) + errors = append(errors, ValidateEnrichMetrics(profile.Metrics)...) + errors = append(errors, ValidateEnrichMetricTags(profile.MetricTags)...) + return errors +} + // ValidateEnrichMetricTags validates and normalizes metric tags func ValidateEnrichMetricTags(metricTags []MetricTagConfig) []string { var errors []string