From eb9f93c47de8e5ed272d2a69d220384e62ae84ef Mon Sep 17 00:00:00 2001 From: Geert van der Cruijsen Date: Thu, 2 Aug 2018 00:09:37 +0200 Subject: [PATCH] Add "Remove Agent" function (#70) * added a function to remove agents * added test for removing agents * added docs for "remove agent" function * added -Force param and option to add multiple agents at once * Added test to cover the situation where it throws. * I decided to change the level to High. * Updated readme and psd1 for Version 3.0.5 * The pools are changing so fast it breaks our integration tests. I changed the condition to account for that. * Missed a - on Be --- .docs/Remove-VSTeamAgent.md | 50 +++++++++++ .docs/synopsis/Remove-VSTeamAgent.md | 1 + README.md | 6 ++ VSTeam.psd1 | 8 +- docs/Team.md | 4 + docs/readme.md | 4 + en-US/VSTeam-Help.xml | 109 ++++++++++++++++++++++++ integration/test/010_projects.Tests.ps1 | 10 ++- src/agents.psm1 | 34 +++++++- unit/test/agents.Tests.ps1 | 21 +++++ 10 files changed, 238 insertions(+), 9 deletions(-) create mode 100644 .docs/Remove-VSTeamAgent.md create mode 100644 .docs/synopsis/Remove-VSTeamAgent.md diff --git a/.docs/Remove-VSTeamAgent.md b/.docs/Remove-VSTeamAgent.md new file mode 100644 index 000000000..93e9f8375 --- /dev/null +++ b/.docs/Remove-VSTeamAgent.md @@ -0,0 +1,50 @@ + + +# Remove-VSTeamAgent + +## SYNOPSIS + + + +## SYNTAX + +## DESCRIPTION + + + +## EXAMPLES + +## PARAMETERS + +### -PoolId + +Id of the pool. + +```yaml +Type: String +Required: True +Accept pipeline input: true (ByValue) +``` + +### -Id + +Id of the agent to remove. + +```yaml +Type: int +Aliases: AgentID +Required: True +Accept pipeline input: true (ByPropertyName) +``` + + + +## INPUTS + +### System.String + +## OUTPUTS + +## NOTES + +## RELATED LINKS \ No newline at end of file diff --git a/.docs/synopsis/Remove-VSTeamAgent.md b/.docs/synopsis/Remove-VSTeamAgent.md new file mode 100644 index 000000000..e7bee3538 --- /dev/null +++ b/.docs/synopsis/Remove-VSTeamAgent.md @@ -0,0 +1 @@ +Removes an agent from a pool. \ No newline at end of file diff --git a/README.md b/README.md index 4420a2949..92f21415f 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,12 @@ The cases of every file is very important. This module is to be used on Windows, ## Release Notes +### 3.0.5 + +Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/70) from [Geert van der Cruijsen](https://github.com/Geertvdc) which included the following: + +Added a function to remove vsts agents from a pool. by calling Remove-Agent or Remove-VSTeamAgent + ### 3.0.4 The ProjectName dynamic parameter that enables Tab Complete of project names was getting called approximately 20 times when tab completing a function name. To reduce the number of calls a rudimentary cache was put in place. diff --git a/VSTeam.psd1 b/VSTeam.psd1 index db2b11206..8c0343c1c 100644 --- a/VSTeam.psd1 +++ b/VSTeam.psd1 @@ -13,7 +13,7 @@ RootModule = '' # Version number of this module. - ModuleVersion = '3.0.4' + ModuleVersion = '3.0.5' # Supported PSEditions # CompatiblePSEditions = @() @@ -202,7 +202,8 @@ 'Get-VSTeamWorkItem', 'Show-VSTeamWorkItem', 'Get-VSTeamGitRef', - 'Get-VSTeamAgent') + 'Get-VSTeamAgent', + 'Remove-VSTeamAgent') # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. # CmdletsToExport = @() @@ -302,7 +303,8 @@ 'Update-Policy', 'Remove-Policy', 'Get-GitRef', - 'Get-Agent') + 'Get-Agent', + 'Remove-Agent') # DSC resources to export from this module # DscResourcesToExport = @() diff --git a/docs/Team.md b/docs/Team.md index d0f038bd6..ea3438899 100644 --- a/docs/Team.md +++ b/docs/Team.md @@ -223,6 +223,10 @@ Removes a team from a project. Clears your default project, account name and personal access token. +### [Remove-VSTeamAgent](Remove-VSTeamAgent.md) + +Removes an agent from a pool. + ### [Remove-VSTeamBuild](Remove-VSTeamBuild.md) Deletes the build. diff --git a/docs/readme.md b/docs/readme.md index 424770a94..d585844fb 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -228,6 +228,10 @@ Removes a team from a project. Clears your default project, account name and personal access token. +### [Remove-VSTeamAgent](Remove-VSTeamAgent.md) + +Removes an agent from a pool. + ### [Remove-VSTeamBuild](Remove-VSTeamBuild.md) Deletes the build. diff --git a/en-US/VSTeam-Help.xml b/en-US/VSTeam-Help.xml index 0e6be9754..05580e813 100644 --- a/en-US/VSTeam-Help.xml +++ b/en-US/VSTeam-Help.xml @@ -8860,6 +8860,115 @@ Demo-CI Demo-CI-45 notStarted + + + Remove-VSTeamAgent + Remove + VSTeamAgent + + Removes an agent from a pool. + + + + Removes an agent from a pool. + + + + Remove-VSTeamAgent + + PoolId + + Id of the pool. + + String + + String + + + None + + + Id + + Id of the agent to remove. + + int + + int + + + None + + + Force + + Forces the command without confirmation + + + SwitchParameter + + + False + + + + + + PoolId + + Id of the pool. + + String + + String + + + None + + + Id + + Id of the agent to remove. + + int + + int + + + None + + + Force + + Forces the command without confirmation + + SwitchParameter + + SwitchParameter + + + False + + + + + + System.String + + + + + + + + + + + + + + + Remove-VSTeamBuild diff --git a/integration/test/010_projects.Tests.ps1 b/integration/test/010_projects.Tests.ps1 index f30bd4ed1..bdd313d48 100644 --- a/integration/test/010_projects.Tests.ps1 +++ b/integration/test/010_projects.Tests.ps1 @@ -10,9 +10,11 @@ if ($null -eq $env:TEAM_CIBUILD) { ############################################################## # Before running these tests you must set the following # Environment variables. -# $env:API_VERSION = TFS2017, TFS2018 or VSTS depending on the value used for ACCT +# $env:API_VERSION = TFS2017, TFS2018 or VSTS depending on +# the value used for ACCT # $env:EMAIL = Email of user to remove and re-add to account -# $env:ACCT = VSTS Account Name or full TFS URL including collection +# $env:ACCT = VSTS Account Name or full TFS URL including +# collection # $env:PAT = Personal Access token of ACCT ############################################################## # THESE TEST ARE DESTRUCTIVE. USE AN EMPTY ACCOUNT. # @@ -169,7 +171,7 @@ Describe 'VSTeam Integration Tests' -Tag 'integration' { $actual.name | Should Be 'Default' } else { - $actual.Count | Should Be 6 + $actual.Count | Should -Not -Be 0 } } } @@ -193,7 +195,7 @@ Describe 'VSTeam Integration Tests' -Tag 'integration' { } else { $global:queueId = $actual[0].id - $actual.Count | Should Be 6 + $actual.Count | Should -Not -Be 0 } } diff --git a/src/agents.psm1 b/src/agents.psm1 index b65330ae9..636e9f79a 100644 --- a/src/agents.psm1 +++ b/src/agents.psm1 @@ -43,8 +43,38 @@ function Get-VSTeamAgent { } } +function Remove-VSTeamAgent { + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "High")] + param( + [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] + [int] $PoolId, + + [parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 1)] + [Alias('AgentID')] + [int[]] $Id, + + # Forces the command without confirmation + [switch] $Force + ) + + process { + foreach ($item in $Id) { + if ($force -or $pscmdlet.ShouldProcess($item,"Delete agent")) { + try { + _callAPI -Method Delete -Area "distributedtask/pools/$PoolId" -Resource agents -Id $item -Version $VSTeamVersionTable.DistributedTask | Out-Null + Write-Output "Deleted agent $item" + } + catch { + _handleException $_ + } + } + } + } +} + Set-Alias Get-Agent Get-VSTeamAgent +Set-Alias Remove-Agent Remove-VSTeamAgent Export-ModuleMember ` - -Function Get-VSTeamAgent ` - -Alias Get-Agent \ No newline at end of file + -Function Get-VSTeamAgent, Remove-VSTeamAgent ` + -Alias Get-Agent, Remove-Agent \ No newline at end of file diff --git a/unit/test/agents.Tests.ps1 b/unit/test/agents.Tests.ps1 index 6e0fbe1e1..3c4c9d048 100644 --- a/unit/test/agents.Tests.ps1 +++ b/unit/test/agents.Tests.ps1 @@ -66,5 +66,26 @@ InModuleScope agents { } } } + + Context 'Remove-VSTeamAgent by ID' { + Mock Invoke-RestMethod + + It 'should remove the agent with passed in Id' { + Remove-VSTeamAgent -Pool 36 -Id 950 -Force + + Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter { + $Method -eq 'Delete' -and + $Uri -eq "https://test.visualstudio.com/_apis/distributedtask/pools/36/agents/950?api-version=$($VSTeamVersionTable.DistributedTask)" + } + } + } + + Context 'Remove-VSTeamAgent throws' { + Mock Invoke-RestMethod { throw 'boom' } + + It 'should remove the agent with passed in Id' { + { Remove-VSTeamAgent -Pool 36 -Id 950 -Force } | Should Throw + } + } } } \ No newline at end of file