Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor_change] Add support for vpc connected to fex in resource mso_schema_site_anp_epg_bulk_staticport (DCNE-226) #314

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
91 changes: 5 additions & 86 deletions mso/resource_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"
"strings"

Expand Down Expand Up @@ -180,10 +179,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 +204,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 +260,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 +380,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 +405,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 +461,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 +545,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 @@ -653,15 +584,3 @@ func resourceMSOSchemaSiteAnpEpgBulkStaticPortDelete(d *schema.ResourceData, m i
d.SetId("")
return resourceMSOSchemaSiteAnpEpgBulkStaticPortRead(d, m)
}

func getStaticPortPathValues(pathValue string, re *regexp.Regexp) map[string]string {
match := re.FindStringSubmatch(pathValue) //list of matched strings
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}

return result
}
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
55 changes: 55 additions & 0 deletions mso/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"

"github.com/ciscoecosystem/mso-go-client/client"
Expand Down Expand Up @@ -240,3 +241,57 @@ func getSchemaIdFromName(msoClient *client.Client, name string) (string, error)

return "", fmt.Errorf("Schema of specified name not found")
}

func getStaticPortPathValues(pathValue string, re *regexp.Regexp) map[string]string {
match := re.FindStringSubmatch(pathValue) //list of matched strings
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}

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)
}
}
Loading