rimashah25 commented on code in PR #6544:
URL: https://github.com/apache/trafficcontrol/pull/6544#discussion_r851334706
##########
traffic_ops/traffic_ops_golang/dbhelpers/db_helpers.go:
##########
@@ -1979,3 +1979,114 @@ WHERE server.id = $2;`
return nil
}
+
+// GetCommonServerPropertiesFromV4 converts CommonServerPropertiesV40 to
CommonServerProperties struct.
+func GetCommonServerPropertiesFromV4(s tc.ServerV40, tx *sql.Tx)
(tc.CommonServerProperties, error) {
+ var id int
+ var desc string
+ if len(s.ProfileNames) == 0 {
+ return tc.CommonServerProperties{}, fmt.Errorf("profileName
doesnot exist in server: %v", *s.ID)
+ }
+ rows, err := tx.Query("SELECT id, description from profile WHERE
name=$1", (s.ProfileNames)[0])
+ if err != nil {
+ return tc.CommonServerProperties{}, fmt.Errorf("querying
profile id and description by profile_name: %w", err)
+ }
+ defer log.Close(rows, "closing rows in GetCommonServerPropertiesFromV4")
+
+ for rows.Next() {
+ if err := rows.Scan(&id, &desc); err != nil {
+ return tc.CommonServerProperties{},
fmt.Errorf("scanning profile: %w", err)
+ }
+ }
+
+ return tc.CommonServerProperties{
+ Cachegroup: s.Cachegroup,
+ CachegroupID: s.CachegroupID,
+ CDNID: s.CDNID,
+ CDNName: s.CDNName,
+ DeliveryServices: s.DeliveryServices,
+ DomainName: s.DomainName,
+ FQDN: s.FQDN,
+ FqdnTime: s.FqdnTime,
+ GUID: s.GUID,
+ HostName: s.HostName,
+ HTTPSPort: s.HTTPSPort,
+ ID: s.ID,
+ ILOIPAddress: s.ILOIPAddress,
+ ILOIPGateway: s.ILOIPGateway,
+ ILOIPNetmask: s.ILOIPNetmask,
+ ILOPassword: s.ILOPassword,
+ ILOUsername: s.ILOUsername,
+ LastUpdated: s.LastUpdated,
+ MgmtIPAddress: s.MgmtIPAddress,
+ MgmtIPGateway: s.MgmtIPGateway,
+ MgmtIPNetmask: s.MgmtIPNetmask,
+ OfflineReason: s.OfflineReason,
+ Profile: &(s.ProfileNames)[0],
+ ProfileDesc: &desc,
+ ProfileID: &id,
+ PhysLocation: s.PhysLocation,
+ PhysLocationID: s.PhysLocationID,
+ Rack: s.Rack,
+ RevalPending: s.RevalPending,
+ Status: s.Status,
+ StatusID: s.StatusID,
+ TCPPort: s.TCPPort,
+ Type: s.Type,
+ TypeID: s.TypeID,
+ UpdPending: s.UpdPending,
+ XMPPID: s.XMPPID,
+ XMPPPasswd: s.XMPPPasswd,
+ }, nil
+}
+
+// UpdateServerProfilesForV4 updates server_profile table via update function
for APIv4.
+func UpdateServerProfilesForV4(id int, profile []string, tx *sql.Tx) error {
+ profileNames := make([]string, 0, len(profile))
+ priority := make([]int, 0, len(profile))
+ for i, _ := range profile {
+ priority = append(priority, i)
+ }
+
+ //Delete existing rows from server_profile to get the priority correct
for profile_name changes
+ _, err := tx.Exec("DELETE FROM server_profile WHERE server=$1", id)
+ if err != nil {
+ return fmt.Errorf("updating server_profile by server id: %d,
error: %w", id, err)
+ }
+
+ query := `WITH inserted AS (
+ INSERT INTO server_profile
+ SELECT $1, "profile_name", "priority"
+ FROM UNNEST($2::text[], $3::int[]) AS tmp("profile_name",
"priority")
+ RETURNING profile_name, priority
+ )
+ SELECT ARRAY_AGG(profile_name)
+ FROM (
+ SELECT profile_name
+ FROM inserted
+ ORDER BY priority ASC
+ ) AS returned(profile_name)
+`
+ err = tx.QueryRow(query, id, pq.Array(profile),
pq.Array(priority)).Scan(pq.Array(&profileNames))
+ if err != nil {
+ return fmt.Errorf("failed to insert/read into/from
server_profile table, %w", err)
+ }
+ return nil
+}
+
+// UpdateServerProfileTableForV2V3 updates CommonServerPropertiesV40 struct
and server_profile table via Update (server) function for API v2/v3.
+func UpdateServerProfileTableForV2V3(id *int, newProfile *string, origProfile
string, tx *sql.Tx) ([]string, error) {
+ var profileName []string
+ query := `UPDATE server_profile SET profile_name=$1 WHERE server=$2 AND
profile_name=$3`
Review Comment:
Ok, I should rephrase my earlier comment.
So, if you make changes (PUT) via API v3/v2 to an existing server created
via POST API v3/v2, then it will update the profile with priority `0` since
v3/v2 allows 1 profile to a server.
But if you create server via API v4 (multiple profile) and change it via API
v2/v3, then the only change one shall see is to the profile with priority `0`
but other profile will remain intact.
So, in your eg:
Example 1:
POST API v2/v3, profile `a`
PUT API v2/v3, profile `b`, then `a` is replaced with `b` OR
PUT API v4, profile `["a", "b", "c"]`, then `a` is replaced with `["a", "b",
"c"]`
Example 2:
POST API v4, profile `["a", "b"]`
PUT API v2/v3, profile `c`, then `a` is replaced with `c` as `["c", "b"]` OR
PUT API v4, profile `["b", "c"]`, then `["a", "b"]` is replaced with `["b",
"c"]`
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]