https://github.com/tinuwalther/psautomic
Example with Pode Rest APIs
https://github.com/tinuwalther/psautomic
api pode powershell pwsh
Last synced: 20 days ago
JSON representation
Example with Pode Rest APIs
- Host: GitHub
- URL: https://github.com/tinuwalther/psautomic
- Owner: tinuwalther
- Created: 2023-04-07T08:55:51.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-13T14:52:18.000Z (almost 2 years ago)
- Last Synced: 2025-01-15T23:12:19.541Z (over 1 year ago)
- Topics: api, pode, powershell, pwsh
- Language: PowerShell
- Homepage:
- Size: 93.9 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PowerShell RestAPI
Example with Pode Rest APIs. In this example I demostrate how you can create and delete a Docker Image and Container over RestAPI.
````mermaid
sequenceDiagram
Postman->>RestAPI: invoke request
RestAPI->>FileWatcher: queue result
RestAPI->>FileWatcher: queue result
FileWatcher->>Docker: new docker container
FileWatcher->>Docker: del docker container
FileWatcher->>Docker: new docker container
````
## Requirements
This example require the following PowerShell Modules:
- Microsoft.PowerShell.SecretManagement
- SecretManagement.KeePass
- Pode
You need also to install Docker Desktop and KeePass on your computer.
## Configure KeePass
Create a KeePassDB with the name 'PSOctomes' on your computer and define an Entry with a Username and Password for the Bearer Token that you can access the RestAPI.
Modify the script Config-Secrets.ps1 and enter the path to your KeePass-File.
Execute the script Config-Secrets.ps1.
## Start Pode RestAPI
Open a PowerShell or Terminal and start the Pode server. If you send the Request the first one, you have to enter the KeePass Master Password.
````powershell
.\PSAutoMic\bin\Start-PSAutoMic.ps1
Running Pode server on D:\DevOps\github.com\PSAutoMic\bin
Press Ctrl. + C to terminate the Pode server
Listening on the following 1 endpoint(s) [2 thread(s)]:
- http://localhost:8080/
Keepass Master Password
Enter the Keepass Master password for: C:\Users\Admin\OneDrive\Do*ument*\PSOctomes.kdbx
Password for user Keepass Master Password: ********
````

## Request a Linux over PowerShell
Current available Kernel:
- Almalinux
- Ubuntu
- Photon OS
Request your first almalinux over RestAPI. The owner is also the logged-in user and is member of sudoers.
````powershell
$BearerToken = ""
$headers = @{
'Content-Type' = 'application/json'
'Authorization' = "Bearer $BearerToken"
}
$body = @{
hostname = 'almalnx'
os = 'almalinux'
version = '9'
imagename = 'almal_image'
container = 'almal_container'
action = 'create'
owner = 'tinu'
scout = $false
pass = 'T0pS£creT!'
} | ConvertTo-Json -Compress
$Properties = @{
Method = 'POST'
Headers = $headers
Uri = "http://localhost:8080/api/v1/docker"
Body = $body
}
$response = Invoke-RestMethod @Properties
````
Almalinux is created:

## Remove a Linux over PowerShell
Remove your almalinux over RestAPI.
````powershell
$BearerToken = ""
$headers = @{
'Content-Type' = 'application/json'
'Authorization' = "Bearer $BearerToken"
}
$body = @{
os = 'almalinux'
imagename = 'almal_image'
container = 'almal_container'
action = 'delete'
} | ConvertTo-Json -Compress
$Properties = @{
Method = 'POST'
Headers = $headers
Uri = "http://localhost:8080/api/v1/docker"
Body = $body
}
$response = Invoke-RestMethod @Properties
````
## Request a Linux over Postman
Send a RestAPI call to create a ubuntu over Postman:

Ubuntu is created:

Docker containers:

## Request a Linux over Bruno
Send a RestAPI call to create an Almalinux over Bruno:

Almalinux is created:


## Start a container interactive
To start a container, that already exists:
````powershell
docker start photon_container -i
````