From b5699dd2e3959cf1f5ecabe5bcb739ba7ccb297c Mon Sep 17 00:00:00 2001 From: Justyna Chowaniec <79261946+juchowan@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:35:56 +0200 Subject: [PATCH] [minor_change] Add l3out_schema_id, l3out_template and l3out_on_apic attributes to mso_schema_site_external_epg (#291) --- examples/schema_site_external_epg/main.tf | 19 ++++ mso/resource_mso_schema_site_external_epg.go | 92 +++++++++++++++++-- .../r/schema_site_external_epg.html.markdown | 3 + 3 files changed, 106 insertions(+), 8 deletions(-) diff --git a/examples/schema_site_external_epg/main.tf b/examples/schema_site_external_epg/main.tf index c8ee311f..0f65f870 100644 --- a/examples/schema_site_external_epg/main.tf +++ b/examples/schema_site_external_epg/main.tf @@ -74,3 +74,22 @@ resource "mso_schema_site_external_epg" "site_extepg1" { external_epg_name = mso_schema_template_external_epg.extepg1.external_epg_name l3out_name = mso_schema_template_l3out.template_l3out.l3out_name } + +resource "mso_schema_template_external_epg" "extepg2" { + schema_id = mso_schema.schema_blocks.id + template_name = one(mso_schema.schema_blocks.template).name + external_epg_name = "extepg2" + display_name = "extepg2" + vrf_name = mso_schema_template_vrf.vrf1.name + vrf_schema_id = mso_schema_template_vrf.vrf1.schema_id + vrf_template_name = mso_schema_template_vrf.vrf1.template +} + +resource "mso_schema_site_external_epg" "site_extepg2" { + site_id = mso_schema_site.demo_schema_site.id + schema_id = mso_schema.schema_blocks.id + template_name = one(mso_schema.schema_blocks.template).name + external_epg_name = mso_schema_template_external_epg.extepg2.external_epg_name + l3out_name = "L3out2" + l3out_on_apic = true +} \ No newline at end of file diff --git a/mso/resource_mso_schema_site_external_epg.go b/mso/resource_mso_schema_site_external_epg.go index 71e78896..f7719e13 100644 --- a/mso/resource_mso_schema_site_external_epg.go +++ b/mso/resource_mso_schema_site_external_epg.go @@ -56,6 +56,25 @@ func resourceMSOSchemaSiteExternalEpg() *schema.Resource { Computed: true, ValidateFunc: validation.StringLenBetween(1, 1000), }, + "l3out_template_name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + ConflictsWith: []string{"l3out_on_apic"}, + }, + "l3out_schema_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: validation.StringLenBetween(1, 1000), + ConflictsWith: []string{"l3out_on_apic"}, + }, + "l3out_on_apic": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + ConflictsWith: []string{"l3out_schema_id", "l3out_template_name"}, + }, }), } } @@ -106,15 +125,28 @@ func resourceMSOSchemaSiteExternalEpgImport(d *schema.ResourceData, m interface{ d.Set("site_id", apiSiteId) l3outRef := models.StripQuotes(externalEpgCont.S("l3outRef").String()) + l3outDn := models.StripQuotes(externalEpgCont.S("l3outDn").String()) if l3outRef != "{}" && l3outRef != "" { reL3out := regexp.MustCompile("/schemas/(.*?)/templates/(.*?)/l3outs/(.*)") matchL3out := reL3out.FindStringSubmatch(l3outRef) log.Printf("[TRACE] resourceMSOSchemaSiteExternalEpgRead l3outRef: %s matchL3out: %s", l3outRef, matchL3out) if len(matchL3out) >= 4 { d.Set("l3out_name", matchL3out[3]) + d.Set("l3out_schema_id", matchL3out[1]) + d.Set("l3out_template_name", matchL3out[2]) } else { return nil, fmt.Errorf("Error in parsing l3outRef to get L3Out name") } + } else if l3outDn != "{}" && l3outDn != "" { + reL3out := regexp.MustCompile("uni/tn-(.*?)/out-(.*)") + matchL3out := reL3out.FindStringSubmatch(l3outDn) + log.Printf("[TRACE] resourceMSOSchemaSiteExternalEpgRead l3outDn: %s matchL3out: %s", l3outDn, matchL3out) + if len(matchL3out) >= 2 { + d.Set("l3out_name", matchL3out[1]) + d.Set("l3out_on_apic", true) + } else { + return nil, fmt.Errorf("Error in parsing l3outDn to get L3Out name") + } } found = true @@ -147,6 +179,9 @@ func resourceMSOSchemaSiteExternalEpgCreate(d *schema.ResourceData, m interface{ externalEpgName := d.Get("external_epg_name").(string) templateName := d.Get("template_name").(string) l3outName := d.Get("l3out_name").(string) + l3outTemplate := d.Get("l3out_template_name").(string) + l3outSchema := d.Get("l3out_schema_id").(string) + l3outOnApic := d.Get("l3out_on_apic").(bool) siteEpgMap := make(map[string]interface{}) @@ -159,11 +194,24 @@ func resourceMSOSchemaSiteExternalEpgCreate(d *schema.ResourceData, m interface{ l3outRefMap := make(map[string]interface{}) - l3outRefMap["schemaId"] = schemaId - l3outRefMap["templateName"] = templateName - l3outRefMap["l3outName"] = l3outName + if l3outOnApic { + siteEpgMap["l3outRef"] = "" + } else { + if l3outTemplate == "" { + l3outRefMap["templateName"] = templateName + } else { + l3outRefMap["templateName"] = l3outTemplate + } + if l3outSchema == "" { + l3outRefMap["schemaId"] = schemaId + } else { + l3outRefMap["schemaId"] = l3outSchema + } + l3outRefMap["l3outName"] = l3outName + + siteEpgMap["l3outRef"] = l3outRefMap + } - siteEpgMap["l3outRef"] = l3outRefMap siteEpgMap["l3outDn"] = fmt.Sprintf("uni/tn-%s/out-%s", tenantName, l3outName) } else { siteEpgMap["l3outDn"] = "" @@ -248,15 +296,28 @@ func resourceMSOSchemaSiteExternalEpgRead(d *schema.ResourceData, m interface{}) d.Set("site_id", apiSiteId) l3outRef := models.StripQuotes(externalEpgCont.S("l3outRef").String()) + l3outDn := models.StripQuotes(externalEpgCont.S("l3outDn").String()) if l3outRef != "{}" && l3outRef != "" { reL3out := regexp.MustCompile("/schemas/(.*?)/templates/(.*?)/l3outs/(.*)") matchL3out := reL3out.FindStringSubmatch(l3outRef) log.Printf("[TRACE] resourceMSOSchemaSiteExternalEpgRead l3outRef: %s matchL3out: %s", l3outRef, matchL3out) if len(matchL3out) >= 4 { d.Set("l3out_name", matchL3out[3]) + d.Set("l3out_schema_id", matchL3out[1]) + d.Set("l3out_template_name", matchL3out[2]) } else { return fmt.Errorf("Error in parsing l3outRef to get L3Out name") } + } else if l3outDn != "{}" && l3outDn != "" { + reL3out := regexp.MustCompile("uni/tn-(.*?)/out-(.*)") + matchL3out := reL3out.FindStringSubmatch(l3outDn) + log.Printf("[TRACE] resourceMSOSchemaSiteExternalEpgRead l3outDn: %s matchL3out: %s", l3outDn, matchL3out) + if len(matchL3out) >= 2 { + d.Set("l3out_name", matchL3out[2]) + d.Set("l3out_on_apic", true) + } else { + return fmt.Errorf("Error in parsing l3outDn to get L3Out name") + } } found = true @@ -287,6 +348,9 @@ func resourceMSOSchemaSiteExternalEpgUpdate(d *schema.ResourceData, m interface{ templateName := d.Get("template_name").(string) externalEpgName := d.Get("external_epg_name").(string) l3outName := d.Get("l3out_name").(string) + l3outTemplate := d.Get("l3out_template_name").(string) + l3outSchema := d.Get("l3out_schema_id").(string) + l3outOnApic := d.Get("l3out_on_apic").(bool) siteEpgMap := make(map[string]interface{}) @@ -299,11 +363,23 @@ func resourceMSOSchemaSiteExternalEpgUpdate(d *schema.ResourceData, m interface{ l3outRefMap := make(map[string]interface{}) - l3outRefMap["schemaId"] = schemaId - l3outRefMap["templateName"] = templateName - l3outRefMap["l3outName"] = l3outName + if l3outOnApic { + siteEpgMap["l3outRef"] = "" + } else { + if l3outTemplate == "" { + l3outRefMap["templateName"] = templateName + } else { + l3outRefMap["templateName"] = l3outTemplate + } + if l3outSchema == "" { + l3outRefMap["schemaId"] = schemaId + } else { + l3outRefMap["schemaId"] = l3outSchema + } + l3outRefMap["l3outName"] = l3outName - siteEpgMap["l3outRef"] = l3outRefMap + siteEpgMap["l3outRef"] = l3outRefMap + } siteEpgMap["l3outDn"] = fmt.Sprintf("uni/tn-%s/out-%s", tenantName, l3outName) } else { siteEpgMap["l3outDn"] = "" diff --git a/website/docs/r/schema_site_external_epg.html.markdown b/website/docs/r/schema_site_external_epg.html.markdown index 244695c6..458f075f 100644 --- a/website/docs/r/schema_site_external_epg.html.markdown +++ b/website/docs/r/schema_site_external_epg.html.markdown @@ -32,6 +32,9 @@ resource "mso_schema_site_external_epg" "external_epg_1" { ## Attribute Reference ## * `l3out_name` - (Optional) Name of the L3Out. +* `l3out_schema_id` - (Optional) ID of the schema that defines the referenced L3Out. If this attribute is unspecified, it defaults to the current schema. This is mutually exclusive with `l3out_on_apic`. +* `l3out_template_name` - (Optional) The template that defines the referenced L3Out. If this parameter is unspecified, it defaults to the current template. This is mutually exclusive with `l3out_on_apic`. +* `l3out_on_apic` - (Optional) Indicates that L3Out is created only localy on APIC. This is mutually exclusive with `l3out_schema_id` and `l3out_template_name`. ## Importing ##