Skip to content

Commit

Permalink
Adds bulk org membership operation support, role assertions, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RobFaie committed Feb 19, 2020
1 parent 284c179 commit 2458df1
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 17 deletions.
10 changes: 9 additions & 1 deletion functions/Get-OrganizationMembership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function Get-OrganizationMembership {
# Unique Id of the organization membership to retrieve
[Parameter(Mandatory = $true,
ParameterSetName = 'Id')]
[Parameter(Mandatory = $false,
ParameterSetName = 'UserId')]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$Id,
Expand All @@ -33,6 +35,8 @@ function Get-OrganizationMembership {
$Context = $null
)

Assert-IsAgent -Context $Context

$key = 'organization_memberships'

switch ($PSCMDlet.ParameterSetName) {
Expand All @@ -42,7 +46,11 @@ function Get-OrganizationMembership {
}

'UserId' {
$path = "api/v2/users/$UserId/organization_memberships.json"
if ($PSBoundParameters.ContainsKey('Id')) {
$path = "/api/v2/users/$UserId/organization_memberships/$Id.json"
} else {
$path = "/api/v2/users/$UserId/organization_memberships.json"
}
}

'OrganizationId' {
Expand Down
36 changes: 27 additions & 9 deletions functions/New-OrganizationMembership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,53 @@ function New-OrganizationMembership {
Param (

# The ID of the user for whom this memberships belongs
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true,
ParameterSetName = 'Properties')]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$UserId,

# he ID of the organization associated with this user, in this membership
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true,
ParameterSetName = 'Properties')]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$OrganizationId,

# Denotes whether this is the default organization membership for the user.
[Parameter(Mandatory = $false)]
[Parameter(Mandatory = $false,
ParameterSetName = 'Properties')]
[Switch]
$Default,

# Full Membership objects
[Parameter(Mandatory = $true,
ParameterSetName = 'Object')]
[PSCustomObject]
$Membership,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

$path = 'api/v2/group_memberships.json'
$body = @{
group_membership = @{
user_id = $UserId
organization_id = $OrganizationId
default = $Default
Assert-IsAgent -Context $Context

if ($PSCmdlet.ParameterSetName -eq 'Properties') {
$path = '/api/v2/organization_memberships.json'
$body = @{
group_membership = @{
user_id = $UserId
organization_id = $OrganizationId
default = $Default
}
}
} else {
$path = '/api/v2/organization_memberships/create_many.json'
$body = @{
group_memberships = @($Membership)
}
}

Expand Down
25 changes: 21 additions & 4 deletions functions/Remove-OrganizationMembership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,35 @@ function Remove-OrganizationMembership {
[Int64[]]
$Id,

# Unique Id of the user to remove organization membership for
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[ValidateNotNullOrEmpty()]
[Int64[]]
$UserId,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

if ($Id.count -gt 1) {
$ids = $Id -join ','
$path = "/api/v2/organization_memberships/destroy_many.json?ids=$ids"
Assert-IsAgent -Context $Context

if ($PSBoundParameters.ContainsKey('UserId')) {
if ($Id.count -gt 1) {
throw 'Bulk delete not supported when associated with a specific user.'
} else {
$path = "/api/v2/users/$UserId/organization_memberships/$Id.json"
}
} else {
$path = "/api/v2/organization_memberships/$Id.json"
if ($Id.count -gt 1) {
$ids = $Id -join ','
$path = "/api/v2/organization_memberships/destroy_many.json?ids=$ids"
} else {
$path = "/api/v2/organization_memberships/$Id.json"
}
}

if ($PSCmdlet.ShouldProcess("$Id", 'Delete Organization Membership')) {
Expand Down
8 changes: 5 additions & 3 deletions functions/Set-OrganizationMembershipAsDefault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Set-OrganizationMembershipAsDefault {
[ValidateRange(1, [Int64]::MaxValue)]
[ValidateNotNullOrEmpty()]
[Int64[]]
$MembershipId,
$Id,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
Expand All @@ -26,9 +26,11 @@ function Set-OrganizationMembershipAsDefault {
$Context = $null
)

$path = "/api/v2/users/$UserId/organization_memberships/$MembershipId/make_default.json"
Assert-IsAgent -Context $Context

if ($PSCmdlet.ShouldProcess($UserId, "Set default Organization Membership: $MembershipId")) {
$path = "/api/v2/users/$UserId/organization_memberships/$Id/make_default.json"

if ($PSCmdlet.ShouldProcess($UserId, "Set default Organization Membership: $Id")) {
$result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference
$result
}
Expand Down
Loading

0 comments on commit 2458df1

Please sign in to comment.