Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/other changes #96

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cmd/commands/datasetIngestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ For Windows you need instead to specify -user username:password on the command l
} else {
// TODO: change tapecopies param type of UpadateMetaData from pointer to regular int
// (it's not changed within the function)
datasetIngestor.UpdateMetaData(client, APIServer, user, originalMap, metaDataMap, startTime, endTime, owner, &tapecopies)
datasetIngestor.UpdateMetaData(client, APIServer, user, originalMap, metaDataMap, startTime, endTime, owner, tapecopies)
pretty, _ := json.MarshalIndent(metaDataMap, "", " ")

log.Printf("Updated metadata object:\n%s\n", pretty)
Expand Down Expand Up @@ -317,7 +317,10 @@ For Windows you need instead to specify -user username:password on the command l
metaDataMap["datasetlifecycle"].(map[string]interface{})["archiveStatusMessage"] = "datasetCreated"
metaDataMap["datasetlifecycle"].(map[string]interface{})["archivable"] = true
}
datasetId := datasetIngestor.IngestDataset(client, APIServer, metaDataMap, fullFileArray, user)
datasetId, err := datasetIngestor.IngestDataset(client, APIServer, metaDataMap, fullFileArray, user)
if err != nil {
log.Fatalf("ingestion returned an error: %v\n", err)
}
// add attachment optionally
if addAttachment != "" {
datasetIngestor.AddAttachment(client, APIServer, datasetId, metaDataMap, user["accessToken"], addAttachment, addCaption)
Expand Down
24 changes: 14 additions & 10 deletions datasetIngestor/createDatasetEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package datasetIngestor
import (
"bytes"
"encoding/json"
"log"
"errors"
"fmt"
"net/http"
)

Expand All @@ -26,12 +27,13 @@ Returns:
- A string representing the "pid" of the newly created dataset. If the function encounters an error, it will not return a value.

Note: This function will terminate the program if it encounters an error, such as a failure to serialize the metaDataMap, a failure to send the HTTP request, a non-200 response from the server, or an unrecognized dataset type.
Note 2: This function is unused in cmd
*/
func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[string]interface{}, accessToken string) (datasetId string) {
func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[string]interface{}, accessToken string) (datasetId string, err error) {
// assemble json structure
bm, err := json.Marshal(metaDataMap)
if err != nil {
log.Fatal("Connect serialize meta data map:", metaDataMap)
return "", fmt.Errorf("couldn't marshal metadata map: %v", metaDataMap)
}
if val, ok := metaDataMap["type"]; ok {
dstype := val.(string)
Expand All @@ -47,15 +49,18 @@ func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[s
} else if dstype == "base" {
myurl = APIServer + "/Datasets"
} else {
log.Fatal("Unknown dataset type encountered:", dstype)
return "", fmt.Errorf("unknown dataset type encountered: %v", dstype)
}

req, err := http.NewRequest("POST", myurl+"?access_token="+accessToken, bytes.NewBuffer(bm))
if err != nil {
return datasetId, err
}
req.Header.Set("Content-Type", "application/json")
//fmt.Printf("request to message broker:%v\n", req)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
Expand All @@ -67,15 +72,14 @@ func CreateDatasetEntry(client *http.Client, APIServer string, metaDataMap map[s
var d PidType
err := decoder.Decode(&d)
if err != nil {
log.Fatal("Could not decode pid from dataset entry:", err)
return "", fmt.Errorf("could not decode pid from dataset entry: %v", err)
}
// fmtlog.Printf("Extracted pid:%s", d.Pid)
return d.Pid
return d.Pid, nil
} else {
log.Fatalf("CreateDatasetEntry:Failed to create new dataset: status code %v\n", resp.StatusCode)
return "", fmt.Errorf("createDatasetEntry:Failed to create new dataset: status code %v", resp.StatusCode)
}
} else {
log.Fatal("Type of dataset not defined:")
return "", errors.New("type of dataset not defined")
}
return "This should never happen"
}
21 changes: 12 additions & 9 deletions datasetIngestor/createDatasetEntry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ import (

func TestCreateDatasetEntry(t *testing.T) {
var capturedPath string

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{"pid": "1234"}`))
capturedPath = req.URL.Path
}))
// Close the server when test finishes
defer server.Close()

// Create a map for the metaData
metaDataMap := map[string]interface{}{
"type": "raw",
"type": "raw",
}

// Create a client
client := server.Client()

// Call the function
datasetId := CreateDatasetEntry(client, server.URL, metaDataMap, "testToken")

datasetId, err := CreateDatasetEntry(client, server.URL, metaDataMap, "testToken")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

// Check the returned datasetId
if datasetId != "1234" {
t.Errorf("Expected datasetId to be '1234', but got '%s'", datasetId)
}

// Check the URL based on the type field
expectedPath := ""
switch metaDataMap["type"].(string) {
Expand All @@ -43,7 +46,7 @@ func TestCreateDatasetEntry(t *testing.T) {
case "base":
expectedPath = "/Datasets"
}

// Check the URL
if capturedPath != expectedPath {
t.Errorf("Expected path to be '%s', but got '%s'", expectedPath, capturedPath)
Expand Down
12 changes: 7 additions & 5 deletions datasetIngestor/deleteDatasetEntry.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package datasetIngestor

import (
"log"
"net/http"
"strings"
)

func DeleteDatasetEntry(client *http.Client, APIServer string, datasetId string, accessToken string) {
// note: this function is unused in cmd after a change in datasetIngestor command
func DeleteDatasetEntry(client *http.Client, APIServer string, datasetId string, accessToken string) error {
req, err := http.NewRequest("DELETE", APIServer+"/Datasets/"+strings.Replace(datasetId, "/", "%2F", 1)+"?access_token="+accessToken, nil)
// fmt.Printf("Request:%v\n",req)
if err != nil {
return err
}
resp, err := client.Do(req)
// fmt.Printf("resp %v %v\n",resp.Body,resp.StatusCode)
if err != nil {
log.Fatal(err)
return err
}
defer resp.Body.Close()
return nil
}
3 changes: 0 additions & 3 deletions datasetIngestor/resetUpdateMetaData.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package datasetIngestor

import (
)

func ResetUpdatedMetaData(originalMap map[string]string, metaDataMap map[string]interface{}) {
for k, v := range originalMap {
metaDataMap[k] = v
Expand Down
19 changes: 9 additions & 10 deletions datasetIngestor/sendFilesReadyCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package datasetIngestor
import (
"bytes"
"encoding/json"
"io"
"log"
"fmt"
"net/http"
"strings"
)
Expand All @@ -25,7 +24,7 @@ The URL for the request is constructed using the APIServer and datasetId paramet
If the request is successful (HTTP status code 200), the function logs a success message along with the response body.
If the request fails, the function logs a failure message along with the status code and metadata map.
*/
func MarkFilesReady(client *http.Client, APIServer string, datasetId string, user map[string]string) {
func MarkFilesReady(client *http.Client, APIServer string, datasetId string, user map[string]string) error {
var metaDataMap = map[string]interface{}{}
metaDataMap["datasetlifecycle"] = map[string]interface{}{}
metaDataMap["datasetlifecycle"].(map[string]interface{})["archiveStatusMessage"] = "datasetCreated"
Expand All @@ -36,17 +35,17 @@ func MarkFilesReady(client *http.Client, APIServer string, datasetId string, use

myurl := APIServer + "/Datasets/" + strings.Replace(datasetId, "/", "%2F", 1) + "?access_token=" + user["accessToken"]
req, err := http.NewRequest("PUT", myurl, bytes.NewBuffer(cmm))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
//fmt.Printf("request to message broker:%v\n", req)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, _ := io.ReadAll(resp.Body)
log.Printf("Successfully updated %v\n", string(body))
} else {
log.Fatalf("SendFilesReadyCommand: Failed to update datasetLifecycle %v %v\n", resp.StatusCode, metaDataMap)
if resp.StatusCode != 200 {
return fmt.Errorf("failed to update datasetLifecycle %v %v", resp.StatusCode, metaDataMap)
}
return nil
}
6 changes: 5 additions & 1 deletion datasetIngestor/sendFilesReadyCommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ func TestSendFilesReadyCommand(t *testing.T) {
client := &http.Client{}

// Call the function
MarkFilesReady(client, server.URL, "testDatasetId", user)
err := MarkFilesReady(client, server.URL, "testDatasetId", user)
if err != nil {
// TODO: write cases that trigger errors maybe
t.Errorf("Error encountered: %v", err)
}
}
Loading