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

feat(tuple write): add support for csv files #222

Merged
merged 4 commits into from
Jan 10, 2024
Merged

Conversation

sergiught
Copy link
Contributor

@sergiught sergiught commented Jan 10, 2024

Description

This PR adds support for csv files within the fga tuple write command.

It also updates the helper text for this command to:

$ go run main.go tuple write -h                                                                                                                   
Add relationship tuples to the store. This command allows for the creation of relationship tuples either through direct command line arguments or by specifying a file. The file can be in JSON, YAML, or CSV format.

When using a CSV file, the file must adhere to a specific header structure for the command to correctly interpret the data. The required CSV header structure is as follows:
- "user_type":         Specifies the type of the user in the relationship tuple. (e.g. "team")
- "user_id":           The unique identifier of the user. (e.g. "marketing")
- "user_relation":     Defines the user relation forming a userset. (optional) (e.g. "member")
- "relation":          Defines the tuple relation. (e.g. "viewer")
- "object_type":       Specifies the type of the object in the relationship tuple. (e.g. "document")
- "object_id":         The unique identifier of the object. (e.g. "roadmap")
- "condition_name":    The name of the condition. (optional) (e.g. "inOfficeIP")
- "condition_context": The context of the condition as a json object. (optional) (e.g. "{""ip_addr"":""10.0.0.1""}")

For example, a valid CSV file might start with a row like:
user_type,user_id,user_relation,relation,object_type,object_id,condition_name,condition_context

This command is flexible in accepting data inputs, making it easier to add multiple relationship tuples in various convenient formats.

Usage:
  fga tuple write [flags]

Examples:
  fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 user:anne can_view document:roadmap
  fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 user:anne can_view document:roadmap --condition-name inOffice --condition-context '{"office_ip":"10.0.1.10"}'
  fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 --file tuples.json
  fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 --file tuples.yaml
  fga tuple write --store-id=01H0H015178Y2V4CX10C2KGHF4 --file tuples.csv

Flags:
      --condition-context string    Condition Context (as a JSON string)
      --condition-name string       Condition Name
      --file string                 Tuples file
  -h, --help                        help for write
      --max-parallel-requests int   Max number of requests to issue to the server in parallel. (default 10)
      --max-tuples-per-write int    Max tuples per write chunk. (default 1)
      --model-id string             Model ID

Global Flags:
      --api-audience string       API Audience. Used when performing the Client Credentials flow
      --api-token string          API Token. Will be sent in as a Bearer in the Authorization header
      --api-token-issuer string   API Token Issuer. API responsible for issuing the API Token. Used in the Client Credentials flow
      --api-url string            OpenFGA API URI e.g. https://api.fga.example:8080 (default "http://localhost:8080")
      --client-id string          Client ID. Sent to the Token Issuer during the Client Credentials flow
      --client-secret string      Client Secret. Sent to the Token Issuer during the Client Credentials flow
      --config string             config file (default is $HOME/.fga.yaml)
      --server-url string         OpenFGA API URI e.g. https://api.fga.example:8080 (default "http://localhost:8080")
      --store-id string           Store ID

Some refactor of the command was necessary in order to break down complexity.

Tests were also added for the logic that parses tuples from files.

References

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

@sergiught sergiught force-pushed the GH-206/import-from-csv branch from 0e023c8 to 89b1946 Compare January 10, 2024 16:34
@sergiught sergiught force-pushed the GH-206/import-from-csv branch from 89b1946 to b89e46a Compare January 10, 2024 16:43
@sergiught sergiught force-pushed the GH-206/import-from-csv branch from 7b588d5 to b43d112 Compare January 10, 2024 17:47
@sergiught sergiught marked this pull request as ready for review January 10, 2024 18:00
@sergiught sergiught requested a review from a team as a code owner January 10, 2024 18:00
@rhamzeh rhamzeh enabled auto-merge January 10, 2024 18:02
@rhamzeh rhamzeh added this pull request to the merge queue Jan 10, 2024
Merged via the queue into main with commit dd1ece2 Jan 10, 2024
12 checks passed
@rhamzeh rhamzeh deleted the GH-206/import-from-csv branch January 10, 2024 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants