DX Infrastructure Manager

Expand all | Collapse all

Web service method to orchestrate maintenance mode

  • 1.  Web service method to orchestrate maintenance mode

    Posted 01-21-2015 10:50 PM

    Hi All

     

    Is there any API to enable maintenance mode on USM groups and/or individual server?

     

    Thanks



  • 2.  Re: Web service method to orchestrate maintenance mode

    Posted 01-22-2015 12:07 AM


  • 3.  Re: Web service method to orchestrate maintenance mode

    Posted 01-31-2015 08:11 PM

    And because calling REST functions can be a little daunting until you've done it once, here's an example to parse an Excel spreadsheet and create accounts and contacts (solves the "no bulk user load" issue with UMP). This runs as a VB script (file.vbs) on Windows 7. Should work fine on other similar OS versions.

     

    You need to adjust the URL to your UMP instnace, username, password, and path to the excel file for this to work but that should be pretty straight forward.

     

    Oh, and as far as credit goes, there's a lot of material here scraped from other web examples and I appologize in advance for not retaining credit. If you are interested, I did very little to adjust the actual code snippits so dropping an obscure piece into Google will likely take you to where I got the smippit from. The usage and assemply is mine though.

     

    And I left a number of the popup boxes from debugging in place - not sure why but sometimes the REST calls fail if executed too quickly and the delay of having to click the OK button eliminates that. That might be fixed in 8.1 but it's easier to click the OK button than unwind corrupted accounts and contacts.

     

    Option Explicit
    Dim WshShell

    sub CreateAccount(AccountName, FirstName, LastName, Origins, Site, Email)
    Dim restReq, url, userName, password, data, accountIDPath, oParser, accountID
    Dim contactID, contactIDPath, a, x

    userName = "ZZZZZZZZZZZZ"
    password = "ZZZZZZZZZZZZ"

    '##########################################
    ' Create account

    url = "https://ump.instance.com/rest/accounts"
    data = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> " & _
    "<account> " & _
    " <address>Teststreet 42</address> " & _
    " <city>Testcity</city> " & _
    " <country>TestCountry</country> " & _
    " <description>" & Site & "</description> " & _
    " <fax>Test Fax no 4711</fax> " & _
    " <name>" & AccountName & "</name> " & _
    " <origins></origins> " & _
    " <phone>Test phone 0815</phone> " & _
    " <postalCode>123456</postalCode> " & _
    " <state>Teststate</state> " & _
    " <webSite>www.testrest.com</webSite> " & _
    " </account>"

    wscript.echo data

    Set restReq = CreateObject("MSXML2.ServerXMLHTTP")
    restReq.open "POST", url, false, username, password
    restReq.setRequestHeader "Content-Type", "application/xml"
    restReq.send data

    WScript.echo restReq.responseText

    '##########################################
    ' Get account id for newly created account

    accountIDPath = "//account/accountId"

    Set oParser = CreateObject("Microsoft.XMLDOM")
    oParser.LoadXML restReq.responseText
    set accountID = oParser.documentElement.selectSingleNode (accountIDPath )

    wscript.echo "New Account ID " & accountID.text

    set oParser = nothing
    set restReq = nothing

    '##########################################
    ' Associate origins with new account

    url = "https://ump.instance.com/rest/accounts/" & accountID.text & "/origins"
    data = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> " & _
    "<origins> "
    a=split(Origins, ":")
    for each x in a
    data = data & " <origin>" & x & "</origin> "
    next
    data = data & " </origins>"

    wscript.echo (data)

    Set restReq = CreateObject("MSXML2.ServerXMLHTTP")
    restReq.open "POST", url, false, username, password
    restReq.setRequestHeader "Content-Type", "application/xml"
    restReq.send data

    'WScript.echo restReq.responseText

    set restReq = nothing

    '##########################################
    ' Create contact and associate with account

    url = "https://ump.instance.com/rest/contacts"
    data = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?> " & _
    "<contact> " & _
    " <accountId>" & accountID.text & "</accountId> " & _
    " <acl>Customer</acl> " & _
    " <contactId>0</contactId> " & _
    " <firstName>" & FirstName & "</firstName> " & _
    " <lastName>" & LastName & "</lastName> " & _
    " <loginName>" & AccountName & "</loginName> " & _
    " <email>" & Email & "</email> " & _
    " <password>Merge123</password> " & _
    "</contact>"

    'wscript.echo data

    Set restReq = CreateObject("MSXML2.ServerXMLHTTP")
    restReq.open "POST", url, false, username, password
    restReq.setRequestHeader "Content-Type", "application/xml"
    restReq.send data

    'WScript.echo restReq.responseText

    '##########################################
    ' Get contact id for newly created contact

    contactIDPath = "//contact/contactId"

    Set oParser = CreateObject("Microsoft.XMLDOM")
    oParser.LoadXML restReq.responseText
    set contactId = oParser.documentElement.selectSingleNode (contactIDPath )

    'wscript.echo "New Contact ID " & contactID.text

    set oParser = nothing
    set restReq = nothing
    end sub

    Set WshShell = WScript.CreateObject("WScript.Shell")

    Dim objExcel, objWkBook, objSheet, myXlsFile, objRow, nConsecutiveBlankRows

    ' Get a reference to an Excel Application object:
    Set objExcel = CreateObject("Excel.Application")

    myXlsFile = "C:\Users\user.name\Desktop\PortalUsers.xlsx"
    Set objWkBook = objExcel.Workbooks.Open(myXlsFile)
    Set objSheet = objWkBook.Sheets(1)

    nConsecutiveBlankRows = 0
    For Each objRow In objSheet.Rows
    ' Check to see if we've reached a row in which
    ' the first cell is empty. We'll want to ignore
    ' these and keep track of how many we find in a
    ' row so we can exit the loop without having to
    ' spend time going through every single row in
    ' the spreadsheet:
    If objRow.Cells(1, 2).Value = "" or objRow.Cells(1, 1).Value = "account" Then
    nConsecutiveBlankRows = nConsecutiveBlankRows + 1
    If nConsecutiveBlankRows > 5 Then Exit For
    Else
    ' We found a non-blank row, so reset our counter
    nConsecutiveBlankRows = 0
    'AccountName, FirstName, LastName, Origins, site, email
    CreateAccount objRow.Cells(1, 1).Value, objRow.Cells(1, 2).Value, objRow.Cells(1, 3).Value, objRow.Cells(1, 4).Value, objRow.Cells(1, 5).Value, objRow.Cells(1, 6).Value
    End If
    next

    objWkBook.Close False ' False = "Don't save"
    objExcel.Quit

    set WshShell = nothing

     



  • 4.  Re: Web service method to orchestrate maintenance mode

    Posted 02-01-2015 01:21 AM

    Here's another example for perl, if you're not on windows. It's only login though, but at least it's a start.

     

    http://forum.nimsoft.com/t5/General/How-to-Login-by-usingRest-API/m-p/22795#M8268

     

    -jon



  • 5.  Re: Web service method to orchestrate maintenance mode

    Posted 02-18-2015 08:43 PM

    Here is some example perl code using json to get accounts and create a account.

     

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    use MIME::Base64;
    use Data&colon;:smileyvery-happy:umper;
     
    # http://search.cpan.org/~makamaka/JSON/lib/JSON.pm
    # Example install using cpanm:
    #   sudo cpanm -i JSON
    use JSON;
     
    # http://search.cpan.org/~mcrawfor/REST-Client/lib/REST/Client.pm
    # Example install using cpanm:
    #   sudo cpanm -i REST::Client
    use REST::Client;
     
    # Set the request parameters
    my $host = 'http://umpurl/rest';
    my $user = 'administrator';
    my $pwd = 'admin';
    my %request_body_map = (
            'address' => 'test2' , 
            'city' => 'testcity2' ,
            'country' => 'USA' ,
            'description' => 'test from rest with update' ,
            'fax' => '',
            'name' => 'RestTest2' ,
            'origin' => 'primary' ,
            'phone' => '' ,
            'postalCode' => '' ,
            'state' => 'Co' ,
            'website' => ''
    );
    
     
    my $client = REST::Client->new(host => $host);
    
    my $encoded_auth = encode_base64("$user:$pwd", '');
    
    #gets account add account id at end to get specific but output changes then
    $client->GET('/accounts',
                  {'Authorization' => "Basic $encoded_auth",
                   'Content-Type' => 'application/json',
                   'Accept' => 'application/json'});
    
    
    if ($client->responseCode() == '200') {
      print 'Response: ' . $client->responseContent() . "\n";
      print 'Status: ' . $client->responseCode() . "\n";
      print "\n";
      my $jsonobj = JSON::from_json($client->responseContent);
      foreach my $account ( @{ $jsonobj->{account} } )
      {
            printf "$account->{accountId}  -  $account->{name} - $account->{origin}\n";
           print $jsonobj->{'alarm'}[0]->{'level'} . "\n" . $jsonobj->{'alarm'}[0]->{'severity'} . "\n" . $jsonobj->{'alarm'}[7]->{'message'} . "\n";
      }
      print "\n";
      foreach ( $client->responseHeaders() ) {
        print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
      }
    }
    else {
      print 'Response status: ' . $client->responseCode() . "\n";
      print 'Response: ' . $client->responseContent() . "\n";
      die('GET request failed');
    }
     
    # POST to the incident table
    $client->POST('/accounts',
                    encode_json(\%request_body_map),
                    {'Authorization' => "Basic $encoded_auth",
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json'});
    
    if ($client->responseCode() == '200') {
            print "\nGood to Go\n";
            print 'Response: ' . $client->responseContent() . "\n";
            print 'Response status: ' . $client->responseCode() . "\n";
            foreach ( $client->responseHeaders() ) {
              print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n";
            }
    }
    else {
            print "\nfailed reponse code\n";
            print 'Response: ' . $client->responseContent() . "\n";
            print 'Response status: ' . $client->responseCode() . "\n";
    }