Skip to content

Commit

Permalink
Merge pull request #18 from awickham10/development
Browse files Browse the repository at this point in the history
Fix Add-DpaAnnotation
  • Loading branch information
awickham10 authored Mar 26, 2019
2 parents 9125ce1 + b0367dd commit 53d8f84
Show file tree
Hide file tree
Showing 33 changed files with 680 additions and 161 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests/Constants.Local.ps1
4 changes: 3 additions & 1 deletion Build/build.requirements.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
Target = '$ENV:USERPROFILE\Documents\WindowsPowerShell\Modules'
AddToPath = $True
Parameters = @{
Force = $True
#Force = $True
}
}

'PSScriptAnalyzer' = '1.17.1'
'psake' = '4.7.0'
'PSDeploy' = '0.2.5'
'BuildHelpers' = '1.1.1'
'Pester' = '4.6.0'
'PSFramework' = '0.10.31.176'
'PSCodeCovIo' = '1.0.1'
'Az' = '1.0.0'
}
9 changes: 4 additions & 5 deletions Build/deploy.psdeploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ if(
}
else
{
"Skipping deployment: To deploy, ensure that...`n" +
"`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" +
"`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" +
"`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)" |
Write-Host
Write-PSFMessage -Level 'Output' -Message ("Skipping deployment: To deploy, ensure that...`n" +
"`t* You are in a known build system (Current: $ENV:BHBuildSystem)`n" +
"`t* You are committing to the master branch (Current: $ENV:BHBranchName) `n" +
"`t* Your commit message includes !deploy (Current: $ENV:BHCommitMessage)")
}

# Publish to AppVeyor if we're in AppVeyor
Expand Down
62 changes: 55 additions & 7 deletions Build/psake.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,47 @@ Task Init {
}

Task Test -Depends Init {
$lines
"`n`tSTATUS: Starting DPA VM"

if (-not $ENV:PSDPA_AZ) {
Stop-PSFFunction -Message "Environment variable PSDPA_AZ is not set" -EnableException $true
}

if (-not $ENV:PSDPA_AZ_APP) {
Stop-PSFFunction -Message "Environment variable PSDPA_AZ_APP is not set" -EnableException $true
}

try {
$azPassword = ConvertTo-SecureString -String $ENV:PSDPA_AZ -AsPlainText -Force
$azCredential = New-Object -TypeName 'System.Management.Automation.PSCredential' -ArgumentList @($ENV:PSDPA_AZ_APP, $azPassword)
$null = Connect-AzAccount -Tenant $ENV:PSDPA_AZ_TENANT -Credential $azCredential -ServicePrincipal

$dpaVm = Get-AzVM -ResourceGroupName 'PSDPA' -Name 'dpa' -Status
$dpaVmStatus = $dpaVm.Statuses | Where-Object { $_.Code -like 'PowerState/*' }

if ($dpaVmStatus.DisplayStatus -ne 'VM running') {
"Starting DPA VM"
$azStart = $dpaVm | Start-AzVM

"Waiting up to 10 minutes for DPA to start"
$maxCycles = 60
$cycles = 0
do {
Start-Sleep -Seconds 10
$cycles++
} until ((Test-NetConnection '13.67.213.239' -Port 8123 -ErrorAction 'SilentlyContinue' -WarningAction 'SilentlyContinue' | Where-Object { $_.TcpTestSucceeded }) -or $cycles++ -ge $maxCycles )

Start-Sleep -Seconds 180
} else {
"DPA VM is already running"
}
} catch {
Stop-PSFFunction -Message "Could not start Azure DPA VM" -ErrorRecord $_ -EnableException $true
}
$lines
"`n"

$lines
"`n`tSTATUS: Testing with PowerShell $PSVersion"

Expand Down Expand Up @@ -59,13 +100,23 @@ Task Test -Depends Init {
}

Remove-Item "$ProjectRoot\$TestFile" -Force -ErrorAction SilentlyContinue

try {
$dpaVm | Stop-AzVM -Confirm:$false -Force
} catch {
Stop-PSFFunction -Message "Could not stop Azure DPA VM" -ErrorRecord $_ -EnableException $true
}

# Failed tests?
# Need to tell psake or it will proceed to the deployment. Danger!
if($TestResults.FailedCount -gt 0)
{
Write-Error "Failed '$($TestResults.FailedCount)' tests, build failed"
}
"`n"

$lines
"`n`tSTATUS: Stopping DPA VM"
}

