Skip to content

Commit

Permalink
feat(tuple write): add support for csv files (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jan 10, 2024
2 parents 742405f + e48222d commit dd1ece2
Show file tree
Hide file tree
Showing 13 changed files with 460 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ linters-settings:

goimports:
local-prefixes: "github.com/openfga/cli"

issues:
exclude-use-default: true
exclude-rules:
- path: "cmd/tuple/write(.*).go"
linters:
- lll
55 changes: 52 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ A cross-platform CLI to interact with an OpenFGA server
- [Relationship Tuples](#relationship-tuples)
- [Read Relationship Tuple Changes (Watch)](#read-relationship-tuple-changes-watch)
- [Read Relationship Tuples](#read-relationship-tuples)
- [Create Relationship Tuples](#create-relationship-tuples)
- [Write Relationship Tuples](#write-relationship-tuples)
- [Delete Relationship Tuples](#delete-relationship-tuples)
- [Relationship Queries](#relationship-queries)
- [Check](#check)
Expand Down Expand Up @@ -590,7 +590,7 @@ fga tuple **write** <user> <relation> <object> --store-id=<store-id>
* `--condition-context`: Condition context (optional)
* `--store-id`: Specifies the store id
* `--model-id`: Specifies the model id to target (optional)
* `--file`: Specifies the file name, `yaml` and `json` files are supported
* `--file`: Specifies the file name, `json`, `yaml` and `csv` files are supported
* `--max-tuples-per-write`: Max tuples to send in a single write (optional, default=1)
* `--max-parallel-requests`: Max requests to send in parallel (optional, default=4)
Expand All @@ -600,12 +600,61 @@ fga tuple **write** <user> <relation> <object> --store-id=<store-id>
###### Response
```json5
{}
{
"successful": [
{
"object":"document:roadmap",
"relation":"writer",
"user":"user:annie"
}
],
}
```
###### Example (with file)
`fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 --file tuples.json`
If using a `csv` file, the format should be:
```csv
user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context
folder,product,,parent,folder,product-2021,inOfficeIP,"{""ip_addr"":""10.0.0.1""}"
```
If using a `yaml` file, the format should be:
```yaml
- user: folder:5
relation: parent
object: folder:product-2021
- user: folder:product-2021
relation: parent
object: folder:product-2021Q1
```
If using a `json` file, the format should be:
```json
[
{
"user": "user:anne",
"relation": "owner",
"object": "folder:product"
},
{
"user": "folder:product",
"relation": "parent",
"object": "folder:product-2021"
},
{
"user": "user:beth",
"relation": "viewer",
"object": "folder:product-2021"
}
]
```
###### Response
```json5
{
Expand Down
4 changes: 4 additions & 0 deletions cmd/tuple/testdata/tuples.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context
user,anne,,owner,folder,product,inOfficeIP,
folder,product,,parent,folder,product-2021,inOfficeIP,"{""ip_addr"":""10.0.0.1""}"
team,fga,member,viewer,folder,product-2021,,
17 changes: 17 additions & 0 deletions cmd/tuple/testdata/tuples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"user": "user:anne",
"relation": "owner",
"object": "folder:product"
},
{
"user": "folder:product",
"relation": "parent",
"object": "folder:product-2021"
},
{
"user": "user:beth",
"relation": "viewer",
"object": "folder:product-2021"
}
]
Empty file added cmd/tuple/testdata/tuples.toml
Empty file.
9 changes: 9 additions & 0 deletions cmd/tuple/testdata/tuples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- user: user:anne
relation: owner
object: folder:product
- user: folder:product
relation: parent
object: folder:product-2021
- user: user:beth
relation: viewer
object: folder:product-2021
Empty file.
1 change: 1 addition & 0 deletions cmd/tuple/testdata/tuples_missing_required_headers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user_type,user_id,user_relation,relation,object_type,object_identifier,condition_name,condition_context
2 changes: 2 additions & 0 deletions cmd/tuple/testdata/tuples_with_invalid_rows.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context
user,beth,,viewer,folder,product-2021,a,b,d,,
1 change: 1 addition & 0 deletions cmd/tuple/testdata/tuples_wrong_headers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a,b,c,d
Loading

0 comments on commit dd1ece2

Please sign in to comment.