Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CreateProject.ps1 #147

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 74 additions & 23 deletions REST/PowerShell/Projects/CreateProject.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,82 @@
# ==================================================================================================================
# This script creates a standard Project on the specified Octopus Server.
# To make the new Project Git-enabled (config-as-code), navigate to the new Project URL > Settings > Version Control
# ==================================================================================================================

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "https://youroctourl"
$octopusAPIKey = "API-YOURAPIKEY"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "default"
$projectName = "MyProject"
$projectDescription = "MyDescription"
$projectGroupName = "Default project group"
$lifecycleName = "Default lifecycle"
# ====== BYPASS PROMPTS? ======
$BypassPrompts = $false # Set to $true if you wish to predefine your parameters


# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}
# ====== PARAMETERS ======
If ($BypassPrompts) {

# === Predefined Parameters (Optional) ===
$OctopusURL = "http://YOUR_OCTOPUS_URL.bla"
$OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX"
$SpaceId = "Spaces-XX"
$ProjectName = "My Project"
$ProjectDescription = "My Description"
$ProjectGroupName = "Default Project Group"
$LifecycleName = "Default Lifecycle"
}

# Get project group
$projectGroup = (Invoke-RestMethod -Method Get "$octopusURL/api/$($space.Id)/projectgroups/all" -Headers $header) | Where-Object {$_.Name -eq $projectGroupName}
If (!$BypassPrompts) {

# === Prompted Parameters ===
$OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"')
$OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"')
$SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"')
$ProjectName = (Read-Host "Enter a name for your new Project (example: `"My Project`")").trim('"')
$ProjectDescription = (Read-Host "Enter a description for your new Project (example: `"My Description`")").trim('"')
$ProjectGroupName = (Read-Host "Enter the name of an existing Project Group in `"$($SpaceId)`" for your new Project (example: `"Default Project Group`")").trim('"')
$LifecycleName = (Read-Host "Enter the name of an existing Lifecycle in `"$($SpaceId)`" for your new Project (example: `"Default Lifecycle`")").trim('"')
}

# Get Lifecycle
$lifeCycle = (Invoke-RestMethod -Method Get "$octopusURL/api/$($space.Id)/lifecycles/all" -Headers $header) | Where-Object {$_.Name -eq $lifecycleName}
$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }

# Create project json payload
$jsonPayload = @{
Name = $projectName
Description = $projectDescription
ProjectGroupId = $projectGroup.Id
LifeCycleId = $lifeCycle.Id

# ====== SCRIPT BODY ======
# Try to GET the ProjectGroupId for $ProjectGroupName
Try {
$ProjectGroup = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/ProjectGroups/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectGroupName}
If (!$ProjectGroup) {throw}
}
Catch {
Write-Warning "Unable to find a ProjectGroupId for the Project Group Name `"$($ProjectGroupName)`" via `"$($OctopusURL)/api/$($SpaceId)/ProjectGroups/all`""
Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine."
break
}

# Create project
Invoke-RestMethod -Method Post -Uri "$octopusURL/api/$($space.Id)/projects" -Body ($jsonPayload | ConvertTo-Json -Depth 10) -Headers $header
# Try to GET the LifecycleId for $LifecycleName
Try {
$Lifecycle = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Lifecycles/all" -Headers $Header) | Where-Object {$_.Name -eq $LifecycleName}
If (!$Lifecycle) {throw}
}
Catch {
Write-Warning "Unable to find a LifecycleId for the Lifecycle Name `"$($LifecycleName)`" via `"$($OctopusURL)/api/$($SpaceId)/Lifecycles/all`""
Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine."
break
}

# Create Json payload for new Project creation
$JsonPayload = @{
Name = $ProjectName
Description = $ProjectDescription
ProjectGroupId = $ProjectGroup.Id
LifecycleId = $Lifecycle.Id
}

# Create Project using $JsonPayload
Try {
$CheckProjName = (Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName}
If ($CheckProjName) {throw}
Else {
$NewProject = Invoke-RestMethod -Method POST -Uri "$($OctopusURL)/api/$($SpaceId)/projects" -Body ($JsonPayload | ConvertTo-Json -Depth 10) -Headers $Header
Write-Host "You may view your new Project at: $($OctopusURL)$($NewProject.Links.Self)"
}
}
Catch {
Write-Warning "A Project with the name `"$($ProjectName)`" already exists in `"$($SpaceId)`". Please choose a Project Name that does not exist in this Space."
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including more detailed exceptions for other possibilities could be a good improvement, to save someone scratching their head at why a server error returned a message saying a project already exists.

Catch {
    $exception = $_.Exception
    if($exception.Message.Contains("404") -or $exception.Message.Contains("403")){
        write-host "Request failed with the following message: $($exception.Message)"
    }else{
        Write-Warning "A Project with the name `"$($ProjectName)`" already exists in `"$($SpaceId)`". Please choose a Project Name that does not exist in this Space."
    }
}

79 changes: 66 additions & 13 deletions REST/PowerShell/Projects/DeleteProjectByName.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
$ErrorActionPreference = "Stop";
# ==================================================================================================================
# This script deletes a Project on the specified Octopus Server.
# ==================================================================================================================

# Define working variables
$octopusURL = "https://youroctourl"
$octopusAPIKey = "API-YOURAPIKEY"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "Default"
$projectName = "MyProject"
$ErrorActionPreference = "Stop";

# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}
# ====== BYPASS PROMPTS? ======
$BypassPrompts = $false # Set to $true if you wish to predefine your parameters

# Get project
$project = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header) | Where-Object {$_.Name -eq $projectName}

# Delete project
Invoke-RestMethod -Method Delete -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)" -Headers $header
# ====== PARAMETERS ======
If ($BypassPrompts) {

# === Predefined Parameters (Optional) ===
$OctopusURL = "http://YOUR_OCTOPUS_URL.bla"
$OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX"
$SpaceId = "Spaces-XX"
$ProjectName = "My Project"
}

If (!$BypassPrompts) {

# === Prompted Parameters ===
$OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"')
$OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"')
$SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"')
$ProjectName = (Read-Host "Enter a name for your new Project (example: `"My Project`")").trim('"')
}

$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }

# ====== SCRIPT BODY ======
# Try to GET the ProjectId for $ProjectName
Try {
$Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName}
If (!$Project) {throw}
}
Catch {
Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`""
Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine."
break
}

# Confirmation (ignored if $BypassPrompts = $true)
If (!$BypassPrompts) {
Write-Host "=================================================================================================================================="
$Confirm = (Read-Host "Are you sure you want to DELETE the Project `"$ProjectName`" ($($Project.Id))? This cannot be undone. (Type Y to continue or N to quit)").trim('"')
While (($Confirm -ne "N") -and ($Confirm -ne "Y")) {
$Confirm = (Read-Host "Are you sure you want to DELETE the Project `"$ProjectName`" ($($Project.Id))? This cannot be undone. (Type Y to continue or N to quit)").trim('"')
}
If ($Confirm -eq "N") {
Write-Warning "Aborted. No changes were made."
break
}
}

# Delete Project
Invoke-RestMethod -Method DEL "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header
Try {
$DeleteCheck = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName}
If (!$DeleteCheck) {
Write-Host "The Project named `"$($ProjectName)`" ($($Project.Id)) was DELETED."
}
If ($DeleteCheck) {throw}
}
Catch {
Write-Warning "Unable to DELETE the Project `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)`""
Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine."
break
}

This file was deleted.

83 changes: 83 additions & 0 deletions REST/PowerShell/Projects/DisableOrEnableProject.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# ==================================================================================================================
# This script disables or enables a Project on the specified Octopus Server.
# ==================================================================================================================

$ErrorActionPreference = "Stop";

# ====== BYPASS PROMPTS? ======
$BypassPrompts = $false # Set to $true if you wish to predefine your parameters


# ====== PARAMETERS ======
If ($BypassPrompts) {

# === Predefined Parameters (Optional) ===
$OctopusURL = "http://YOUR_OCTOPUS_URL.bla"
$OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX"
$SpaceId = "Spaces-XX"
$ProjectName = "My Project"
$ProjectEnabled = $false
}

If (!$BypassPrompts) {

# === Prompted Parameters ===
$OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"')
$OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"')
$SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"')
$ProjectName = (Read-Host "Enter a name for your new Project (example: `"My Project`")").trim('"')
$ProjectStatusPrompt = (Read-Host "Type `"D`" to Disable or `"E`" to Enable `"$($ProjectName)`"").trim('"')
While (($ProjectStatusPrompt -ne "D") -and ($ProjectStatusPrompt -ne "E")) {
$ProjectStatusPrompt = (Read-Host "Type `"D`" to Disable or `"E`" to Enable `"$($ProjectName)`"").trim('"')
}
If ($ProjectStatusPrompt -eq "D") {$ProjectEnabled = $false}
If ($ProjectStatusPrompt -eq "E") {$ProjectEnabled = $true}
}

$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }


# ====== SCRIPT BODY ======
# Try to GET the ProjectId for $ProjectName
Try {
$Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName}
If (!$Project) {throw}
}
Catch {
Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`""
Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine."
break
}

# Check if Project is already Disabled/Enabled
If ($Project.IsDisabled -eq !$ProjectEnabled) {
If ($Project.IsDisabled) {
Write-Host "`"$($ProjectName)`" ($($Project.Id)) already disabled! No action required!"
break
}
If (!$Project.IsDisabled) {
Write-Host "`"$($ProjectName)`" ($($Project.Id)) already enabled! No action required!"
break
}
}

# Disable/Enable Project
$Project.IsDisabled = !$ProjectEnabled
If ($ProjectEnabled -eq $false) {
Write-Host "Disabling `"$($ProjectName)`" ($($Project.Id))"
}
If ($ProjectEnabled -eq $true) {
Write-Host "Enabling `"$($ProjectName)`" ($($Project.Id))"
}
# Save Project changes
Try {
$SaveProject = Invoke-RestMethod -Method PUT -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header -Body ($Project | ConvertTo-Json -Depth 10)
}
Catch {
Write-Warning "Something went wrong when attempting a PUT via `"$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)`"."
}

$ProjectCheck = Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header
If ($ProjectCheck.IsDisabled -eq !$ProjectEnabled) {
Write-Host "Success!"
}
23 changes: 0 additions & 23 deletions REST/PowerShell/Projects/DisableProject.ps1

This file was deleted.

Loading