unifi

package module
v0.0.0-...-706f5e7 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2017 License: MIT Imports: 12 Imported by: 0

README

unifi

Build Status Go Report Card GoDoc

Package unifi is a Golang SDK for UBNT Unifi APIs to interact with Unifi Controller.

Currently, it focuses on guest authorization which is useful to implment customized portal server.

Features
  • Login / Logout Unifi Controller.
  • Authorize / Unauthorize Guest.
Timeout and Cancelation Support
Requirement
  • Go 1.7 or higher is required(Go 1.7 moves the golang.org/x/net/context package into the standard library as context).
Example
func Example() {
        var err error

        unifiURL := "https://192.168.1.56:8443"
        userName := "admin"
        password := "admin"

        defer func() {
                if err != nil {
                        log.Printf("error: %v", err)
                }
        }()

        // New an Unifi instance.
        u, err := unifi.New(unifiURL, userName, password)
        if err != nil {
                return
        }

        // Set debug mode to true to output debug messages(default is false).
        unifi.SetDebugMode(true)
        // Set timeout to 5 seconds.
        timeout := time.Duration(time.Second * 5)
        // Create context with timeout.
        ctx, cancel := context.WithTimeout(context.Background(), timeout)
        defer cancel()

        exit := make(chan error)
        go func() {
                // Login
                if err = u.Login(ctx); err != nil {
                        exit <- err
                        return
                }

                // Logout before return if login successfully.
                defer func() {
                        exit <- u.Logout(ctx)
                }()

                mac := "aa:bb:cc:dd:ee:ff" // MAC address.
                min := 5                   // time to authorize in minutes.

                // Authorize guest with MAC and time.
                if err = u.AuthorizeGuest(ctx, "default", mac, min); err != nil {
                        exit <- err
                        return
                }


                // Unauthorize guest with MAC.
                /*if err = u.UnAuthorizeGuest(ctx, "default", mac); err != nil {
                        exit <- err
                        return
                }*/

                exit <- nil
        }()

        select {
        case err = <-exit:
                log.Printf("goroutine exited")
                return

        case <-ctx.Done():
                err = ctx.Err()
                return
        }
        // Output:
}
Documentation
License

Documentation

Overview

Package unifi is a Golang SDK for UBNT APIs to interact with Unifi Controller.

Example
package main

import (
	"context"
	"log"
	"time"

	"github.com/northbright/unifi"
)

