Skip to content

Commit

Permalink
Add Service EPG support for cloud sites (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrsr authored Mar 2, 2022
1 parent 2048402 commit 9b8bba4
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 43 deletions.
3 changes: 2 additions & 1 deletion examples/schema_site_anp_epg/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ resource "mso_schema_site_anp_epg" "foo_schema_site_anp_epg" {
site_id = "5c7c95d9510000cf01c1ee3d"
anp_name = "ANP"
epg_name = "DB"
}
private_link_label = "Cloud"
}
5 changes: 4 additions & 1 deletion examples/schema_template_anp_epg/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ resource "mso_schema_template_anp_epg" "anp_epg" {
vrf_schema_id = "5eafeb792c0000a18e0a2900"
bd_schema_id = "5eafeb792c0000a18e0a2900"
vrf_template_name = "stemplate1"

access_type= "private"
deployment_type = "cloud_native"
service_type = "custom"
custom_service_type = "Test"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-mso
go 1.13

require (
github.com/ciscoecosystem/mso-go-client v1.3.0
github.com/ciscoecosystem/mso-go-client v1.4.0
github.com/hashicorp/terraform-plugin-sdk v1.14.0
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQ
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
github.com/ciscoecosystem/mso-go-client v1.2.6 h1:CU8l0cs15WNJFHQLJA/hJA6H4/vgra2WbP/+Rt4h9uM=
github.com/ciscoecosystem/mso-go-client v1.2.6/go.mod h1:ffy+aqN7Zo7GZC8e0pSt+Kbc32wjqkc+zDD42Ug0lWw=
github.com/ciscoecosystem/mso-go-client v1.3.0 h1:nu5au4Q+3mu3wgRlfiystAclt1sYbjCHPf4tFe4FBe0=
github.com/ciscoecosystem/mso-go-client v1.3.0/go.mod h1:ffy+aqN7Zo7GZC8e0pSt+Kbc32wjqkc+zDD42Ug0lWw=
github.com/ciscoecosystem/mso-go-client v1.4.0 h1:zgIbzsfwJmWYqIsGgvnm584vwE4LX7huJaKAVgNaoHA=
github.com/ciscoecosystem/mso-go-client v1.4.0/go.mod h1:ffy+aqN7Zo7GZC8e0pSt+Kbc32wjqkc+zDD42Ug0lWw=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
75 changes: 68 additions & 7 deletions mso/resource_mso_schema_site_anp_epg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func resourceMSOSchemaSiteAnpEpg() *schema.Resource {
return &schema.Resource{
Create: resourceMSOSchemaSiteAnpEpgCreate,
Update: resourceMSOSchemaSiteAnpEpgUpdate,
Read: resourceMSOSchemaSiteAnpEpgRead,
Delete: resourceMSOSchemaSiteAnpEpgDelete,

Expand Down Expand Up @@ -54,6 +55,10 @@ func resourceMSOSchemaSiteAnpEpg() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"private_link_label": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
}),
}
}
Expand All @@ -75,8 +80,8 @@ func resourceMSOSchemaSiteAnpEpgImport(d *schema.ResourceData, m interface{}) ([

stateSite := get_attribute[2]
found := false
stateAnp := get_attribute[4]
stateEpg := get_attribute[6]
stateAnp := get_attribute[6]
stateEpg := get_attribute[8]

for i := 0; i < count; i++ {
tempCont, err := cont.ArrayElement(i, "sites")
Expand Down Expand Up @@ -118,7 +123,12 @@ func resourceMSOSchemaSiteAnpEpgImport(d *schema.ResourceData, m interface{}) ([
d.Set("template_name", split[4])
d.Set("anp_name", split[6])
d.Set("epg_name", apiEPG)

privatelinklabelsCont := epgCont.S("privateLinkLabel")
if models.StripQuotes(privatelinklabelsCont.S("name").String()) == "{}" {
d.Set("private_link_label", "")
} else {
d.Set("private_link_label", models.StripQuotes(privatelinklabelsCont.S("name").String()))
}
found = true
break
}
Expand Down Expand Up @@ -153,12 +163,21 @@ func resourceMSOSchemaSiteAnpEpgCreate(d *schema.ResourceData, m interface{}) er
anpEpgRefMap["epgName"] = epgName

path := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", siteId, templateName, anpName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", path, anpEpgRefMap)

privateLinkLabel := make(map[string]interface{})
if val, ok := d.GetOk("private_link_label"); ok {
map_private_link_label := make(map[string]interface{})
map_private_link_label["name"] = val
privateLinkLabel = map_private_link_label
} else {
privateLinkLabel = nil
}
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", path, privateLinkLabel, anpEpgRefMap)
_, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if err != nil {
return err
}

return resourceMSOSchemaSiteAnpEpgRead(d, m)
}

Expand Down Expand Up @@ -246,7 +265,7 @@ func resourceMSOSchemaSiteAnpEpgRead(d *schema.ResourceData, m interface{}) erro

}

func resourceMSOSchemaSiteAnpEpgDelete(d *schema.ResourceData, m interface{}) error {
func resourceMSOSchemaSiteAnpEpgUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] Site Anp Epg: Beginning Update")
msoClient := m.(*client.Client)

Expand All @@ -263,14 +282,56 @@ func resourceMSOSchemaSiteAnpEpgDelete(d *schema.ResourceData, m interface{}) er
anpEpgRefMap["epgName"] = epgName

path := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/%s", siteId, templateName, anpName, epgName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("remove", path, anpEpgRefMap)

response, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
privateLinkLabel := make(map[string]interface{})
if val, ok := d.GetOk("private_link_label"); ok {
map_private_link_label := make(map[string]interface{})
map_private_link_label["name"] = val
privateLinkLabel = map_private_link_label
} else {
privateLinkLabel = nil
}
anpEpgStruct := models.NewSchemaSiteAnpEpg("replace", path, privateLinkLabel, anpEpgRefMap)
_, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if err != nil {
return err
}

return resourceMSOSchemaSiteAnpEpgRead(d, m)
}

func resourceMSOSchemaSiteAnpEpgDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] Site Anp Epg: Beginning Delete")
msoClient := m.(*client.Client)

schemaId := d.Get("schema_id").(string)
siteId := d.Get("site_id").(string)
templateName := d.Get("template_name").(string)
anpName := d.Get("anp_name").(string)
epgName := d.Get("epg_name").(string)

anpEpgRefMap := make(map[string]interface{})
anpEpgRefMap["schemaId"] = schemaId
anpEpgRefMap["templateName"] = templateName
anpEpgRefMap["anpName"] = anpName
anpEpgRefMap["epgName"] = epgName

path := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/%s", siteId, templateName, anpName, epgName)
privateLinkLabel := make(map[string]interface{})
if val, ok := d.GetOk("private_link_label"); ok {
map_private_link_label := make(map[string]interface{})
map_private_link_label["name"] = val
privateLinkLabel = map_private_link_label
} else {
privateLinkLabel = nil
}
anpEpgStruct := models.NewSchemaSiteAnpEpg("remove", path, privateLinkLabel, anpEpgRefMap)
response, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
// Ignoring Error with code 141: Resource Not Found when deleting
if err != nil && !(response.Exists("code") && response.S("code").String() == "141") {
return err
}

d.SetId("")
return nil
}
6 changes: 4 additions & 2 deletions mso/resource_mso_schema_site_anp_epg_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ func resourceMSOSchemaSiteAnpEpgDomainCreate(d *schema.ResourceData, m interface
anpEpgRefMap["epgName"] = epgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", apiSite, apiTemplate, anpName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down Expand Up @@ -515,7 +516,8 @@ func resourceMSOSchemaSiteAnpEpgDomainCreate(d *schema.ResourceData, m interface
anpEpgRefMap["epgName"] = epgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", apiSite, apiTemplate, anpName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down
6 changes: 4 additions & 2 deletions mso/resource_mso_schema_site_anp_epg_static_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ func resourceMSOSchemaSiteAnpEpgStaticleafCreate(d *schema.ResourceData, m inter
anpEpgRefMap["epgName"] = epgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", apiSite, apiTemplate, anpName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down Expand Up @@ -287,7 +288,8 @@ func resourceMSOSchemaSiteAnpEpgStaticleafCreate(d *schema.ResourceData, m inter
anpEpgRefMap["epgName"] = epgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", apiSite, apiTemplate, anpName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down
6 changes: 4 additions & 2 deletions mso/resource_mso_schema_site_anp_epg_static_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ func resourceMSOSchemaSiteAnpEpgStaticPortCreate(d *schema.ResourceData, m inter
anpEpgRefMap["epgName"] = stateEpgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", apiSite, apiTemplate, stateANPName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down Expand Up @@ -392,7 +393,8 @@ func resourceMSOSchemaSiteAnpEpgStaticPortCreate(d *schema.ResourceData, m inter
anpEpgRefMap["epgName"] = stateEpgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", stateSiteId, stateTemplateName, stateANPName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down
6 changes: 4 additions & 2 deletions mso/resource_mso_schema_site_anp_epg_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ func resourceMSOSchemaSiteAnpEpgSubnetCreate(d *schema.ResourceData, m interface
anpEpgRefMap["epgName"] = stateEpgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", apiSite, apiTemplate, stateANPName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down Expand Up @@ -337,7 +338,8 @@ func resourceMSOSchemaSiteAnpEpgSubnetCreate(d *schema.ResourceData, m interface
anpEpgRefMap["epgName"] = stateEpgName

pathEpg := fmt.Sprintf("/sites/%s-%s/anps/%s/epgs/-", stateSiteId, stateTemplateName, stateANPName)
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, anpEpgRefMap)
//private_link_label argument used in resource site_anp_epg is set to nil here
anpEpgStruct := models.NewSchemaSiteAnpEpg("add", pathEpg, nil, anpEpgRefMap)

_, ers := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), anpEpgStruct)
if ers != nil {
Expand Down
Loading

0 comments on commit 9b8bba4

Please sign in to comment.