Skip to content

Commit

Permalink
[minor_change] Add mso_schema_site_l3out resource and datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
lhercot committed Feb 11, 2023
1 parent b9e04d0 commit 9350897
Show file tree
Hide file tree
Showing 6 changed files with 833 additions and 0 deletions.
66 changes: 66 additions & 0 deletions examples/schema_site_l3out/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
terraform {
required_providers {
mso = {
source = "CiscoDevNet/mso"
}
}
}

provider "mso" {
username = "" # <MSO username>
password = "" # <MSO pwd>
url = "" # <MSO URL>
insecure = true
}

data "mso_site" "example" {
name = "example"
}

data "mso_tenant" "example" {
name = "example"
display_name = "example"
}

resource "mso_schema" "example" {
name = "example"
template_name = "example"
tenant_id = data.mso_tenant.example.id
}

resource "mso_schema_template_vrf" "vrf" {
schema_id = mso_schema.example.id
template = mso_schema.example.template_name
name = "example"
}

resource "mso_schema_template_l3out" "l3out" {
schema_id = mso_schema.example.id
template_name = mso_schema.example.template_name
l3out_name = "example"
vrf_name = mso_schema_template_vrf.vrf.id
vrf_schema_id = mso_schema_template_vrf.vrf.schema_id
vrf_template_name = mso_schema_template_vrf.vrf.template
}


resource "mso_schema_site" "example" {
schema_id = mso_schema.example.id
site_id = data.mso_site.example.id
template_name = "example"
}

resource "mso_schema_site_vrf" "example" {
template_name = mso_schema_site.example.template_name
site_id = mso_schema_site.example.site_id
schema_id = mso_schema_site.example.schema_id
vrf_name = mso_schema_template_vrf.example.name
}

resource "mso_schema_site_l3out" "example" {
schema_id = mso_schema_site.example.schema_id
l3out_name = mso_schema_template_l3out.l3out.l3out_name
template_name = mso_schema_site.example.template_name
vrf_name = mso_schema_site_vrf.example.vrf_name
site_id = mso_schema_site.example.site_id
}
70 changes: 70 additions & 0 deletions mso/datasource_mso_schema_site_l3out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mso

import (
"log"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/ciscoecosystem/mso-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)

func datasourceMSOSchemaSiteL3out() *schema.Resource {
return &schema.Resource{
Read: datasourceMSOSchemaSiteL3outRead,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"l3out_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"vrf_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"site_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"schema_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
},
}
}

func datasourceMSOSchemaSiteL3outRead(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] Schema Site L3out: Beginning Read")
msoClient := m.(*client.Client)
schemaId := d.Get("schema_id").(string)
siteId := d.Get("site_id").(string)
templateName := d.Get("template_name").(string)
vrfName := d.Get("vrf_name").(string)
l3outName := d.Get("l3out_name").(string)
l3outMap := models.IntersiteL3outs{
SchemaID: schemaId,
SiteId: siteId,
TemplateName: templateName,
VRFName: vrfName,
L3outName: l3outName,
}
l3outMapRemote, err := msoClient.ReadIntersiteL3outs(&l3outMap)
if err != nil {
d.SetId("")
return err
}
setMSOSchemaSiteL3outAttributes(l3outMapRemote, d)
d.SetId(L3outModelToL3outId(&l3outMap))
log.Println("[DEBUG] Schema Site L3out: Reading Completed", d.Id())
return nil
}
220 changes: 220 additions & 0 deletions mso/datasource_mso_schema_site_l3out_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package mso