func main() {
	var err error

	unifiURL := "https://192.168.1.56:8443"
	userName := "admin"
	password := "admin"

	defer func() {
		if err != nil {
			log.Printf("error: %v", err)
		}
	}()

	// New an Unifi instance.
	u, err := unifi.New(unifiURL, userName, password)
	if err != nil {
		return
	}

	// Set debug mode to true to output debug messages(default is false).
	unifi.SetDebugMode(true)

	// Set timeout to 5 seconds.
	timeout := time.Duration(time.Second * 5)
	// Create context with timeout.
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	exit := make(chan error)
	go func() {
		// Login
		if err = u.Login(ctx); err != nil {
			exit <- err
			return
		}

		// Logout before return if login successfully.
		defer func() {
			exit <- u.Logout(ctx)
		}()

		mac := "aa:bb:cc:dd:ee:ff" // MAC address.
		min := 5                   // time to authorize in minutes.

		// Authorize guest with MAC and time.
		if err = u.AuthorizeGuest(ctx, "default", mac, min); err != nil {
			exit <- err
			return
		}

		down := 2048 // download speed in KB.
		up := 512    // upload speed in KB.
		quota := 20  // Quota limit in MB.

		// Authorize guest with MAC, time, download speed, upload speed and quota.
		if err = u.AuthorizeGuestWithQos(ctx, "default", mac, min, down, up, quota); err != nil {
			exit <- err
			return
		}

		// Unauthorize guest with MAC.
		/*if err = u.UnAuthorizeGuest(ctx, "default", mac); err != nil {
			exit <- err
			return
		}*/

		exit <- nil
	}()

	select {
	case err = <-exit:
		log.Printf("goroutine exited")
		return

	case <-ctx.Done():
		err = ctx.Err()
		return
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDebugMode

func IsDebugMode() bool

IsDebugMode returns if it's in debug mode or not.

func ParseJSON

func ParseJSON(b []byte) (map[string]interface{}, bool, error)

ParseJSON parses the JSON returned by Unifi APIs.

Params:

b: Bytes returned by Unifi APIs which contains JSON string.

Return:

map[string]interface{} as parsed JSON object.
true or false if "rc" is "ok".

func SetDebugMode

func SetDebugMode(f bool)

SetDebugMode sets debug mode for package unifi.

Types

type Unifi

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

Unifi provides functions to call Unifi APIs.

func New

func New(unifiURL, userName, password string) (*Unifi, error)

New creates a new Unifi.

Params:

unifiURL: Unifi Controller's URL. E.g. https://10.0.1.100:8443
userName: User name of Unifi Controller.
password: Password of Unifi Controller.

func (*Unifi) AuthorizeGuest

func (u *Unifi) AuthorizeGuest(ctx context.Context, site, mac string, min int) error

AuthorizeGuest authorizes guest by MAC, time.It's a wrapper of AuthorizeGuestWithQos.

Params:

ctx: Parent context. You may use context.Background() to create an empty context.
     See http://godoc.org/context for more info.
site: Site name. It's **NOT** the "Site Name"(just description) in Unifi GUI.
      If you only have 1 site. Just use "default" or leave it empty.
      If you've created new sites, follow this to get the site name:
      https://github.com/northbright/Notes/blob/master/Software/unifi/use-compass-to-explore-mongodb-of-unifi/use-compass-to-explore-mongodb-of-unifi.md

mac: MAC address of guest to be authorized. It's in "aa:bb:cc:dd:ee:ff" format.
min: Timeout in minutes.

func (*Unifi) AuthorizeGuestWithQos

func (u *Unifi) AuthorizeGuestWithQos(ctx context.Context, site, mac string, min, down, up, quota int) error

AuthorizeGuestWithQos authorizes guest by MAC, time and set qos.

Params:

ctx: Parent context. You may use context.Background() to create an empty context.
     See http://godoc.org/context for more info.
site: Site name. It's **NOT** the "Site Name"(just description) in Unifi GUI.
      If you only have 1 site. Just use "default" or leave it empty.
      If you've created new sites, follow this to get the site name:
      https://github.com/northbright/Notes/blob/master/Software/unifi/use-compass-to-explore-mongodb-of-unifi/use-compass-to-explore-mongodb-of-unifi.md
mac: MAC address of guest to be authorized. It's in "aa:bb:cc:dd:ee:ff" format.
min: Timeout in minutes.
down: Max download speed in KB.
up: Max upload speed in KB.
quota: Quota in MB.

func (*Unifi) Do

func (u *Unifi) Do(r *http.Request, readRespBody bool) ([]byte, error)

Do does the HTTP request for Unifi API.

Params:

r: *http.Request.
readRespBody: if read response body or not.
              it will return the response body if it's true or return nil if it's false.

Return:

response body if readRespBody is true.

func (*Unifi) Login

func (u *Unifi) Login(ctx context.Context) error

Login logins Unifi Controller.

Params:

ctx: Parent context. You may use context.Background() to create an empty context.
     See http://godoc.org/context for more info.

func (*Unifi) Logout

func (u *Unifi) Logout(ctx context.Context) error

Logout logouts Unifi Controller.

Params:

ctx: Parent context. You may use context.Background() to create an empty context.
     See http://godoc.org/context for more info.

func (*Unifi) UnAuthorizeGuest

func (u *Unifi) UnAuthorizeGuest(ctx context.Context, site, mac string) error

UnAuthorizeGuest unauthorizes guest by MAC.

Params:

ctx: Parent context. You may use context.Background() to create an empty context.
     See http://godoc.org/context for more info.
site: Site name. It's **NOT** the "Site Name"(just description) in Unifi GUI.
      If you only have 1 site. Just use "default" or leave it empty.
      If you've created new sites, follow this to get the site name:
      https://github.com/northbright/Notes/blob/master/Software/unifi/use-compass-to-explore-mongodb-of-unifi/use-compass-to-explore-mongodb-of-unifi.md
mac: MAC address of guest to be unauthorized. It's in "aa:bb:cc:dd:ee:ff" format.

Jump to

Keyboard shortcuts

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