Skip to content

Commit

Permalink
Merge pull request #1114 from neicnordic/feature/sda-admin-dataset-user
Browse files Browse the repository at this point in the history
[sda-admin] Add user to create dataset command
  • Loading branch information
jbygdell authored Nov 15, 2024
2 parents f285d47 + bf0bf1a commit fc7c79c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions sda-admin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ sda-admin file set-accession -filepath /path/to/file.c4gh -user test-user@exampl

## Create a dataset from a list of accession IDs and a dataset ID

Use the following command to create a dataset `dataset001` from accession IDs `my-accession-id-1` and `my-accession-id-2`
Use the following command to create a dataset `dataset001` from accession IDs `my-accession-id-1` and `my-accession-id-2` for files that belongs to the user `[email protected]`

```sh
sda-admin dataset create -dataset-id dataset001 my-accession-id-1 my-accession-id-2
sda-admin dataset create -user [email protected] -dataset-id dataset001 my-accession-id-1 my-accession-id-2
```

## Release a dataset for downloading
Expand Down
4 changes: 3 additions & 1 deletion sda-admin/dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
type RequestBodyDataset struct {
AccessionIDs []string `json:"accession_ids"`
DatasetID string `json:"dataset_id"`
User string `json:"user"`
}

// Create creates a dataset from a list of accession IDs and a dataset ID.
func Create(apiURI, token, datasetID string, accessionIDs []string) error {
func Create(apiURI, token, datasetID, username string, accessionIDs []string) error {
parsedURL, err := url.Parse(apiURI)
if err != nil {
return err
Expand All @@ -25,6 +26,7 @@ func Create(apiURI, token, datasetID string, accessionIDs []string) error {
requestBody := RequestBodyDataset{
AccessionIDs: accessionIDs,
DatasetID: datasetID,
User: username,
}

jsonBody, err := json.Marshal(requestBody)
Expand Down
10 changes: 6 additions & 4 deletions sda-admin/dataset/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func TestCreate_Success(t *testing.T) {

expectedURL := "http://example.com/dataset/create"
token := "test-token"
user := "[email protected]"
datasetID := "dataset-123"
accessionIDs := []string{"accession-1", "accession-2"}
jsonBody := []byte(`{"accession_ids":["accession-1","accession-2"],"dataset_id":"dataset-123"}`)
jsonBody := []byte(`{"accession_ids":["accession-1","accession-2"],"dataset_id":"dataset-123","user":"[email protected]"}`)

mockHelpers.On("PostRequest", expectedURL, token, jsonBody).Return([]byte(`{}`), nil)

err := Create("http://example.com", token, datasetID, accessionIDs)
err := Create("http://example.com", token, datasetID, user, accessionIDs)
assert.NoError(t, err)
mockHelpers.AssertExpectations(t)
}
Expand All @@ -47,13 +48,14 @@ func TestCreate_PostRequestFailure(t *testing.T) {

expectedURL := "http://example.com/dataset/create"
token := "test-token"
user := "[email protected]"
datasetID := "dataset-123"
accessionIDs := []string{"accession-1", "accession-2"}
jsonBody := []byte(`{"accession_ids":["accession-1","accession-2"],"dataset_id":"dataset-123"}`)
jsonBody := []byte(`{"accession_ids":["accession-1","accession-2"],"dataset_id":"dataset-123","user":"[email protected]"}`)

mockHelpers.On("PostRequest", expectedURL, token, jsonBody).Return([]byte(nil), errors.New("failed to send request"))

err := Create("http://example.com", token, datasetID, accessionIDs)
err := Create("http://example.com", token, datasetID, user, accessionIDs)
assert.Error(t, err)
assert.EqualError(t, err, "failed to send request")
mockHelpers.AssertExpectations(t)
Expand Down
17 changes: 11 additions & 6 deletions sda-admin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Commands:
Trigger ingestion of a given file.
file set-accession -filepath FILEPATH -user USERNAME -accession-id accessionID
Assign accession ID to a file.
dataset create -dataset-id DATASET_ID accessionID [accessionID ...]
dataset create -user SUBMISSION_USER -dataset-id DATASET_ID accessionID [accessionID ...]
Create a dataset from a list of accession IDs and a dataset ID.
dataset release -dataset-id DATASET_ID
Release a dataset for downloading.
Expand Down Expand Up @@ -92,7 +92,7 @@ Options:
-accession-id ID Specify the accession ID to assign to the file.`

var datasetUsage = `Create a dataset:
Usage: sda-admin dataset create -dataset-id DATASET_ID [ACCESSION_ID ...]
Usage: sda-admin dataset create -user SUBMISSION_USER -dataset-id DATASET_ID [ACCESSION_ID ...]
Create a dataset from a list of accession IDs and a dataset ID.
Release a dataset:
Expand All @@ -105,8 +105,8 @@ Options:
Use 'sda-admin help dataset <command>' for information on a specific command.`

var datasetCreateUsage = `Usage: sda-admin dataset create -dataset-id DATASET_ID [ACCESSION_ID ...]
Create a dataset from a list of accession IDs and a dataset ID.
var datasetCreateUsage = `Usage: sda-admin dataset create -user SUBMISSION_USER -dataset-id DATASET_ID [ACCESSION_ID ...]
Create a dataset from a list of accession IDs and a dataset ID belonging to a given user.
Options:
-dataset-id DATASET_ID Specify the unique identifier for the dataset.
Expand Down Expand Up @@ -410,8 +410,9 @@ func handleDatasetCommand() error {

func handleDatasetCreateCommand() error {
datasetCreateCmd := flag.NewFlagSet("create", flag.ExitOnError)
var datasetID string
var datasetID, username string
datasetCreateCmd.StringVar(&datasetID, "dataset-id", "", "ID of the dataset to create")
datasetCreateCmd.StringVar(&username, "user", "", "Username to associate with the file")

if err := datasetCreateCmd.Parse(flag.Args()[2:]); err != nil {
return fmt.Errorf("error: failed to parse command line arguments, reason: %v", err)
Expand All @@ -423,7 +424,11 @@ func handleDatasetCreateCommand() error {
return fmt.Errorf("error: -dataset-id and at least one accession ID are required.\n%s", datasetCreateUsage)
}

err := dataset.Create(apiURI, token, datasetID, accessionIDs)
if username == "" {
return fmt.Errorf("error: -user is required.\n%s", datasetCreateUsage)
}

err := dataset.Create(apiURI, token, datasetID, username, accessionIDs)
if err != nil {
return fmt.Errorf("error: failed to create dataset, reason: %v", err)
}
Expand Down

0 comments on commit fc7c79c

Please sign in to comment.