import (
"fmt"
"regexp"
"testing"

"github.com/ciscoecosystem/mso-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMSOSchemaSiteL3out_DataSource(t *testing.T) {
var l3outModel models.IntersiteL3outs
resourceName := "mso_schema_site_l3out.test"
dataSourceName := "mso_schema_site_l3out.test"
vrf := makeTestVariable(acctest.RandString(5))
l3out := makeTestVariable(acctest.RandString(5))
prnames := makeTestVariable(acctest.RandString(5))
randomParameter := acctest.RandStringFromCharSet(5, "abcdefghijklmnopqrstuvwxyz")
randomValue := acctest.RandString(5)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMSOSchemaSiteL3outDestroy,
Steps: []resource.TestStep{
{
Config: MSOSchemaSiteL3outDataSourceWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "schema_id"),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
{
Config: MSOSchemaSiteL3outDataSourceWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "l3out_name"),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
{
Config: MSOSchemaSiteL3outDataSourceWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "template_name"),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
{
Config: MSOSchemaSiteL3outDataSourceWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "vrf_name"),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
{
Config: MSOSchemaSiteL3outDataSourceWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "site_id"),
ExpectError: regexp.MustCompile(`Missing required argument`),
},
{
Config: MSOSchemaSiteL3outDataSourceAttr(siteNames[0], tenantNames[0], prnames, vrf, l3out, randomParameter, randomValue),
ExpectError: regexp.MustCompile(`An argument named(.)+is not expected here.`),
},
{
Config: MSOSchemaSiteL3outDataSourceInvalidName(siteNames[0], tenantNames[0], prnames, vrf, l3out),
ExpectError: regexp.MustCompile(`unable to find siteL3out`),
},
{
Config: MSOSchemaSiteL3outDataSourceWithRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out),
Check: resource.ComposeTestCheckFunc(
testAccCheckMSOSchemaSiteL3outExists(resourceName, &l3outModel),
resource.TestCheckResourceAttrPair(resourceName, "schema_id", dataSourceName, "schema_id"),
resource.TestCheckResourceAttrPair(resourceName, "l3out_name", dataSourceName, "l3out_name"),
resource.TestCheckResourceAttrPair(resourceName, "template_name", dataSourceName, "template_name"),
resource.TestCheckResourceAttrPair(resourceName, "vrf_name", dataSourceName, "vrf_name"),
resource.TestCheckResourceAttrPair(resourceName, "site_id", dataSourceName, "site_id"),
),
},
},
})
}

func MSOSchemaSiteL3outDataSourceWithoutRequired(site, tenant, name, vrf, l3out, attr string) string {
rBlock := CreatSchemaSiteConfig(site, tenant, name)
rBlock += `
resource "mso_schema_site_vrf" "test" {
template_name = mso_schema_site.test.template_name
site_id = mso_schema_site.test.site_id
schema_id = mso_schema_site.test.schema_id
vrf_name = "%s"
}
resource "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site.test.schema_id
l3out_name = "%s"
template_name = mso_schema_site.test.template_name
vrf_name = mso_schema_site_vrf.test.vrf_name
site_id = mso_schema_site.test.site_id
}
`
switch attr {
case "schema_id":
rBlock += `
data "mso_schema_site_l3out" "test" {
# schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = mso_schema_site_l3out.test.l3out_name
template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
}`
case "l3out_name":
rBlock += `
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
# l3out_name = mso_schema_site_l3out.test.l3out_name
template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
}`
case "template_name":
rBlock += `
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = mso_schema_site_l3out.test.l3out_name
# template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
}
`
case "vrf_name":
rBlock += `
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = mso_schema_site_l3out.test.l3out_name
template_name = mso_schema_site_l3out.test.template_name
# vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
}
`
case "site_id":
rBlock += `
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = mso_schema_site_l3out.test.l3out_name
template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
# site_id = mso_schema_site_l3out.test.site_id
}
`
}
return fmt.Sprintf(rBlock, vrf, l3out)
}

