rawlinp commented on a change in pull request #3964: Add GET/POST/DELETE for server_server_capabilities URL: https://github.com/apache/trafficcontrol/pull/3964#discussion_r334639750
########## File path: traffic_ops/traffic_ops_golang/server/servers_server_capability.go ########## @@ -0,0 +1,167 @@ +package server + +/* + * 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" + + "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" +) + +const ( + ServerCapabilityQueryParam = "serverCapability" + ServerQueryParam = "serverId" + ServerHostNameQueryParam = "serverHostName" +) + +// we need a type alias to define functions on +type TOServerServerCapability struct { + api.APIInfoImpl `json:"-"` + tc.ServerServerCapability +} + +func (ssc *TOServerServerCapability) SetLastUpdated(t tc.TimeNoMod) { ssc.LastUpdated = &t } +func (ssc *TOServerServerCapability) NewReadObj() interface{} { + return &tc.ServerServerCapability{} +} +func (ssc *TOServerServerCapability) SelectQuery() string { return scSelectQuery() } +func (ssc *TOServerServerCapability) ParamColumns() map[string]dbhelpers.WhereColumnInfo { + return map[string]dbhelpers.WhereColumnInfo{ + ServerCapabilityQueryParam: dbhelpers.WhereColumnInfo{"sc.server_capability", nil}, + ServerQueryParam: dbhelpers.WhereColumnInfo{"s.id", api.IsInt}, + ServerHostNameQueryParam: dbhelpers.WhereColumnInfo{"s.host_name", nil}, + } + +} +func (ssc *TOServerServerCapability) DeleteQuery() string { return scDeleteQuery() } + +func (ssc TOServerServerCapability) GetKeyFieldsInfo() []api.KeyFieldInfo { + return []api.KeyFieldInfo{ + {ServerQueryParam, api.GetIntKey}, + {ServerCapabilityQueryParam, api.GetStringKey}, + } +} + +// Need to satisfy Identifier interface but is a no-op as path does not have Update +func (ssc TOServerServerCapability) GetKeys() (map[string]interface{}, bool) { + if ssc.ServerID == nil { + return map[string]interface{}{ServerQueryParam: 0}, false + } + if ssc.ServerCapability == nil { + return map[string]interface{}{ServerCapabilityQueryParam: 0}, false + } + return map[string]interface{}{ + ServerQueryParam: *ssc.ServerID, + ServerCapabilityQueryParam: *ssc.ServerCapability, + }, true +} + +func (ssc *TOServerServerCapability) SetKeys(keys map[string]interface{}) { + sID, _ := keys[ServerQueryParam].(int) + ssc.ServerID = &sID + + sc, _ := keys[ServerCapabilityQueryParam].(string) + ssc.ServerCapability = &sc +} + +func (ssc *TOServerServerCapability) GetAuditName() string { + if ssc.ServerCapability != nil { + return *ssc.ServerCapability + } + return "unknown" +} + +func (ssc *TOServerServerCapability) GetType() string { + return "server server_capability" +} + +// Validate fulfills the api.Validator interface +func (ssc TOServerServerCapability) Validate() error { + errs := validation.Errors{ + ServerQueryParam: validation.Validate(ssc.ServerID, validation.Required), + ServerCapabilityQueryParam: validation.Validate(ssc.ServerCapability, validation.Required), + } + + return util.JoinErrs(tovalidate.ToErrors(errs)) +} + +func (ssc *TOServerServerCapability) Update() (error, error, int) { + return nil, nil, http.StatusNotImplemented +} + +func (ssc *TOServerServerCapability) Read() ([]interface{}, error, error, int) { + return api.GenericRead(ssc) +} + +func (ssc *TOServerServerCapability) Delete() (error, error, int) { + return api.GenericDelete(ssc) +} + +func (ssc *TOServerServerCapability) Create() (error, error, int) { + resultRows, err := ssc.APIInfo().Tx.NamedQuery(scInsertQuery(), ssc) Review comment: Yeah I think maybe we should hold off and possibly do it in a follow-up PR. Seems doable, but the other "many-to-many" API we have is "deliveryservice-servers" (assigning DSes to servers). That one actually has two endpoints that basically do the same thing. One _replaces_ the association by deleting the association first, and the other seems to return an error if the association is already there. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services