Skip to content

Commit

Permalink
updateMetaData: change pointer parameters into pass-by value, avoid l…
Browse files Browse the repository at this point in the history
…ogging, better error handling
  • Loading branch information
consolethinks committed Aug 13, 2024
1 parent 0afb74f commit 3b19c3f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 100 deletions.
103 changes: 47 additions & 56 deletions datasetIngestor/updateMetaData.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,61 @@ package datasetIngestor
import (
"encoding/json"
"io"
"log"
"net/http"
"strings"
"time"

"github.com/fatih/color"
)

const (
Classification = "classification"
AVLow = "AV=low"
AVMedium = "AV=medium"
INMedium = "IN=medium"
COLow = "CO=low"
AVLow = "AV=low"
AVMedium = "AV=medium"
INMedium = "IN=medium"
COLow = "CO=low"
)

/*
getAVFromPolicy retrieves the AV (?) from a policy.
getAVFromPolicy retrieves the AV (?) from a policy.
Parameters:
- client: An HTTP client used to send requests.
- APIServer: The URL of the API server.
- user: A map containing user information. It should contain an "accessToken" key.
- owner: The owner of the policy.
The function constructs a URL using the APIServer, owner, and user's access token, and sends a GET request to this URL.
If the response status code is 200, it reads the response body and unmarshals it into a slice of Policy structs.
If there are no policies available for the owner, it logs a warning and sets the level to "low".
The function constructs a URL using the APIServer, owner, and user's access token, and sends a GET request to this URL.
If the response status code is 200, it reads the response body and unmarshals it into a slice of Policy structs.
If there are no policies available for the owner, it logs a warning and sets the level to "low".
If there are policies available, it sets the level to the TapeRedundancy of the first policy.
Returns:
- level: The TapeRedundancy level of the first policy if available, otherwise "low".
*/
func getAVFromPolicy(client *http.Client, APIServer string, user map[string]string, owner string) (level string) {
level = "low" // default value

var myurl = APIServer + "/Policies?filter=%7B%22where%22%3A%7B%22ownerGroup%22%3A%22" + owner + "%22%7D%7D&access_token=" + user["accessToken"]
resp, _ := client.Get(myurl)
resp, err := client.Get(myurl)
if err != nil {
return level
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return level
}

level = "low"
if resp.StatusCode == 200 {
body, _ := io.ReadAll(resp.Body)
type Policy struct {
TapeRedundancy string
AutoArchive bool
}
var policies []Policy
_ = json.Unmarshal(body, &policies)
if len(policies) == 0 {
color.Set(color.FgYellow)
log.Printf("No Policy available for owner %v\n", owner)
color.Set(color.FgGreen)
} else {
level = policies[0].TapeRedundancy
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return level
}
type Policy struct {
TapeRedundancy string
AutoArchive bool
}
var policies []Policy
_ = json.Unmarshal(body, &policies)
if len(policies) > 0 {
level = policies[0].TapeRedundancy
}
return level
}
Expand Down Expand Up @@ -88,67 +89,63 @@ The function logs a message each time it updates a field. If the "classification
The function does not return a value.
*/
func UpdateMetaData(client *http.Client, APIServer string, user map[string]string,
originalMap map[string]string, metaDataMap map[string]interface{}, startTime time.Time, endTime time.Time, owner string, tapecopies *int) {
originalMap map[string]string, metaDataMap map[string]interface{}, startTime time.Time, endTime time.Time, owner string, tapecopies int) {
updateFieldIfDummy(metaDataMap, originalMap, "creationTime", DUMMY_TIME, startTime)
updateFieldIfDummy(metaDataMap, originalMap, "ownerGroup", DUMMY_OWNER, owner)

if metaDataMap["type"] == "raw" {
updateFieldIfDummy(metaDataMap, originalMap, "endTime", DUMMY_TIME, endTime)
}

addFieldIfNotExists(metaDataMap, "license", "CC BY-SA 4.0")
addFieldIfNotExists(metaDataMap, "isPublished", false)

updateClassificationField(client, APIServer, user, metaDataMap, tapecopies)
}

func updateFieldIfDummy(metaDataMap map[string]interface{}, originalMap map[string]string, fieldName string, dummyValue interface{}, newValue interface{}) {
if metaDataMap[fieldName] == dummyValue {
originalMap[fieldName] = metaDataMap[fieldName].(string)
metaDataMap[fieldName] = newValue
log.Printf("%s field added: %v\n", fieldName, metaDataMap[fieldName])
//log.Printf("%s field added: %v\n", fieldName, metaDataMap[fieldName])
}
}

func addFieldIfNotExists(metaDataMap map[string]interface{}, fieldName string, value interface{}) {
if _, ok := metaDataMap[fieldName]; !ok {
metaDataMap[fieldName] = value
log.Printf("%s field added: %v\n", fieldName, metaDataMap[fieldName])
//log.Printf("%s field added: %v\n", fieldName, metaDataMap[fieldName])
}
}

func updateClassificationField(client *http.Client, APIServer string, user map[string]string, metaDataMap map[string]interface{}, tapecopies *int) {
func updateClassificationField(client *http.Client, APIServer string, user map[string]string, metaDataMap map[string]interface{}, tapecopies int) {
if _, ok := metaDataMap[Classification]; !ok {
addDefaultClassification(client, APIServer, user, metaDataMap)
}
if *tapecopies == 1 || *tapecopies == 2 {

if tapecopies == 1 || tapecopies == 2 {
updateAVField(metaDataMap, tapecopies)
}

if strings.Contains(metaDataMap[Classification].(string), AVMedium) {
logAVMediumMessage()
}
}

func addDefaultClassification(client *http.Client, APIServer string, user map[string]string, metaDataMap map[string]interface{}) {
metaDataMap[Classification] = INMedium + ",AV=" + getAVFromPolicy(client, APIServer, user, metaDataMap["ownerGroup"].(string)) + "," + COLow
log.Printf("classification field added: %v\n", metaDataMap[Classification])
//log.Printf("classification field added: %v\n", metaDataMap[Classification])
}

func updateAVField(metaDataMap map[string]interface{}, tapecopies *int) {
func updateAVField(metaDataMap map[string]interface{}, tapecopies int) {
av := getAVValue(tapecopies)
if _, ok := metaDataMap[Classification]; ok {
newresult := getUpdatedClassification(metaDataMap, av)
metaDataMap[Classification] = strings.Join(newresult, ",")
} else {
metaDataMap[Classification] = INMedium + "," + av + "," + COLow
}
log.Printf("classification field adjusted: %s\n", metaDataMap[Classification])
//log.Printf("classification field adjusted: %s\n", metaDataMap[Classification])
}

func getAVValue(tapecopies *int) string {
if *tapecopies == 1 {
func getAVValue(tapecopies int) string {
if tapecopies == 1 {
return AVLow
}
return AVMedium
Expand All @@ -162,10 +159,10 @@ func getUpdatedClassification(metaDataMap map[string]interface{}, av string) []s
for _, element := range result {
if element == "" {
continue
}
if strings.HasPrefix(element, "AV=") {
newresult = append(newresult, av)
found = true
}
if strings.HasPrefix(element, "AV=") {
newresult = append(newresult, av)
found = true
} else {
newresult = append(newresult, element)
}
Expand All @@ -175,9 +172,3 @@ func getUpdatedClassification(metaDataMap map[string]interface{}, av string) []s
}
return newresult
}

func logAVMediumMessage() {
color.Set(color.FgYellow)
log.Printf("Note: this dataset, if archived, will be copied to two tape copies")
color.Unset()
}
Loading

0 comments on commit 3b19c3f

Please sign in to comment.