Skip to content

Commit

Permalink
[minor_change] add attributes filter_type, directives, action and pri…
Browse files Browse the repository at this point in the history
…ority to filter_relationship attribute and add target_dscp and priority attributes to mso_schema_template_conract and add input validation for attributes and align datasource with resource
  • Loading branch information
akinross committed Aug 25, 2023
1 parent b82d916 commit ef5465a
Show file tree
Hide file tree
Showing 4 changed files with 477 additions and 481 deletions.
119 changes: 48 additions & 71 deletions mso/datasource_mso_schema_template_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ 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"
)
Expand Down Expand Up @@ -45,7 +43,15 @@ func dataSourceMSOTemplateContract() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"filter_relationships": {
"target_dscp": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"priority": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"filter_relationship": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Expand All @@ -62,6 +68,10 @@ func dataSourceMSOTemplateContract() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"filter_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"action": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand All @@ -71,89 +81,56 @@ func dataSourceMSOTemplateContract() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"priority": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
"filter_relationships": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"filter_schema_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"filter_template_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"filter_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
"directives": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
}),
}
}

func dataSourceMSOTemplateContractRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] %s: Beginning Read", d.Id())

msoClient := m.(*client.Client)

schemaId := d.Get("schema_id").(string)

cont, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaId))
templateName := d.Get("template_name").(string)
contractName := d.Get("contract_name").(string)
schemaCont, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaId))
if err != nil {
return err
return errorForObjectNotFound(err, d.Id(), schemaCont, d)
}
count, err := cont.ArrayCount("templates")
err = setContractFromSchema(d, schemaCont, schemaId, templateName, contractName)
if err != nil {
return fmt.Errorf("No Template found")
}
stateTemplate := d.Get("template_name").(string)
stateContract := d.Get("contract_name").(string)

found := false
for i := 0; i < count && !found; i++ {
tempCont, err := cont.ArrayElement(i, "templates")
if err != nil {
return err
}
apiTemplate := models.StripQuotes(tempCont.S("name").String())

if apiTemplate == stateTemplate {
contractCount, err := tempCont.ArrayCount("contracts")
if err != nil {
return fmt.Errorf("Unable to get contract list")
}
for j := 0; j < contractCount; j++ {
contractCont, err := tempCont.ArrayElement(j, "contracts")
if err != nil {
return err
}
apiContract := models.StripQuotes(contractCont.S("name").String())
if apiContract == stateContract {
d.SetId(fmt.Sprintf("%s/templates/%s/contracts/%s", schemaId, stateTemplate, stateContract))
d.Set("contract_name", apiContract)
d.Set("schema_id", schemaId)
d.Set("template_name", apiTemplate)
d.Set("display_name", models.StripQuotes(contractCont.S("displayName").String()))
d.Set("filter_type", models.StripQuotes(contractCont.S("filterType").String()))
d.Set("scope", models.StripQuotes(contractCont.S("scope").String()))

var filterList []interface{}
count, _ := contractCont.ArrayCount("filterRelationships")
for i := 0; i < count; i++ {
filterMap := make(map[string]interface{})
filterCont, err := contractCont.ArrayElement(i, "filterRelationships")
if err != nil {
return fmt.Errorf("Unable to parse the filter Relationships list")
}
filRef := filterCont.S("filterRef").Data()
split := strings.Split(filRef.(string), "/")
filterMap["filter_schema_id"] = fmt.Sprintf("%s", split[2])
filterMap["filter_template_name"] = fmt.Sprintf("%s", split[4])
filterMap["filter_name"] = fmt.Sprintf("%s", split[6])
filterMap["directives"] = filterCont.S("directives").Data().([]interface{})
filterMap["action"] = filterCont.S("action").Data().(string)
filterList = append(filterList, filterMap)
}
d.Set("filter_relationships", filterList)

found = true
break
}
}
}
}

if !found {
return fmt.Errorf("Unable to find the Contract %s in Template %s of Schema Id %s", stateContract, stateTemplate, schemaId)
return err
}

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

0 comments on commit ef5465a

Please sign in to comment.