Skip to content

Instantly share code, notes, and snippets.

@peterhpchen
Last active August 31, 2018 06:00
Show Gist options
  • Save peterhpchen/a1487789ab6cf28be955ef9edda808a8 to your computer and use it in GitHub Desktop.
Save peterhpchen/a1487789ab6cf28be955ef9edda808a8 to your computer and use it in GitHub Desktop.
Grafana Add, Update and Delete User To Organization and Login
<!--#include file="../util/base64.asp"--><!--https://stackoverflow.com/a/506992-->
<!--#include file="../util/jsonObject.class.asp" --><!--https://github.com/rcdmk/aspJSON-->
<%
response.LCID = 1046
Function AddGrafanaUser(grafanaUrl, basicAuth, orgName, userName, password, userRole)
Dim orgId
orgId = getOrgId(grafanaUrl, orgName, basicAuth)
If orgId = 0 then
orgId = createOrg(grafanaUrl, orgName, basicAuth)
End If
Dim userId
userId = createUser(grafanaUrl, orgName, userName, password, basicAuth)
addUserToOrg grafanaUrl, orgId, userName, userRole, basicAuth
switchUserToOrg grafanaUrl, orgId, userId, basicAuth
AddGrafanaUser = userId
End Function
Function switchUserToOrg(grafanaUrl, orgId, userId, basicAuth)
Dim url, body
url = grafanaUrl & "/api/users/" & Server.URLEncode(userId) & "/using/" & Server.URLEncode(orgId)
body = ""
Dim result
result = post(url, basicAuth, body, "message")
switchUserToOrg = result <> false
End Function
Function addUserToOrg(grafanaUrl, orgId, user, role, basicAuth)
Dim url, body
url = grafanaUrl & "/api/orgs/" & Server.URLEncode(orgId) & "/users"
body = "{""role"": """ & role & """, ""loginOrEmail"": """ & user & """}"
Dim result
result = post(url, basicAuth, body, "message")
addUserToOrg = result <> false
End Function
Function createUser(grafanaUrl, orgName, user, password, basicAuth)
Dim url, body
url = grafanaUrl & "/api/admin/users"
body = "{""login"": """ & user & """, ""password"": """ & password & """}"
Dim result
result = post(url, basicAuth, body, "id")
createUser = result
End Function
Function createOrg(grafanaUrl, orgName, basicAuth)
Dim url, body
url = grafanaUrl & "/api/orgs/"
body = "{""name"": """ & orgName & """}"
Dim result
result = post(url, basicAuth, body, "orgId")
createOrg = result
End Function
Function getOrgId(grafanaUrl, orgName, basicAuth)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "GET", grafanaUrl & "/api/orgs/name/" & Server.URLEncode(orgName)
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send
If xmlHttp.status <> 200 then
getOrgId = 0
Exit Function
End If
Set jsonObj = new JSONobject
jsonObj.parse(xmlHttp.responseText)
getOrgId = jsonObj("id")
Set xmlHttp = nothing
Set jsonObj = nothing
End Function
Function post(url, basicAuth, body, returnProp)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "POST", url
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send body
If xmlHttp.status <> 200 then
post = false
Exit Function
End If
Set jsonObj = New JSONobject
jsonObj.parse(xmlHttp.responseText)
post = jsonObj(returnProp)
Set xmlHttp = nothing
Set jsonObj = nothing
End Function
Dim basicAuth
basicAuth = "Basic " & Base64Encode("admin" & ":" & "admin")
AddGrafanaUser "http://localhost:3000", basicAuth, "TestProject", "viewer", "viewer", "Viewer"
%>
<!--#include file="../util/base64.asp"--><!--https://stackoverflow.com/a/506992-->
<!--#include file="../util/jsonObject.class.asp" --><!--https://github.com/rcdmk/aspJSON-->
<!--#include file="./get-organisation.asp" -->
<%
response.LCID = 1046
Function DeleteGrafanaOrganisation(grafanaUrl, orgName, basicAuth)
Dim orgId
orgId = GetOrganisationIdByName(grafanaUrl, orgName, basicAuth)
If orgId = 0 Then
Exit Function
End If
deleteOrganisation grafanaUrl, orgId, basicAuth
End Function
Function deleteOrganisation(grafanaUrl, orgId, basicAuth)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "DELETE", grafanaUrl & "/api/orgs/" & Server.URLEncode(orgId)
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send
deleteOrganisation = xmlHttp.status = 200
Set xmlHttp = nothing
End Function
Dim basicAuth
basicAuth = "Basic " & Base64Encode("admin" & ":" & "admin")
DeleteGrafanaOrganisation "http://localhost:3000", "TestProject", basicAuth
%>
<!--#include file="../util/base64.asp"--><!--https://stackoverflow.com/a/506992-->
<!--#include file="../util/jsonObject.class.asp" --><!--https://github.com/rcdmk/aspJSON-->
<!--#include file="./get-user.asp" -->
<%
response.LCID = 1046
Function DeleteGrafanaUser(grafanaUrl, userName, basicAuth)
Dim userId
userId = GetUserIdByName(grafanaUrl, userName, basicAuth)
If userId = 0 Then
Exit Function
End If
deleteUser grafanaUrl, userId, basicAuth
End Function
Function deleteUser(grafanaUrl, userId, basicAuth)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "DELETE", grafanaUrl & "/api/admin/users/" & Server.URLEncode(userId)
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send
deleteUser = xmlHttp.status = 200
Set xmlHttp = nothing
End Function
Dim basicAuth
basicAuth = "Basic " & Base64Encode("admin" & ":" & "admin")
DeleteGrafanaUser "http://localhost:3000", "viewer2", basicAuth
%>
<!-- This script needs jsonObject.class.asp -->
<%
response.LCID = 1046
Function GetOrganisationIdByName(grafanaUrl, orgName, basicAuth)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "GET", grafanaUrl & "/api/orgs/name/" & Server.URLEncode(orgName)
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send
If xmlHttp.status <> 200 then
GetOrganisationIdByName = 0
Exit Function
End If
Set jsonObj = new JSONobject
jsonObj.parse(xmlHttp.responseText)
GetOrganisationIdByName = jsonObj("id")
Set xmlHttp = nothing
Set jsonObj = nothing
End Function
%>
<!-- This script needs jsonObject.class.asp -->
<%
response.LCID = 1046
Function GetUserIdByName(grafanaUrl, userName, basicAuth)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "GET", grafanaUrl & "/api/users/lookup?loginOrEmail=" & Server.URLEncode(userName)
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send
If xmlHttp.status <> 200 then
GetUserIdByName = 0
Exit Function
End If
set jsonObj = new JSONobject
jsonObj.parse(xmlHttp.responseText)
GetUserIdByName = jsonObj("id")
Set xmlHttp = nothing
Set jsonObj = nothing
End Function
%>
<%
Function LoginGrafana(grafanaUrl, grafanaUser, grafanaPassword)
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "POST", grafanaUrl & "/login"
xmlhttp.setRequestHeader "Content-Type", "application/json"
Dim body
body = "{""user"": """ & grafanaUser & """, ""email"": """", ""password"": """ & grafanaPassword & """}"
xmlhttp.send body
If xmlhttp.status <> 200 Then
LoginGrafana = false
Exit Function
End If
LoginGrafana = true
Set cookieRegExp = New RegExp
cookieRegExp.Global = True
cookieRegExp.Multiline = True
cookieRegExp.Pattern = "Set-Cookie:[; ]+([\w-]+=[^\s;]*)"
Set cookieMatches = cookieRegExp.Execute(xmlhttp.getAllResponseHeaders())
For Each cookieMatch in cookieMatches
Set cookieSubMatches = cookieMatch.SubMatches
response.AddHeader "Set-Cookie", CStr("" & cookieSubMatches(0) & "; Path=/")
Set cookieSubMatches = nothing
Next
Set xmlHttp = nothing
Set cookieRegExp = nothing
Set cookieMatches = nothing
response.redirect grafanaUrl
End Function
Dim domain, port, host, url
domain = "172.16.12.248"
domain = "PC061203"
domain = "localhost"
port = "3000"
host = domain & ":" & port
url = "http://" & host
LoginGrafana url, "admin", "admin"
%>
<!--#include file="../util/base64.asp"--><!--https://stackoverflow.com/a/506992-->
<%
response.LCID = 1046
Function UpdateGrafanaUser(grafanaUrl, oldUserName, oldPassword, newUsername, newPassword)
updateUserName grafanaUrl, oldUserName, newUserName, oldPassword
updatePassword grafanaUrl, newUserName, oldPassword, newPassword
End Function
Function updatePassword(grafanaUrl, userName, oldPassword, newPassword)
Dim basicAuth, body
basicAuth = "Basic " & Base64Encode(userName & ":" & oldPassword)
body = "{""oldPassword"": """ & oldPassword & """, ""newPassword"": """ & newPassword & """, ""confirmNew"": """ & newPassword & """}"
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "PUT", grafanaUrl & "/api/user/password"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send body
updatePassword = xmlHttp.status = 200
Set xmlHttp = nothing
End Function
Function updateUserName(grafanaUrl, oldUserName, newUserName, password)
Dim basicAuth, body
basicAuth = "Basic " & Base64Encode(oldUserName & ":" & password)
body = "{""login"": """ & newUserName & """, ""email"": """ & newUserName & """}"
Set xmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
xmlHttp.open "PUT", grafanaUrl & "/api/user"
xmlhttp.setRequestHeader "Content-Type", "application/json"
xmlHttp.setRequestHeader "Authorization", basicAuth
xmlHttp.send body
updateUserName = xmlHttp.status = 200
Set xmlHttp = nothing
End Function
UpdateGrafanaUser "http://localhost:3000", "viewer1", "viewer1", "viewer2", "viewer2"
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment