-
Notifications
You must be signed in to change notification settings - Fork 6
Add -r name=id relationship flag to api and create #103
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
Open
shwetamurali
wants to merge
7
commits into
main
Choose a base branch
from
shweta/TF-40351
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f69125e
add relationships
shwetamurali 2482623
Update api.go
shwetamurali 64999d7
create: add -r flag for JSON:API relationships
shwetamurali 15820c0
Create ENHANCEMENTS-20260819-161936.yaml
shwetamurali ffe86b0
debug logging
shwetamurali 3a51220
Ambiguous relationship reports as "unknown
shwetamurali b266cf5
create doesnt have to reparse spec
shwetamurali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| kind: ENHANCEMENTS | ||
| body: "Added a `-r name=id` flag to `api` and `create` for setting JSON:API relationships without a full request body. The relationship's linkage type is inferred from the schema (e.g. `-r project=prj-...` links to type `projects`); ambiguous or unknown relationships can be pinned with `-r name:type=id`, and to-many relationships accept comma-separated ids" | ||
| time: 2026-08-19T16:19:36.000000-04:00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,22 +44,27 @@ const ( | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Opts stores the options parsed from flags for the API command. | ||||||||||||||||||||||||
| type Opts struct { | ||||||||||||||||||||||||
| IO iostreams.IOStreams | ||||||||||||||||||||||||
| Output *format.Outputter | ||||||||||||||||||||||||
| Client *client.Client | ||||||||||||||||||||||||
| Quiet bool | ||||||||||||||||||||||||
| DryRun bool | ||||||||||||||||||||||||
| Headers []string | ||||||||||||||||||||||||
| URL *url.URL | ||||||||||||||||||||||||
| Attributes map[string]string | ||||||||||||||||||||||||
| Query map[string]string | ||||||||||||||||||||||||
| PathParams map[string]string | ||||||||||||||||||||||||
| InputRequest string | ||||||||||||||||||||||||
| Method string | ||||||||||||||||||||||||
| ResourceType string | ||||||||||||||||||||||||
| All bool | ||||||||||||||||||||||||
| PageSize int | ||||||||||||||||||||||||
| PageNumber int | ||||||||||||||||||||||||
| IO iostreams.IOStreams | ||||||||||||||||||||||||
| Output *format.Outputter | ||||||||||||||||||||||||
| Client *client.Client | ||||||||||||||||||||||||
| Quiet bool | ||||||||||||||||||||||||
| DryRun bool | ||||||||||||||||||||||||
| Headers []string | ||||||||||||||||||||||||
| URL *url.URL | ||||||||||||||||||||||||
| Attributes map[string]string | ||||||||||||||||||||||||
| Relationships map[string]string | ||||||||||||||||||||||||
| Query map[string]string | ||||||||||||||||||||||||
| PathParams map[string]string | ||||||||||||||||||||||||
| InputRequest string | ||||||||||||||||||||||||
| Method string | ||||||||||||||||||||||||
| ResourceType string | ||||||||||||||||||||||||
| All bool | ||||||||||||||||||||||||
| PageSize int | ||||||||||||||||||||||||
| PageNumber int | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Schema, when set, is consulted to infer relationship linkage types and | ||||||||||||||||||||||||
| // cardinality for -r. Nil falls back to the embedded spec. | ||||||||||||||||||||||||
| Schema openapi.Schema | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Authorizer, when set, can permit a noninteractive DELETE based on an | ||||||||||||||||||||||||
| // active exec session. Nil in tests that don't exercise session behavior. | ||||||||||||||||||||||||
|
|
@@ -70,13 +75,14 @@ type Opts struct { | |||||||||||||||||||||||
| // maps/slices initialized to empty values. | ||||||||||||||||||||||||
| func NewOpts(io iostreams.IOStreams, output *format.Outputter, apiClient *client.Client) *Opts { | ||||||||||||||||||||||||
| return &Opts{ | ||||||||||||||||||||||||
| IO: io, | ||||||||||||||||||||||||
| Output: output, | ||||||||||||||||||||||||
| Client: apiClient, | ||||||||||||||||||||||||
| Headers: []string{}, | ||||||||||||||||||||||||
| Attributes: map[string]string{}, | ||||||||||||||||||||||||
| Query: map[string]string{}, | ||||||||||||||||||||||||
| PathParams: map[string]string{}, | ||||||||||||||||||||||||
| IO: io, | ||||||||||||||||||||||||
| Output: output, | ||||||||||||||||||||||||
| Client: apiClient, | ||||||||||||||||||||||||
| Headers: []string{}, | ||||||||||||||||||||||||
| Attributes: map[string]string{}, | ||||||||||||||||||||||||
| Relationships: map[string]string{}, | ||||||||||||||||||||||||
| Query: map[string]string{}, | ||||||||||||||||||||||||
| PathParams: map[string]string{}, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -172,6 +178,14 @@ func NewCmdAPI(inv *cmd.Invocation) *cmd.Command { | |||||||||||||||||||||||
| Repeatable: true, | ||||||||||||||||||||||||
| Value: flagvalue.SimpleMap(nil, &opts.Attributes), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| Name: "relationship", | ||||||||||||||||||||||||
| Shorthand: "r", | ||||||||||||||||||||||||
| DisplayValue: "NAME=ID", | ||||||||||||||||||||||||
| Description: "Relationship for JSON:API request bodies as name=id (repeatable). Implies POST method. The linkage type is inferred from the schema; override an unresolved one with name:type=id. Comma-separate ids for to-many relationships.", | ||||||||||||||||||||||||
| Repeatable: true, | ||||||||||||||||||||||||
| Value: flagvalue.SimpleMap(nil, &opts.Relationships), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| Name: "field", | ||||||||||||||||||||||||
| Shorthand: "f", | ||||||||||||||||||||||||
|
|
@@ -207,6 +221,10 @@ func NewCmdAPI(inv *cmd.Invocation) *cmd.Command { | |||||||||||||||||||||||
| Preamble: "Create a project using attributes", | ||||||||||||||||||||||||
| Command: heredoc.New(inv.IO, heredoc.WithNoWrap(), heredoc.WithPreserveNewlines()).Mustf(`$ %s api /projects -a name=myproject`, version.Name), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| Preamble: "Create a workspace in a project (relationship type inferred from the schema)", | ||||||||||||||||||||||||
| Command: heredoc.New(inv.IO, heredoc.WithNoWrap(), heredoc.WithPreserveNewlines()).Mustf(`$ %s api /organizations/{organization}/workspaces -a name=foo -r project=prj-12dff4673ab9`, version.Name), | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| Preamble: "Add remote state consumer", | ||||||||||||||||||||||||
| Command: heredoc.New(inv.IO, heredoc.WithNoWrap(), heredoc.WithPreserveNewlines()).Mustf(`$ %s api /workspaces/{workspace}/remote-state-consumers -p 'workspace=my-workspace' -i '{ "data": [ | ||||||||||||||||||||||||
|
|
@@ -270,6 +288,7 @@ func NewCmdAPI(inv *cmd.Invocation) *cmd.Command { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| opts.URL = resolvedURL | ||||||||||||||||||||||||
| opts.Client = apiClient | ||||||||||||||||||||||||
| opts.Schema = oas | ||||||||||||||||||||||||
| opts.Quiet = inv.IsQuiet() | ||||||||||||||||||||||||
| opts.DryRun = inv.IsDryRun() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -431,13 +450,43 @@ func RunAPI(ctx context.Context, opts *Opts) error { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| opts.URL.RawQuery = query.Encode() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Resolve relationship linkage types and cardinality from the schema. The | ||||||||||||||||||||||||
| // embedded spec is used when no schema was injected (e.g. by the create | ||||||||||||||||||||||||
| // command or tests). | ||||||||||||||||||||||||
| var linkages map[string]linkage | ||||||||||||||||||||||||
| var haveSchema bool | ||||||||||||||||||||||||
| if len(opts.Relationships) > 0 { | ||||||||||||||||||||||||
| oas := opts.Schema | ||||||||||||||||||||||||
| if oas == nil { | ||||||||||||||||||||||||
| oas = openapi.LoadEmbeddedSchema() | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| // Spec paths are relative to the API version base (e.g. /api/v2), which | ||||||||||||||||||||||||
| // the resolved request URL carries as a prefix; trim it before matching. | ||||||||||||||||||||||||
| specPath := opts.URL.Path | ||||||||||||||||||||||||
| if opts.Client != nil && opts.Client.BaseURL != nil { | ||||||||||||||||||||||||
| specPath = strings.TrimPrefix(specPath, strings.TrimRight(opts.Client.BaseURL.Path, "/")) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| linkages, haveSchema = relationshipLinkages(oas, specPath) | ||||||||||||||||||||||||
| if haveSchema { | ||||||||||||||||||||||||
| names := make([]string, 0, len(linkages)) | ||||||||||||||||||||||||
| for name := range linkages { | ||||||||||||||||||||||||
| names = append(names, name) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+469
to
+474
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💅 Scoped linkages to condition and used assignment instead of append for names
Suggested change
|
||||||||||||||||||||||||
| logger.Debug("resolved relationship linkages from schema", "path", specPath, "relationships", names) | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| // Not fatal: the request can still be built if every -r carries an | ||||||||||||||||||||||||
| // explicit name:type=id. Otherwise buildRelationships returns a clear error. | ||||||||||||||||||||||||
| logger.Debug("no relationship linkages resolved from schema; explicit types required for -r", "path", specPath) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Construct a request | ||||||||||||||||||||||||
| body, contentType, err := buildRequestBody(opts.URL.Path, opts.InputRequest, opts.Attributes, opts.ResourceType, opts.IO.In()) | ||||||||||||||||||||||||
| body, contentType, err := buildRequestBody(opts.URL.Path, opts.InputRequest, opts.Attributes, opts.Relationships, opts.ResourceType, linkages, haveSchema, opts.IO.In()) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| method := inferMethod(opts.Method, len(opts.Attributes) > 0, opts.InputRequest != "") | ||||||||||||||||||||||||
| method := inferMethod(opts.Method, len(opts.Attributes) > 0 || len(opts.Relationships) > 0, opts.InputRequest != "") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| requestHeaders, err := parseHeaders(opts.Headers) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
|
|
@@ -628,7 +677,7 @@ func parseTypedValue(raw string) any { | |||||||||||||||||||||||
| return raw | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| func buildRequestBody(path, input string, attrs map[string]string, resourceType string, stdin io.Reader) ([]byte, string, error) { | ||||||||||||||||||||||||
| func buildRequestBody(path, input string, attrs, rels map[string]string, resourceType string, linkages map[string]linkage, haveSchema bool, stdin io.Reader) ([]byte, string, error) { | ||||||||||||||||||||||||
| if input != "" { | ||||||||||||||||||||||||
| var data []byte | ||||||||||||||||||||||||
| var err error | ||||||||||||||||||||||||
|
|
@@ -645,7 +694,7 @@ func buildRequestBody(path, input string, attrs map[string]string, resourceType | |||||||||||||||||||||||
| return data, "application/vnd.api+json", nil | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if len(attrs) == 0 { | ||||||||||||||||||||||||
| if len(attrs) == 0 && len(rels) == 0 { | ||||||||||||||||||||||||
| return nil, "", nil | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -656,19 +705,25 @@ func buildRequestBody(path, input string, attrs map[string]string, resourceType | |||||||||||||||||||||||
| return nil, "", errors.New("could not infer resource type from path; use --type") | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| attributes := make(map[string]any, len(attrs)) | ||||||||||||||||||||||||
| for key, value := range attrs { | ||||||||||||||||||||||||
| attributes[key] = parseTypedValue(value) | ||||||||||||||||||||||||
| data := map[string]any{"type": resourceType} | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if len(attrs) > 0 { | ||||||||||||||||||||||||
| attributes := make(map[string]any, len(attrs)) | ||||||||||||||||||||||||
| for key, value := range attrs { | ||||||||||||||||||||||||
| attributes[key] = parseTypedValue(value) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| data["attributes"] = attributes | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| body := map[string]any{ | ||||||||||||||||||||||||
| "data": map[string]any{ | ||||||||||||||||||||||||
| "type": resourceType, | ||||||||||||||||||||||||
| "attributes": attributes, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| if len(rels) > 0 { | ||||||||||||||||||||||||
| relationships, err := buildRelationships(rels, linkages, haveSchema) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
| return nil, "", err | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| data["relationships"] = relationships | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| encoded, err := json.Marshal(body) | ||||||||||||||||||||||||
| encoded, err := json.Marshal(map[string]any{"data": data}) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
| return nil, "", err | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this code path get some tests?