Forum Discussion

usnishguha's avatar
usnishguha
Icon for Neophyte rankNeophyte
4 years ago

Regarding GO API Programming

Hi All,

 I am new to both GO as well as to API Programming. I have downloaded the SDK and kept it inside one of our Linux boxes (where GO is installed). When I am running go mod tidy, this is the output I am receiving. 

[root@server]# go mod tidy
go: finding module for package github.com/go-openapi/runtime
go: finding module for package github.com/go-openapi/runtime/client
go: finding module for package github.com/go-openapi/strfmt
go: finding module for package github.com/logicmonitor/lm-sdk-go/client/lm
go: finding module for package github.com/go-openapi/errors
go: finding module for package github.com/go-openapi/swag
go: finding module for package github.com/logicmonitor/lm-sdk-go/models
go: finding module for package github.com/go-openapi/validate
github.com/LM/github.com/logicmonitor/lm-sdk-go/client imports
        github.com/go-openapi/runtime: module github.com/go-openapi/runtime: Get "https://proxy.golang.org/github.com/go-openapi/runtime/@v/list": read tcp x.x.x.x :21126->142.250.191.113:443: read: connection reset by peer
github.com/LM/github.com/logicmonitor/lm-sdk-go/client imports
        github.com/go-openapi/runtime/client: module github.com/go-openapi/runtime/client: Get "https://proxy.golang.org/github.com/go-openapi/runtime/client/@v/list": read tcp x.x.x.x:21130->142.250.191.113:443: read: connection reset by peer
github.com/LM/github.com/logicmonitor/lm-sdk-go/client imports
        github.com/go-openapi/strfmt: module github.com/go-openapi/strfmt: Get "https://proxy.golang.org/github.com/go-openapi/strfmt/@v/list": read tcp x.x.x.x:21132->142.250.191.113:443: read: connection reset by peer

I am inside this path /home/user/LM and the SDKs are unzipped under /home/user/github.com. I have put "x" instead of the actual server IP. 
Please suggest.

Thanks

  • Anonymous's avatar
    Anonymous

    Ok, here's a quick tutorial on getting the python sdk up and running. I did this in docker because it's easier than installing a full vm and it's conceptually simpler (to me) than virtual python environments. Following this completely requires that you have docker installed on a machine that has access to the internet.

    1. Start with the ubuntu:latest container. This should be equivalent to Ubuntu 20.04.3 LTS:
    docker run --rm -it ubuntu:latest

    2. Install python 3 and pip (3) (and make sure everything is up to date):
    apt update
    apt upgrade -y
    apt install -y python3 python3-pip
    pip3 install --upgrade pip

    3. Install the logicmonitor sdk:
    pip3 install logicmonitor-sdk

    4. Write your script (this one fetches 50 alerts, see documentation for other methods you can run in the try block):

    from __future__ import print_function
    import time
    import logicmonitor_sdk
    from logicmonitor_sdk.rest import ApiException
    from pprint import pprint
    
    
    # Configure API key authorization: LMv1
    configuration = logicmonitor_sdk.Configuration()
    configuration.company = 'YOUR PORTAL NAME'
    configuration.access_id = 'YOUR ACCESS ID'
    configuration.access_key = 'YOUR ACCESS KEY'
    
    # create an instance of the API class
    api_instance = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(configuration))
    
    try:
        # get alert list
        api_response = api_instance.get_alert_list()
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling LMApi->getAlertList: %s\n" % e)

     

    If you will be using it a lot, or if you want to use the interactive python interpreter, you can put my LM Wrapper in the same directory as your script and call it. It will do all the messy work of setting up the objects and get you ready in one line:

    4. After step 3 (Instead of writing out the whole script), install nano and curl:
    apt install -y curl nano

    5. Download the wrapper:
    curl -o lm.py https://raw.githubusercontent.com/sweenig/lmcommunity/master/LMAutomation/lmwrapper/lm.py

    6. Setup your creds file. This will be a json file in the same directory called "creds.json". It should contain JSON with your access id, key, and company name:

    {
      "API_ACCESS_ID": "adelarthrosomatous",
      "API_ACCESS_KEY": "zygomaticoauricularis",
      "COMPANY_NAME": "yourportalname"
    }

     

    7. Either write your script or run it interactively:

    from lm import lm
    alerts = lm.get_alert_list()
    devices = lm.get_device_list(size=1000)

     

  • Anonymous's avatar
    Anonymous

    I'll see if I can make time next week to make a walkthrough. 

    Python3 -m pip install logicmonitor-sdk

    Should work if you have internet access. 

     

     

     

  • @usnishguha if your linux host doesn't have open internet connectivity, that's probably why `go mod tidy` is failing; it can't download dependencies.

  • Hi @Stuart Weenig, I tried that. Python kept failing due to multiple library dependencies. I am looking for something like a "Step by Step" guide to run at least one on of the simplest programs in Python from Linux. I think I am missing something, also in our environment its not possible to "pip install ****" due to not having open internet connectivity from Linux.

  • Anonymous's avatar
    Anonymous

    I don't know anything about Go, but if you're new to both Go and Python and you have the option, I'd recommend you choose Python and use that SDK.