Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 22 additions & 72 deletions mso/datasource_mso_schema_template_anp_epg_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package mso
import (
"fmt"
"log"
"regexp"

"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"
)
Expand Down Expand Up @@ -56,8 +54,12 @@ func dataSourceMSOTemplateAnpEpgContract() *schema.Resource {
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"relationship_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
// Computed: true,
// Changed to required because relationship_type is an identifier value
// A contract could be added with the same reference but different relationship_type
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
}),
}
Expand All @@ -75,14 +77,11 @@ func dataSourceMSOTemplateAnpEpgContractRead(d *schema.ResourceData, m interface
if err != nil {
return err
}
count, err := cont.ArrayCount("templates")
if err != nil {
return fmt.Errorf("No Template found")
}

template := d.Get("template_name").(string)
anp := d.Get("anp_name")
epg := d.Get("epg_name")
contract := d.Get("contract_name")
anp := d.Get("anp_name").(string)
epg := d.Get("epg_name").(string)
contract := d.Get("contract_name").(string)
contractSchemaId := d.Get("contract_schema_id").(string)
if contractSchemaId == "" {
contractSchemaId = schemaId
Expand All @@ -91,73 +90,24 @@ func dataSourceMSOTemplateAnpEpgContractRead(d *schema.ResourceData, m interface
if contractTemplateName == "" {
contractTemplateName = template
}
found := false
for i := 0; i < count && !found; i++ {
tempCont, err := cont.ArrayElement(i, "templates")
if err != nil {
return err
}
currentTemplate := models.StripQuotes(tempCont.S("name").String())

if currentTemplate == template {
anpCount, err := tempCont.ArrayCount("anps")
if err != nil {
return fmt.Errorf("Unable to get ANP list")
}
for j := 0; j < anpCount && !found; j++ {
anpCont, err := tempCont.ArrayElement(j, "anps")
if err != nil {
return err
}
currentAnp := models.StripQuotes(anpCont.S("name").String())
if currentAnp == anp {
epgCount, err := anpCont.ArrayCount("epgs")
if err != nil {
return fmt.Errorf("Unable to get EPG list")
}
for k := 0; k < epgCount && !found; k++ {
epgCont, err := anpCont.ArrayElement(k, "epgs")
if err != nil {
return err
}
currentEpg := models.StripQuotes(epgCont.S("name").String())
if currentEpg == epg {
crefCount, err := epgCont.ArrayCount("contractRelationships")
if err != nil {
return fmt.Errorf("Unable to get the contract relationships list")
}
for l := 0; l < crefCount; l++ {
crefCont, err := epgCont.ArrayElement(l, "contractRelationships")
if err != nil {
return err
}
contractRef := models.StripQuotes(crefCont.S("contractRef").String())
re := regexp.MustCompile("/schemas/(.*)/templates/(.*)/contracts/(.*)")
match := re.FindStringSubmatch(contractRef)
if match[3] == contract && match[1] == contractSchemaId && match[2] == contractTemplateName {
d.SetId(fmt.Sprintf("%s/templates/%s/anps/%s/epgs/%s/contracts/%s-%s-%s", schemaId, template, anp, epg, contractSchemaId, contractTemplateName, contract))
d.Set("contract_name", contract)
d.Set("contract_schema_id", contractSchemaId)
d.Set("contract_template_name", contractTemplateName)
d.Set("relationship_type", models.StripQuotes(crefCont.S("relationshipType").String()))
found = true
break
}
}

}

}
}
}
}
relationshipType := d.Get("relationship_type").(string)

index, _, err := getSchemaTemplateEPGContract(cont, template, anp, epg, contract, contractSchemaId, contractTemplateName, relationshipType)
if err != nil {
return err
}

if !found {
if index == -1 {
d.SetId("")
return fmt.Errorf("Unable to find the ANP EPG Contract %s in Template %s of Schema Id %s ", contract, contractTemplateName, contractSchemaId)
}

d.SetId(fmt.Sprintf("%s/templates/%s/anps/%s/epgs/%s/contracts/%s-%s-%s", schemaId, template, anp, epg, contractSchemaId, contractTemplateName, contract))
d.Set("contract_name", contract)
d.Set("contract_schema_id", contractSchemaId)
d.Set("contract_template_name", contractTemplateName)
d.Set("relationship_type", relationshipType)

log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
return nil

Expand Down
64 changes: 64 additions & 0 deletions mso/datasource_mso_schema_template_anp_epg_contract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package mso

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMSOSchemaTemplateAnpEpgContractDatasource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMSOSchemaTemplateAnpEpgContractResourceDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: Read EPG Contract datasource not found error") },
Config: testAccMSOSchemaTemplateAnpEpgContractDatasourceNotFoundConfig(),
ExpectError: regexp.MustCompile("Unable to find the ANP EPG Contract"),
},
{
PreConfig: func() { fmt.Println("Test: Read EPG Contract datasource") },
Config: testAccMSOSchemaTemplateAnpEpgContractDatasourceConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.mso_schema_template_anp_epg_contract.contract", "schema_id"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_contract.contract", "template_name", msoSchemaTemplateName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_contract.contract", "anp_name", msoSchemaTemplateAnpName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_contract.contract", "epg_name", msoSchemaTemplateAnpEpgName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_contract.contract", "contract_name", msoSchemaTemplateContractName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_contract.contract", "relationship_type", "provider"),
resource.TestCheckResourceAttrSet("data.mso_schema_template_anp_epg_contract.contract", "contract_schema_id"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_contract.contract", "contract_template_name", msoSchemaTemplateName),
),
},
},
})
}

