goidoit

package module
v0.0.0-...-85695c6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2019 License: MIT Imports: 10 Imported by: 0

README

i-doit-go-api/goidoit

GoDoc Go Report Card License Build Status Built with Mage Coverage

is an I-doit API client implementation written in GOLANG.

Its focused on simplicity to create easy writable code for your projects.

the package supports:

  • Login
  • Requests
  • Searching
  • Getting object information by id, id slice, string or custom field
  • creating, altering, deleting, archiving, quickpurging objects
  • reports
  • categories (only parts implemented) (wip)
  • object_type_categories
  • dialog

Other types of requests like

  • object_types (coming soon)
  • object_type_groups (coming soon)
  • objects_by_relation (coming soon)
  • categories (category, category_info) (wip)
  • location_tree
  • workstation_components
  • logbook
  • impact

are not implemented yet (and maybe never will) but can easily be implemented using the goidoit.Request function there are some advanced examples of how to do this.

There is also a second repo i-doit-go-tools where you can find some more scripts for special use-cases that can be used as an even more advanced example :P.

Installation

If you already installed GO on your system and configured it properly than its simply:

go get github.com/cseeger-epages/i-doit-go-api

If not follow these instructions.

Usage

Simple example
package main

import (
	"fmt"
	"github.com/cseeger-epages/i-doit-go-api"
)

func main() {
  // create api object
  a, _ := goidoit.NewAPI("https://example.com/src/jsonrpc.php", "yourapikey")

  // or login using X-RPC-Auth (recommended)
  // a, _ := goidoit.NewLogin("https://example.com/src/jsonrpc.php", "yourapikey", "username", "password")

  // search for sth like test
  s, _ := a.Search("test")

  // thats it
  fmt.Println(s)
}
Advanced examples

There are more advanced examples documented in the repo for common use cases.

Code Documentation

You find the full code documentation here

Code Testing

This library uses mage for testing and coverage. After installing mage just run:

mage test

also code coverage in detail can be seen via browser by using

mage coverage

Additional Informations

This project uses sematic versioning.

Other great client implementations

Thanks for your code inspirations!

Contribution

Thank you for participating to this project. Please see our Contribution Guidlines for more information.

Documentation

Overview

Package goidoit is an https://www.i-doit.com/ API client implementation written in https://golang.org

Install using go get

go get github.com/cseeger-epages/i-doit-go-api

Initialize your API object

a, err := goidoit.NewLogin("https://example.com/src/jsonrpc.php", "yourapikey", "username", "password")
if err != nil {
	log.Fatal(err)
}

and do stuff like get your idoit version or search stuff etc.

v, err := a.Version()
if err != nil {
	log.Fatal(err)
}

fmt.Println(v.Result[0]["version"])

s, err := a.Search("test")
if err != nil {
	log.Fatal(err)
}

for _, v := range s.Result {
	fmt.Println(v)
}

for Debuging use

goidoit.Debug(true)

and to disable TLS Certificate Verification (if you use self signed certs for i-doit)

goidoit.SkipTLSVerify(true)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(v bool)

Debug function to enable debug output

func GetParams

func GetParams(a API, parameters interface{}) interface{}

GetParams is used to append nessesary parameters to user provided one

func SkipTLSVerify

func SkipTLSVerify(v bool)

SkipTLSVerify disable SSL/TLS verification for self signed certificates

Types

type API

type API struct {
	URL, APIkey, Username, Password, SessionID string
}

API struct used for implementing the apiMethods interface

func NewAPI

func NewAPI(url string, apikey string) (*API, error)

NewAPI implements API constructor

func NewLogin

func NewLogin(url string, apikey string, username string, password string) (*API, error)

NewLogin implements API constructor using login x-rpc auth

func (*API) CreateCat

func (a *API) CreateCat(ObjID int, CatgID string, Params interface{}) (GenericResponse, error)

CreateCat creates a category given by its category ID and the defined content in params for a given object ID

func (*API) CreateObj

func (a *API) CreateObj(Params interface{}) (GenericResponse, error)

CreateObj creates an object

func (*API) DelCatObj

func (a *API) DelCatObj(ObjID int, CatgID string, CateID string) (GenericResponse, error)

DelCatObj deletes a given category for a given object

func (*API) DeleteObj

func (a *API) DeleteObj(deleteMe interface{}) (GenericResponse, error)

DeleteObj delete/archive/purge an object

func (*API) GetCat

func (a *API) GetCat(objID int, query interface{}) (GenericResponse, error)

GetCat returns the category defined by query for an object given by its object ID

func (*API) GetDialog

func (a *API) GetDialog(category, property string) (GenericResponse, error)

GetDialog returns the dialog+ defined values

func (*API) GetObjTypeCat

func (a *API) GetObjTypeCat(query interface{}) (GenericResponse, error)

GetObjTypeCat returns the objecttype category

func (*API) GetObject

func (a *API) GetObject(query interface{}) (GenericResponse, error)

GetObject function returns an object matching the given query

func (*API) GetObjectByType

func (a *API) GetObjectByType(objType string, obj interface{}) (GenericResponse, error)

GetObjectByType function return an object of a specific type

func (*API) GetObjectsByType

func (a *API) GetObjectsByType(objType string) (GenericResponse, error)

GetObjectsByType returns multiple objects of the given type

func (*API) GetReport

func (a *API) GetReport(RepID int) (GenericResponse, error)

