Skip to content

Commit

Permalink
[minor_change] Add support for vpc connected to fex in resource mso_s…
Browse files Browse the repository at this point in the history
…chema_site_anp_epg_bulk_staticport
  • Loading branch information
akinross authored and lhercot committed Dec 2, 2024
1 parent 0f4cc1a commit 8d8bf0e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 160 deletions.
22 changes: 1 addition & 21 deletions mso/datasource_mso_schema_site_anp_epg_bulk_staticport.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mso
import (
"fmt"
"log"
"regexp"
"strconv"

"github.com/ciscoecosystem/mso-go-client/client"
Expand Down Expand Up @@ -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<podValue>.*)\/paths-(?P<leafValue>.*)\/extpaths-(?P<fexValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
vpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/protpaths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
dpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/paths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)

staticPortsList := make([]interface{}, 0, 1)
for i := 0; i < portCount; i++ {
portCont, err := epgCont.ArrayElement(i, "staticPorts")
Expand All @@ -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)
}
Expand Down
12 changes: 2 additions & 10 deletions mso/datasource_mso_schema_site_anp_epg_static_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
120 changes: 47 additions & 73 deletions mso/resource_mso_schema_site_anp_epg_bulk_staticport.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortImport(d *schema.ResourceData, m i
return nil, fmt.Errorf("Unable to get Static Port list")
}

portPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/paths-(?P<leafValue>.*)\/extpaths-(?P<fexValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
vpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/protpaths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
dpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/paths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)

staticPortsList := make([]interface{}, 0, 1)
for i := 0; i < portCount; i++ {
portCont, err := epgCont.ArrayElement(i, "staticPorts")
Expand All @@ -209,22 +205,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)
}
Expand Down Expand Up @@ -280,17 +261,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)
}
Expand Down Expand Up @@ -410,10 +381,6 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortRead(d *schema.ResourceData, m int
return fmt.Errorf("Unable to get Static Port list")
}

portPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/paths-(?P<leafValue>.*)\/extpaths-(?P<fexValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
vpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/protpaths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
dpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/paths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)

staticPortsList := make([]interface{}, 0, 1)
for i := 0; i < portCount; i++ {
portCont, err := epgCont.ArrayElement(i, "staticPorts")
Expand All @@ -439,22 +406,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)
}
Expand Down Expand Up @@ -510,17 +462,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)
}
Expand Down Expand Up @@ -604,17 +546,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)
}
Expand Down Expand Up @@ -665,3 +597,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<podValue>.*)\/paths-(?P<leafValue>.*)\/extpaths-(?P<fexValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
vpcFexPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/protpaths-(?P<leafValue>.*)\/extprotpaths-(?P<fexValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
vpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/protpaths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)
// dpcPath also handles the port without FEX defined in the path
dpcPath := regexp.MustCompile(`(topology\/(?P<podValue>.*)\/paths-(?P<leafValue>.*)\/pathep-\[(?P<pathValue>.*)\])`)

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)
}
}
68 changes: 12 additions & 56 deletions mso/resource_mso_schema_site_anp_epg_static_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8d8bf0e

Please sign in to comment.