Today I knocked out some Comvault code to integrate with our other automation here in our environment, I coded this out in VB.net to match it’s surrounding integrations.
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 |
Imports System.IO Imports System.Net Imports System.Text Imports System.Xml Public Module CommVault Private service As String = "http://FQDNofCommVault.Domain.Com:81/SearchSvc/CVWebService.svc/" Public Function CommVaultLogin() As String Dim user As String = "UserName" Dim pwd As String = "PasswordGoeshere" Dim token As String = GetSessionToken(user, pwd) If String.IsNullOrEmpty(token) Then Debug.WriteLine("Login Failed") Else Debug.WriteLine("Login Successful") End If CVRESTAPIGetClientSample(token) End Function Private Function GetSessionToken(ByVal userName As String, ByVal password As String) As String Dim token As String = String.Empty Dim loginService As String = service & "Login" Dim pwd As Byte() = System.Text.Encoding.UTF8.GetBytes(password) Dim encodedPassword As String = Convert.ToBase64String(pwd, 0, pwd.Length, Base64FormattingOptions.None) Dim loginReq As String = String.Format("<DM2ContentIndexing_CheckCredentialReq username=""{0}"" password=""{1}"" />", userName, encodedPassword) Dim resp As HttpWebResponse = SendRequest(loginService, "POST", Nothing, loginReq) If resp.StatusCode = HttpStatusCode.OK Then Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load(resp.GetResponseStream()) token = xmlDoc.SelectSingleNode("/DM2ContentIndexing_CheckCredentialResp/@token").Value Else Debug.WriteLine(String.Format("Login Failed. Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription)) End If Return token End Function Private Function SendRequest(ByVal serviceURL As String, ByVal httpMethod As String, ByVal token As String, ByVal requestBody As String) As HttpWebResponse Dim req As WebRequest = WebRequest.Create(serviceURL) req.Method = httpMethod req.ContentType = "application/xml; charset=utf-8" If Not String.IsNullOrEmpty(token) Then req.Headers.Add("Authtoken", token) If Not String.IsNullOrEmpty(requestBody) Then WriteRequest(req, requestBody) Return TryCast(req.GetResponse(), HttpWebResponse) End Function Public Sub CVRESTAPIGetClientSample(token As String) Dim clientPropService As String = service & "client/" + CStr("1005") 'Some Random ID' Dim ClientResp As HttpWebResponse = SendRequest(clientPropService, "GET", token, Nothing) If ClientResp.StatusCode = HttpStatusCode.OK Then Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load(ClientResp.GetResponseStream()) Debug.WriteLine(String.Format("Client properties response: ", xmlDoc.InnerXml)) Dim clientName As String = xmlDoc.SelectSingleNode("/App_GetClientPropertiesResponse/clientProperties/client/clientEntity/@clientName").Value Dim clientHostName As String = xmlDoc.SelectSingleNode("/App_GetClientPropertiesResponse/clientProperties/client/clientEntity/@hostName").Value Dim clientDescription As String = xmlDoc.SelectSingleNode("/App_GetClientPropertiesResponse/clientProperties/client/@clientDescription").Value Else Debug.WriteLine(String.Format("Get Client properties request Failed. Status Code: {0}, Status Description: {1}", ClientResp.StatusCode, ClientResp.StatusDescription)) End If End Sub Private Sub WriteRequest(ByVal req As WebRequest, ByVal input As String) req.ContentLength = Encoding.UTF8.GetByteCount(input) Using stream As Stream = req.GetRequestStream() stream.Write(Encoding.UTF8.GetBytes(input), 0, Encoding.UTF8.GetByteCount(input)) End Using End Sub Public Function CommvaultLoginDoesNotWork() As String Dim objHTTP = CreateObject("MSXML2.serverXMLHTTP") Dim strEnvelope = "{ ""domain"":"""", ""username"":""svc_User"", ""password"":""MyPassw0rdgoesHere"", ""commserver"":"""" }" Dim InfobloxUsername As String = Environment.GetEnvironmentVariable("CommVaultusername") If InfobloxUsername = "" Then Console.WriteLine("You do not have a username set, enter it now. If not Control+C, in the future use set CommVaultusername=[username]") Environment.SetEnvironmentVariable("CommVaultusername", Console.ReadLine.Trim) InfobloxUsername = Environment.GetEnvironmentVariable("CommVaultusername") End If Dim InfobloxPassword As String = Environment.GetEnvironmentVariable("CommVaultpassword") If InfobloxPassword = "" Then Console.WriteLine("You do not have a username set, enter it now. If not Control+C, in the future use set CommVaultpassword=[password]") Environment.SetEnvironmentVariable("CommVaultpassword", Console.ReadLine.Trim) InfobloxPassword = Environment.GetEnvironmentVariable("CommVaultpassword") End If objHTTP.SetOption(2, 13056) objHTTP.Open("POST", (service & "Login"), False, InfobloxUsername, InfobloxPassword) objHTTP.setRequestHeader("Content-Type", "application/json; charset=utf-8") objHTTP.send(strEnvelope) Dim response As String = objHTTP.responseText Debug.WriteLine(response) If response.Contains("""Error"":") Then Console.WriteLine(response) Environment.Exit(-1) Else Return response End If End Function End Module |