GetReport returns the report given by its id

func (API) IsLoggedIn

func (a API) IsLoggedIn() bool

IsLoggedIn checks the login status

func (*API) Login

func (a *API) Login() error

Login implements idoit.login method

func (API) Logout

func (a API) Logout() error

Logout implements idoit.logout method

func (*API) Quickpurge

func (a *API) Quickpurge(ids interface{}) (GenericResponse, error)

Quickpurge ftw

func (API) Request

func (a API) Request(method string, parameters interface{}) (Response, error)

Request implements basic idoit requests

func (*API) Search

func (a *API) Search(query string) (GenericResponse, error)

Search implements the idoit.search method

func (*API) UpdateCat

func (a *API) UpdateCat(ObjID int, CatgID string, Params interface{}) (GenericResponse, error)

UpdateCat updates a category given by its category ID and the defined content in params for a given object ID

func (*API) UpdateObj

func (a *API) UpdateObj(Params interface{}) (GenericResponse, error)

UpdateObj updates an object

func (*API) Version

func (a *API) Version() (GenericResponse, error)

Version implements idoit.version method

type APIMethods

type APIMethods interface {
	// idoit.login using X-RPC-AUTH
	// goidoit.NewLogin wraps this function
	// eg. idoit.NewLogin(<url>, <api-key>, <username>, <password>)
	Login() error

	// check login state
	IsLoggedIn() bool

	// Destroy X-RPC-Session
	Logout() error

	// i-doit api request structure
	// as defined in https://kb.i-doit.com/pages/viewpage.action?pageId=7831613
	// also there is a list of methods available
	Request(string, interface{}) (Response, error)

	// search CMDB using a string
	//
	// The search function does handle type assertions
	// for simple output usage
	Search(string) (GenericResponse, error)

	// version information
	Version() (GenericResponse, error)

	// get object(s) data,
	// input can be of type int, []int, string or a custom filter struct
	GetObject(interface{}) (GenericResponse, error)

	// shortcut function for Type Filter using C__OBJTYPE const
	GetObjectByType(string, interface{}) (GenericResponse, error)

	// shortcut function for get all objects of type <C__OBJTYPE__>
	GetObjectsByType(string) (GenericResponse, error)

	// get Object Type Categories using category id or constant
	// eg. GetObjTypeCat("C__OBJTYPE__PERSON")
	// or GetObjTypeCat(50)
	GetObjTypeCat(interface{}) (GenericResponse, error)

	// create objects using struct
	CreateObj(interface{}) (GenericResponse, error)

	// update object
	UpdateObj(interface{}) (GenericResponse, error)

	/* Delete/Archive/Purge object, input can be int (using the object id) or
	data := struct {
		Id int `json:"id"`
		Status string `json:"status"`
	}

	where Id represents the object id
	and Status can be
	C__RECORD_STATUS__ARCHIVED
	C__RECORD_STATUS__DELETED
	C__RECORD_STATUS__PURGE
	*/
	DeleteObj(interface{}) (GenericResponse, error)

	// fast delete option where archive, delete and purge will be executed one after another
	// accepts id or []id as input
	Quickpurge(interface{}) (GenericResponse, error)

	// get categorys for object using object id and category id or category constant
	// eg. GetCat(20,50)
	// or GetCat(20,"C__CATG__CUSTOM_FIELD_TEST")
	GetCat(int, interface{}) (GenericResponse, error)

	// create category using struct
	CreateCat(int, string, interface{}) (GenericResponse, error)

	// update category using struct
	UpdateCat(int, string, interface{}) (GenericResponse, error)

	// delete entry from category
	DelCatObj(int, string, string) (GenericResponse, error)

	// get report data via id
	GetReport(int) (GenericResponse, error)
}

APIMethods is the interface describing all importand functions

type APIkey

type APIkey struct {
	APIkey string `json:"apikey"`
}

APIkey used for requests

type F1

type F1 struct {
	Data []int `json:"ids"`
}

F1 implements an object filter type int or []int

type F2

type F2 struct {
	Data string `json:"title"`
}

F2 implements an Object filter type string

type GenericResponse

type GenericResponse struct {
	Jsonrpc string
	Result  []map[string]interface{}
	Error   IdoitError
}

GenericResponse implements a generic i-doit api response structure the map is used to handle type assertions

func TypeAssertResult

func TypeAssertResult(data Response) (GenericResponse, error)

TypeAssertResult is a generic type assert function

type IdoitError

type IdoitError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

IdoitError implements i-doit api error structure

type OF1

type OF1 struct {
	Title []int  `json:"ids"`
	Type  string `json:"type"`
}

OF1 implements a more complex object type filter for ids and type

type OF2

type OF2 struct {
	Title string `json:"title"`
	Type  string `json:"type"`
}

OF2 implements a more complex object type filter for title and type

type OSF1

type OSF1 struct {
	Type string `json:"type"`
}

OSF1 implements object type only filter

type Request

type Request struct {
	Version string      `json:"version"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params"`
	ID      int         `json:"id"`
}

Request implements i-doit api request structure

type Response

type Response struct {
	Jsonrpc string      `json:"jsonrpc"`
	Result  interface{} `json:"result"`
	Error   IdoitError  `json:"error"`
}

Response implements i-doit api response structure

func ParseResponse

func ParseResponse(resp *http.Response) Response

ParseResponse parses json response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL