Public/object-store.ps1

# Object storage pools and the buckets created in them.

function Get-CSObjectStoragePool {
    <#
    .SYNOPSIS
        Lists object storage pools.

    .DESCRIPTION
        Wraps listObjectStoragePools, the object stores (for example MinIO, Ceph RGW,
        or Simulator) registered for buckets.

    .PARAMETER Id
        Filter by object storage pool ID

    .PARAMETER Name
        Filter by name

    .PARAMETER Keyword
        Filter by keyword

    .PARAMETER Page
        Page number of results to return

    .PARAMETER PageSize
        Number of results per page

    .EXAMPLE
        Get-CSObjectStoragePool
        Lists the registered object storage pools.
    #>

    [CmdletBinding()]
    param(
        [string]$Id,

        [string]$Name,

        [string]$Keyword,

        [int]$Page,

        [int]$PageSize
    )

    $apiParams = @{}
    Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
        Id = 'id'; Name = 'name'; Keyword = 'keyword'; Page = 'page'; PageSize = 'pagesize'
    })
    ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'listObjectStoragePools' -Parameters $apiParams) -Command 'listObjectStoragePools'
}

function Add-CSObjectStoragePool {
    <#
    .SYNOPSIS
        Adds an object storage pool.

    .DESCRIPTION
        Wraps addObjectStoragePool, registering an object store that buckets can be
        created in. -Url and -Provider are provider-specific; extra settings go in
        -Details.

    .PARAMETER Name
        Name for the pool

    .PARAMETER Url
        The pool's endpoint URL

    .PARAMETER Provider
        The object store provider (for example MinIO, Simulator)

    .PARAMETER Details
        Extra provider settings as a hashtable (for example access/secret keys)

    .EXAMPLE
        Add-CSObjectStoragePool -Name 'minio-1' -Url 'http://10.0.0.8:9000' -Provider MinIO -Details @{ accesskey = 'AK'; secretkey = 'SK' }
        Registers a MinIO object store.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Name,

        [Parameter(Mandatory = $true)]
        [string]$Url,

        [Parameter(Mandatory = $true)]
        [string]$Provider,

        [hashtable]$Details
    )

    $apiParams = @{ name = $Name; url = $Url; provider = $Provider }
    Add-CSMapParameter -ApiParameters $apiParams -Name 'details' -Map $Details
    if ($PSCmdlet.ShouldProcess("object storage pool $Name", 'Add')) {
        ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'addObjectStoragePool' -Parameters $apiParams) -Command 'addObjectStoragePool'
    }
}

function Set-CSObjectStoragePool {
    <#
    .SYNOPSIS
        Updates an object storage pool.

    .DESCRIPTION
        Wraps updateObjectStoragePool. Only the attributes you supply are changed.
        Accepts object storage pool objects on the pipeline.

    .PARAMETER Id
        The pool to update. Binds from a piped pool's id.

    .PARAMETER Name
        New name

    .PARAMETER Url
        New endpoint URL

    .EXAMPLE
        Set-CSObjectStoragePool -Id $poolId -Url 'https://minio.example.com'
        Updates a pool's endpoint.

    .EXAMPLE
        Get-CSObjectStoragePool -Name 'minio-1' | Set-CSObjectStoragePool -Name 'minio-primary'
        Renames a pool located by name.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('ObjectStoragePoolId', 'ObjectStorageId')]
        [string]$Id,

        [string]$Name,

        [string]$Url
    )

    process {
        $apiParams = @{ id = $Id }
        Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
            Name = 'name'; Url = 'url'
        })
        if ($PSCmdlet.ShouldProcess("object storage pool $Id", 'Update')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'updateObjectStoragePool' -Parameters $apiParams) -Command 'updateObjectStoragePool'
        }
    }
}

function Remove-CSObjectStoragePool {
    <#
    .SYNOPSIS
        Deletes an object storage pool.

    .DESCRIPTION
        Wraps deleteObjectStoragePool. Accepts object storage pool objects on the
        pipeline.

    .PARAMETER Id
        The pool to delete. Binds from a piped pool's id.

    .EXAMPLE
        Remove-CSObjectStoragePool -Id $poolId
        Deletes an object storage pool after confirmation.

    .EXAMPLE
        Get-CSObjectStoragePool -Name 'old-minio' | Remove-CSObjectStoragePool
        Deletes a pool located by name.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('ObjectStoragePoolId', 'ObjectStorageId')]
        [string]$Id
    )

    process {
        if ($PSCmdlet.ShouldProcess("object storage pool $Id", 'Delete')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'deleteObjectStoragePool' -Parameters @{ id = $Id }) -Command 'deleteObjectStoragePool'
        }
    }
}

function Get-CSBucket {
    <#
    .SYNOPSIS
        Lists buckets.

    .DESCRIPTION
        Wraps listBuckets, the buckets created across object storage pools.

    .PARAMETER Id
        Filter by bucket ID

    .PARAMETER Name
        Filter by bucket name

    .PARAMETER ObjectStorageId
        Filter by object storage pool ID

    .PARAMETER Account
        Filter by account name. Must be used with -DomainId.

    .PARAMETER DomainId
        Filter by domain ID

    .PARAMETER ProjectId
        Filter by project ID

    .PARAMETER Keyword
        Filter by keyword

    .PARAMETER Page
        Page number of results to return

    .PARAMETER PageSize
        Number of results per page

    .EXAMPLE
        Get-CSBucket
        Lists every bucket the caller can see.

    .EXAMPLE
        Get-CSBucket -ObjectStorageId $poolId
        Lists the buckets in one object storage pool.
    #>

    [CmdletBinding()]
    param(
        [string]$Id,

        [string]$Name,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string]$ObjectStorageId,

        [string]$Account,

        [string]$DomainId,

        [string]$ProjectId,

        [string]$Keyword,

        [int]$Page,

        [int]$PageSize
    )

    process {
        $apiParams = @{}
        Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
            Id = 'id'; Name = 'name'; ObjectStorageId = 'objectstorageid'; Account = 'account'; DomainId = 'domainid'
            ProjectId = 'projectid'; Keyword = 'keyword'; Page = 'page'; PageSize = 'pagesize'
        })
        ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'listBuckets' -Parameters $apiParams) -Command 'listBuckets'
    }
}

