From 9350897edd6c054eef6c9f34ff21deff4447e04c Mon Sep 17 00:00:00 2001 From: Lionel Hercot Date: Sat, 11 Feb 2023 14:31:03 +0100 Subject: [PATCH] [minor_change] Add mso_schema_site_l3out resource and datasource --- examples/schema_site_l3out/main.tf | 66 ++++ mso/datasource_mso_schema_site_l3out.go | 70 +++++ mso/datasource_mso_schema_site_l3out_test.go | 220 +++++++++++++ mso/provider.go | 2 + mso/resource_mso_schema_site_l3out.go | 162 ++++++++++ mso/resource_mso_schema_site_l3out_test.go | 313 +++++++++++++++++++ 6 files changed, 833 insertions(+) create mode 100644 examples/schema_site_l3out/main.tf create mode 100644 mso/datasource_mso_schema_site_l3out.go create mode 100644 mso/datasource_mso_schema_site_l3out_test.go create mode 100644 mso/resource_mso_schema_site_l3out.go create mode 100644 mso/resource_mso_schema_site_l3out_test.go diff --git a/examples/schema_site_l3out/main.tf b/examples/schema_site_l3out/main.tf new file mode 100644 index 00000000..bc03e72b --- /dev/null +++ b/examples/schema_site_l3out/main.tf @@ -0,0 +1,66 @@ +terraform { + required_providers { + mso = { + source = "CiscoDevNet/mso" + } + } +} + +provider "mso" { + username = "" # + password = "" # + 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 +} \ No newline at end of file diff --git a/mso/datasource_mso_schema_site_l3out.go b/mso/datasource_mso_schema_site_l3out.go new file mode 100644 index 00000000..5e420ffe --- /dev/null +++ b/mso/datasource_mso_schema_site_l3out.go @@ -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 +} diff --git a/mso/datasource_mso_schema_site_l3out_test.go b/mso/datasource_mso_schema_site_l3out_test.go new file mode 100644 index 00000000..3e38d340 --- /dev/null +++ b/mso/datasource_mso_schema_site_l3out_test.go @@ -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 +} diff --git a/mso/provider.go b/mso/provider.go index 7b942949..b3288bc0 100644 --- a/mso/provider.go +++ b/mso/provider.go @@ -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(), @@ -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(), diff --git a/mso/resource_mso_schema_site_l3out.go b/mso/resource_mso_schema_site_l3out.go new file mode 100644 index 00000000..2f9d33d6 --- /dev/null +++ b/mso/resource_mso_schema_site_l3out.go @@ -0,0 +1,162 @@ +package mso + +import ( + "fmt" + "log" + "strings" + + "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 resourceMSOSchemaSiteL3out() *schema.Resource { + return &schema.Resource{ + Create: resourceMSOSchemaSiteL3outCreate, + Read: resourceMSOSchemaSiteL3outRead, + Delete: resourceMSOSchemaSiteL3outDelete, + Importer: &schema.ResourceImporter{ + State: resourceMSOSchemaSiteL3outImport, + }, + SchemaVersion: 1, + Schema: map[string]*schema.Schema{ + "l3out_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + }, + "vrf_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + }, + "template_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + }, + "site_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + }, + "schema_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + }, + }, + } +} + +func setMSOSchemaSiteL3outAttributes(l3outMap *models.IntersiteL3outs, d *schema.ResourceData) { + d.Set("l3out_name", l3outMap.L3outName) + d.Set("vrf_name", l3outMap.VRFName) + d.Set("template_name", l3outMap.TemplateName) + d.Set("site_id", l3outMap.SiteId) + d.Set("schema_id", l3outMap.SchemaID) +} + +func resourceMSOSchemaSiteL3outImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + log.Println("[DEBUG] Schema Site L3out: Beginning Import", d.Id()) + msoClient := m.(*client.Client) + id := d.Id() + L3out, err := L3outIdToL3outModel(id) + if err != nil { + return nil, err + } + l3outMapRemote, err := msoClient.ReadIntersiteL3outs(L3out) + if err != nil { + return nil, err + } + setMSOSchemaSiteL3outAttributes(l3outMapRemote, d) + d.SetId(id) + log.Println("[DEBUG] Schema Site L3out: Import Completed", d.Id()) + return []*schema.ResourceData{d}, nil +} + +func resourceMSOSchemaSiteL3outCreate(d *schema.ResourceData, m interface{}) error { + log.Printf("[DEBUG] Schema Site L3out: Beginning Creation") + 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{ + L3outName: l3outName, + VRFName: vrfName, + SiteId: siteId, + TemplateName: templateName, + SchemaID: schemaId, + } + err := msoClient.CreateIntersiteL3outs(&l3outMap) + if err != nil { + return err + } + l3outId := L3outModelToL3outId(&l3outMap) + d.SetId(l3outId) + log.Printf("[DEBUG] Schema Site L3out: Creation Completed") + return resourceMSOSchemaSiteL3outRead(d, m) +} + +func resourceMSOSchemaSiteL3outRead(d *schema.ResourceData, m interface{}) error { + log.Println("[DEBUG] Schema Site L3out: Beginning Read", d.Id()) + msoClient := m.(*client.Client) + id := d.Id() + l3out, err := L3outIdToL3outModel(id) + if err != nil { + return err + } + l3outMapRemote, err := msoClient.ReadIntersiteL3outs(l3out) + if err != nil { + d.SetId("") + return nil + } + setMSOSchemaSiteL3outAttributes(l3outMapRemote, d) + d.SetId(L3outModelToL3outId(l3out)) + log.Println("[DEBUG] Schema Site L3out: Reading Completed", d.Id()) + return nil +} + +func resourceMSOSchemaSiteL3outDelete(d *schema.ResourceData, m interface{}) error { + log.Println("[DEBUG] Schema Site L3out: Beginning Destroy", d.Id()) + msoClient := m.(*client.Client) + id := d.Id() + l3out, err := L3outIdToL3outModel(id) + if err != nil { + return err + } + err = msoClient.DeleteIntersiteL3outs(l3out) + if err != nil { + return err + } + log.Println("[DEBUG] Schema Site L3out: Beginning Destroy", d.Id()) + d.SetId("") + return err +} + +func L3outModelToL3outId(m *models.IntersiteL3outs) string { + return fmt.Sprintf("%s/site/%s/template/%s/vrf/%s/l3out/%s", m.SchemaID, m.SiteId, m.TemplateName, m.VRFName, m.L3outName) +} + +func L3outIdToL3outModel(id string) (*models.IntersiteL3outs, error) { + getAttributes := strings.Split(id, "/") + if len(getAttributes) != 9 || getAttributes[1] != "site" || getAttributes[3] != "template" || getAttributes[5] != "vrf" || getAttributes[7] != "l3out" { + return nil, fmt.Errorf("invalid mso_schema_site_l3out id format") + } + l3outMap := models.IntersiteL3outs{ + SchemaID: getAttributes[0], + SiteId: getAttributes[2], + TemplateName: getAttributes[4], + VRFName: getAttributes[6], + L3outName: getAttributes[8], + } + return &l3outMap, nil +} diff --git a/mso/resource_mso_schema_site_l3out_test.go b/mso/resource_mso_schema_site_l3out_test.go new file mode 100644 index 00000000..09e8e58b --- /dev/null +++ b/mso/resource_mso_schema_site_l3out_test.go @@ -0,0 +1,313 @@ +package mso + +import ( + "fmt" + "regexp" + "testing" + + "github.com/ciscoecosystem/mso-go-client/client" + "github.com/ciscoecosystem/mso-go-client/models" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccMSOSchemaSiteL3out_Basic(t *testing.T) { + var l3out1 models.IntersiteL3outs + var l3out2 models.IntersiteL3outs + resourceName := "mso_schema_site_l3out.test" + vrf := makeTestVariable(acctest.RandString(5)) + vrfOther := makeTestVariable(acctest.RandString(5)) + l3out := makeTestVariable(acctest.RandString(5)) + l3outOther := makeTestVariable(acctest.RandString(5)) + prnames := makeTestVariable(acctest.RandString(5)) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckMSOSchemaSiteL3outDestroy, + Steps: []resource.TestStep{ + { + Config: MSOSchemaSiteL3outWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "schema_id"), + ExpectError: regexp.MustCompile(`Missing required argument`), + }, + { + Config: MSOSchemaSiteL3outWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "l3out_name"), + ExpectError: regexp.MustCompile(`Missing required argument`), + }, + { + Config: MSOSchemaSiteL3outWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "template_name"), + ExpectError: regexp.MustCompile(`Missing required argument`), + }, + { + Config: MSOSchemaSiteL3outWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "vrf_name"), + ExpectError: regexp.MustCompile(`Missing required argument`), + }, + { + Config: MSOSchemaSiteL3outWithoutRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out, "site_id"), + ExpectError: regexp.MustCompile(`Missing required argument`), + }, + { + Config: MSOSchemaSiteL3outWithRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOSchemaSiteL3outExists(resourceName, &l3out1), + resource.TestCheckResourceAttrSet(resourceName, "schema_id"), + resource.TestCheckResourceAttr(resourceName, "l3out_name", l3out), + resource.TestCheckResourceAttr(resourceName, "template_name", prnames), + resource.TestCheckResourceAttr(resourceName, "vrf_name", vrf), + resource.TestCheckResourceAttrSet(resourceName, "site_id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: MSOSchemaSiteL3outWithRequired(siteNames[0], tenantNames[0], prnames, vrf, l3outOther), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOSchemaSiteL3outExists(resourceName, &l3out2), + resource.TestCheckResourceAttrSet(resourceName, "schema_id"), + resource.TestCheckResourceAttr(resourceName, "l3out_name", l3outOther), + resource.TestCheckResourceAttr(resourceName, "template_name", prnames), + resource.TestCheckResourceAttr(resourceName, "vrf_name", vrf), + resource.TestCheckResourceAttrSet(resourceName, "site_id"), + testAccCheckMSOSchemaSiteL3outIdNotEqual(&l3out1, &l3out2), + ), + }, + { + Config: MSOSchemaSiteL3outWithRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out), + }, + { + Config: MSOSchemaSiteL3outWithRequired(siteNames[0], tenantNames[0], prnames, vrfOther, l3out), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOSchemaSiteL3outExists(resourceName, &l3out2), + resource.TestCheckResourceAttrSet(resourceName, "schema_id"), + resource.TestCheckResourceAttr(resourceName, "l3out_name", l3out), + resource.TestCheckResourceAttr(resourceName, "template_name", prnames), + resource.TestCheckResourceAttr(resourceName, "vrf_name", vrfOther), + resource.TestCheckResourceAttrSet(resourceName, "site_id"), + testAccCheckMSOSchemaSiteL3outIdNotEqual(&l3out1, &l3out2), + ), + }, + }, + }) +} + +func TestAccMSOSchemaSiteL3out_Negative(t *testing.T) { + 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: MSOSchemaSiteL3outWithRequired(siteNames[0], tenantNames[0], prnames, vrf, acctest.RandString(1001)), + ExpectError: regexp.MustCompile(`1 - 1000`), + }, + { + Config: MSOSchemaSiteL3outAttr(siteNames[0], tenantNames[0], prnames, vrf, l3out, randomParameter, randomValue), + ExpectError: regexp.MustCompile(`An argument named(.)+is not expected here.`), + }, + { + Config: MSOSchemaSiteL3outWithRequired(siteNames[0], tenantNames[0], prnames, vrf, l3out), + }, + }, + }) +} + +func TestAccMSOSchemaSiteL3out_MultipleCreateDelete(t *testing.T) { + vrf := makeTestVariable(acctest.RandString(5)) + l3out := makeTestVariable(acctest.RandString(5)) + prnames := makeTestVariable(acctest.RandString(5)) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckMSOSchemaSiteL3outDestroy, + Steps: []resource.TestStep{ + { + Config: MSOSchemaSiteL3outMultiple(siteNames[0], tenantNames[0], prnames, vrf, l3out), + }, + }, + }) +} + +func testAccCheckMSOSchemaSiteL3outIdNotEqual(m1, m2 *models.IntersiteL3outs) resource.TestCheckFunc { + return func(s *terraform.State) error { + id1 := L3outModelToL3outId(m1) + id2 := L3outModelToL3outId(m2) + if id1 == id2 { + return fmt.Errorf("Schema Site L3out Ids are equal") + } + return nil + } +} + +func testAccCheckMSOSchemaSiteL3outDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*client.Client) + + for _, rs := range s.RootModule().Resources { + + if rs.Type == "mso_schema_site_l3out" { + id := rs.Primary.ID + l3out, _ := L3outIdToL3outModel(id) + _, err := client.ReadIntersiteL3outs(l3out) + if err == nil { + return fmt.Errorf("Schema Site L3out still exist") + } + } + } + return nil +} + +func testAccCheckMSOSchemaSiteL3outExists(l3outName string, m *models.IntersiteL3outs) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := testAccProvider.Meta().(*client.Client) + rs, ok := s.RootModule().Resources[l3outName] + if !ok { + return fmt.Errorf("Schema Site L3out %s not found", l3outName) + } + if rs.Primary.ID == "" { + return fmt.Errorf("No Schema Site L3out Id was set") + } + l3out, err := L3outIdToL3outModel(rs.Primary.ID) + if err != nil { + return err + } + var read *models.IntersiteL3outs + read, err = client.ReadIntersiteL3outs(l3out) + if err != nil { + return err + } + *m = *read + return nil + } +} + +func MSOSchemaSiteL3outWithoutRequired(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" + } + ` + switch attr { + case "schema_id": + rBlock += ` + 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 + }` + case "l3out_name": + rBlock += ` + 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 + }` + case "template_name": + rBlock += ` + 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 + } + ` + case "vrf_name": + rBlock += ` + 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 + } + ` + case "site_id": + rBlock += ` + 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 + } + ` + } + return fmt.Sprintf(rBlock, vrf, l3out) +} + +func MSOSchemaSiteL3outAttr(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 + %s = "%s" + } + `, vrf, l3out, key, val) + return resource +} + +func MSOSchemaSiteL3outWithRequired(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 + } + `, vrf, l3out) + return resource +} + +func MSOSchemaSiteL3outMultiple(site, tenant, name, vrf, l3out string) string { + resource := CreatSchemaSiteConfig(site, tenant, name) + 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${count.index}" + 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 + count = 5 + } + `, vrf, l3out) + return resource +}