Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 16 | import ( |
| 17 | "fmt" |
| 18 | "net/url" |
| 19 | "strings" |
| 20 | ) |
| 21 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 22 | type Storage interface { |
| 23 | Put(id string, update StatusMsg) error |
| 24 | Get(id string) (*StatusMsg, error) |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 25 | Delete(id string) error |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 26 | List() ([]StatusMsg, error) |
| 27 | } |
| 28 | |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 29 | func NewStorage(spec string) (Storage, error) { |
| 30 | conn, err := url.Parse(spec) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | |
| 35 | switch strings.ToUpper(conn.Scheme) { |
| 36 | case "MEMORY": |
| 37 | return NewMemoryStorage(), nil |
| 38 | case "CONSUL": |
| 39 | return NewConsulStorage(spec) |
| 40 | default: |
| 41 | return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", conn.Scheme) |
| 42 | } |
| 43 | } |
| 44 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 45 | type MemoryStorage struct { |
| 46 | Data map[string]StatusMsg |
| 47 | } |
| 48 | |
| 49 | func NewMemoryStorage() *MemoryStorage { |
| 50 | return &MemoryStorage{ |
| 51 | Data: make(map[string]StatusMsg), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func (s *MemoryStorage) Put(id string, update StatusMsg) error { |
| 56 | s.Data[id] = update |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 57 | return nil |
| 58 | } |
| 59 | |
| 60 | func (s *MemoryStorage) Get(id string) (*StatusMsg, error) { |
| 61 | m, ok := s.Data[id] |
| 62 | if !ok { |
| 63 | return nil, nil |
| 64 | } |
| 65 | return &m, nil |
| 66 | } |
| 67 | |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 68 | func (s *MemoryStorage) Delete(id string) error { |
| 69 | delete(s.Data, id) |
| 70 | return nil |
| 71 | } |
| 72 | |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 73 | func (s *MemoryStorage) List() ([]StatusMsg, error) { |
| 74 | r := make([]StatusMsg, len(s.Data)) |
| 75 | i := 0 |
| 76 | for _, v := range s.Data { |
| 77 | r[i] = v |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 78 | i += 1 |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 79 | } |
| 80 | return r, nil |
| 81 | } |