service

package
v0.6.10 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodGet    = "GET"
	MethodPost   = "POST"
	MethodPut    = "PUT"
	MethodDelete = "DELETE"
	MethodPatch  = "PATCH"
)
View Source
const (
	MockVersionResponseJSON = "{\"django-version\": \"4.2.10\"}"
)

Variables

View Source
var (
	MockTagsGetResponse = Response[objects.Tag]{
		Count:    2,
		Next:     nil,
		Previous: nil,
		Results: []objects.Tag{
			{
				ID:          0,
				Name:        "Source: proxmox",
				Slug:        "source-proxmox",
				Color:       "9e9e9e",
				Description: "Automatically created tag by netbox-ssot for source proxmox",
			},
			{
				ID:          1,
				Name:        "netbox-ssot",
				Slug:        "netbox-ssot",
				Color:       "00add8",
				Description: "Tag used by netbox-ssot to mark devices that are managed by it",
			},
		},
	}
	MockTagPatchResponse = objects.Tag{
		ID:          1,
		Name:        "netbox-ssot",
		Slug:        "netbox-ssot",
		Color:       "00add8",
		Description: "patched description",
	}
	MockTagCreateResponse = objects.Tag{
		ID:          1,
		Name:        "netbox-ssot",
		Slug:        "netbox-ssot",
		Color:       "00add8",
		Description: "created description",
	}
)

Hardcoded mock API responses for tags endpoint.

View Source
var (
	MockTenantsGetResponse = Response[objects.Tenant]{
		Count:    2,
		Next:     nil,
		Previous: nil,
		Results: []objects.Tenant{
			{
				NetboxObject: objects.NetboxObject{
					ID: 1,
					Tags: []*objects.Tag{
						MockDefaultSsotTag,
					},
				},
				Name: "MockTenant1",
				Slug: "mock-tenant-1",
			},
			{
				NetboxObject: objects.NetboxObject{
					ID: 2,
					Tags: []*objects.Tag{
						MockDefaultSsotTag,
					},
				},
				Name: "MockTenant2",
				Slug: "mock-tenant-2",
			},
		},
	}
	MockTenantCreateResponse = objects.Tenant{
		NetboxObject: objects.NetboxObject{
			ID: 3,
		},
		Name: "MockTenant3",
		Slug: "mock-tenant-3",
	}
	MockTenantPatchResponse = objects.Tenant{
		NetboxObject: objects.NetboxObject{
			ID: 1,
		},
		Name: "MockPatched",
		Slug: "mock-patched-tenant",
	}
)

Hardcoded mock api return values for tenant endpoint.

View Source
var (
	MockSitesGetResponse = Response[objects.Site]{
		Count:    2,
		Next:     nil,
		Previous: nil,
		Results: []objects.Site{
			{
				NetboxObject: objects.NetboxObject{
					ID: 1,
					Tags: []*objects.Tag{
						MockDefaultSsotTag,
					},
				},
				Name: "MockSite1",
				Slug: "mock-site-1",
			},
			{
				NetboxObject: objects.NetboxObject{
					ID: 2,
					Tags: []*objects.Tag{
						MockDefaultSsotTag,
					},
				},
				Name: "MockSite2",
				Slug: "mock-site-2",
			},
		},
	}
	MockSiteCreateResponse = objects.Site{
		NetboxObject: objects.NetboxObject{
			ID: 3,
		},
		Name: "MockSite3",
		Slug: "mock-site-3",
	}
	MockSitePatchResponse = objects.Site{
		NetboxObject: objects.NetboxObject{
			ID: 1,
		},
		Name: "MockSitePatched",
		Slug: "mock-site-patched",
	}
)

Hardcoded mock api return values for site endpoint.

View Source
var FailingMockNetboxClient = &NetboxClient{
	HTTPClient: &http.Client{Transport: &FailingHTTPClient{}},
	Logger:     &logger.Logger{Logger: log.Default()},
	BaseURL:    "",
	APIToken:   "testtoken",
	Timeout:    constants.DefaultAPITimeout,
}
View Source
var MockDefaultSsotTag = &objects.Tag{
	ID:   0,
	Name: constants.DefaultSourceName,
}
View Source
var MockNetboxClient = &NetboxClient{
	HTTPClient: &http.Client{},
	Logger:     &logger.Logger{Logger: log.Default()},
	BaseURL:    "",
	APIToken:   "testtoken",
	Timeout:    constants.DefaultAPITimeout,
}
View Source
var MockNetboxClientWithReadError = &NetboxClient{
	HTTPClient: &http.Client{Transport: &FailingHTTPClientRead{}},
	Logger:     &logger.Logger{Logger: log.Default()},
	BaseURL:    "",
	APIToken:   "testtoken",
	Timeout:    constants.DefaultAPITimeout,
}

Functions

func Create

func Create[T any](ctx context.Context, netboxClient *NetboxClient, object *T) (*T, error)

Create func creates the new NetboxObject of type T, with the given api path and body.

func CreateMockServer added in v0.3.3

func CreateMockServer() *httptest.Server

func GetAll

func GetAll[T any](ctx context.Context, netboxClient *NetboxClient, extraParams string) ([]T, error)

GetAll queries all objects of type T from Netbox's API. It is querying objects via pagination of limit=100.

extraParams in a string format of: &extraParam1=...&extraParam2=...

func Patch

func Patch[T any](ctx context.Context, netboxClient *NetboxClient, objectID int, body map[string]interface{}) (*T, error)

Patch func patches the object of type T, with the given api path and body. Path of the object (must contain the id), for example /api/dcim/devices/1/.

Types

type APIResponse

type APIResponse struct {
	StatusCode int
	Body       []byte
}

APIResponse is a struct that represents a response from the Netbox API.

type FailingHTTPClient added in v0.3.3

type FailingHTTPClient struct{}

func (*FailingHTTPClient) RoundTrip added in v0.3.3

func (m *FailingHTTPClient) RoundTrip(_ *http.Request) (*http.Response, error)

type FailingHTTPClientRead added in v0.3.3

type FailingHTTPClientRead struct{}

func (*FailingHTTPClientRead) RoundTrip added in v0.3.3

func (m *FailingHTTPClientRead) RoundTrip(_ *http.Request) (*http.Response, error)

type FaultyReader added in v0.3.3

type FaultyReader struct{}

func (*FaultyReader) Read added in v0.3.3

func (m *FaultyReader) Read(_ []byte) (n int, err error)

type NetboxClient added in v0.3.3

type NetboxClient struct {
	Logger     *logger.Logger
	HTTPClient *http.Client
	BaseURL    string
	APIToken   string
	Timeout    int // in seconds
	MaxRetires int
}

NetboxClient is a service used for communicating with the Netbox API. It is created via constructor func newNetboxAPI().

func NewNetboxClient added in v0.3.3

func NewNetboxClient(ctx context.Context, logger *logger.Logger, baseURL string, apiToken string, validateCert bool, timeout int) *NetboxClient

Constructor function for creating a new netBoxAPI instance.

func (*NetboxClient) BulkDeleteObjects added in v0.3.3

func (api *NetboxClient) BulkDeleteObjects(ctx context.Context, objectPath string, idSet map[int]bool) error

Function that deletes object on path objectPath. It deletes objects in pages of 50 so we don't stress the API too much.

func (*NetboxClient) DeleteObject added in v0.6.0

func (api *NetboxClient) DeleteObject(ctx context.Context, objectPath string, id int) error

Function that deletes objectas on path objectPath. It deletes a single object at a time. It is alternative to bulk delete because if one delete fails other still go.

type Response

type Response[T any] struct {
	Count    int     `json:"count"`
	Next     *string `json:"next"`
	Previous *string `json:"previous"`
	Results  []T     `json:"results"`
}

Standard response format from Netbox's API.

Jump to

Keyboard shortcuts

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