cmdb_sdk

package module
v0.0.0-...-7e7a329 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

cmdb-sdk-golang

golang sdk of cmdb operations

install

go get "github.com/veops/cmdb-sdk-golang"

Operation of CI

// create a helper firstly
// urlPrefix is used to combine http request eg. the final query url with urlPrefix https://demo.veops.cn/api/v0.1 is https://demo.veops.cn/api/v0.1/ci/s
// key and secret is obtained from ACL
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#%E5%9B%9Bapi%E9%89%B4%E6%9D%83%E6%96%B9%E6%B3%95
helper := cmdb_sdk.NewHelper("urlprefix", "your key", "your secret")

// suppose you have created a ci type called mycitype
// with three attributes server_name, ip and custom_attr
// you can add a ci instance as following
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#2-%E6%96%B0%E5%A2%9Eci%E6%8E%A5%E5%8F%A3
attrs := map[string]any{
  "server_name": "test-1",
  "ip":          "192.168.0.1",
  "custom_attr": 123,
}
addCIRes, err := helper.AddCI("mycitype", cmdb_sdk.NoAttrPolicyDefault, cmdb_sdk.ExistPolicyDefault, attrs)

// you are able to get the ci instance created above by query now
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#1-ci%E6%9F%A5%E8%AF%A2%E6%8E%A5%E5%8F%A3
getCIRes, err := helper.GetCI("_type:mycitype", "", "", "", 0, 0, cmdb_sdk.RetKeyDefault)

// now let's update some attribute of a ci instance
// reminder that you can get ci_id from the return result of AddCI or instance info of GetCI
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#3-%E4%BF%AE%E6%94%B9ci%E6%8E%A5%E5%8F%A3
updates := map[string]any{
  "ip":          "127.0.0.1",
  "custom_attr": 321,
}
updateCIRes, err := helper.UpdateCI(666, "mycitype", cmdb_sdk.NoAttrPolicyDefault, updates)

// finally, delete this ci if you want
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#4-%E5%88%A0%E9%99%A4ci%E6%8E%A5%E5%8F%A3
deleteCIRes, err := helper.DeleteCI(666)

Operation of CI Relation

// create a helper firstly
helper := cmdb_sdk.NewHelper("urlprefix", "your key", "your secret")

// assuming you now have create a relation between two ci types
// and two ci instances with ci_id=666 and 777 sperately of these types
// you can add a relation to these instances
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#2-%E5%A2%9E%E5%8A%A0ci%E5%85%B3%E7%B3%BB%E6%8E%A5%E5%8F%A3
AddCIRRes, err := helper.AddRelation(666, 777)

// you will get a cr_id after the creation which can be used to delete the relation
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#3-%E5%88%A0%E9%99%A4ci%E5%85%B3%E7%B3%BB%E6%8E%A5%E5%8F%A3
deleteCIRRes, err := helper.DeleteRelation(666777, 0, 0)
// in the case that you don't keep the ci_id, you can delete relation by two ci_id
deleteCIRRes, err := helper.DeleteRelation(0, 666, 777)

// except for root_id,reverse and level, relation query is much similar to ci query
//
// doc https://github.com/veops/cmdb/blob/master/docs/cmdb_api.md#1-ci%E5%85%B3%E7%B3%BB%E6%9F%A5%E8%AF%A2%E6%8E%A5%E5%8F%A3
getCIRRes, err := helper.GetRelation(666, 0, "1", "", "", "", "", 0, 0, cmdb_sdk.RetKeyDefault)

Documentation

Index

Constants

View Source
const (
	RetKeyDefault = RetKey("")
	RetKeyID      = RetKey("id")
	RetKeyAlias   = RetKey("alias")
	RetKeyName    = RetKey("name")
)
View Source
const (
	NoAttrPolicyDefault = NoAttrPolicy("")
	NoAttrPolicyReject  = NoAttrPolicy("reject")
)
View Source
const (
	ExistPolicyDefault = ExistPolicy("")
	ExistPolicyNeed    = ExistPolicy("need")
	ExistPolicyReject  = ExistPolicy("reject")
	ExistPolicyReplace = ExistPolicy("replace")
)

