This is an automated email from the ASF dual-hosted git repository.

ocket8888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new e82248015f add missing lastUpdated prop to PUT /roles (#7402)
e82248015f is described below

commit e82248015f35d383e921f084563c6dfd30fdf7b8
Author: Kunal Kundu <[email protected]>
AuthorDate: Thu Mar 16 00:12:34 2023 +0530

    add missing lastUpdated prop to PUT /roles (#7402)
    
    * add missing lastUpdated prop to PUT /roles
    
    * update docs for /roles endpoints with lastUpdated
    
    * use errors.Is for comparing errors
---
 docs/source/api/v4/roles.rst                 |  9 +++++++--
 docs/source/api/v5/roles.rst                 |  9 +++++++--
 traffic_ops/traffic_ops_golang/role/roles.go | 23 ++++++++---------------
 3 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/docs/source/api/v4/roles.rst b/docs/source/api/v4/roles.rst
index a0219bbd9c..137824acbe 100644
--- a/docs/source/api/v4/roles.rst
+++ b/docs/source/api/v4/roles.rst
@@ -67,6 +67,7 @@ Response Structure
 :permissions:  An array of the names of the Permissions given to this 
:term:`Role`
 :description:  A description of the :term:`Role`
 :name:         The name of the :term:`Role`
+:lastUpdated: The date and time at which this :term:`Role` was last updated, 
in :rfc:`3339` format
 
 .. code-block:: http
        :caption: Response Example
@@ -171,6 +172,7 @@ Response Structure
 
 :description: A description of the :term:`Role`
 :name:        The name of the :term:`Role`
+:lastUpdated: The date and time at which this :term:`Role` was last updated, 
in :rfc:`3339` format
 
 .. code-block:: http
        :caption: Response Example
@@ -194,7 +196,8 @@ Response Structure
        "response": {
                "name": "test",
                "description": "quest",
-               "permissions": null
+               "permissions": null,
+               "lastUpdated": "2021-05-03T14:50:18.93513-06:00"
        }}
 
 ``PUT``
@@ -249,6 +252,7 @@ Response Structure
 
 :description: A description of the :term:`Role`
 :name:        The name of the :term:`Role`
+:lastUpdated: The date and time at which this :term:`Role` was last updated, 
in :rfc:`3339` format
 
 .. code-block:: http
        :caption: Response Example
@@ -275,7 +279,8 @@ Response Structure
                "response": {
                        "name": "test",
                        "description": "quest_updated",
-                       "permissions": null
+                       "permissions": null,
+                       "lastUpdated": "2021-05-03T14:50:18.93513-06:00"
                }
        }
 
diff --git a/docs/source/api/v5/roles.rst b/docs/source/api/v5/roles.rst
index 48c208ed0b..ef915b4058 100644
--- a/docs/source/api/v5/roles.rst
+++ b/docs/source/api/v5/roles.rst
@@ -67,6 +67,7 @@ Response Structure
 :permissions:  An array of the names of the Permissions given to this 
:term:`Role`
 :description:  A description of the :term:`Role`
 :name:         The name of the :term:`Role`
+:lastUpdated: The date and time at which this :term:`Role` was last updated, 
in :rfc:`3339` format
 
 .. code-block:: http
        :caption: Response Example
@@ -171,6 +172,7 @@ Response Structure
 
 :description: A description of the :term:`Role`
 :name:        The name of the :term:`Role`
+:lastUpdated: The date and time at which this :term:`Role` was last updated, 
in :rfc:`3339` format
 
 .. code-block:: http
        :caption: Response Example
@@ -194,7 +196,8 @@ Response Structure
        "response": {
                "name": "test",
                "description": "quest",
-               "permissions": null
+               "permissions": null,
+               "lastUpdated": "2021-05-03T14:50:18.93513-06:00"
        }}
 
 ``PUT``
@@ -222,6 +225,7 @@ Request Structure
 
 :description: A helpful description of the :term:`Role`'s purpose.
 :name:        The new name of the :term:`Role`
+:lastUpdated: The date and time at which this :term:`Role` was last updated, 
in :rfc:`3339` format
 
 .. code-block:: http
        :caption: Request Example
@@ -275,7 +279,8 @@ Response Structure
                "response": {
                        "name": "test",
                        "description": "quest_updated",
-                       "permissions": null
+                       "permissions": null,
+                       "lastUpdated": "2021-05-03T14:50:18.93513-06:00"
                }
        }
 
diff --git a/traffic_ops/traffic_ops_golang/role/roles.go 
b/traffic_ops/traffic_ops_golang/role/roles.go
index e39317e7dc..e71accc4b7 100644
--- a/traffic_ops/traffic_ops_golang/role/roles.go
+++ b/traffic_ops/traffic_ops_golang/role/roles.go
@@ -20,6 +20,7 @@ package role
  */
 
 import (
+       "database/sql"
        "encoding/json"
        "errors"
        "fmt"
@@ -394,24 +395,15 @@ func Update(w http.ResponseWriter, r *http.Request) {
                api.HandleErr(w, r, tx, http.StatusPreconditionFailed, 
api.ResourceModifiedError, nil)
                return
        }
-       rows, err := tx.Query(updateRoleQuery(), roleV4.Name, 
roleV4.Description, currentRoleName)
+       err = tx.QueryRow(updateRoleQuery(), roleV4.Name, roleV4.Description, 
currentRoleName).Scan(&roleV4.LastUpdated)
        if err != nil {
-               usrErr, sysErr, code := api.ParseDBError(err)
-               api.HandleErr(w, r, tx, code, usrErr, fmt.Errorf("updating 
role: %w", sysErr))
-               return
-       }
-       defer rows.Close()
-       if !rows.Next() {
-               api.HandleErr(w, r, tx, http.StatusNotFound, errors.New("no 
such role"), nil)
-               return
-       }
-       var lastUpdated time.Time
-       for rows.Next() {
-               if err := rows.Scan(&lastUpdated); err != nil {
-                       api.HandleErr(w, r, tx, http.StatusInternalServerError, 
nil, fmt.Errorf("scanning lastUpdated from role update: %w", err))
+               if errors.Is(err, sql.ErrNoRows) {
+                       api.HandleErr(w, r, tx, http.StatusNotFound, 
errors.New("no such role"), nil)
                        return
                }
-               roleV4.LastUpdated = &lastUpdated
+               usrErr, sysErr, code := api.ParseDBError(err)
+               api.HandleErr(w, r, tx, code, usrErr, fmt.Errorf("updating role 
and scanning lastUpdated : %w", sysErr))
+               return
        }
 
        userErr, sysErr, errCode = deleteRoleCapabilityAssociations(inf.Tx, 
roleV4.Name)
@@ -430,6 +422,7 @@ func Update(w http.ResponseWriter, r *http.Request) {
                Name:        roleV4.Name,
                Permissions: roleV4.Permissions,
                Description: roleV4.Description,
+               LastUpdated: roleV4.LastUpdated,
        }
        api.WriteAlertsObj(w, r, http.StatusOK, alerts, roleResponse)
        changeLogMsg := fmt.Sprintf("ROLE: %s, ID: %d, ACTION: Updated Role", 
roleV4.Name, roleID)

Reply via email to