Skip to content

Commit

Permalink
Maded changes to support change of parameter filter_relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
anvitha-jain authored and lhercot committed Sep 24, 2021
1 parent cc8f520 commit e718007
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 10 deletions.
92 changes: 84 additions & 8 deletions examples/schema_template_contract/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,93 @@ provider "mso" {
insecure = true
}

resource "mso_tenant" "tenant1" {
name = "test_tenant"
display_name = "test_tenant"
description = "DemoTenant"
site_associations {
site_id = mso_site.test_site.id
}
}

resource "mso_schema" "schema1" {
name = "test_schema"
template_name = "Template1"
tenant_id = mso_tenant.tenant1.id
}

resource "mso_schema_template_filter_entry" "filter_entry" {
schema_id = mso_schema.schema1.id
template_name = mso_schema.schema1.template_name
name = "Filter1"
display_name = "Filter1"
entry_name = "entry1"
entry_display_name = "entry1"
entry_description = "DemoEntry"
ether_type = "arp"
ip_protocol = "eigrp"
tcp_session_rules = ["acknowledgement"]
destination_from ="unspecified"
destination_to ="unspecified"
source_from ="unspecified"
source_to ="unspecified"
arp_flag ="unspecified"
stateful = false
match_only_fragments= false
}

resource "mso_schema_template_filter_entry" "filter_entry_2" {
schema_id = mso_schema_template_filter_entry.filter_entry.schema_id
template_name = mso_schema_template_filter_entry.filter_entry.template_name
name = "Filter2"
display_name = "Filter2"
entry_name = "entry2"
entry_display_name = "entry2"
entry_description = "DemoEntry"
ether_type = "arp"
ip_protocol = "eigrp"
tcp_session_rules = ["acknowledgement"]
destination_from ="unspecified"
destination_to ="unspecified"
source_from ="unspecified"
source_to ="unspecified"
arp_flag ="unspecified"
stateful = false
match_only_fragments= false
}

resource "mso_schema_template_contract" "template_contract" {
schema_id = "5c4d5bb72700000401f80948"
template_name = "Template1"
contract_name = "C2"
display_name = "C2"
schema_id = mso_schema_template_filter_entry.filter_entry_2.schema_id
template_name = mso_schema_template_filter_entry.filter_entry_2.template_name
contract_name = "Contract1"
display_name = "Contract1"
filter_type = "bothWay"
scope = "context"
filter_relationship {
filter_schema_id = mso_schema_template_filter_entry.filter_entry_2.schema_id
filter_template_name = mso_schema_template_filter_entry.filter_entry_2.template_name
filter_name = mso_schema_template_filter_entry.filter_entry.name
}
filter_relationship {
filter_schema_id = mso_schema_template_filter_entry.filter_entry_2.schema_id
filter_template_name = mso_schema_template_filter_entry.filter_entry_2.template_name
filter_name = mso_schema_template_filter_entry.filter_entry_2.name
}
directives = ["none"]
}

// The below format of using filter_relationships will be deprecated.
resource "mso_schema_template_contract" "template_contract2" {
schema_id = mso_schema_template_contract.template_contract.schema_id
template_name = mso_schema_template_contract.template_contract.template_name
contract_name = "Contract2"
display_name = "Contract2"
filter_type = "bothWay"
scope = "context"
filter_relationships ={
filter_schema_id = "5c4d5bb72700000401f80948"
filter_template_name = "Template1"
filter_name = "filter1"
filter_relationships = {
filter_schema_id = mso_schema_template_filter_entry.filter_entry_2.schema_id
filter_template_name = mso_schema_template_filter_entry.filter_entry_2.template_name
filter_name = mso_schema_template_filter_entry.filter_entry.name
}
directives = ["none"]
}
70 changes: 68 additions & 2 deletions mso/resource_mso_schema_template_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,39 @@ func resourceMSOTemplateContract() *schema.Resource {
},
"filter_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
},
},
},
Optional: true,
ConflictsWith: []string{"filter_relationship"},
Deprecated: "use filter_relationship instead",
},
"filter_relationship": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"filter_schema_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"filter_template_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"filter_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
},
},
Required: true,
},
"directives": {
Type: schema.TypeList,
Expand Down Expand Up @@ -153,6 +181,19 @@ func resourceMSOTemplateContractImport(d *schema.ResourceData, m interface{}) ([
}
d.Set("filter_relationships", filterMap)

filterList := make([]interface{}, 0, 1)
filter_relationship := contractCont.S("filter_relationship").Data().([]interface{})
for _, tempFilter := range filter_relationship {
filterRelationship := tempFilter.(map[string]interface{})

filterMap := make(map[string]interface{})
filterMap["cidr_ip"] = filterRelationship["ip"]
filterMap["primary"] = filterRelationship["primary"]

filterList = append(filterList, filterMap)
}
d.Set("node_relationship", filterList)

found = true
break
}
Expand Down Expand Up @@ -216,6 +257,31 @@ func resourceMSOTemplateContractCreate(d *schema.ResourceData, m interface{}) er
}
filter = append(filter, filterMap)

filterList := make([]interface{}, 0, 1)
filterRelMap := make(map[string]interface{})
filter_relationship := d.Get("filter_relationship").([]interface{})
for _, tempFilter := range filter_relationship {
filterRelationship := tempFilter.(map[string]interface{})

filterRefMap := make(map[string]interface{})
filterRefMap["schemaId"] = filterRelationship["filter_schema_id"]
filterRefMap["templateName"] = filterRelationship["filter_template_name"]
filterRefMap["filterName"] = filterRelationship["filter_name"]

filterRelMap["filterRef"] = filterRefMap
if tempVar, ok := d.GetOk("directives"); ok {
filterRelMap["directives"] = tempVar
}

}
filterList = append(filterList, filterRelMap)

// var filter_check map[int]string
filter_check := filter[0].(map[string]interface{})
if filter_check == nil {
filter = filterList
}

path := fmt.Sprintf("/templates/%s/contracts/-", templateName)
contractStruct := models.NewTemplateContract("add", path, contractName, displayName, scope, filter_type, filter)
_, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaID), contractStruct)
Expand Down

0 comments on commit e718007

Please sign in to comment.