-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor_change] Add mso_schema_site_l3out resource and datasource
- Loading branch information
Showing
6 changed files
with
833 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.