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

Add resource_id() extension method to Secrets #2052

Merged
merged 2 commits into from
Feb 3, 2025
Merged

Conversation

heaths
Copy link
Member

@heaths heaths commented Feb 2, 2025

Parsing the name anv version from the resource ID (URL) is something we do across other Azure SDK languages and makes the API much more user-friendly. It's common to need to parse the name and version from a secret (or key or certificate) and pass into subsequent calls.

Parsing the name anv version from the resource ID (URL) is something we do across other Azure SDK languages and makes the API much more user-friendly. It's common to need to parse the name and version from a secret (or key or certificate) and pass into subsequent calls.
@heaths
Copy link
Member Author

heaths commented Feb 2, 2025

@RickWinter @JeffreyRichter I think we really need to take this one. We have similar utilities in our public APIs for most (all?) other Azure SDK languages because parsing the name and version out of a resource ID (URL) is often necessary to pass back into the client. This is also a good example of how service partners add value to their otherwise generated crates.

/cc @chlowell @jackrichins

@heaths
Copy link
Member Author

heaths commented Feb 2, 2025

As an example, instead of devs having to write code like this (or even less concise, which is likely for people newer to the language):

https://github.com/Azure/azure-sdk-for-rust/pull/2051/files#diff-e68c23fe7f799586bbbcaf760ba0cd25ac804e07b08c0c4519192f0ae3d00a2aR47-R56

They can simply write:

let id = secret.resource_id()?;
let secret = client.get_secret(id.name, id.version.unwrap_or_default(), None).await?;

@JeffreyRichter
Copy link
Member

I'm OK with this in concept. For Azure Storage, all languages should have an equivalent of this type: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob#URLParts which has a ParseURL function.
This functionality was NOT put on the client on purpose. URLs (not clients) are passed between machines and may be manipulated without ever creating a client. So, given any Blob/KeyVault URL, you can parse it to get its interesting parts. A client should not have to be constructed first.

That being said, if a "convenience" function were put on a client to wrap this no-client-required functionality, I think that would be OK. In Go, we did not do this for all the various Blob clients.

@heaths
Copy link
Member Author

heaths commented Feb 3, 2025

Hanging it off a resource as an "extension method" makes sense in this case. It's not associated with a client either.

For Storage I could see something like Go's (/cc @vincenttran-msft) but Key Vault really only needs to expose the name and optional version. That said, I could get behind the "parts" nomenclature as a general rule of thumb. In most other languages for Key Vault we called this something like KeyVaultSecretIdentifier:

I'll name the getters the same and expose the vault URI but I think the extension method approach here is more idiomatic. In hindsight, I don't know why I didn't do that for .NET.

@heaths
Copy link
Member Author

heaths commented Feb 3, 2025

It'd be great to return a bunch of borrowed &str slices but the fact that constructing a url::Url takes ownership of the string - along with other problems in the past couple weeks - I opened #2053. This would allow cheaper parsing and less allocations for something like this.

This also turns the `source_id` (nee `id`) and `vault_url` (new) into owned `String`s because we don't really intend customers to do anything with them as `Url`s. If they really need a `Url` from it they can parse it into one, but there are really no supported scenarios for this for callers of this crate. The `endpoint` parameter - the `vault_url` here - `name`, and `version` parameters are all `&str` slices.
@heaths
Copy link
Member Author

heaths commented Feb 3, 2025

I turned the source_id (nee id) and vault_url (new) into owned Strings because we don't really intend customers to do anything with them as Urls. If they really need a Url from it they can parse it into one, but there are really no supported scenarios for this for callers of this crate. The endpoint parameter - the vault_url here - name, and version parameters are all &str slices.

/cc @chlowell

@heaths heaths merged commit 4688e67 into Azure:main Feb 3, 2025
13 checks passed
@heaths heaths deleted the keyvault-ext branch February 3, 2025 19:02
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