-
Notifications
You must be signed in to change notification settings - Fork 344
/
asns.go
206 lines (184 loc) · 5.85 KB
/
asns.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package asn
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import (
"errors"
"net/http"
"strconv"
"time"
"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/lib/go-tc/tovalidate"
"github.com/apache/trafficcontrol/lib/go-util"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
validation "github.com/go-ozzo/ozzo-validation"
)
// ASNsPrivLevel ...
const ASNsPrivLevel = 10
//we need a type alias to define functions on
type TOASNV11 struct {
api.APIInfoImpl `json:"-"`
tc.ASNNullable
}
func (v *TOASNV11) GetLastUpdated() (*time.Time, bool, error) {
return api.GetLastUpdated(v.APIInfo().Tx, *v.ID, "asn")
}
func (v *TOASNV11) SetLastUpdated(t tc.TimeNoMod) { v.LastUpdated = &t }
func (v *TOASNV11) InsertQuery() string { return insertQuery() }
func (v *TOASNV11) NewReadObj() interface{} { return &tc.ASNNullable{} }
func (v *TOASNV11) SelectQuery() string { return selectQuery() }
func (v *TOASNV11) ParamColumns() map[string]dbhelpers.WhereColumnInfo {
return map[string]dbhelpers.WhereColumnInfo{
"asn": dbhelpers.WhereColumnInfo{Column: "a.asn", Checker: api.IsInt},
"cachegroup": dbhelpers.WhereColumnInfo{Column: "c.id", Checker: api.IsInt},
"id": dbhelpers.WhereColumnInfo{Column: "a.id", Checker: api.IsInt},
"cachegroupName": dbhelpers.WhereColumnInfo{Column: "c.name"},
}
}
func (v *TOASNV11) UpdateQuery() string { return updateQuery() }
func (v *TOASNV11) DeleteQuery() string { return deleteQuery() }
func (asn TOASNV11) GetKeyFieldsInfo() []api.KeyFieldInfo {
return []api.KeyFieldInfo{{Field: "id", Func: api.GetIntKey}}
}
// func (asn TOASNV12) GetKeyFieldsInfo() []api.KeyFieldInfo { return TOASNV11(asn).GetKeyFieldsInfo() }
// Implementation of the Identifier, Validator interface functions
func (asn TOASNV11) GetKeys() (map[string]interface{}, bool) {
if asn.ID == nil {
return map[string]interface{}{"id": 0}, false
}
return map[string]interface{}{"id": *asn.ID}, true
}
func (asn *TOASNV11) SetKeys(keys map[string]interface{}) {
i, _ := keys["id"].(int) //this utilizes the non panicking type assertion, if the thrown away ok variable is false i will be the zero of the type, 0 here.
asn.ID = &i
}
func (asn TOASNV11) GetAuditName() string {
if asn.ASN != nil {
return strconv.Itoa(*asn.ASN)
}
if asn.ID != nil {
return strconv.Itoa(*asn.ID)
}
return "unknown"
}
func (asn TOASNV11) GetType() string {
return "asn"
}
func (asn TOASNV11) Validate() error {
errs := validation.Errors{
"asn": validation.Validate(asn.ASN, validation.NotNil, validation.Min(0)),
"cachegroupId": validation.Validate(asn.CachegroupID, validation.NotNil, validation.Min(0)),
}
return util.JoinErrs(tovalidate.ToErrors(errs))
}
func (as *TOASNV11) Create() (error, error, int) {
err := as.ASNExists(true)
if err != nil {
return err, nil, http.StatusBadRequest
}
return api.GenericCreate(as)
}
func (as *TOASNV11) Read(h http.Header, useIMS bool) ([]interface{}, error, error, int, *time.Time) {
api.DefaultSort(as.APIInfo(), "asn")
return api.GenericRead(h, as, useIMS)
}
func (v *TOASNV11) SelectMaxLastUpdatedQuery(where, orderBy, pagination, tableName string) string {
return `SELECT max(t) from (
SELECT max(a.last_updated) as t from asn a
JOIN
cachegroup c ON a.cachegroup = c.id ` + where + orderBy + pagination +
` UNION ALL
select max(last_updated) as t from last_deleted l where l.table_name='asn') as res`
}
func (as *TOASNV11) Update(h http.Header) (error, error, int) {
err := as.ASNExists(false)
if err != nil {
return err, nil, http.StatusBadRequest
}
return api.GenericUpdate(h, as)
}
func (as *TOASNV11) Delete() (error, error, int) { return api.GenericDelete(as) }
func (asn TOASNV11) ASNExists(create bool) error {
if asn.APIInfo() == nil || asn.APIInfo().Tx == nil {
return errors.New("couldn't perform check to see if asn number exists already")
}
if asn.ASN == nil || asn.CachegroupID == nil {
return errors.New("no asn or cachegroup ID specified")
}
query := `SELECT id from asn where asn=$1`
rows, err := asn.APIInfo().Tx.Query(query, *asn.ASN)
if err != nil {
return errors.New("selecting asns: " + err.Error())
}
defer rows.Close()
if rows.Next() {
if create {
return errors.New("an asn with the specified number already exists")
}
var v int
id := *asn.ID
err = rows.Scan(&v)
if err != nil {
return errors.New("couldn't check if this number exists")
}
if v != id {
return errors.New("another asn exists for this number")
}
}
return nil
}
func selectQuery() string {
return `
SELECT
a.id,
a.asn,
a.last_updated,
a.cachegroup AS cachegroup_id,
c.name AS cachegroup
FROM
asn a
JOIN
cachegroup c ON a.cachegroup = c.id
`
}
func insertQuery() string {
return `
INSERT INTO
asn (asn, cachegroup)
VALUES
(:asn, :cachegroup_id)
RETURNING id, last_updated
`
}
func updateQuery() string {
return `
UPDATE
asn
SET
asn = :asn,
cachegroup = :cachegroup_id
WHERE
id = :id
RETURNING
last_updated
`
}
func deleteQuery() string {
return `DELETE FROM asn WHERE id=:id`
}