Skip to content

Commit

Permalink
Added Team Drives support (all API calls covered)
Browse files Browse the repository at this point in the history
  • Loading branch information
scrthq committed Mar 12, 2017
1 parent abb8654 commit 8a9e1ca
Show file tree
Hide file tree
Showing 7 changed files with 477 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Public/Get-GSDriveFileInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function Get-GSDriveFileInfo {
[cmdletbinding(DefaultParameterSetName="Depth")]
Param
(
[parameter(Mandatory=$true)]
[parameter(Mandatory=$true,Position=0)]
[String]
$FileID,
[parameter(Mandatory=$false)]
Expand Down
8 changes: 4 additions & 4 deletions Public/Get-GSDriveFileList.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ function Get-GSDriveFileList {
[cmdletbinding()]
Param
(
[parameter(Mandatory=$false)]
[parameter(Mandatory=$false,Position=0)]
[string]
$Owner = $Script:PSGSuite.AdminEmail,
[parameter(Mandatory=$false)]
[String[]]
$Query,
[parameter(Mandatory=$false)]
[ValidateScript({[int]$_ -le 1000})]
[Int]
$PageSize="1000",
Expand All @@ -14,9 +17,6 @@ function Get-GSDriveFileList {
[String[]]
$OrderBy,
[parameter(Mandatory=$false)]
[String[]]
$Query,
[parameter(Mandatory=$false)]
[String]
$AccessToken,
[parameter(Mandatory=$false)]
Expand Down
59 changes: 59 additions & 0 deletions Public/Get-GSTeamDrive.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function Get-GSTeamDrive {
[cmdletbinding()]
Param
(
[parameter(Mandatory=$true,Position=0)]
[String]
$TeamDriveID,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$Owner = $Script:PSGSuite.AdminEmail,
[parameter(Mandatory=$false)]
[String]
$AccessToken,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$P12KeyPath = $Script:PSGSuite.P12KeyPath,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$AppEmail = $Script:PSGSuite.AppEmail,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$AdminEmail = $Script:PSGSuite.AdminEmail
)
if (!$AccessToken)
{
$AccessToken = Get-GSToken -P12KeyPath $P12KeyPath -Scopes "https://www.googleapis.com/auth/drive" -AppEmail $AppEmail -AdminEmail $Owner
}
$header = @{
Authorization="Bearer $AccessToken"
}
$URI = "https://www.googleapis.com/drive/v3/teamdrives/$TeamDriveID"

try
{
$response = Invoke-RestMethod -Method Get -Uri $URI -Headers $header -ContentType "application/json" | ForEach-Object {if($_.kind -like "*#*"){$_.PSObject.TypeNames.Insert(0,$(Convert-KindToType -Kind $_.kind));$_}else{$_}}
}
catch
{
try
{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$resp = $reader.ReadToEnd()
$response = $resp | ConvertFrom-Json |
Select-Object @{N="Error";E={$Error[0]}},@{N="Code";E={$_.error.Code}},@{N="Message";E={$_.error.Message}},@{N="Domain";E={$_.error.errors.domain}},@{N="Reason";E={$_.error.errors.reason}}
}
catch
{
$response = $resp
}
}
return $response
}
89 changes: 89 additions & 0 deletions Public/Get-GSTeamDrivesList.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
function Get-GSTeamDrivesList {
[cmdletbinding()]
Param
(
[parameter(Mandatory=$false)]
[string]
$Owner = $Script:PSGSuite.AdminEmail,
[parameter(Mandatory=$false)]
[ValidateScript({[int]$_ -le 100})]
[Int]
$PageSize="100",
[parameter(Mandatory=$false)]
[String]
$AccessToken,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$P12KeyPath = $Script:PSGSuite.P12KeyPath,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$AppEmail = $Script:PSGSuite.AppEmail
)
if (!$AccessToken)
{
$AccessToken = Get-GSToken -P12KeyPath $P12KeyPath -Scopes "https://www.googleapis.com/auth/drive" -AppEmail $AppEmail -AdminEmail $Owner
}
$header = @{
Authorization="Bearer $AccessToken"
}
$URI = "https://www.googleapis.com/drive/v3/teamdrives?pageSize=$PageSize"
if ($Query)
{
$Query = $($Query -join " and ")
$URI = "$URI&q=$Query"
}
if ($OrderBy)
{
$OrderByJoined = $OrderBy -join ','
$URI = "$URI&orderBy=$OrderByJoined"
}
try
{
Write-Verbose "Constructed URI: $URI"
$response = @()
[int]$i=1
do
{
if ($i -eq 1)
{
$result = Invoke-RestMethod -Method Get -Uri $URI -Headers $header -Verbose:$false
}
else
{
$result = Invoke-RestMethod -Method Get -Uri "$URI&pageToken=$pageToken" -Headers $header -Verbose:$false
}
$response += $result.teamDrives | ForEach-Object {if($_.kind -like "*#*"){$_.PSObject.TypeNames.Insert(0,$(Convert-KindToType -Kind $_.kind));$_}else{$_}}
$returnSize = $result.teamDrives.Count
$pageToken="$($result.nextPageToken)"
[int]$retrieved = ($i + $result.teamDrives.Count) - 1
Write-Verbose "Retrieved $retrieved Team Drives..."
[int]$i = $i + $result.teamDrives.Count
}
until
($returnSize -lt $PageSize)
return $response
}
catch
{
try
{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$resp = $reader.ReadToEnd()
$response = $resp | ConvertFrom-Json |
Select-Object @{N="Error";E={$Error[0]}},@{N="Code";E={$_.error.Code}},@{N="Message";E={$_.error.Message}},@{N="Domain";E={$_.error.errors.domain}},@{N="Reason";E={$_.error.errors.reason}}
Write-Error "$(Get-HTTPStatus -Code $response.Code): $($response.Domain) / $($response.Message) / $($response.Reason)"
return
}
catch
{
Write-Error $resp
return
}
}
return $response
}
132 changes: 132 additions & 0 deletions Public/New-GSTeamDrive.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
function New-GSTeamDrive {
[cmdletbinding()]
Param
(
[parameter(Mandatory=$false,Position=0)]
[ValidateNotNullOrEmpty()]
[String]
$Owner = $Script:PSGSuite.AdminEmail,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$RequestID=$((New-Guid).Guid),
[parameter(Mandatory=$true)]
[String]
$Name,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanAddChildren,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanComment,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanCopy,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanDeleteTeamDrive,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanDownload,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanEdit,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanListChildren,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanManageMembers,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanReadRevisions,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanRemoveChildren,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanRename,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanRenameTeamDrive,
[parameter(Mandatory=$false)]
[ValidateSet($true,$false)]
[String]
$CanShare,
[parameter(Mandatory=$false)]
[String]
$AccessToken,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$P12KeyPath = $Script:PSGSuite.P12KeyPath,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$AppEmail = $Script:PSGSuite.AppEmail
)
if (!$AccessToken)
{
$AccessToken = Get-GSToken -P12KeyPath $P12KeyPath -Scopes "https://www.googleapis.com/auth/drive" -AppEmail $AppEmail -AdminEmail $Owner
}
$header = @{
Authorization="Bearer $AccessToken"
}
$body = @{
name = "$Name"
capabilities = @{}
}
if($CanAddChildren -eq $true -or $CanAddChildren -eq $false){$body["capabilities"].Add("canAddChildren",$CanAddChildren)}
if($CanComment -eq $true -or $CanComment -eq $false){$body["capabilities"].Add("canComment",$CanComment)}
if($CanCopy -eq $true -or $CanCopy -eq $false){$body["capabilities"].Add("canCopy",$CanCopy)}
if($CanDeleteTeamDrive -eq $true -or $CanDeleteTeamDrive -eq $false){$body["capabilities"].Add("canDeleteTeamDrive",$CanDeleteTeamDrive)}
if($CanDownload -eq $true -or $CanDownload -eq $false){$body["capabilities"].Add("canDownload",$CanDownload)}
if($CanEdit -eq $true -or $CanEdit -eq $false){$body["capabilities"].Add("canEdit",$CanEdit)}
if($CanListChildren -eq $true -or $CanListChildren -eq $false){$body["capabilities"].Add("CanListChildren",$CanListChildren)}
if($CanManageMembers -eq $true -or $CanManageMembers -eq $false){$body["capabilities"].Add("canManageMembers",$CanManageMembers)}
if($CanReadRevisions -eq $true -or $CanReadRevisions -eq $false){$body["capabilities"].Add("canReadRevisions",$CanReadRevisions)}
if($CanRemoveChildren -eq $true -or $CanRemoveChildren -eq $false){$body["capabilities"].Add("canRemoveChildren",$CanRemoveChildren)}
if($CanRename -eq $true -or $CanRename -eq $false){$body["capabilities"].Add("canRename",$CanRename)}
if($CanRenameTeamDrive -eq $true -or $CanRenameTeamDrive -eq $false){$body["capabilities"].Add("canRenameTeamDrive",$CanRenameTeamDrive)}
if($CanShare -eq $true -or $CanShare -eq $false){$body["capabilities"].Add("canShare",$CanShare)}

$body = $body | ConvertTo-Json
$URI = "https://www.googleapis.com/drive/v3/teamdrives?requestId=$RequestID"
try
{
$response = Invoke-RestMethod -Method Post -Uri $URI -Headers $header -Body $body -ContentType "application/json" | ForEach-Object {if($_.kind -like "*#*"){$_.PSObject.TypeNames.Insert(0,$(Convert-KindToType -Kind $_.kind));$_}else{$_}}
}
catch
{
try
{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$resp = $reader.ReadToEnd()
$response = $resp | ConvertFrom-Json |
Select-Object @{N="Error";E={$Error[0]}},@{N="Code";E={$_.error.Code}},@{N="Message";E={$_.error.Message}},@{N="Domain";E={$_.error.errors.domain}},@{N="Reason";E={$_.error.errors.reason}}
Write-Error "$(Get-HTTPStatus -Code $response.Code): $($response.Domain) / $($response.Message) / $($response.Reason)"
return
}
catch
{
Write-Error $resp
return
}
}
return $response
}
62 changes: 62 additions & 0 deletions Public/Remove-GSTeamDrive.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function Remove-GSTeamDrive {
[cmdletbinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
Param
(
[parameter(Mandatory=$true,Position=0)]
[String]
$TeamDriveID,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$Owner = $Script:PSGSuite.AdminEmail,
[parameter(Mandatory=$false)]
[String]
$AccessToken,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$P12KeyPath = $Script:PSGSuite.P12KeyPath,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$AppEmail = $Script:PSGSuite.AppEmail,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[String]
$AdminEmail = $Script:PSGSuite.AdminEmail
)
if (!$AccessToken)
{
$AccessToken = Get-GSToken -P12KeyPath $P12KeyPath -Scopes "https://www.googleapis.com/auth/drive" -AppEmail $AppEmail -AdminEmail $Owner
}
$header = @{
Authorization="Bearer $AccessToken"
}
$URI = "https://www.googleapis.com/drive/v3/teamdrives/$TeamDriveID"
if ($PSCmdlet.ShouldProcess($User))
{
try
{
$response = Invoke-RestMethod -Method Delete -Uri $URI -Headers $header -ContentType "application/json"
if (!$response){Write-Host "Team Drive $TeamDriveID successfully removed from owner $Owner"}
}
catch
{
try
{
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$resp = $reader.ReadToEnd()
$response = $resp | ConvertFrom-Json |
Select-Object @{N="Error";E={$Error[0]}},@{N="Code";E={$_.error.Code}},@{N="Message";E={$_.error.Message}},@{N="Domain";E={$_.error.errors.domain}},@{N="Reason";E={$_.error.errors.reason}}
}
catch
{
$response = $resp
}
}
}
return $response
}
Loading

0 comments on commit 8a9e1ca

Please sign in to comment.