Public/Approve-EdgeDevApp.ps1

# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Function Approve-EdgeDevApp {
    <#
    .SYNOPSIS
        Approve a developer app, or a credential within an app.

    .DESCRIPTION
        Set the status of the developer app to 'Approved', which means the credentials
        will be treated as valid, at runtime. Or, alternatively, approve a single
        credential within a developer app.

    .PARAMETER AppName
        The name of the app. You must specify the -Developer option if you use -AppName.

    .PARAMETER Name
        Synonum for AppName.

    .PARAMETER AppId
        The id of the app. Use this in lieu of -AppName and -Developer.

    .PARAMETER Developer
        The id or email of the developer that owns the app.

    .PARAMETER Key
        The Key to revoke. Use this to revoke a single credential, rather than the entire app.

    .PARAMETER Org
        The Apigee Edge organization. The default is to use the value from Set-EdgeConnection.

    .EXAMPLE
        Revoke-EdgeDevApp -Name abcdefg-1 -Developer Elaine@example.org

    .EXAMPLE
        Revoke-EdgeDevApp -Name abcdefg-1 -Developer Elaine@example.org -Key B34aa6c286524cF1A8Dd

    .FUNCTIONALITY
        ApigeeEdge

    #>


    [cmdletbinding()]
    PARAM(
        [string]$Name,
        [string]$AppName,
        [string]$Developer,
        [string]$AppId,
        [string]$Key,
        [string]$Org
    )

    $Options = @{
       QParams = $( ConvertFrom-HashtableToQueryString @{ action = 'approve' } )
    }

    if ($PSBoundParameters['Debug']) {
        $Options['Debug'] = $Debug
    }

    if ($PSBoundParameters['Developer']) {
        if (!$PSBoundParameters['Name'] -and !$PSBoundParameters['AppName']) {
            throw [System.ArgumentNullException] "AppName", 'use -AppName and -Developer.'
        }
        $RealAppName = if ($PSBoundParameters['AppName']) { $AppName } else { $Name }
        # also handle key approval? Not sure I like this option.
        if ($PSBoundParameters['Key']) {
            $Options['Collection'] = $( Join-Parts -Separator '/' -Parts 'developers', $Developer, 'apps', $RealAppName, 'keys' )
            $Options['Name'] = $Key
        }
        else {
            $Options['Collection'] = $( Join-Parts -Separator '/' -Parts 'developers', $Developer, 'apps' )
            $Options['Name'] = $RealAppName
        }
    }
    else {
        if (!$PSBoundParameters['AppId']) {
          throw [System.ArgumentNullException] "AppId", 'use -AppId if not specifying -AppName and -Developer'
        }
        if ($PSBoundParameters['Key']) {
            $Options['Collection'] = $( Join-Parts -Separator '/' -Parts 'apps', $AppId, 'keys' )
            $Options['Name'] = $Key
        }
        else {
          $Options['Collection'] = 'apps'
          $Options['Name'] = $AppId
        }
    }

    if ($PSBoundParameters['Org']) {
        $Options['Org'] = $Org
    }

    Write-Debug ([string]::Format("Approve-EdgeDevApp Options {0}`n", $(ConvertTo-Json $Options -Compress ) ) )
    Send-EdgeRequest @Options
}