func testAccMSOSchemaTemplateAnpEpgContractDatasourceConfig() string {
return fmt.Sprintf(`%s
data "mso_schema_template_anp_epg_contract" "contract" {
schema_id = mso_schema.%[2]s.id
template_name = "%[3]s"
anp_name = "%[4]s"
epg_name = "%[5]s"
contract_name = mso_schema_template_anp_epg_contract.%[6]s_provider.contract_name
relationship_type = "provider"
}
`, testAccMSOSchemaTemplateAnpEpgContractConfigProvider(), msoSchemaName, msoSchemaTemplateName, msoSchemaTemplateAnpName, msoSchemaTemplateAnpEpgName, msoSchemaTemplateContractName)
}

func testAccMSOSchemaTemplateAnpEpgContractDatasourceNotFoundConfig() string {
return fmt.Sprintf(`%s
data "mso_schema_template_anp_epg_contract" "contract" {
schema_id = mso_schema.%[2]s.id
template_name = "%[3]s"
anp_name = "%[4]s"
epg_name = "%[5]s"
contract_name = "non_existent_contract"
relationship_type = "provider"
}
`, testAccMSOSchemaTemplateAnpEpgContractConfigProvider(), msoSchemaName, msoSchemaTemplateName, msoSchemaTemplateAnpName, msoSchemaTemplateAnpEpgName)
}
62 changes: 62 additions & 0 deletions mso/datasource_mso_schema_template_anp_epg_subnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mso

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMSOSchemaTemplateAnpEpgSubnetDatasource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMSOSchemaTemplateAnpEpgSubnetDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: Read EPG Subnet datasource not found error") },
Config: testAccMSOSchemaTemplateAnpEpgSubnetDatasourceNotFound(),
ExpectError: regexp.MustCompile("Unable to find the ANP EPG Subnet"),
},
{
PreConfig: func() { fmt.Println("Test: Read EPG Subnet datasource") },
Config: testAccMSOSchemaTemplateAnpEpgSubnetDatasource(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.mso_schema_template_anp_epg_subnet.subnet", "schema_id"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "template", msoSchemaTemplateName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "anp_name", msoSchemaTemplateAnpName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "epg_name", msoSchemaTemplateAnpEpgName),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "ip", msoSchemaTemplateAnpEpgSubnetIp),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "scope", "private"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "shared", "false"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "querier", "false"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "no_default_gateway", "false"),
resource.TestCheckResourceAttr("data.mso_schema_template_anp_epg_subnet.subnet", "primary", "false"),
),
},
},
})
}

func testAccMSOSchemaTemplateAnpEpgSubnetDatasource() string {
return fmt.Sprintf(`%s
data "mso_schema_template_anp_epg_subnet" "subnet" {
schema_id = mso_schema.%[2]s.id
template = "%[3]s"
anp_name = "%[4]s"
epg_name = "%[5]s"
ip = mso_schema_template_anp_epg_subnet.%[6]s_subnet.ip
}`, testAccMSOSchemaTemplateAnpEpgSubnetConfigCreate(), msoSchemaName, msoSchemaTemplateName, msoSchemaTemplateAnpName, msoSchemaTemplateAnpEpgName, msoSchemaTemplateAnpEpgName)
}

func testAccMSOSchemaTemplateAnpEpgSubnetDatasourceNotFound() string {
return fmt.Sprintf(`%s
data "mso_schema_template_anp_epg_subnet" "subnet" {
schema_id = mso_schema.%[2]s.id
template = "%[3]s"
anp_name = "%[4]s"
epg_name = "%[5]s"
ip = "99.99.99.99/32"
}`, testAccMSOSchemaTemplateAnpEpgSubnetConfigCreate(), msoSchemaName, msoSchemaTemplateName, msoSchemaTemplateAnpName, msoSchemaTemplateAnpEpgName)
}
Loading
Loading