-
Notifications
You must be signed in to change notification settings - Fork 53
/
scope.go
116 lines (99 loc) · 3.42 KB
/
scope.go
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
// Copyright 2019 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package spoe
import (
"fmt"
"github.com/go-openapi/strfmt"
conf "github.com/haproxytech/client-native/v5/configuration"
"github.com/haproxytech/client-native/v5/models"
)
// GetScopes returns configuration version and an array of
// configured scopes Returns error on fail.
func (c *SingleSpoe) GetScopes(transactionID string) (int64, models.SpoeScopes, error) {
p, err := c.GetParser(transactionID)
if err != nil {
return 0, nil, err
}
v, err := c.GetVersion(transactionID)
if err != nil {
return 0, nil, err
}
sNames := p.Parsers
scopes := models.SpoeScopes{}
for name := range sNames {
if p.IsScope(name) {
name := models.SpoeScope(name)
scopes = append(scopes, name)
}
}
return v, scopes, nil
}
// GetScope returns configuration version and a requested scope
// Returns error on fail or if scope does not exist.
func (c *SingleSpoe) GetScope(name, transactionID string) (int64, *models.SpoeScope, error) {
v, scopes, err := c.GetScopes(transactionID)
if err != nil {
return 0, nil, err
}
for _, s := range scopes {
if s == models.SpoeScope(name) {
return v, &s, nil
}
}
return v, nil, conf.NewConfError(conf.ErrObjectDoesNotExist, fmt.Sprintf("scope %s does not exist", name))
}
// DeleteScope deletes a scope in configuration
// One of version or transactionID is mandatory
// Returns error on fail, nil on success.
func (c *SingleSpoe) DeleteScope(name, transactionID string, version int64) error {
p, t, err := c.loadDataForChange(transactionID, version)
if err != nil {
return err
}
_, _, err = c.GetScope(name, transactionID)
if err != nil {
e := conf.NewConfError(conf.ErrObjectDoesNotExist, fmt.Sprintf("scope %s does not exist", name))
return c.Transaction.HandleError(name, "", "", t, transactionID == "", e)
}
if err := p.ScopeDelete(name); err != nil {
return c.Transaction.HandleError(name, "", "", t, transactionID == "", err)
}
return c.Transaction.SaveData(p, t, transactionID == "")
}
// CreateScope creates a scope in configuration
// One of version or transactionID is mandatory
// Returns error on fail, nil on success.
func (c *SingleSpoe) CreateScope(data *models.SpoeScope, transactionID string, version int64) error {
if c.Transaction.UseModelsValidation {
validationErr := data.Validate(strfmt.Default)
if validationErr != nil {
return conf.NewConfError(conf.ErrValidationError, validationErr.Error())
}
}
p, t, err := c.loadDataForChange(transactionID, version)
if err != nil {
return err
}
scope := string(*data)
_, s, _ := c.GetScope(scope, transactionID)
if s != nil {
e := conf.NewConfError(conf.ErrObjectAlreadyExists, fmt.Sprintf("scope %s already exists", scope))
return c.Transaction.HandleError(scope, "", "", t, transactionID == "", e)
}
if err := p.ScopeCreate(scope); err != nil {
return err
}
return c.Transaction.SaveData(p, t, transactionID == "")
}