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: hardcoded resource cmds #203

Merged
merged 9 commits into from
Apr 24, 2024

Conversation

k3llymariee
Copy link
Contributor

@k3llymariee k3llymariee commented Apr 23, 2024

Adds a hardcoded implementation of the NewResourceCmd and NewOperationCmd functions that we plan to generate in the future.

Calling help:

$ go run main.go teams create --help
Create a team. To learn more, read [Creating a team](https://docs.launchdarkly.com/home/teams/creating).

### Expanding the teams response
LaunchDarkly supports four fields for expanding the "Create team" response. By default, these fields are **not** included in the response.

To expand the response, append the `expand` query parameter and add a comma-separated list with any of the following fields:

* `members` includes the total count of members that belong to the team.
* `roles` includes a paginated list of the custom roles that you have assigned to the team.
* `projects` includes a paginated list of the projects that the team has any write access to.
* `maintainers` includes a paginated list of the maintainers that you have assigned to the team.

For example, `expand=members,roles` includes the `members` and `roles` fields in the response.

Usage:
  ldcli teams create [flags]

Flags:
  -d, --data string     Input data in JSON
  -e, --expand string   A comma-separated list of properties that can reveal additional information in the response. Supported fields are explained above.
  -h, --help            help for create

Global Flags:
      --access-token string   LaunchDarkly API token with write-level access
      --base-uri string       LaunchDarkly base URI (default "https://app.launchdarkly.com")

Calling the command w/required flags:

$ go run main.go teams create --data '{"key": "my-team-key", "name": "My Team Name"}'
would be making a request to /api/v2/teams here, with args: map[data:map[key:my-team-key name:My Team Name] expand:]

Comment on lines +18 to +19
"A team is a group of members in your LaunchDarkly account.",
"A team can have maintainers who are able to add and remove team members. It also can have custom roles assigned to it that allows shared access to those roles for all team members. To learn more, read [Teams](https://docs.launchdarkly.com/home/teams).\n\nThe Teams API allows you to create, read, update, and delete a team.\n\nSeveral of the endpoints in the Teams API require one or more member IDs. The member ID is returned as part of the [List account members](/tag/Account-members#operation/getMembers) response. It is the `_id` field of each element in the `items` array.",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a snippet from the teams tag description, which comes from here

I stripped out the header manually, hoping that the format holds true for the other resources (looking like mayybe it is? Will check with Molly).


require.NoError(t, err)
s := string(output)
assert.Contains(t, s, "would be making a request to /api/v2/teams here, with args: map[data:map[key:team-key name:Team Name] expand:]\n")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't mocked out a client yet since we're not actually making any requests. will update this test with a mock client when that's a thing

Use: resourceName,
Short: shortDescription,
Long: longDescription,
//TODO: add tracking here
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually not sure if we wanna add tracking when just the resource cmd is run? maybe we do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been adding them as PersistentPreRun. That means they won't run when just the resource command runs. But they'll run when subcommands on the resource run. This is what that looks like on my branch/PR

Copy link
Contributor Author

@k3llymariee k3llymariee Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup there's a comment in there - will make sure to add it in there in a subsequent PR!

gen_TeamsResourceCmd := resources.NewResourceCmd(
rootCmd,
"teams",
"A team is a group of members in your LaunchDarkly account.",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first sentence from the "Teams" tag description (after the headers).

Our previously established format is "Make requests (list, create, etc.) on {resource name}" - not sure which is better in this context 🤔

@@ -129,6 +134,7 @@ func Execute(analyticsTracker analytics.Tracker, version string) {
FlagsClient: flags.NewClient(version),
MembersClient: members.NewClient(version),
ProjectsClient: projects.NewClient(version),
GenericClient: &http.Client{Timeout: time.Second * 3},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess this isn't required yet but wanted to get feedback on where we'd be passing it in

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine until we can remove the other clients.

@k3llymariee k3llymariee marked this pull request as ready for review April 23, 2024 23:49
Copy link
Contributor

@dbolson dbolson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great

}
}

fmt.Fprintf(cmd.OutOrStdout(), fmt.Sprintf("would be making a request to %s here, with args: %s\n", op.Path, paramVals))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can remove the fmt.Sprintf since Fprintf acts the same.

}

cmd := &cobra.Command{
Args: validators.Validate(), // tbd on this validator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need different validators depending on the operation, could we add it as a field on OperationData?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this is ok for now? Since we're pretty much just validating the required flags, and our existing validator already does that right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense.

@@ -129,6 +134,7 @@ func Execute(analyticsTracker analytics.Tracker, version string) {
FlagsClient: flags.NewClient(version),
MembersClient: members.NewClient(version),
ProjectsClient: projects.NewClient(version),
GenericClient: &http.Client{Timeout: time.Second * 3},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine until we can remove the other clients.

}

for _, p := range op.Params {
shorthand := fmt.Sprintf(p.Name[0:1]) // todo: how do we handle potential dupes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a low chance, if it's even possible. "params" comes from here. I duplicated the line and reran the generator, getting this error

Operations must have unique `name` + `in` parameters. Repeats of `in:query` + `name:expand`.

Copy link
Contributor Author

@k3llymariee k3llymariee Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i meant duplicate shorthands - e.g. limit and list would both be l (not that list is a thing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can see if that actually ends up being an issue once we get the rest in there maybe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's interesting. It looks like it will panic with

panic: unable to redefine 'e' shorthand in "get" flagset: it's already used for "expand" flag

I suppose we could iterate through all the existing flags to check if it's already been set and then set it to something else (e2?), or we just don't set the shorthand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya or add them to a map and check before adding it. was thinking it could be the first two letters if the one letter was already taken (but even then that's not a guaranteed unique value)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e
ex
exp
expa
expan
expand

Copy link
Contributor

@sunnyguduru sunnyguduru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once PersistentPreRun is added to NewResourceCmd it looks good to me.

@k3llymariee k3llymariee merged commit b8dc52a into main Apr 24, 2024
2 checks passed
@k3llymariee k3llymariee deleted the kelly/sc-241150/hardcoded-resource-cmds branch April 24, 2024 20:26
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.

3 participants