Skip to content

Commit b48e05c

Browse files
authored
[bugfix] Fixed idempotency on site service graph. (#286)
1 parent c84c00b commit b48e05c

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

mso/resource_mso_schema_site_contract_service_graph.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func resourceMSOSchemaSiteContractServiceGraph() *schema.Resource {
5252
Type: schema.TypeString,
5353
Optional: true,
5454
ForceNew: true,
55+
Computed: true,
5556
ValidateFunc: validation.StringLenBetween(1, 1000),
5657
},
5758
"service_graph_template_name": &schema.Schema{

mso/resource_mso_schema_site_service_graph.go

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -111,44 +111,52 @@ func resourceMSOSchemaSiteServiceGraph() *schema.Resource {
111111
return err
112112
}
113113

114-
sgCont, _, err := getTemplateServiceGraphCont(cont, templateName.(string), graphName.(string))
115-
if strings.Contains(fmt.Sprint(err), "No Template found") {
116-
// The function getTemplateServiceGraphCont() is not required when the template is attched to physical site.
117-
return nil
118-
} else if err != nil {
119-
log.Printf("graphcont err %v", err)
120-
return err
121-
} else {
122-
/* The function getTemplateServiceGraphCont() is required when the template is attached to cloud sites.
123-
provider_connector_type is applicable only for cloud sites. */
124-
var templateServiceNodeList []string
125-
serviceNodes := sgCont.S("serviceNodes").Data().([]interface{})
126-
for _, val := range serviceNodes {
127-
serviceNodeValues := val.(map[string]interface{})
128-
nodeId := models.StripQuotes(serviceNodeValues["serviceNodeTypeId"].(string))
129-
130-
nodeType, err := getNodeNameFromId(msoClient, nodeId)
131-
if err != nil {
132-
return err
133-
}
134-
135-
templateServiceNodeList = append(templateServiceNodeList, nodeType)
136-
}
137-
138-
/* Loop trough the templateServiceNodeList and validate the site level user input(provider_connector_type)
139-
to verify it's value for nodetype 'other' and 'firewall'. */
140-
_, siteServiceNodes := diff.GetChange("service_node")
141-
142-
for i, val := range siteServiceNodes.([]interface{}) {
143-
serviceNode := val.(map[string]interface{})
144-
if templateServiceNodeList[i] == "other" && !valueInSliceofStrings(serviceNode["provider_connector_type"].(string), []string{"none", "redir"}) {
145-
return fmt.Errorf("The expected value for service_node.%d.provider_connector_type have to be one of [none, redir] when template's service node type is other, got %s.", i, serviceNode["provider_connector_type"])
146-
} else if templateServiceNodeList[i] == "firewall" && !valueInSliceofStrings(serviceNode["provider_connector_type"].(string), []string{"none", "redir", "snat", "dnat", "snat_dnat"}) {
147-
return fmt.Errorf("The expected value for service_node.%d.provider_connector_type have to be one of [none, redir, snat, dnat, snat_dnat] when template's service node type is firewall, got %s.", i, serviceNode["provider_connector_type"])
114+
_, serviceNode := diff.GetChange("service_node")
115+
if len(serviceNode.([]interface{})) != 0 {
116+
for _, node := range serviceNode.([]interface{}) {
117+
serviceNodeMap := node.(map[string]interface{})
118+
if !valueInSliceofStrings(serviceNodeMap["provider_connector_type"].(string), []string{"none", "redir"}) { // If provider_connector_type is not none, then validate the user input.
119+
sgCont, _, err := getTemplateServiceGraphCont(cont, templateName.(string), graphName.(string))
120+
if strings.Contains(fmt.Sprint(err), "No Template found") {
121+
// The function getTemplateServiceGraphCont() is not required when the template is attached to physical site.
122+
return nil
123+
} else if err != nil {
124+
return err
125+
} else {
126+
/* The function getTemplateServiceGraphCont() is required when the template is attached to cloud sites.
127+
provider_connector_type is applicable only for cloud sites. */
128+
var templateServiceNodeList []string
129+
serviceNodes := sgCont.S("serviceNodes").Data().([]interface{})
130+
for _, val := range serviceNodes {
131+
serviceNodeValues := val.(map[string]interface{})
132+
nodeId := models.StripQuotes(serviceNodeValues["serviceNodeTypeId"].(string))
133+
134+
nodeType, err := getNodeNameFromId(msoClient, nodeId)
135+
if err != nil {
136+
return err
137+
}
138+
139+
templateServiceNodeList = append(templateServiceNodeList, nodeType)
140+
}
141+
142+
/* Loop trough the templateServiceNodeList and validate the site level user input(provider_connector_type)
143+
to verify it's value for nodetype 'other' and 'firewall'. */
144+
_, siteServiceNodes := diff.GetChange("service_node")
145+
146+
for i, val := range siteServiceNodes.([]interface{}) {
147+
serviceNode := val.(map[string]interface{})
148+
if templateServiceNodeList[i] == "other" && !valueInSliceofStrings(serviceNode["provider_connector_type"].(string), []string{"none", "redir"}) {
149+
return fmt.Errorf("The expected value for service_node.%d.provider_connector_type have to be one of [none, redir] when template's service node type is other, got %s.", i, serviceNode["provider_connector_type"])
150+
} else if templateServiceNodeList[i] == "firewall" && !valueInSliceofStrings(serviceNode["provider_connector_type"].(string), []string{"none", "redir", "snat", "dnat", "snat_dnat"}) {
151+
return fmt.Errorf("The expected value for service_node.%d.provider_connector_type have to be one of [none, redir, snat, dnat, snat_dnat] when template's service node type is firewall, got %s.", i, serviceNode["provider_connector_type"])
152+
}
153+
}
154+
return nil
155+
}
148156
}
149157
}
150-
return nil
151158
}
159+
return nil
152160
},
153161
}
154162
}

mso/resource_mso_schema_template_contract.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ func resourceMSOTemplateContractRead(d *schema.ResourceData, m interface{}) erro
524524
if err != nil {
525525
return errorForObjectNotFound(err, d.Id(), schemaCont, d)
526526
}
527-
setContractFromSchema(d, schemaCont, schemaId, templateName, contractName)
527+
err = setContractFromSchema(d, schemaCont, schemaId, templateName, contractName)
528+
if err != nil {
529+
d.SetId("")
530+
log.Printf("[DEBUG] Resetting Id due to setContractFromSchema returning '%s'", err)
531+
}
528532
log.Printf("[DEBUG] %s: Read finished successfully", d.Id())
529533
return nil
530534
}

0 commit comments

Comments
 (0)