Public/Acronis/Enable-AcronisAllOfferings.ps1

function Enable-AcronisAllOfferings {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$CustomerId
    )

    try {
        #Check if Cyper protection is already enabled
        $response = Invoke-AcronisApi -Uri "api/2/tenants/$($CustomerId)/applications" -Method Get
        $cyberProtectionApp = $response.items | Where-Object { $_ -eq '6e6d758d-8e74-3ae3-ac84-50eb0dff12eb' }

        if (-not $cyberProtectionApp) {
            #Enabeling Cyber Protection
            Write-ModuleLog -Message "Cyber Protection Acronis Application is not enabled for customer $CustomerId, enabling now..." -Level Verbose -Component 'AcronisAllOfferings'
            $response = Invoke-AcronisApi -Uri "api/2/applications/6e6d758d-8e74-3ae3-ac84-50eb0dff12eb/bindings/tenants/$($CustomerId)" -Method Post -ContentType 'application/json'
            $response = Invoke-AcronisApi -Uri "api/2/tenants/$($CustomerId)/applications" -Method Get
            $cyberProtectionApp = $response.items | Where-Object { $_ -eq '6e6d758d-8e74-3ae3-ac84-50eb0dff12eb' }
            if ($cyberProtectionApp) {
                Write-ModuleLog -Message "Cyber Protection Acronis Application has been successfully enabled for customer $CustomerId" -Level Verbose -Component 'AcronisAllOfferings'
            }
            else {
                Write-ModuleLog -Message "Failed to enable Cyber Protection Acronis Application for customer $CustomerId" -Level Error -Component 'AcronisAllOfferings'
                Write-ModuleLog -Message "Response: $($response | ConvertTo-Json -Depth 10)" -Level Error -Component 'AcronisAllOfferings'
                throw "Failed to enable Cyber Protection Acronis Application for customer $CustomerId"
            }
        }
        else {
            Write-ModuleLog -Message "Cyber Protection Acronis Application is already enabled for customer $CustomerId" -Level Verbose -Component 'AcronisAllOfferings'
        }
    }
    catch {
        throw $_.Exception.Message
    }

    try {
        $queryParameters = @{
            edition = "pck_per_workload"
            kind = "customer"
        }
        
        $response = Invoke-AcronisApi -Method Get -Uri "api/2/tenants/$($script:DefaultConfig.AcronisTenantId)/offering_items/available_for_child" -RawBody $queryParameters
        $offeringItems = $response.items       
        $json = @{ offering_items = $offeringItems } | ConvertTo-Json -Depth 100
        $result = Invoke-AcronisApi -Method Put -Uri "api/2/tenants/$($CustomerId)/offering_items" -RawBody $json -ContentType "application/json"
        if( $result.items ) {
            Write-ModuleLog -Message "Successfully enabled all offerings for customer $CustomerId" -Level Verbose -Component 'AcronisAllOfferings'
            return $result.items
        }
        else {
            Write-ModuleLog -Message "No offerings were enabled for customer $CustomerId" -Level Warning -Component 'AcronisAllOfferings'
        }
    }
    catch {
        Write-ModuleLog -Message "Failed to enable all offerings for customer $CustomerId" -Level Error -Component 'AcronisAllOfferings' -ErrorRecord $_
    }
}