jheitz200 commented on a change in pull request #4006: implement 
deliveryservice_server_capabilities api endpoint
URL: https://github.com/apache/trafficcontrol/pull/4006#discussion_r336736749
 
 

 ##########
 File path: 
traffic_ops/traffic_ops_golang/deliveryservice/deliveryservice_server_capabilities.go
 ##########
 @@ -0,0 +1,196 @@
+package deliveryservice
+
+/*
+ * 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 (
+       deliveryServiceQueryParam  = "deliveryServiceID"
+       serverCapabilityQueryParam = "serverCapability"
+       xmlIDQueryParam            = "xmlID"
+)
+
+// ServerCapability is a type alias to define functions on
+type ServerCapability struct {
+       api.APIInfoImpl `json:"-"`
+       tc.DeliveryServiceServerCapability
+}
+
+// SetLastUpdated implements the api.GenericCreator interfaces and
+// sets the timestamp on insert
+func (sc *ServerCapability) SetLastUpdated(t tc.TimeNoMod) { sc.LastUpdated = 
&t }
+
+// NewReadObj implements the api.GenericReader interfaces.
+func (sc *ServerCapability) NewReadObj() interface{} {
+       return &tc.DeliveryServiceServerCapability{}
+}
+
+// SelectQuery implements the api.GenericReader interface
+func (sc *ServerCapability) SelectQuery() string {
+       return `SELECT
+       sc.server_capability,
+       sc.deliveryservice_id,
+       ds.xml_id,
+       sc.last_updated
+       FROM deliveryservice_server_capability sc
+       JOIN deliveryservice ds ON ds.id = sc.deliveryservice_id`
+}
+
+// ParamColumns implements the api.GenericReader interface
+func (sc *ServerCapability) ParamColumns() 
map[string]dbhelpers.WhereColumnInfo {
+       return map[string]dbhelpers.WhereColumnInfo{
+               deliveryServiceQueryParam: dbhelpers.WhereColumnInfo{
+                       Column:  "sc.deliveryservice_id",
+                       Checker: api.IsInt,
+               },
+               xmlIDQueryParam: dbhelpers.WhereColumnInfo{
+                       Column:  "ds.xml_id",
+                       Checker: nil,
+               },
+               serverCapabilityQueryParam: dbhelpers.WhereColumnInfo{
+                       Column:  "sc.server_capability",
+                       Checker: nil,
+               },
+       }
+}
+
+// DeleteQuery implements the api.GenericDeleter interface
+func (sc *ServerCapability) DeleteQuery() string {
+       return `DELETE FROM deliveryservice_server_capability
+       WHERE deliveryservice_id = :deliveryservice_id AND server_capability = 
:server_capability`
+}
+
+// GetKeyFieldsInfo implements the api.Identifier interface
+func (sc ServerCapability) GetKeyFieldsInfo() []api.KeyFieldInfo {
+       return []api.KeyFieldInfo{
+               {
+                       Field: deliveryServiceQueryParam,
+                       Func:  api.GetIntKey,
+               },
+               {
+                       Field: xmlIDQueryParam,
+                       Func:  api.GetStringKey,
+               },
+               {
+                       Field: serverCapabilityQueryParam,
+                       Func:  api.GetStringKey,
+               },
+       }
+}
+
+// GetKeys implements the api.Identifier interface and is not needed
+// because Update is not available
+func (sc ServerCapability) GetKeys() (map[string]interface{}, bool) {
+       return nil, false
+}
+
+// SetKeys implements the api.Identifier interface and allows the
+// create handler to assign deliveryServiceID and serverCapability.
+func (sc *ServerCapability) SetKeys(keys map[string]interface{}) {
+       // this utilizes the non panicking type assertion, if the thrown
+       // away ok variable is false it will be the zero of the type
+       id, _ := keys[deliveryServiceQueryParam].(int)
+       sc.DeliveryServiceID = &id
+
+       capability, _ := keys[serverCapabilityQueryParam].(string)
+       sc.ServerCapability = &capability
+}
+
+// GetAuditName implements the api.Identifier interface and
+// returns the name of the object.
+func (sc *ServerCapability) GetAuditName() string {
+       if sc.ServerCapability != nil {
+               return *sc.ServerCapability
+       }
+       return "unknown"
+}
+
+// GetType implements the api.Identifier interface and
+// returns the name of the struct
+func (sc *ServerCapability) GetType() string {
+       return "deliveryservice.ServerCapability"
+}
+
+// Validate implements the api.Validator interface
+func (sc ServerCapability) Validate() error {
+       errs := validation.Errors{
+               deliveryServiceQueryParam:  
validation.Validate(sc.DeliveryServiceID, validation.Required),
+               serverCapabilityQueryParam: 
validation.Validate(sc.ServerCapability, validation.Required),
+       }
+
+       return util.JoinErrs(tovalidate.ToErrors(errs))
+}
+
+// Update implements the api.CRUDer interface
+func (sc *ServerCapability) Update() (error, error, int) {
+       return nil, nil, http.StatusNotImplemented
+}
+
+// Read implements the api.CRUDer interface
+func (sc *ServerCapability) Read() ([]interface{}, error, error, int) {
+       return api.GenericRead(sc)
 
 Review comment:
   @ocket8888 thanks for the comment, I wasnt familiar with this. I will update 
the code to check tenancy.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to