From 91df5b33bb9489947b69d4d5f682328f75fd09fc Mon Sep 17 00:00:00 2001 From: akinross Date: Mon, 11 Nov 2024 11:34:44 +0100 Subject: [PATCH] [minor_change] Add support for vpc connected to fex in resource mso_schema_site_anp_epg_bulk_staticport --- ...mso_schema_site_anp_epg_bulk_staticport.go | 22 +--- ...rce_mso_schema_site_anp_epg_static_port.go | 12 +- ...mso_schema_site_anp_epg_bulk_staticport.go | 120 +++++++----------- ...rce_mso_schema_site_anp_epg_static_port.go | 68 ++-------- 4 files changed, 62 insertions(+), 160 deletions(-) diff --git a/mso/datasource_mso_schema_site_anp_epg_bulk_staticport.go b/mso/datasource_mso_schema_site_anp_epg_bulk_staticport.go index 738afc38..f2897dad 100644 --- a/mso/datasource_mso_schema_site_anp_epg_bulk_staticport.go +++ b/mso/datasource_mso_schema_site_anp_epg_bulk_staticport.go @@ -3,7 +3,6 @@ package mso import ( "fmt" "log" - "regexp" "strconv" "github.com/ciscoecosystem/mso-go-client/client" @@ -136,10 +135,6 @@ func datasourceMSOSchemaSiteAnpEpgBulkStaticPortRead(d *schema.ResourceData, m i return fmt.Errorf("Unable to get Static Port list") } - portPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/extpaths-(?P.*)\/pathep-\[(?P.*)\])`) - vpcPath := regexp.MustCompile(`(topology\/(?P.*)\/protpaths-(?P.*)\/pathep-\[(?P.*)\])`) - dpcPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/pathep-\[(?P.*)\])`) - staticPortsList := make([]interface{}, 0, 1) for i := 0; i < portCount; i++ { portCont, err := epgCont.ArrayElement(i, "staticPorts") @@ -165,22 +160,7 @@ func datasourceMSOSchemaSiteAnpEpgBulkStaticPortRead(d *schema.ResourceData, m i staticPortMap["mode"] = models.StripQuotes(portCont.S("mode").String()) } - pathValue := models.StripQuotes(portCont.S("path").String()) - - matchedMap := make(map[string]string) - - if portPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, portPath) - staticPortMap["fex"] = matchedMap["fexValue"] - } else if vpcPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, vpcPath) - } else if dpcPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, dpcPath) - } - - staticPortMap["pod"] = matchedMap["podValue"] - staticPortMap["leaf"] = matchedMap["leafValue"] - staticPortMap["path"] = matchedMap["pathValue"] + setValuesFromPortPath(staticPortMap, models.StripQuotes(portCont.S("path").String())) staticPortsList = append(staticPortsList, staticPortMap) } diff --git a/mso/datasource_mso_schema_site_anp_epg_static_port.go b/mso/datasource_mso_schema_site_anp_epg_static_port.go index 3b740daf..d247a67e 100644 --- a/mso/datasource_mso_schema_site_anp_epg_static_port.go +++ b/mso/datasource_mso_schema_site_anp_epg_static_port.go @@ -147,16 +147,8 @@ func datasourceMSOSchemaSiteAnpEpgStaticPortRead(d *schema.ResourceData, m inter if err != nil { return err } - if pathType == "port" && fex != "" { - portPath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", pod, leaf, fex, path) - } else if pathType == "vpc" { - portPath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", pod, leaf, path) - } else { - portPath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", pod, leaf, path) - } - currentPortPath := models.StripQuotes(portCont.S("path").String()) - currentType := models.StripQuotes(portCont.S("type").String()) - if portPath == currentPortPath && pathType == currentType { + portPath = createPortPath(pathType, pod, leaf, fex, path) + if portPath == models.StripQuotes(portCont.S("path").String()) && pathType == models.StripQuotes(portCont.S("type").String()) { found = true d.SetId(fmt.Sprintf("%s/sites/%s-%s/anps/%s/epgs/%s/staticPorts/%s", schemaId, siteId, templateName, anp, epg, portPath)) if portCont.Exists("type") { diff --git a/mso/resource_mso_schema_site_anp_epg_bulk_staticport.go b/mso/resource_mso_schema_site_anp_epg_bulk_staticport.go index 3aba5575..be0a8574 100644 --- a/mso/resource_mso_schema_site_anp_epg_bulk_staticport.go +++ b/mso/resource_mso_schema_site_anp_epg_bulk_staticport.go @@ -170,10 +170,6 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortImport(d *schema.ResourceData, m i return nil, fmt.Errorf("Unable to get Static Port list") } - portPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/extpaths-(?P.*)\/pathep-\[(?P.*)\])`) - vpcPath := regexp.MustCompile(`(topology\/(?P.*)\/protpaths-(?P.*)\/pathep-\[(?P.*)\])`) - dpcPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/pathep-\[(?P.*)\])`) - staticPortsList := make([]interface{}, 0, 1) for i := 0; i < portCount; i++ { portCont, err := epgCont.ArrayElement(i, "staticPorts") @@ -199,22 +195,7 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortImport(d *schema.ResourceData, m i staticPortMap["mode"] = models.StripQuotes(portCont.S("mode").String()) } - pathValue := models.StripQuotes(portCont.S("path").String()) - - matchedMap := make(map[string]string) - - if portPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, portPath) - staticPortMap["fex"] = matchedMap["fexValue"] - } else if vpcPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, vpcPath) - } else if dpcPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, dpcPath) - } - - staticPortMap["pod"] = matchedMap["podValue"] - staticPortMap["leaf"] = matchedMap["leafValue"] - staticPortMap["path"] = matchedMap["pathValue"] + setValuesFromPortPath(staticPortMap, models.StripQuotes(portCont.S("path").String())) staticPortsList = append(staticPortsList, staticPortMap) } @@ -270,17 +251,7 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortCreate(d *schema.ResourceData, m i static_port_fex = staticPort["fex"].(string) } - var portpath string - - if staticPortMap["type"] == "port" && static_port_fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_fex, static_port_path) - } else if staticPortMap["type"] == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) - } - - staticPortMap["path"] = portpath + staticPortMap["path"] = createPortPath(staticPortMap["type"].(string), static_port_pod, static_port_leaf, static_port_fex, static_port_path) staticPortsList = append(staticPortsList, staticPortMap) } @@ -400,10 +371,6 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortRead(d *schema.ResourceData, m int return fmt.Errorf("Unable to get Static Port list") } - portPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/extpaths-(?P.*)\/pathep-\[(?P.*)\])`) - vpcPath := regexp.MustCompile(`(topology\/(?P.*)\/protpaths-(?P.*)\/pathep-\[(?P.*)\])`) - dpcPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/pathep-\[(?P.*)\])`) - staticPortsList := make([]interface{}, 0, 1) for i := 0; i < portCount; i++ { portCont, err := epgCont.ArrayElement(i, "staticPorts") @@ -429,22 +396,7 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortRead(d *schema.ResourceData, m int staticPortMap["mode"] = models.StripQuotes(portCont.S("mode").String()) } - pathValue := models.StripQuotes(portCont.S("path").String()) - - matchedMap := make(map[string]string) - - if portPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, portPath) - staticPortMap["fex"] = matchedMap["fexValue"] - } else if vpcPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, vpcPath) - } else if dpcPath.MatchString(pathValue) { - matchedMap = getStaticPortPathValues(pathValue, dpcPath) - } - - staticPortMap["pod"] = matchedMap["podValue"] - staticPortMap["leaf"] = matchedMap["leafValue"] - staticPortMap["path"] = matchedMap["pathValue"] + setValuesFromPortPath(staticPortMap, models.StripQuotes(portCont.S("path").String())) staticPortsList = append(staticPortsList, staticPortMap) } @@ -500,17 +452,7 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortUpdate(d *schema.ResourceData, m i static_port_fex = staticPort["fex"].(string) } - var portpath string - - if staticPortMap["type"] == "port" && static_port_fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_fex, static_port_path) - } else if staticPortMap["type"] == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) - } - - staticPortMap["path"] = portpath + staticPortMap["path"] = createPortPath(staticPortMap["type"].(string), static_port_pod, static_port_leaf, static_port_fex, static_port_path) staticPortsList = append(staticPortsList, staticPortMap) } @@ -594,17 +536,7 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortDelete(d *schema.ResourceData, m i static_port_fex = staticPort["fex"].(string) } - var portpath string - - if staticPortMap["path_type"] == "port" && staticPort["fex"] != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_fex, static_port_path) - } else if staticPortMap["path_type"] == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) - } - - staticPortMap["path"] = portpath + staticPortMap["path"] = createPortPath(staticPortMap["type"].(string), static_port_pod, static_port_leaf, static_port_fex, static_port_path) staticPortsList = append(staticPortsList, staticPortMap) } @@ -655,3 +587,45 @@ func getStaticPortPathValues(pathValue string, re *regexp.Regexp) map[string]str return result } + +func setValuesFromPortPath(staticPortMap map[string]interface{}, pathValue string) { + + portFexPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/extpaths-(?P.*)\/pathep-\[(?P.*)\])`) + vpcFexPath := regexp.MustCompile(`(topology\/(?P.*)\/protpaths-(?P.*)\/extprotpaths-(?P.*)\/pathep-\[(?P.*)\])`) + vpcPath := regexp.MustCompile(`(topology\/(?P.*)\/protpaths-(?P.*)\/pathep-\[(?P.*)\])`) + // dpcPath also handles the port without FEX defined in the path + dpcPath := regexp.MustCompile(`(topology\/(?P.*)\/paths-(?P.*)\/pathep-\[(?P.*)\])`) + + matchedMap := make(map[string]string) + + if portFexPath.MatchString(pathValue) { + matchedMap = getStaticPortPathValues(pathValue, portFexPath) + } else if vpcFexPath.MatchString(pathValue) { + matchedMap = getStaticPortPathValues(pathValue, vpcFexPath) + } else if vpcPath.MatchString(pathValue) { + matchedMap = getStaticPortPathValues(pathValue, vpcPath) + } else if dpcPath.MatchString(pathValue) { + matchedMap = getStaticPortPathValues(pathValue, dpcPath) + } + + staticPortMap["pod"] = matchedMap["podValue"] + staticPortMap["leaf"] = matchedMap["leafValue"] + staticPortMap["path"] = matchedMap["pathValue"] + if fexValue, ok := matchedMap["fexValue"]; ok { + staticPortMap["fex"] = fexValue + } + +} + +func createPortPath(path_type, static_port_pod, static_port_leaf, static_port_fex, static_port_path string) string { + + if path_type == "port" && static_port_fex != "" { + return fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_fex, static_port_path) + } else if path_type == "vpc" && static_port_fex != "" { + return fmt.Sprintf("topology/%s/protpaths-%s/extprotpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_fex, static_port_path) + } else if path_type == "vpc" { + return fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) + } else { + return fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", static_port_pod, static_port_leaf, static_port_path) + } +} diff --git a/mso/resource_mso_schema_site_anp_epg_static_port.go b/mso/resource_mso_schema_site_anp_epg_static_port.go index 918acf40..69ab8734 100644 --- a/mso/resource_mso_schema_site_anp_epg_static_port.go +++ b/mso/resource_mso_schema_site_anp_epg_static_port.go @@ -198,21 +198,10 @@ func resourceMSOSchemaSiteAnpEpgStaticPortImport(d *schema.ResourceData, m inter if err != nil { return nil, err } - var portpath string - if pathType == "port" && fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", statepod, stateleaf, fex, statepath) - } else if pathType == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", statepod, stateleaf, statepath) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", statepod, stateleaf, statepath) - } - apiportpath := models.StripQuotes(portCont.S("path").String()) - apiType := models.StripQuotes(portCont.S("type").String()) - if portpath == apiportpath && pathType == apiType { - d.SetId(apiportpath) - if portCont.Exists("type") { - d.Set("path_type", models.StripQuotes(portCont.S("type").String())) - } + portPath := createPortPath(pathType, statepod, stateleaf, fex, statepath) + if portPath == models.StripQuotes(portCont.S("path").String()) && pathType == models.StripQuotes(portCont.S("type").String()) { + d.SetId(portPath) + d.Set("path_type", pathType) if portCont.Exists("path") { d.Set("pod", statepod) d.Set("leaf", stateleaf) @@ -403,17 +392,9 @@ func resourceMSOSchemaSiteAnpEpgStaticPortCreate(d *schema.ResourceData, m inter } } } - var portpath string - if pathType == "port" && fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", pod, leaf, fex, path) - } else if pathType == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", pod, leaf, path) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", pod, leaf, path) - } - + portPath := createPortPath(pathType, pod, leaf, fex, path) pathsp := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/%s/staticPorts/-", stateSiteId, stateTemplateName, stateANPName, stateEpgName) - staticStruct := models.NewSchemaSiteAnpEpgStaticPort("add", pathsp, pathType, portpath, vlan, deploymentImmediacy, microsegvlan, mode) + staticStruct := models.NewSchemaSiteAnpEpgStaticPort("add", pathsp, pathType, portPath, vlan, deploymentImmediacy, microsegvlan, mode) _, errs := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), staticStruct) if errs != nil { return errs @@ -499,21 +480,10 @@ func resourceMSOSchemaSiteAnpEpgStaticPortRead(d *schema.ResourceData, m interfa if err != nil { return err } - var portpath string - if pathType == "port" && fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", statepod, stateleaf, fex, statepath) - } else if pathType == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", statepod, stateleaf, statepath) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", statepod, stateleaf, statepath) - } - apiportpath := models.StripQuotes(portCont.S("path").String()) - apiType := models.StripQuotes(portCont.S("type").String()) - if portpath == apiportpath && pathType == apiType { - d.SetId(apiportpath) - if portCont.Exists("type") { - d.Set("type", models.StripQuotes(portCont.S("type").String())) - } + portPath := createPortPath(pathType, statepod, stateleaf, fex, statepath) + if portPath == models.StripQuotes(portCont.S("path").String()) && pathType == models.StripQuotes(portCont.S("type").String()) { + d.SetId(portPath) + d.Set("type", pathType) if portCont.Exists("path") { d.Set("pod", statepod) d.Set("leaf", stateleaf) @@ -658,14 +628,7 @@ func resourceMSOSchemaSiteAnpEpgStaticPortUpdate(d *schema.ResourceData, m inter if err != nil { return err } - var portpath string - if pathType == "port" && fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", pod, leaf, fex, path) - } else if pathType == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", pod, leaf, path) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", pod, leaf, path) - } + portpath := createPortPath(pathType, pod, leaf, fex, path) apiportpath := models.StripQuotes(portCont.S("path").String()) if portpath == apiportpath { index := l @@ -789,14 +752,7 @@ func resourceMSOSchemaSiteAnpEpgStaticPortDelete(d *schema.ResourceData, m inter if err != nil { return err } - var portpath string - if pathType == "port" && fex != "" { - portpath = fmt.Sprintf("topology/%s/paths-%s/extpaths-%s/pathep-[%s]", pod, leaf, fex, path) - } else if pathType == "vpc" { - portpath = fmt.Sprintf("topology/%s/protpaths-%s/pathep-[%s]", pod, leaf, path) - } else { - portpath = fmt.Sprintf("topology/%s/paths-%s/pathep-[%s]", pod, leaf, path) - } + portpath := createPortPath(pathType, pod, leaf, fex, path) apiportpath := models.StripQuotes(portCont.S("path").String()) if portpath == apiportpath { index := l