From e3a367e309b04eaf7084df86e55cf196952d4026 Mon Sep 17 00:00:00 2001 From: ssrish Date: Tue, 27 Jul 2021 09:53:29 -0700 Subject: [PATCH] modification to mso_rest, site selector and its test file --- mso/resource_mso_rest.go | 45 ++- ...source_mso_schema_site_anp_epg_selector.go | 37 ++- ...e_mso_schema_site_anp_epg_selector_test.go | 291 +++++++++--------- mso/resource_mso_schema_template_vrf.go | 2 +- mso/resource_mso_template_bd_test.go | 2 +- 5 files changed, 199 insertions(+), 178 deletions(-) diff --git a/mso/resource_mso_rest.go b/mso/resource_mso_rest.go index 323b6b63..ca37792a 100644 --- a/mso/resource_mso_rest.go +++ b/mso/resource_mso_rest.go @@ -37,6 +37,17 @@ func resourceMSORest() *schema.Resource { Type: schema.TypeString, Required: true, }, + + "ignore_on_destroy": &schema.Schema{ + Type: schema.TypeBool, + Required: true, + }, + + "schema_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, }), } } @@ -93,22 +104,26 @@ func resourceMSORestUpdate(d *schema.ResourceData, m interface{}) error { } func resourceMSORestDelete(d *schema.ResourceData, m interface{}) error { - var method, path, payload string - path = d.Get("path").(string) - payload = d.Get("payload").(string) - if tempVar, ok := d.GetOk("method"); ok { - method = tempVar.(string) - } else { - method = "DELETE" - } - if !contains(HTTP_METHODS, method) { - return fmt.Errorf("Invalid method %s passed", method) - } - msoClient := m.(*client.Client) - _, err := MakeRestRequest(msoClient, path, method, payload) + var ignore_on_destroy = d.Get("ignore_on_destroy").(bool) + if ignore_on_destroy == false { + var method, path, payload string + path = d.Get("path").(string) + payload = d.Get("payload").(string) + if tempVar, ok := d.GetOk("method"); ok { + method = tempVar.(string) + } else { + method = "DELETE" + } + if !contains(HTTP_METHODS, method) { + return fmt.Errorf("Invalid method %s passed", method) + } - if err != nil { - return err + msoClient := m.(*client.Client) + _, err := MakeRestRequest(msoClient, path, method, payload) + + if err != nil { + return err + } } d.SetId("") return nil diff --git a/mso/resource_mso_schema_site_anp_epg_selector.go b/mso/resource_mso_schema_site_anp_epg_selector.go index e4b1eca8..5d1288df 100644 --- a/mso/resource_mso_schema_site_anp_epg_selector.go +++ b/mso/resource_mso_schema_site_anp_epg_selector.go @@ -104,18 +104,29 @@ func resourceMSOSchemaSiteAnpEpgSelector() *schema.Resource { } } +var importReadSchemaID, importReadSiteID, importReadTemplate, importReadANP, importReadEPG string + func resourceSchemaSiteApnEpgSelectorImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { log.Printf("[DEBUG] %s: Beginning Import", d.Id()) found := false msoClient := m.(*client.Client) - get_attribute := strings.Split(d.Id(), "/") - schemaID := get_attribute[0] - siteID := get_attribute[2] - template := get_attribute[4] - anpName := get_attribute[6] - epgName := get_attribute[8] - name := get_attribute[10] - + var schemaID, siteID, template, anpName, epgName, name string + if !strings.Contains(d.Id(), "/") { + name = d.Id() + schemaID = importReadSchemaID + siteID = importReadSiteID + template = importReadTemplate + anpName = importReadANP + epgName = importReadEPG + } else { + get_attribute := strings.Split(d.Id(), "/") + schemaID = get_attribute[0] + siteID = get_attribute[2] + template = get_attribute[4] + anpName = get_attribute[6] + epgName = get_attribute[8] + name = get_attribute[10] + } cont, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaID)) if err != nil { return nil, err @@ -180,7 +191,7 @@ func resourceSchemaSiteApnEpgSelectorImport(d *schema.ResourceData, m interface{ currentName := models.StripQuotes(selectorCont.S("name").String()) if currentName == name { found = true - d.SetId(name) + d.SetId(currentName) d.Set("name", currentName) exps := selectorCont.S("expressions").Data().([]interface{}) @@ -357,14 +368,17 @@ func resourceSchemaSiteApnEpgSelectorUpdate(d *schema.ResourceData, m interface{ func resourceSchemaSiteApnEpgSelectorRead(d *schema.ResourceData, m interface{}) error { found := false msoClient := m.(*client.Client) - dn := d.Id() schemaID := d.Get("schema_id").(string) siteID := d.Get("site_id").(string) template := d.Get("template_name").(string) anpName := d.Get("anp_name").(string) epgName := d.Get("epg_name").(string) - + importReadSchemaID = schemaID + importReadSiteID = siteID + importReadTemplate = template + importReadANP = anpName + importReadEPG = epgName cont, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaID)) if err != nil { return err @@ -429,7 +443,6 @@ func resourceSchemaSiteApnEpgSelectorRead(d *schema.ResourceData, m interface{}) currentName := models.StripQuotes(selectorCont.S("name").String()) if currentName == dn { found = true - d.SetId(dn) d.Set("name", currentName) exps := selectorCont.S("expressions").Data().([]interface{}) diff --git a/mso/resource_mso_schema_site_anp_epg_selector_test.go b/mso/resource_mso_schema_site_anp_epg_selector_test.go index 9c126433..4a10dbfd 100644 --- a/mso/resource_mso_schema_site_anp_epg_selector_test.go +++ b/mso/resource_mso_schema_site_anp_epg_selector_test.go @@ -11,16 +11,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/terraform" ) -// func TestAccMSOSchemaSiteAnpEpgSelector_Initial(t *testing.T) { -// resource.Test(t, resource.TestCase{ -// Providers: testAccProviders, -// Steps: []resource.TestStep{ -// { -// Config: testAccCheckMSOSiteAnpEpgSelectorConfig_initial("ANP"), -// }, -// }, -// }) -// } func TestAccMSOSchemaSiteAnpEpgSelector_Basic(t *testing.T) { var ss SiteAnpEpgSelectorTest resource.Test(t, resource.TestCase{ @@ -29,10 +19,10 @@ func TestAccMSOSchemaSiteAnpEpgSelector_Basic(t *testing.T) { CheckDestroy: testAccCheckMSOSchemaSiteAnpEpgSelectorDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("one"), + Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("in"), Check: resource.ComposeTestCheckFunc( testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss), - testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("one", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("in", &ss), ), }, @@ -54,108 +44,114 @@ func TestAccMSOSchemaSiteAnpEpgSelector_Update(t *testing.T) { CheckDestroy: testAccCheckMSOSchemaSiteAnpEpgSelectorDestroy, Steps: []resource.TestStep{ { - Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("one"), + Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("in"), Check: resource.ComposeTestCheckFunc( testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss), - testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("one", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("in", &ss), ), }, { - Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("two"), + Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("equals"), Check: resource.ComposeTestCheckFunc( testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss), - testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("two", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("equals", &ss), ), }, }, }) } -func testAccCheckMSOSiteAnpEpgSelectorConfig_basic(key string) string { +func testAccCheckMSOSiteAnpEpgSelectorConfig_basic(operator string) string { return fmt.Sprintf(` resource "mso_schema" "schema1" { name = "Schema2" template_name = "Template1" tenant_id = "5fb5fed8520000452a9e8911" - - } - - resource "mso_schema_template_vrf" "vrf1" { - schema_id = mso_schema.schema1.id - template = mso_schema.schema1.template_name - name = "VRF" - display_name = "vz1" - layer3_multicast= false - vzany = false - } - - resource "mso_rest" "azure_site" { - path = "api/v1/schemas/${mso_schema.schema1.id}" - method = "PATCH" + } - payload = <