diff --git a/GNUmakefile b/GNUmakefile index cacb6c97..41fabe59 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -17,6 +17,10 @@ test: fmtcheck testacc: fmtcheck TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m +testacccoverage: fmtcheck + -TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m -race -covermode=atomic -coverprofile=coverage.out + go tool cover -html=coverage.out + vet: @echo "go vet ." @go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \ 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 5c2b075f..4a10dbfd 100644 --- a/mso/resource_mso_schema_site_anp_epg_selector_test.go +++ b/mso/resource_mso_schema_site_anp_epg_selector_test.go @@ -19,12 +19,18 @@ 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_site_anp_epg_selector.selector1", &ss), - testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("one", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("in", &ss), ), }, + + { + ResourceName: "mso_schema_site_anp_epg_selector.selector1", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -38,54 +44,174 @@ 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_site_anp_epg_selector.selector1", &ss), - testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("one", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("in", &ss), ), }, { - Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("two"), + Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("equals"), Check: resource.ComposeTestCheckFunc( - testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema_site_anp_epg_selector.selector1", &ss), - testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("two", &ss), + testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &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_rest" "azure_site" { + path = "api/v1/schemas/${mso_schema.schema1.id}" + method = "PATCH" + schema_id=mso_schema.schema1.id + ignore_on_destroy = true + payload = < ```bash -terraform import mso_schema_template_filter_entry.filter_entry {schema_id}/template/{template_name}/filter/{name} +terraform import mso_schema_template_filter_entry.filter_entry {schema_id}/template/{template_name}/filter/{filter_name}/filter_entry/{name} ``` \ No newline at end of file