Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: watch config files to load/unload maps #948

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions atlas/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package atlas

import (
"context"
"fmt"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -221,6 +222,57 @@ func (a *Atlas) AddMap(m Map) {
a.maps[m.Name] = m
}

// AddMaps registers maps by name, all or nothing. If a map already exists an error will be returned.
func (a *Atlas) AddMaps(maps []Map) error {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
return defaultAtlas.AddMaps(maps)
}
a.Lock()
defer a.Unlock()

// If Atlas is empty, add all the maps, no checks required.
if a.maps == nil {
a.maps = make(map[string]Map, len(maps))
for _, m := range maps {
a.maps[m.Name] = m
}
return nil
}

// Check all the names for conflicts before we add any map, so that we can add all or none.
for _, m := range maps {
if _, exists := a.maps[m.Name]; exists {
return fmt.Errorf("Map with name \"%s\" already exists.", m.Name)
}
}

// Now add all the maps.
for _, m := range maps {
a.maps[m.Name] = m
}

return nil
}

func (a *Atlas) RemoveMaps(names []string) {
if a == nil {
// Use the default Atlas if a, is nil. This way the empty value is
// still useful.
defaultAtlas.RemoveMaps(names)
return
}
a.Lock()
defer a.Unlock()

for _, name := range names {
if _, exists := a.maps[name]; exists {
delete(a.maps, name)
}
}
}

// GetCache returns the registered cache if one is registered, otherwise nil
func (a *Atlas) GetCache() cache.Interface {
if a == nil {
Expand Down
49 changes: 49 additions & 0 deletions atlas/atlas_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package atlas_test

import (
"strings"
"testing"

"github.com/go-spatial/geom"
"github.com/go-spatial/tegola/atlas"
"github.com/go-spatial/tegola/internal/env"
Expand Down Expand Up @@ -51,3 +54,49 @@ var testMap = atlas.Map{
testLayer3,
},
}

func TestAddMaps(t *testing.T) {
a := &atlas.Atlas{}

// Should initialize from empty
maps := []atlas.Map{
{Name: "First Map"},
{Name: "Second Map"},
}
err := a.AddMaps(maps)
if err != nil {
t.Errorf("Unexpected error when addings maps. %s", err)
}

m, err := a.Map("Second Map")
if err != nil {
t.Errorf("Failed retrieving map from Atlas. %s", err)
} else if m.Name != "Second Map" {
t.Errorf("Expected map named \"Second Map\". Found %v.", m)
}

// Should error if duplicate name.
err = a.AddMaps([]atlas.Map{{Name: "First Map"}})
if err == nil || !strings.Contains(err.Error(), "already exists") {
t.Errorf("Should return error for duplicate map name. err=%s", err)
}
}

func TestRemoveMaps(t *testing.T) {
a := &atlas.Atlas{}
a.AddMaps([]atlas.Map{
{Name: "First Map"},
{Name: "Second Map"},
})

if len(a.AllMaps()) != 2 {
t.Error("Unexpected failure setting up Atlas. No maps added.")
return
}

a.RemoveMaps([]string{"Second Map"})
maps := a.AllMaps()
if len(maps) != 1 || maps[0].Name == "Second Map" {
t.Error("Should have deleted \"Second Map\". Didn't.")
}
}
11 changes: 9 additions & 2 deletions cmd/internal/register/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func Maps(a *atlas.Atlas, maps []provider.Map, providers map[string]provider.Til
)

// iterate our maps
newMaps := make([]atlas.Map, 0, len(maps))
for _, m := range maps {
newMap := webMercatorMapFromConfigMap(m)

Expand All @@ -157,9 +158,15 @@ func Maps(a *atlas.Atlas, maps []provider.Map, providers map[string]provider.Til
newMap.Layers = append(newMap.Layers, layer)
}

a.AddMap(newMap)
newMaps = append(newMaps, newMap)
}
return nil

// Register all or nothing.
return a.AddMaps(newMaps)
}

func UnloadMaps(a *atlas.Atlas, names []string) {
a.RemoveMaps(names)
}

// Find allow HTML tag
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/register/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMaps(t *testing.T) {
provArr[i] = tc.providers[i]
}

providers, err := register.Providers(provArr, tc.maps)
providers, err := register.Providers(provArr, tc.maps, "default")
if err != nil {
t.Errorf("unexpected err: %v", err)
return
Expand Down
13 changes: 11 additions & 2 deletions cmd/internal/register/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (e ErrProviderTypeInvalid) Error() string {
}

// Providers registers data provider backends
func Providers(providers []dict.Dicter, maps []provider.Map) (map[string]provider.TilerUnion, error) {
func Providers(providers []dict.Dicter, maps []provider.Map, namespace string) (map[string]provider.TilerUnion, error) {
// holder for registered providers
registeredProviders := map[string]provider.TilerUnion{}

Expand Down Expand Up @@ -72,7 +72,7 @@ func Providers(providers []dict.Dicter, maps []provider.Map) (map[string]provide
}

// register the provider
prov, err := provider.For(ptype, p, maps)
prov, err := provider.For(ptype, p, maps, namespace)
if err != nil {
return registeredProviders, err
}
Expand All @@ -84,3 +84,12 @@ func Providers(providers []dict.Dicter, maps []provider.Map) (map[string]provide

return registeredProviders, nil
}

func UnloadProviders(names []string, namespace string) {
for _, name := range names {
err := provider.Remove(name, namespace)
if err != nil {
log.Errorf("Error unloading provider instance %s: %s", name, err)
}
}
}
2 changes: 1 addition & 1 deletion cmd/internal/register/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestProviders(t *testing.T) {
provArr[i] = tc.config[i]
}

_, err = register.Providers(provArr, nil)
_, err = register.Providers(provArr, nil, "default")
if tc.expectedErr != nil {
if err.Error() != tc.expectedErr.Error() {
t.Errorf("invalid error. expected: %v, got %v", tc.expectedErr, err.Error())
Expand Down
Loading