• 0

Regarding GO API Programming


usnishguha
 Share

Question

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

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

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.

Link to comment
Share on other sites

  • 0
  • Administrators

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)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share