TFSPowershell.ServiceEndpoint.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Rest API version to be used
$BuildApiVersion = "3.0-preview.1"

 # .ExternalHelp .\MAML\TFSPowershell.ServiceEndpoint.Help.xml
Function Get-ServiceEndpoints {
    [CmdletBinding()]
    Param (
        [Parameter(mandatory = $true)]
        [string] $CollectionUrl,

        [Parameter(mandatory = $true)]
        [string] $TeamProjectName
    )
    
    # For compatibility with TFS2015
    Write-Verbose "Retrieving team project Guid (for compatibility with TFS 2015)..." 
    $getTeamProjectUrl = "{0}/_apis/projects/{1}?api-version=1.0" -f $CollectionUrl, $TeamProjectName, $BuildApiVersion
    $teamProject = Invoke-RestAPICall -Uri $getTeamProjectUrl -Method "GET" -Verbose:$VerbosePreference

    Write-Verbose "Retrieving service enpoints for TeamProject ""$TeamProject""..." 
    $getServiceEndpointUrl = "{0}/{1}/_apis/distributedtask/serviceendpoints?api-version={2}" -f $CollectionUrl, $($teamProject.id), $BuildApiVersion
 
    $response = Invoke-RestAPICall -Uri $getServiceEndpointUrl -Method "GET" -Verbose:$VerbosePreference

    return $response
}

 # .ExternalHelp .\MAML\TFSPowershell.ServiceEndpoint.Help.xml
Function New-ServiceEndpoint {
    [CmdletBinding()]
    Param (
        [Parameter(mandatory = $true)]
        [string] $CollectionUrl,

        [Parameter(mandatory = $true)]
        [string] $TeamProjectName,

        [Parameter(mandatory = $true)]
        [string] $NewEndPointJson
    )
    
    # For compatibility with TFS2015
    Write-Verbose "Retrieving team project Guid (for compatibility with TFS 2015)..." 
    $getTeamProjectUrl = "{0}/_apis/projects/{1}?api-version=1.0" -f $CollectionUrl, $TeamProjectName, $BuildApiVersion
    $teamProject = Invoke-RestAPICall -Uri $getTeamProjectUrl -Method "GET" -Verbose:$VerbosePreference
    $teamProjectGuid = $($teamProject.id)

    # Validating endpoint Json body
    try {
        ConvertFrom-Json $NewEndPointJson -ErrorAction Stop
        $body = ([System.Text.Encoding]::UTF8.GetBytes($NewEndPointJson))
    } 
    catch {
        $error = $_.Exception.Message
        Write-Error "The inputed string on parameter 'NewEndPointJson' is not a valid json. Details: ""$error""."
        return
    }

    Write-Verbose "Creating the new endpoint ..." 
    $newDefinitionUrl = "{0}/{1}/_apis/distributedtask/serviceendpoints?api-version={2}" -f $CollectionUrl, $teamProjectGuid, $BuildApiVersion
    $method = "POST"
    $newEndpoint = Invoke-RestAPICall -Uri $newDefinitionUrl -Method $method -Body $body -Verbose:$VerbosePreference

    return $newEndpoint
}