File: consumer.go

package info (click to toggle)
deck 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: makefile: 17; sh: 3
file content (171 lines) | stat: -rw-r--r-- 3,574 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package state

import (
	"fmt"

	memdb "github.com/hashicorp/go-memdb"
	"github.com/kong/deck/utils"
)

const (
	consumerTableName = "consumer"
)

var consumerTableSchema = &memdb.TableSchema{
	Name: consumerTableName,
	Indexes: map[string]*memdb.IndexSchema{
		"id": {
			Name:    "id",
			Unique:  true,
			Indexer: &memdb.StringFieldIndex{Field: "ID"},
		},
		"Username": {
			Name:         "Username",
			Unique:       true,
			Indexer:      &memdb.StringFieldIndex{Field: "Username"},
			AllowMissing: true,
		},
		all: allIndex,
	},
}

// ConsumersCollection stores and indexes Kong Consumers.
type ConsumersCollection collection

// Add adds a consumer to the collection
// An error is thrown if consumer.ID is empty.
func (k *ConsumersCollection) Add(consumer Consumer) error {
	if utils.Empty(consumer.ID) {
		return errIDRequired
	}

	txn := k.db.Txn(true)
	defer txn.Abort()

	var searchBy []string
	searchBy = append(searchBy, *consumer.ID)
	if !utils.Empty(consumer.Username) {
		searchBy = append(searchBy, *consumer.Username)
	}
	_, err := getConsumer(txn, searchBy...)
	if err == nil {
		return fmt.Errorf("inserting consumer %v: %w", consumer.Console(), ErrAlreadyExists)
	} else if err != ErrNotFound {
		return err
	}

	err = txn.Insert(consumerTableName, &consumer)
	if err != nil {
		return err
	}
	txn.Commit()
	return nil
}

func getConsumer(txn *memdb.Txn, IDs ...string) (*Consumer, error) {
	for _, id := range IDs {
		res, err := multiIndexLookupUsingTxn(txn, consumerTableName,
			[]string{"Username", "id"}, id)
		if err == ErrNotFound {
			continue
		}
		if err != nil {
			return nil, err
		}
		consumer, ok := res.(*Consumer)
		if !ok {
			panic(unexpectedType)
		}
		return &Consumer{Consumer: *consumer.DeepCopy()}, nil
	}
	return nil, ErrNotFound
}

// Get gets a consumer by name or ID.
func (k *ConsumersCollection) Get(userNameOrID string) (*Consumer, error) {
	if userNameOrID == "" {
		return nil, errIDRequired
	}

	txn := k.db.Txn(false)
	defer txn.Abort()
	return getConsumer(txn, userNameOrID)
}

// Update udpates an existing consumer.
// It returns an error if the consumer is not already present.
func (k *ConsumersCollection) Update(consumer Consumer) error {
	// TODO abstract this in the go-memdb library itself
	if utils.Empty(consumer.ID) {
		return errIDRequired
	}

	txn := k.db.Txn(true)
	defer txn.Abort()

	err := deleteConsumer(txn, *consumer.ID)
	if err != nil {
		return err
	}

	err = txn.Insert(consumerTableName, &consumer)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

func deleteConsumer(txn *memdb.Txn, userNameOrID string) error {
	consumer, err := getConsumer(txn, userNameOrID)
	if err != nil {
		return err
	}

	err = txn.Delete(consumerTableName, consumer)
	if err != nil {
		return err
	}
	return nil
}

// Delete deletes a consumer by name or ID.
func (k *ConsumersCollection) Delete(userNameOrID string) error {
	if userNameOrID == "" {
		return errIDRequired
	}

	txn := k.db.Txn(true)
	defer txn.Abort()

	err := deleteConsumer(txn, userNameOrID)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

// GetAll gets a consumer by name or ID.
func (k *ConsumersCollection) GetAll() ([]*Consumer, error) {
	txn := k.db.Txn(false)
	defer txn.Abort()

	iter, err := txn.Get(consumerTableName, all, true)
	if err != nil {
		return nil, err
	}

	var res []*Consumer
	for el := iter.Next(); el != nil; el = iter.Next() {
		s, ok := el.(*Consumer)
		if !ok {
			panic(unexpectedType)
		}
		res = append(res, &Consumer{Consumer: *s.DeepCopy()})
	}
	txn.Commit()
	return res, nil
}