You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/10/08 16:51:38 UTC

[GitHub] [trafficcontrol] awilliams commented on a change in pull request #3966: Add server capabilities API

awilliams commented on a change in pull request #3966: Add server capabilities API
URL: https://github.com/apache/trafficcontrol/pull/3966#discussion_r332611770
 
 

 ##########
 File path: traffic_ops/traffic_ops_golang/servercapability/servercapability.go
 ##########
 @@ -0,0 +1,100 @@
+package servercapability
+
+/*
+ * 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 (
+	"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"
+)
+
+type TOServerCapability struct {
+	api.APIInfoImpl `json:"-"`
+	tc.ServerCapability
+}
+
+func (v *TOServerCapability) SetLastUpdated(t tc.TimeNoMod) { v.LastUpdated = &t }
+func (v *TOServerCapability) InsertQuery() string           { return insertQuery() }
+func (v *TOServerCapability) NewReadObj() interface{}       { return &tc.ServerCapability{} }
+func (v *TOServerCapability) SelectQuery() string           { return selectQuery() }
+func (v *TOServerCapability) ParamColumns() map[string]dbhelpers.WhereColumnInfo {
+	return map[string]dbhelpers.WhereColumnInfo{
+		"name": {"sc.name", nil},
+	}
+}
+func (v *TOServerCapability) DeleteQuery() string { return deleteQuery() }
+
+func (v TOServerCapability) GetKeyFieldsInfo() []api.KeyFieldInfo {
+	return []api.KeyFieldInfo{{"name", api.GetStringKey}}
+}
+
+//Implementation of the Identifier, Validator interface functions
+func (v TOServerCapability) GetKeys() (map[string]interface{}, bool) {
+	return map[string]interface{}{"name": v.Name}, true
+}
+
+func (v *TOServerCapability) SetKeys(keys map[string]interface{}) {
+	v.Name, _ = keys["name"].(string)
+}
+
+func (v *TOServerCapability) GetAuditName() string {
+	return v.Name
+}
+
+func (v *TOServerCapability) GetType() string {
+	return "server capability"
+}
+
+func (v *TOServerCapability) Validate() error {
+	errs := validation.Errors{
+		"name": validation.Validate(v.Name, validation.Required),
+	}
+	return util.JoinErrs(tovalidate.ToErrors(errs))
+}
+
+func (v *TOServerCapability) Read() ([]interface{}, error, error, int) { return api.GenericRead(v) }
+func (v *TOServerCapability) Create() (error, error, int)              { return api.GenericCreateNameBasedID(v) }
+func (v *TOServerCapability) Delete() (error, error, int)              { return api.GenericDelete(v) }
+
+func selectQuery() string {
+	return `SELECT
+name,
+last_updated
+FROM server_capability sc`
+}
+
+func insertQuery() string {
+	query := `INSERT INTO server_capability (
+name
+) VALUES (
+:name
+) RETURNING last_updated`
+	return query
+}
+
+func deleteQuery() string {
+	query := `DELETE FROM server_capability
+WHERE name=:name`
+	return query
+}
 
 Review comment:
   I see that this follows an idiom used [elsewhere in the project](https://github.com/apache/trafficcontrol/blob/6ea4a3f53c104b2c3dfa9dc404d3c0f70d00c56a/traffic_ops/traffic_ops_golang/division/divisions.go#L108-L125), ie defining functions which are then used within methods.
   
   For consistency would it be preferable to either define a `query` variable that is then returned, or directly return a string literal in all cases?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services