Skip to content

Commit

Permalink
Fix network ordering causing server resource update
Browse files Browse the repository at this point in the history
Add `ordering_computed` field in network property of
server. The ordering of the networks set by gridscale
backend (if the ordering is not specified by the user).

See also PR #143. Fixes #142
  • Loading branch information
nvthongswansea authored Mar 22, 2021
1 parent 4dd5a15 commit 3857fd8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gridscale/relation-manager/serverRelationManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func readCustomFirewallRules(netData map[string]interface{}) gsclient.FirewallRu
//Check if the firewall rule type is declared in the current network
if rulesInTypeAttr, ok := netData[ruleType]; ok {
//Loop through all rules in the current firewall type
for _, rulesInType := range rulesInTypeAttr.([]interface{}) {
for _, rulesInType := range rulesInTypeAttr.(*schema.Set).List() {
ruleProps := rulesInType.(map[string]interface{})
ruleProperties := gsclient.FirewallRuleProperties{
DstPort: ruleProps["dst_port"].(string),
Expand Down
22 changes: 16 additions & 6 deletions gridscale/resource_gridscale_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -156,28 +157,28 @@ func resourceGridscaleServer() *schema.Resource {
Computed: true,
},
"rules_v4_in": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: getFirewallRuleCommonSchema(),
},
},
"rules_v4_out": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: getFirewallRuleCommonSchema(),
},
},
"rules_v6_in": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: getFirewallRuleCommonSchema(),
},
},
"rules_v6_out": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: getFirewallRuleCommonSchema(),
Expand All @@ -190,7 +191,11 @@ func resourceGridscaleServer() *schema.Resource {
"ordering": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
"ordering_computed": {
Type: schema.TypeInt,
Computed: true,
Description: "The ordering of the networks set by gridscale backend (if the ordering is not specified by the user)",
},
"create_time": {
Type: schema.TypeString,
Expand Down Expand Up @@ -514,6 +519,11 @@ func resourceGridscaleServerRead(d *schema.ResourceData, meta interface{}) error

//readServerNetworkRels extract relationships between server and networks
func readServerNetworkRels(serverNetRels []gsclient.ServerNetworkRelationProperties) []interface{} {
// Sort the server-network relation slice by order
sort.Slice(serverNetRels, func(i, j int) bool {
return serverNetRels[i].Ordering < serverNetRels[j].Ordering
})

networks := make([]interface{}, 0)
for _, rel := range serverNetRels {
network := map[string]interface{}{
Expand All @@ -524,7 +534,7 @@ func readServerNetworkRels(serverNetRels []gsclient.ServerNetworkRelationPropert
"firewall_template_uuid": rel.FirewallTemplateUUID,
"object_name": rel.ObjectName,
"network_type": rel.NetworkType,
"ordering": rel.Ordering,
"ordering_computed": rel.Ordering,
}
//Init all types of firewall rule
v4InRuleProps := make([]interface{}, 0)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/server.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ This resource exports the following attributes:
* `bootdevice` - Make this network the boot device. This can only be set for one network.
* `object_name` - Name of the network.
* `ordering` - Defines the ordering of the network interfaces. Lower numbers have lower PCI-IDs.
* `ordering_computed` - The ordering of the networks set by gridscale backend (if the ordering is not specified by the user).
* `create_time` - Defines the date and time the object was initially created.
* `network_type` - One of network, network_high, network_insane.
* `mac` - network_mac defines the MAC address of the network interface.
Expand Down

0 comments on commit 3857fd8

Please sign in to comment.