Преглед изворни кода

backend/extension: Allow the backend data to be empty

Tom Denham пре 8 година
родитељ
комит
7722da5eb9
1 измењених фајлова са 35 додато и 30 уклоњено
  1. 35 30
      backend/extension/extension_network.go

+ 35 - 30
backend/extension/extension_network.go

@@ -82,23 +82,26 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
 			}
 			}
 
 
 			if len(n.subnetAddCommand) > 0 {
 			if len(n.subnetAddCommand) > 0 {
-				var dat interface{}
-				if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &dat); err != nil {
-					log.Errorf("failed to unmarshal BackendData: %v", err)
-				} else {
-					backendData := dat.(string)
-					cmd_output, err := runCmd([]string{
-						fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
-						fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
-						backendData,
-						"sh", "-c", n.subnetAddCommand)
-
-					if err != nil {
-						log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetAddCommand, err, cmd_output)
-					} else {
-						log.Infof("Ran command: %s\n Output: %s", n.subnetAddCommand, cmd_output)
+				backendData := ""
+
+				if len(evt.Lease.Attrs.BackendData) > 0 {
+					if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &backendData); err != nil {
+						log.Errorf("failed to unmarshal BackendData: %v", err)
+						continue
 					}
 					}
 				}
 				}
+
+				cmd_output, err := runCmd([]string{
+					fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
+					fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
+					backendData,
+					"sh", "-c", n.subnetAddCommand)
+
+				if err != nil {
+					log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetAddCommand, err, cmd_output)
+				} else {
+					log.Infof("Ran command: %s\n Output: %s", n.subnetAddCommand, cmd_output)
+				}
 			}
 			}
 
 
 		case subnet.EventRemoved:
 		case subnet.EventRemoved:
@@ -110,23 +113,25 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
 			}
 			}
 
 
 			if len(n.subnetRemoveCommand) > 0 {
 			if len(n.subnetRemoveCommand) > 0 {
-				var dat interface{}
-				if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &dat); err != nil {
-					log.Errorf("failed to unmarshal BackendData: %v", err)
-				} else {
-					backendData := dat.(string)
-					cmd_output, err := runCmd([]string{
-						fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
-						fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
-						backendData,
-						"sh", "-c", n.subnetRemoveCommand)
-
-					if err != nil {
-						log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetRemoveCommand, err, cmd_output)
-					} else {
-						log.Infof("Ran command: %s\n Output: %s", n.subnetRemoveCommand, cmd_output)
+				backendData := ""
+
+				if len(evt.Lease.Attrs.BackendData) > 0 {
+					if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &backendData); err != nil {
+						log.Errorf("failed to unmarshal BackendData: %v", err)
+						continue
 					}
 					}
 				}
 				}
+				cmd_output, err := runCmd([]string{
+					fmt.Sprintf("SUBNET=%s", evt.Lease.Subnet),
+					fmt.Sprintf("PUBLIC_IP=%s", evt.Lease.Attrs.PublicIP)},
+					backendData,
+					"sh", "-c", n.subnetRemoveCommand)
+
+				if err != nil {
+					log.Errorf("failed to run command: %s Err: %v Output: %s", n.subnetRemoveCommand, err, cmd_output)
+				} else {
+					log.Infof("Ran command: %s\n Output: %s", n.subnetRemoveCommand, cmd_output)
+				}
 			}
 			}
 
 
 		default:
 		default: