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

Update to newer az structure. #1142

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions src/Farmer/Builders/Builders.KeyVault.fs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ type AccessPolicyBuilder() =
let accessPolicy = AccessPolicyBuilder()

type AccessPolicy =
static let createFilter searchField values =
let query =
values
|> Seq.map (fun value -> $"{searchField} eq '{value}'")
|> String.concat " or "

$"\"{query}\""

static let handleSearchResponse =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this have a test of what the expected output would be given some JSON from a search response?

Serialization.ofJson<{| DisplayName: string; Id: Guid |} array>
>> Array.map (fun r -> {| r with Id = ObjectId r.Id |})

/// Quickly creates an access policy for the supplied Principal. If no permissions are supplied, defaults to GET and LIST.
static member create(principal: PrincipalId, ?permissions) = accessPolicy {
object_id principal
Expand All @@ -375,36 +387,21 @@ type AccessPolicy =
secret_permissions (permissions |> Option.defaultValue Secret.ReadSecrets)
}

static member private findEntity(searchField, values, searcher) =
values
|> Seq.map (sprintf "%s eq '%s'" searchField)
|> String.concat " or "
|> sprintf "\"%s\""
|> searcher
|> Result.map (
Serialization.ofJson<
{|
DisplayName: string
ObjectId: Guid
|} array
>
)
|> Result.toOption
|> Option.map (
Array.map (fun r -> {|
r with
ObjectId = ObjectId r.ObjectId
|})
)
|> Option.defaultValue Array.empty

/// Locates users in Azure Active Directory based on the supplied email addresses.
static member findUsers emailAddresses =
AccessPolicy.findEntity ("mail", emailAddresses, Deploy.Az.searchUsers)
let filter = createFilter "mail" emailAddresses

Deploy.Az.searchUsers filter
|> Result.map handleSearchResponse
|> Result.defaultValue Array.empty

/// Locates groups in Azure Active Directory based on the supplied group names.
static member findGroups groupNames =
AccessPolicy.findEntity ("displayName", groupNames, Deploy.Az.searchGroups)
let filter = createFilter "displayName" groupNames

Deploy.Az.searchGroups filter
|> Result.map handleSearchResponse
|> Result.defaultValue Array.empty

[<RequireQualifiedAccess>]
type SimpleCreateMode =
Expand Down
3 changes: 3 additions & 0 deletions src/Farmer/Result.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module Result =
| Ok s -> Some s
| Error _ -> None

let defaultValue defaultValue =
toOption >> Option.defaultValue defaultValue

let ignore result = Result.map ignore result

let sequence results =
Expand Down