function New-CSBucket {
    <#
    .SYNOPSIS
        Creates a bucket in an object storage pool.

    .DESCRIPTION
        Wraps createBucket. Optional features (versioning, encryption, object
        locking) and a -Quota can be set at creation, along with a bucket -Policy
        (Public or Private).

    .PARAMETER Name
        Name for the bucket

    .PARAMETER ObjectStorageId
        The object storage pool to create the bucket in

    .PARAMETER Quota
        Bucket quota in GB

    .PARAMETER Policy
        Bucket access policy: Public or Private

    .PARAMETER Encryption
        Enable server-side encryption

    .PARAMETER Versioning
        Enable object versioning

    .PARAMETER ObjectLocking
        Enable object locking (write-once-read-many)

    .EXAMPLE
        New-CSBucket -Name 'backups' -ObjectStorageId $poolId -Quota 500 -Versioning
        Creates a versioned bucket with a 500 GB quota.

    .EXAMPLE
        Get-CSObjectStoragePool -Name 'minio-1' | New-CSBucket -Name 'archive' -Policy Private
        Creates a private bucket in a pool piped in by object.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Name,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Id')]
        [string]$ObjectStorageId,

        [int]$Quota,

        [ValidateSet('Public', 'Private')]
        [string]$Policy,

        [switch]$Encryption,

        [switch]$Versioning,

        [switch]$ObjectLocking
    )

    process {
        $apiParams = @{ name = $Name; objectstorageid = $ObjectStorageId }
        Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
            Quota = 'quota'; Policy = 'policy'; Encryption = 'encryption'; Versioning = 'versioning'; ObjectLocking = 'objectlocking'
        })
        if ($PSCmdlet.ShouldProcess("bucket $Name", 'Create')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'createBucket' -Parameters $apiParams) -Command 'createBucket'
        }
    }
}

function Set-CSBucket {
    <#
    .SYNOPSIS
        Updates a bucket's properties.

    .DESCRIPTION
        Wraps updateBucket. Only the properties you supply are changed. Accepts
        bucket objects on the pipeline.

    .PARAMETER Id
        The bucket to update. Binds from a piped bucket's id.

    .PARAMETER Quota
        New quota in GB

    .PARAMETER Policy
        New access policy: Public or Private

    .PARAMETER Encryption
        Whether server-side encryption is enabled

    .PARAMETER Versioning
        Whether object versioning is enabled

    .PARAMETER ObjectLocking
        Whether object locking is enabled

    .EXAMPLE
        Set-CSBucket -Id $bucketId -Quota 1000
        Raises a bucket's quota.

    .EXAMPLE
        Get-CSBucket -Name 'backups' | Set-CSBucket -Versioning $true
        Enables versioning on a bucket located by name.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('BucketId')]
        [string]$Id,

        [int]$Quota,

        [ValidateSet('Public', 'Private')]
        [string]$Policy,

        [Nullable[bool]]$Encryption,

        [Nullable[bool]]$Versioning,

        [Nullable[bool]]$ObjectLocking
    )

    process {
        $apiParams = @{ id = $Id }
        Add-CSOptionalParameter -ApiParameters $apiParams -BoundParameters $PSBoundParameters -Map ([ordered]@{
            Quota = 'quota'; Policy = 'policy'
        })
        foreach ($p in @('Encryption', 'Versioning', 'ObjectLocking')) {
            if ($PSBoundParameters.ContainsKey($p)) {
                $apiParams[$p.ToLowerInvariant()] = ([bool](Get-Variable -Name $p -ValueOnly)).ToString().ToLowerInvariant()
            }
        }
        if ($PSCmdlet.ShouldProcess("bucket $Id", 'Update')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'updateBucket' -Parameters $apiParams) -Command 'updateBucket'
        }
    }
}

function Remove-CSBucket {
    <#
    .SYNOPSIS
        Deletes an empty bucket.

    .DESCRIPTION
        Wraps deleteBucket. The bucket must be empty. Accepts bucket objects on the
        pipeline.

    .PARAMETER Id
        The bucket to delete. Binds from a piped bucket's id.

    .EXAMPLE
        Remove-CSBucket -Id $bucketId
        Deletes an empty bucket after confirmation.

    .EXAMPLE
        Get-CSBucket -Name 'scratch' | Remove-CSBucket
        Deletes a bucket located by name.
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('BucketId')]
        [string]$Id
    )

    process {
        if ($PSCmdlet.ShouldProcess("bucket $Id", 'Delete')) {
            ConvertFrom-CSResponse -Response (Invoke-CSApiRequest -Command 'deleteBucket' -Parameters @{ id = $Id }) -Command 'deleteBucket'
        }
    }
}