Task Build -Depends Test {
Expand All @@ -75,16 +126,13 @@ Task Build -Depends Test {
Set-ModuleFunctions

# Bump the module version if we didn't already
Try
{
$GalleryVersion = Get-NextPSGalleryVersion -Name $env:BHProjectName -ErrorAction Stop
$GithubVersion = Get-MetaData -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -ErrorAction Stop
try {
[version]$GalleryVersion = Get-NextNugetPackageVersion -Name $env:BHProjectName -ErrorAction Stop
[version]$GithubVersion = Get-MetaData -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -ErrorAction Stop
if($GalleryVersion -ge $GithubVersion) {
Update-Metadata -Path $env:BHPSModuleManifest -PropertyName ModuleVersion -Value $GalleryVersion -ErrorAction stop
}
}
Catch
{
} catch {
"Failed to update version for '$env:BHProjectName': $_.`nContinuing with existing version"
}
}
Expand Down
88 changes: 88 additions & 0 deletions Invoke-DpaFormatter.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
function Invoke-DpaFormatter {
<#
.SYNOPSIS
Helps formatting function files to dbatools' standards
.DESCRIPTION
Uses PSSA's Invoke-Formatter to format the target files and saves it without the BOM.
.PARAMETER Path
The path to the ps1 file that needs to be formatted
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: Formatting
Author: Simone Bizzotto
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Invoke-DbatoolsFormatter
.EXAMPLE
PS C:\> Invoke-DbatoolsFormatter -Path C:\dbatools\functions\Get-DbaDatabase.ps1
Reformats C:\dbatools\functions\Get-DbaDatabase.ps1 to dbatools' standards
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[object[]]$Path,
[switch]$EnableException
)
begin {
$HasInvokeFormatter = $null -ne (Get-Command Invoke-Formatter -ErrorAction SilentlyContinue).Version
if (!($HasInvokeFormatter)) {
Stop-Function -Message "You need a recent version of PSScriptAnalyzer installed"
}
$CBHRex = [regex]'(?smi)\s+<#[^#]*#>'
$CBHStartRex = [regex]'(?<spaces>[ ]+)<#'
$CBHEndRex = [regex]'(?<spaces>[ ]*)#>'
}
process {
foreach ($p in $Path) {
try {
$realPath = (Resolve-Path -Path $p -ErrorAction Stop).Path
} catch {
Stop-Function -Message "Cannot find or resolve $p" -Continue
}

$content = Get-Content -Path $realPath -Raw -Encoding UTF8
#strip ending empty lines
$content = $content -replace "(?s)`r`n\s*$"
try {
$content = Invoke-Formatter -ScriptDefinition $content -Settings CodeFormattingOTBS -ErrorAction Stop
} catch {
Write-Message -Level Warning "Unable to format $p"
}
#match the ending indentation of CBH with the starting one, see #4373
$CBH = $CBHRex.Match($content).Value
if ($CBH) {
#get starting spaces
$startSpaces = $CBHStartRex.Match($CBH).Groups['spaces']
if ($startSpaces) {
#get end
$newCBH = $CBHEndRex.Replace($CBH, "$startSpaces#>")
if ($newCBH) {
#replace the CBH
$content = $content.Replace($CBH, $newCBH)
}
}
}
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
$realContent = @()
#trim whitespace lines
foreach ($line in $content.Split("`n")) {
$realContent += $line.TrimEnd()
}
[System.IO.File]::WriteAllText($realPath, ($realContent -Join "`r`n"), $Utf8NoBomEncoding)
}
}
}
8 changes: 4 additions & 4 deletions PSDPA/Classes/AccessToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class AccessToken {

[bool] IsValid() {
return `
(Get-Date) -lt $this.Expiration -and
$null -ne $this.AccessToken -and
$null -ne $this.TokenType
(Get-Date) -lt $this.Expiration -and
$null -ne $this.AccessToken -and
$null -ne $this.TokenType
}
}
}
2 changes: 1 addition & 1 deletion PSDPA/Classes/Annotation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ class Annotation {
$this.Description = $Json.description
$this.Time = $Json.time
}
}
}
10 changes: 7 additions & 3 deletions PSDPA/Classes/Monitor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,24 @@ class SqlServerMonitor : Monitor {
}
}

class AzureSQLDatabaseMonitor : Monitor {
AzureSQLDatabaseMonitor ([PSCustomObject] $Json) : base ($Json) {}
}

class MonitorFactory {
static [Monitor[]] $Monitors

static [Object] getByType ([Object] $O) {
return [MonitorFactory]::Monitors.Where({$_ -is $O})
return [MonitorFactory]::Monitors.Where( {$_ -is $O})
}

static [Object] getByName ([String] $Name) {
return [MonitorFactory]::Monitors.Where({$_.Name -eq $Name})
return [MonitorFactory]::Monitors.Where( {$_.Name -eq $Name})
}

[Monitor] New ([PSCustomObject] $Json) {
$type = $Json.databaseType.Replace(' ', '') + 'Monitor'

return (New-Object -TypeName "$type" -ArgumentList $Json)
}
}
}
8 changes: 5 additions & 3 deletions PSDPA/Private/Get-DpaAccessToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ function Get-DpaAccessToken {
$refreshToken = (Get-DpaConfig -Name 'refreshtoken').Value

$request = @{
grant_type = 'refresh_token'
grant_type = 'refresh_token'
refresh_token = $refreshToken
}

try {
Write-PSFMessage -Level 'Verbose' -Message 'Getting an access token'

$response = Invoke-RestMethod -Uri $authTokenUri -Method 'POST' -Body $request
$accessToken = New-Object -TypeName 'AccessToken' -ArgumentList $response

Set-PSFConfig -Module 'psdpa' -Name 'accesstoken' -Value $accessToken
$PSDefaultParameterValues['Invoke-DpaRequest:AccessToken'] = $accessToken

return $accessToken
}
catch {
} catch {
Stop-PSFFunction -Message "Could not obtain access token" -ErrorRecord $_ -EnableException $EnableException
}
}
16 changes: 6 additions & 10 deletions PSDPA/Private/Invoke-DpaRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ function Invoke-DpaRequest {
$Parameters
)

if (-not $PSBoundParameters.ContainsKey('AccessToken')) {
if (-not $PSBoundParameters.ContainsKey('AccessToken') -or -not $AccessToken.IsValid()) {
$AccessToken = Get-DpaAccessToken
}

if (-not $AccessToken.IsValid()) {
Stop-PSFFunction -Message "You do not have a valid access token"
return
}

$headers = @{
'Accept' = 'application/json'
'Content-Type' = 'application/json;charset=UTF-8'
'Accept' = 'application/json'
'Content-Type' = 'application/json;charset=UTF-8'
'Authorization' = $AccessToken.ToAuthorizationHeader()
}

Expand All @@ -48,12 +43,13 @@ function Invoke-DpaRequest {
}

$params = @{
'Uri' = $uri
'Uri' = $uri
'Headers' = $headers
'Method' = $Method
'Method' = $Method
}
if ($PSBoundParameters.ContainsKey('Request')) {
$params['Body'] = $Request | ConvertTo-Json
Write-Verbose $params['Body']
}

Invoke-RestMethod @params
Expand Down
19 changes: 12 additions & 7 deletions PSDPA/Public/Add-DpaAnnotation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,30 @@ function Add-DpaAnnotation {
begin {
if ($PSCmdlet.ParameterSetName -eq 'ByName') {
$Monitor = Get-DpaMonitor -MonitorName $MonitorName
}
elseif ($PSCmdlet.ParameterSetName -eq 'ByDatabaseId') {
} elseif ($PSCmdlet.ParameterSetName -eq 'ByDatabaseId') {
$Monitor = Get-DpaMonitor -DatabaseId $DatabaseId
}
}

process {
foreach ($monitorObject in $Monitor) {
Write-PSFMessage -Level Verbose -Message "Adding annotation to Database ID $($monitorObject.DatabaseId)"
$endpoint = "/databases/$($monitorObject.DatabaseId)/annotations"

$request = @{
'title' = $Title
'title' = $Title
'description' = $Description
'createdBy' = $CreatedBy
'time' = $Time.ToString("yyyy-MM-ddTHH\:mm\:sszzz")
'createdBy' = $CreatedBy
'time' = $Time.ToString("yyyy-MM-ddTHH\:mm\:sszzz")
}

$response = Invoke-DpaRequest -Endpoint $endpoint -Method 'Post' -Request $request
New-Object -TypeName 'Annotation' -ArgumentList $response.data

try {
New-Object -TypeName 'Annotation' -ArgumentList $monitorObject, $response.data
} catch {
Stop-PSFFunction -Level Critical -Message 'Could not create annotation from API response' -ErrorRecord $_ -EnableException $EnableException
}
}
}
}
}
5 changes: 2 additions & 3 deletions PSDPA/Public/Get-DpaAnnotation.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ function Get-DpaAnnotation {
begin {
if ($PSCmdlet.ParameterSetName -eq 'ByName') {
$Monitor = Get-DpaMonitor -MonitorName $MonitorName
}
elseif ($PSCmdlet.ParameterSetName -eq 'ByDatabaseId') {
} elseif ($PSCmdlet.ParameterSetName -eq 'ByDatabaseId') {
$Monitor = Get-DpaMonitor -DatabaseId $DatabaseId
}
}
Expand All @@ -92,7 +91,7 @@ function Get-DpaAnnotation {

$parameters = @{
'startTime' = $StartTime.ToString("yyyy-MM-ddTHH\:mm\:ss.fffzzz")
'endTime' = $EndTime.ToString("yyyy-MM-ddTHH\:mm\:ss.fffzzz")
'endTime' = $EndTime.ToString("yyyy-MM-ddTHH\:mm\:ss.fffzzz")
}

$response = Invoke-DpaRequest -Endpoint $endpoint -Method 'Get' -Parameters $parameters
Expand Down
Loading

0 comments on commit 53d8f84

Please sign in to comment.