Variables

This section is empty.

Functions

func NewHelperError

func NewHelperError(httpCode int, message string) error

Types

type AddCIResult

type AddCIResult struct {
	CIID int `json:"ci_id"`
}

type AddRelationResult

type AddRelationResult struct {
	RelationID int `json:"cr_id"`
}

type DeleteCIResult

type DeleteCIResult struct {
	Message string `json:"message"`
}

type DeleteRelationResult

type DeleteRelationResult struct {
	Message string `json:"message"`
}

type ExistPolicy

type ExistPolicy = string

type GetCIResult

type GetCIResult struct {
	Counter  map[string]int   `json:"counter"`
	Facet    map[string]any   `json:"facet"`
	Numfound int              `json:"numfound"`
	Page     int              `json:"page"`
	Result   []map[string]any `json:"result"`
	Total    int              `json:"total"`
}

type GetRelationResult

type GetRelationResult struct {
	Counter  map[string]int   `json:"counter"`
	Facet    map[string]any   `json:"facet"`
	Numfound int              `json:"numfound"`
	Page     int              `json:"page"`
	Result   []map[string]any `json:"result"`
	Total    int              `json:"total"`
}

type Helper

type Helper struct {
	// contains filtered or unexported fields
}

Helper

func NewHelper

func NewHelper(urlPrefix, key, secret string) *Helper

NewHelper creates a helper instance for cmdb operation

urlPrefix is used to combine http request eg. the final query url with urlPrefix https://demo.veops.cn/api/v0.1 is https://demo.veops.cn/api/v0.1/ci/s

func (*Helper) AddCI

func (h *Helper) AddCI(ciType string, noAttrPolicy NoAttrPolicy, existPolicy ExistPolicy, attrs map[string]any) (res *AddCIResult, err error)

AddCI adds a CI model

you need to add ci_types firstly

func (*Helper) AddRelation

func (h *Helper) AddRelation(srcCIID, dstCIID int) (res *AddRelationResult, err error)

AddRelation adds a relation between two CIs

you need to add ci_relations firstly

func (*Helper) DeleteCI

func (h *Helper) DeleteCI(ciID int) (res *DeleteCIResult, err error)

DeleteCI deletes a CI by id

func (*Helper) DeleteRelation

func (h *Helper) DeleteRelation(relationID, firstCIID, secondCIID int) (res *DeleteRelationResult, err error)

DeleteRelation deletes a CI relation

use relationID or firstCIID and secondCIID firstCIID and secondCIID will be used if ciID is 0

func (*Helper) GetCI

func (h *Helper) GetCI(q, fl, facet, sort string, page, count int, retKey RetKey) (res *GetCIResult, err error)

GetCI queries CIs

func (*Helper) GetRelation

func (h *Helper) GetRelation(rootId, reverse int, level, q, fl, facet, sort string, page, count int, retKey RetKey) (res *GetRelationResult, err error)

GetRelation queries the CI relation

func (*Helper) UpdateCI

func (h *Helper) UpdateCI(ciID int, ciType string, noAttrPolicy NoAttrPolicy, attrs map[string]any) (res *UpdateCIResult, err error)

UpdateCI updates a CI model

type HelperError

type HelperError struct {
	HttpCode int    `json:"httpCode"`
	Message  string `json:"message"`
}

func (HelperError) Error

func (e HelperError) Error() string

type NoAttrPolicy

type NoAttrPolicy = string

type ResponseError

type ResponseError struct {
	Message string `json:"message"`
}

type RetKey

type RetKey = string

type UpdateCIResult

type UpdateCIResult struct {
	CIID int `json:"ci_id"`
}

type UpdateRelationResult

type UpdateRelationResult struct {
	RelationID int `json:"cr_id"`
}

Jump to

Keyboard shortcuts

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