Skip to content
Open
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
55 changes: 55 additions & 0 deletions mso/datasource_mso_schema_template_l3out_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package mso

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMSOSchemaTemplateL3outDatasource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMSOSchemaTemplateL3outDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: Read L3out datasource not found error") },
Config: testAccMSOSchemaTemplateL3outDatasourceNotFound(),
ExpectError: regexp.MustCompile("Unable to find the L3out"),
},
{
PreConfig: func() { fmt.Println("Test: Read L3out datasource") },
Config: testAccMSOSchemaTemplateL3outDatasource(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.mso_schema_template_l3out.l3out", "schema_id"),
resource.TestCheckResourceAttr("data.mso_schema_template_l3out.l3out", "template_name", msoSchemaTemplateName),
resource.TestCheckResourceAttr("data.mso_schema_template_l3out.l3out", "l3out_name", msoSchemaTemplateL3outName),
resource.TestCheckResourceAttr("data.mso_schema_template_l3out.l3out", "display_name", msoSchemaTemplateL3outName),
resource.TestCheckResourceAttr("data.mso_schema_template_l3out.l3out", "vrf_name", msoSchemaTemplateVrfName),
resource.TestCheckResourceAttrSet("data.mso_schema_template_l3out.l3out", "vrf_schema_id"),
resource.TestCheckResourceAttr("data.mso_schema_template_l3out.l3out", "vrf_template_name", msoSchemaTemplateName),
),
},
},
})
}

func testAccMSOSchemaTemplateL3outDatasource() string {
return fmt.Sprintf(`%s
data "mso_schema_template_l3out" "l3out" {
schema_id = mso_schema.%[2]s.id
template_name = "%[3]s"
l3out_name = mso_schema_template_l3out.%[4]s.l3out_name
}`, testAccMSOSchemaTemplateL3outConfigCreate(), msoSchemaName, msoSchemaTemplateName, msoSchemaTemplateL3outName)
}

func testAccMSOSchemaTemplateL3outDatasourceNotFound() string {
return fmt.Sprintf(`%s
data "mso_schema_template_l3out" "l3out" {
schema_id = mso_schema.%[2]s.id
template_name = "%[3]s"
l3out_name = "non_existing_l3out_name"
}`, testAccMSOSchemaTemplateL3outConfigCreate(), msoSchemaName, msoSchemaTemplateName)
}
57 changes: 31 additions & 26 deletions mso/resource_mso_schema_template_l3out.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/ciscoecosystem/mso-go-client/container"
"github.com/ciscoecosystem/mso-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -68,7 +69,8 @@ func resourceMSOTemplateL3out() *schema.Resource {
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
// Commented computed to allow description to be set to empty string
// Computed: true,
},
}),
}
Expand Down Expand Up @@ -256,41 +258,44 @@ func resourceMSOTemplateL3outUpdate(d *schema.ResourceData, m interface{}) error

schemaID := d.Get("schema_id").(string)
l3outName := d.Get("l3out_name").(string)
displayName := d.Get("display_name").(string)
templateName := d.Get("template_name").(string)
vrfName := d.Get("vrf_name").(string)

var vrf_schema_id, vrf_template_name string
updatePath := fmt.Sprintf("/templates/%s/intersiteL3outs/%s", templateName, l3outName)
payloadCont := container.New()
payloadCont.Array()

if tempVar, ok := d.GetOk("vrf_schema_id"); ok {
vrf_schema_id = tempVar.(string)
} else {
vrf_schema_id = schemaID
}
if tempVar, ok := d.GetOk("vrf_template_name"); ok {
vrf_template_name = tempVar.(string)
} else {
vrf_template_name = templateName
if d.HasChange("description") {
err := addPatchPayloadToContainer(payloadCont, "replace", fmt.Sprintf("%s/description", updatePath), d.Get("description").(string))
if err != nil {
return err
}
}

var description string
if tempVar, ok := d.GetOk("description"); ok {
description = tempVar.(string)
if d.HasChange("vrf_schema_id") || d.HasChange("vrf_template_name") || d.HasChange("vrf_name") {
vrfSchemaId := d.Get("vrf_schema_id").(string)
if vrfSchemaId == "" {
vrfSchemaId = schemaID
}
vrfTemplateName := d.Get("vrf_template_name").(string)
if vrfTemplateName == "" {
vrfTemplateName = templateName
}
vrfRef := map[string]interface{}{
"schemaId": vrfSchemaId,
"templateName": vrfTemplateName,
"vrfName": d.Get("vrf_name").(string),
}
err := addPatchPayloadToContainer(payloadCont, "replace", fmt.Sprintf("%s/vrfRef", updatePath), vrfRef)
if err != nil {
return err
}
}

vrfRefMap := make(map[string]interface{})
vrfRefMap["schemaId"] = vrf_schema_id
vrfRefMap["templateName"] = vrf_template_name
vrfRefMap["vrfName"] = vrfName

path := fmt.Sprintf("/templates/%s/intersiteL3outs/%s", templateName, l3outName)
l3outStruct := models.NewTemplateL3out("replace", path, l3outName, displayName, description, vrfRefMap)

_, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaID), l3outStruct)

err := doPatchRequest(msoClient, fmt.Sprintf("api/v1/schemas/%s", schemaID), payloadCont)
if err != nil {
return err
}

return resourceMSOTemplateL3outRead(d, m)
}

Expand Down
Loading
Loading