func MSOSchemaSiteL3outDataSourceInvalidName(site, name, user, vrf, l3out string) string {
resource := CreatSchemaSiteConfig(site, name, user)
resource += fmt.Sprintf(`
resource "mso_schema_site_vrf" "test" {
template_name = mso_schema_site.test.template_name
site_id = mso_schema_site.test.site_id
schema_id = mso_schema_site.test.schema_id
vrf_name = "%s"
}
resource "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site.test.schema_id
l3out_name = "%s"
template_name = mso_schema_site.test.template_name
vrf_name = mso_schema_site_vrf.test.vrf_name
site_id = mso_schema_site.test.site_id
}
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = "${mso_schema_site_l3out.test.l3out_name}_invalid"
template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
}
`, vrf, l3out)
return resource
}

func MSOSchemaSiteL3outDataSourceAttr(site, name, user, vrf, l3out, key, val string) string {
resource := CreatSchemaSiteConfig(site, name, user)
resource += fmt.Sprintf(`
resource "mso_schema_site_vrf" "test" {
template_name = mso_schema_site.test.template_name
site_id = mso_schema_site.test.site_id
schema_id = mso_schema_site.test.schema_id
vrf_name = "%s"
}
resource "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site.test.schema_id
l3out_name = "%s"
template_name = mso_schema_site.test.template_name
vrf_name = mso_schema_site_vrf.test.vrf_name
site_id = mso_schema_site.test.site_id
}
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = mso_schema_site_l3out.test.l3out_name
template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
%s = "%s"
}
`, vrf, l3out, key, val)
return resource
}

func MSOSchemaSiteL3outDataSourceWithRequired(site, name, user, vrf, l3out string) string {
resource := CreatSchemaSiteConfig(site, name, user)
resource += fmt.Sprintf(`
resource "mso_schema_site_vrf" "test" {
template_name = mso_schema_site.test.template_name
site_id = mso_schema_site.test.site_id
schema_id = mso_schema_site.test.schema_id
vrf_name = "%s"
}
resource "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site.test.schema_id
l3out_name = "%s"
template_name = mso_schema_site.test.template_name
vrf_name = mso_schema_site_vrf.test.vrf_name
site_id = mso_schema_site.test.site_id
}
data "mso_schema_site_l3out" "test" {
schema_id = mso_schema_site_l3out.test.schema_id
l3out_name = mso_schema_site_l3out.test.l3out_name
template_name = mso_schema_site_l3out.test.template_name
vrf_name = mso_schema_site_l3out.test.vrf_name
site_id = mso_schema_site_l3out.test.site_id
}
`, vrf, l3out)
return resource
}
2 changes: 2 additions & 0 deletions mso/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func Provider() terraform.ResourceProvider {
"mso_schema_site_vrf": resourceMSOSchemaSiteVrf(),
"mso_schema_site_vrf_region": resourceMSOSchemaSiteVrfRegion(),
"mso_schema_site_bd_subnet": resourceMSOSchemaSiteBdSubnet(),
"mso_schema_site_l3out": resourceMSOSchemaSiteL3out(),
"mso_rest": resourceMSORest(),
"mso_schema_template_deploy": resourceMSOSchemaTemplateDeploy(),
"mso_schema_template_deploy_ndo": resourceNDOSchemaTemplateDeploy(),
Expand Down Expand Up @@ -145,6 +146,7 @@ func Provider() terraform.ResourceProvider {
"mso_schema_site_vrf": dataSourceMSOSchemaSiteVrf(),
"mso_schema_site_vrf_region": dataSourceMSOSchemaSiteVrfRegion(),
"mso_schema_site_bd_subnet": dataSourceMSOSchemaSiteBdSubnet(),
"mso_schema_site_l3out": datasourceMSOSchemaSiteL3out(),
"mso_schema_site_vrf_region_cidr_subnet": dataSourceMSOSchemaSiteVrfRegionCidrSubnet(),
"mso_schema_site_vrf_region_cidr": dataSourceMSOSchemaSiteVrfRegionCidr(),
"mso_schema_template_anp_epg_selector": datasourceMSOSchemaTemplateAnpEpgSelector(),
Expand Down
Loading

0 comments on commit 9350897

Please sign in to comment.