-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-dashboard-api.ps1
More file actions
107 lines (91 loc) · 3.44 KB
/
create-dashboard-api.ps1
File metadata and controls
107 lines (91 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Check if the path to the JSON file is provided as a parameter
if ($args.Length -eq 0) {
Write-Host "Usage: $scriptName <path_to_dashboard_json>"
exit 1
}
$dashboardJsonFile = $args[0]
# Replace 'your_admin_username' and 'your_admin_password' with valid Grafana credentials
$adminUsername = 'admin'
$adminPassword = 'admin'
# Base64 encode the credentials
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$adminUsername"+":"+"$adminPassword"))
# Set the Grafana API endpoint
$apiUrl = 'http://localhost:3000/api/orgs'
# Define the organization name
$orgName = 'apiorg4'
# Create headers with the authorization information
$headers = @{
Authorization = "Basic $base64AuthInfo"
'Content-Type' = 'application/json'
}
# Create the body of the request
$body = @{
name = $orgName
} | ConvertTo-Json
# Make the REST API call to create the organization
try {
# Make the REST API call to create the organization
$orgResponse = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body
# Extract orgId from the response
$orgId = $orgResponse.orgId
Write-Output "Organization ID: $orgId"
}
catch {
Write-Host "Failed step1 to create organization. Error: $($_.Exception.Message)"
exit 1
}
# Step 2: Switch org context
try {
Invoke-RestMethod -Uri "http://localhost:3000/api/user/using/$orgId" -Method Post -Headers $headers
}
catch {
Write-Host "Failed step2 to switch organization context. Error: $($_.Exception.Message)"
exit 1
}
# Step 3: Create Service Account
try {
# Create the body of the request
$body2 = @{
name = "test"
role = "Admin"
} | ConvertTo-Json
$saResponse = Invoke-RestMethod -Uri 'http://localhost:3000/api/serviceaccounts' -Method Post -Headers $headers -Body $body2
$saId = $saResponse.id
}
catch {
Write-Host "Failed step3 to create service account. Error: $($_.Exception.Message)"
exit 1
}
# Step 4: Create Service Account token
try {
$tokenResponse = Invoke-RestMethod -Uri "http://localhost:3000/api/serviceaccounts/$saId/tokens" -Method Post -Headers $headers -Body '{"name":"test-token"}'
$tokenKey = $tokenResponse.key
}
catch {
Write-Host "Failed step4 to create service account token. Error: $($_.Exception.Message)"
exit 1
}
$keyTokenApi = $tokenKey # Replace with the actual API key
$configDataSources= Get-Content -Path .\grafana\datasource-prometheus.json
# Step 5 : Add Data source Prometheus
try {
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Invoke-RestMethod -Uri 'http://localhost:3000/api/datasources' -Method Post -Headers @{"Authorization"="Bearer $keyTokenApi"; "Content-Type"="application/json"} -Body $configDataSources
Write-Output "DataSource Prometheus creation successful."
}
catch {
Write-Host "Failed to create DataSource Prometheus. Error: $($_.Exception.Message)"
exit 1
}
# Step 6: Create the Dashboard from JSON File
$dashboardContent = Get-Content $dashboardJsonFile | Out-String
Write-Host $dashboardContent
try {
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Invoke-RestMethod -Uri 'http://localhost:3000/api/dashboards/db' -Method Post -Headers @{"Authorization"="Bearer $keyTokenApi"; "Content-Type"="application/json"} -Body $dashboardContent
Write-Output "Dashboard creation successful."
}
catch {
Write-Host "Failed to create dashboard. Error: $($_.Exception.Message)"
exit 1
}