nexus.psm1

# Implement your module commands in this script.
$Script:nexusUrl = "http://nexus3.tool.zfu.zb:8081"
$user = 'admin'
$pass = 'admin123'
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($user):$($pass)"))

$Script:Headers = @{Authorization = "Basic $encodedCreds"}

function Search-NexusComponents {
    [CmdletBinding()]
    param (
        # Keyword in regex
        [ValidateNotNullOrEmpty()]
        [string]
        $keyword,
        # Repository
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $repositoryName
    )

    begin {
    }

    process {

        try {
            $nexusUrl = "$Script:nexusUrl/service/rest/v1/search?repository=$repositoryName"
            $result = $null
            $continuationToken = $null
            do {
                $response = $null
                if(-not [string]::IsNullOrEmpty($continuationToken)) {
                    $nexusUrl = "$Script:nexusUrl/service/rest/v1/search?repository=$repositoryName&continuationToken=$continuationToken"
                }
                Write-Verbose -Message "$($MyInvocation.MyCommand) --> Created url for searching Nexus components:$nexusUrl"
                $response = Invoke-RestMethod -Uri $nexusUrl -Method Get -Headers $Script:Headers -NoProxy -ErrorVariable errorDetails
                $result += $response.items | Where-Object {$_.Name -cmatch $keyword}
                $continuationToken = $response.continuationToken
            } while (-not [string]::IsNullOrEmpty($response.continuationToken))
            return $result
        }
        catch {
            $exception = $PSItem | Select-Object * | Format-Custom -Depth 1 | Out-String
            Write-Error -Message $exception -ErrorAction Stop
        }
    }

    end {
    }
}

function Get-NexusComponentsInGroup {
    [CmdletBinding()]
    param (
        # Repository name
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $repositoryName,
        # Component Group
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $componentGroup
    )

    begin {
    }

    process {
        try {
            $nexusUrl = "$Script:nexusUrl/service/rest/v1/search/assets?repository=$repositoryName&group=$componentGroup"
            $result = $null
            $continuationToken = $null
            do {
                $response = $null
                if(-not [string]::IsNullOrEmpty($continuationToken)) {
                    $nexusUrl = "$Script:nexusUrl/service/rest/v1/search?repository=$repositoryName&continuationToken=$continuationToken"
                }
                Write-Verbose -Message "$($MyInvocation.MyCommand) --> Created url for searching Nexus components:$nexusUrl"
                $response = Invoke-RestMethod -Uri $nexusUrl -Method Get -Headers $Script:Headers -NoProxy -ErrorVariable errorDetails
                $result += $response.items | Where-Object {$_.Name -cmatch $keyword}
                $continuationToken = $response.continuationToken
            } while (-not [string]::IsNullOrEmpty($response.continuationToken))
        return $result
        }
        catch {
            $exception = $PSItem | Select-Object * | Format-Custom -Depth 1 | Out-String
            Write-Error -Message $exception -ErrorAction Stop
        }

    }

    end {
    }
}

function Push-NexusArtifact {
    [CmdletBinding()]
    param (
        # Nexus artifact Url
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $artifactUrl,
        # Artifact path
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $artifactPath
    )

    begin {
    }

    process {
        try {
            Invoke-RestMethod -Uri $artifactUrl -Method Put -Headers $Script:Headers -NoProxy -InFile $artifactPath
        }
        catch {
            $exception = $PSItem | Select-Object * | Format-Custom -Depth 1 | Out-String
            Write-Error -Message $exception -ErrorAction Stop
        }

    }

    end {
    }
}

function Remove-NexusComponent {
    [CmdletBinding()]
    param (
        # Id of component
        [Parameter(Mandatory,ValueFromPipeline)]
        [string]
        $ComponentID
    )

    begin {

    }

    process {
        try {
            $nexusUrl = "$Script:nexusUrl/service/rest/v1/components/$ComponentID"
            Invoke-RestMethod -Uri $nexusUrl -Method Delete -Headers $Script:Headers -NoProxy
        }
        catch {
            $exception = $PSItem | Select-Object * | Format-Custom -Depth 1 | Out-String
            Write-Error -Message $exception -ErrorAction Stop
        }
    }

    end {

    }
}

# Export only the functions using PowerShell standard verb-noun naming.
# Be sure to list each exported functions in the FunctionsToExport field of the module manifest file.
# This improves performance of command discovery in PowerShell.
Export-ModuleMember -Function *-*