changeset 1705:dcbe2a7dc532

Scheduled imports: Take extra configuration parameters from attributes table.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 Jan 2019 16:11:07 +0100
parents 897d4d8316ad
children fb05027d93b6
files pkg/imports/config.go pkg/imports/scheduled.go
diffstat 2 files changed, 39 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/config.go	Mon Jan 07 15:34:34 2019 +0100
+++ b/pkg/imports/config.go	Mon Jan 07 16:11:07 2019 +0100
@@ -18,6 +18,7 @@
 	"database/sql"
 	"encoding/json"
 	"fmt"
+	"strings"
 
 	"github.com/robfig/cron"
 
@@ -83,6 +84,12 @@
 	return value, found
 }
 
+// Bool returns a bool value for a given key.
+func (ca ConfigAttributes) Bool(key string) bool {
+	s, found := ca.Get(key)
+	return found && strings.ToLower(s) == "true"
+}
+
 // UnmarshalJSON checks if the incoming string
 // is a registered import type.
 func (ik *ImportKind) UnmarshalJSON(data []byte) error {
--- a/pkg/imports/scheduled.go	Mon Jan 07 15:34:34 2019 +0100
+++ b/pkg/imports/scheduled.go	Mon Jan 07 16:11:07 2019 +0100
@@ -14,6 +14,7 @@
 package imports
 
 import (
+	"errors"
 	"log"
 
 	"gemma.intevation.de/gemma/pkg/common"
@@ -21,43 +22,50 @@
 )
 
 func init() {
-	registerAction(GMJobKind, func(cfg *IDConfig) interface{} {
+	registerAction(GMJobKind, func(cfg *IDConfig) (interface{}, error) {
 		log.Println("info: schedule 'gm' import")
+		insecure := cfg.Attributes.Bool("insecure")
 		return &GaugeMeasurement{
 			URL:      *cfg.URL,
-			Insecure: false,
-		}
+			Insecure: insecure,
+		}, nil
 	})
-	registerAction(FAJobKind, func(cfg *IDConfig) interface{} {
+	registerAction(FAJobKind, func(cfg *IDConfig) (interface{}, error) {
 		log.Println("info: schedule 'fa' import")
+		insecure := cfg.Attributes.Bool("insecure")
 		return &FairwayAvailability{
 			URL:      *cfg.URL,
-			Insecure: false,
-		}
+			Insecure: insecure,
+		}, nil
 	})
-	registerAction(BNJobKind, func(cfg *IDConfig) interface{} {
+	registerAction(BNJobKind, func(cfg *IDConfig) (interface{}, error) {
 		log.Println("info: schedule 'bn' import")
+		insecure := cfg.Attributes.Bool("insecure")
 		return &Bottleneck{
 			URL:      *cfg.URL,
-			Insecure: false,
-		}
+			Insecure: insecure,
+		}, nil
 	})
-	registerAction(WXJobKind, func(cfg *IDConfig) interface{} {
+	registerAction(WXJobKind, func(cfg *IDConfig) (interface{}, error) {
 		log.Println("info: schedule 'wx' import")
-		// TODO: Take this from configuration.
-		var (
-			featureType = "ws-wamos:ienc_wtwaxs"
-			sortBy      = "hydro_scamin"
-		)
+
+		ft, found := cfg.Attributes.Get("feature-type")
+		if !found {
+			return nil, errors.New("cannot find 'feature-type' attribute")
+		}
+		sb, found := cfg.Attributes.Get("sort-by")
+		if !found {
+			return nil, errors.New("cannot find 'sort-by' attribute")
+		}
 		return &WaterwayAxis{
 			URL:         *cfg.URL,
-			FeatureType: featureType,
-			SortBy:      sortBy,
-		}
+			FeatureType: ft,
+			SortBy:      sb,
+		}, nil
 	})
 }
 
-func registerAction(kind JobKind, setup func(cfg *IDConfig) interface{}) {
+func registerAction(kind JobKind, setup func(cfg *IDConfig) (interface{}, error)) {
 
 	action := func(id int64) {
 		cfg, err := loadIDConfig(id)
@@ -74,7 +82,11 @@
 			return
 		}
 
-		what := setup(cfg)
+		what, err := setup(cfg)
+		if err != nil {
+			log.Printf("error: setup failed %v.\n", err)
+			return
+		}
 
 		var serialized string
 		if serialized, err = common.ToJSONString(what); err != nil {