From b9e04d0b248e0cc678733e0125e00eeeb6b733c2 Mon Sep 17 00:00:00 2001 From: Lionel Hercot Date: Sat, 11 Feb 2023 13:56:42 +0100 Subject: [PATCH] [ignore] Add utils function in mso/utils.go and rename from util.go to utils.go --- mso/util.go | 12 ---------- mso/utils.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 12 deletions(-) delete mode 100644 mso/util.go create mode 100644 mso/utils.go diff --git a/mso/util.go b/mso/util.go deleted file mode 100644 index 69dd336c..00000000 --- a/mso/util.go +++ /dev/null @@ -1,12 +0,0 @@ -package mso - -const version = 1 - -func toStringList(configured interface{}) []string { - vs := make([]string, 0, 1) - val, ok := configured.(string) - if ok && val != "" { - vs = append(vs, val) - } - return vs -} diff --git a/mso/utils.go b/mso/utils.go new file mode 100644 index 00000000..9b37e9dd --- /dev/null +++ b/mso/utils.go @@ -0,0 +1,65 @@ +package mso + +import ( + "fmt" + "log" + "regexp" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +const version = 1 + +func toStringList(configured interface{}) []string { + vs := make([]string, 0, 1) + val, ok := configured.(string) + if ok && val != "" { + vs = append(vs, val) + } + return vs +} + +func makeTestVariable(s string) string { + return fmt.Sprintf("acctest_%s", s) +} + +func epgRefValidation() schema.SchemaValidateFunc { + return func(i interface{}, k string) (s []string, es []error) { + v, ok := i.(string) + if !ok { + es = append(es, fmt.Errorf("expected type of %s to be string", k)) + return + } + res, err := regexp.MatchString(`^\/schemas\/(.)+\/templates\/(.)+\/anps\/(.)+\/epgs\/(.)+$`, v) + if !res { + log.Printf("err: %v\n", err) + es = append(es, fmt.Errorf("invalid epg reference:expected format /schema/{schema_id}/templates/{template_name}/anps/{anp_name}/epgs/{epg_name}")) + } + return + } +} + +func externalEpgRefValidation() schema.SchemaValidateFunc { + return func(i interface{}, k string) (s []string, es []error) { + v, ok := i.(string) + if !ok { + es = append(es, fmt.Errorf("expected type of %s to be string", k)) + return + } + res, err := regexp.MatchString(`^\/schemas\/(.)+\/templates\/(.)+\/externalEpgs\/(.)+$`, v) + if !res { + log.Printf("err: %v\n", err) + es = append(es, fmt.Errorf("invalid external epg reference:expected format /schema/{schema_id}/template/{template_name}/externalEpgs/{external_epg_name}")) + } + return + } +} + +func StringInSlice(a string, list []string) bool { + for _, b := range list { + if b == a { + return true + } + } + return false +}