Graphiant-SDK-Go

Prev Next

Getting Started

Go SDK for Graphiant NaaS.

Docs: https://github.com/Graphiant-Inc/graphiant-sdk-go/tree/main/docs

Source code: https://github.com/Graphiant-Inc/graphiant-sdk-go

Package: https://pkg.go.dev/github.com/Graphiant-Inc/graphiant-sdk-go#section-readme

Topology

Install Graphiant SDK Go Package

To Install Graphiant SDK Go package, Please refer https://github.com/Graphiant-Inc/graphiant-sdk-go/blob/main/README.md

Create Graphiant Portal Client

To Create the Graphiant Portal Client (apiClient) object.

Note:

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"

	openapiclient "github.com/Graphiant-Inc/graphiant-sdk-go"
)

func GetAuthToken() (*openapiclient.APIClient, string) {

	username := ** Email Address **
	password := ** Password **

	configuration := openapiclient.NewConfiguration()
	apiClient := openapiclient.NewAPIClient(configuration)
	authReq := openapiclient.NewV1AuthLoginPostRequest()
	authReq.SetUsername(username)
	authReq.SetPassword(password)

	fmt.Printf("Calling V1AuthLoginPost (url: %s)\n", configuration.Servers[0].URL)
	_, httpRes, _ := apiClient.DefaultAPI.
		V1AuthLoginPost(context.Background()).
		V1AuthLoginPostRequest(*authReq).
		Execute()
	defer httpRes.Body.Close()

	var result struct {
		Auth        bool   `json:"auth"`
		AccountType string `json:"accountType"`
		Token       string `json:"token"`
	}

	if err := json.NewDecoder(httpRes.Body).Decode(&result); err != nil {
		fmt.Printf("Failed to decode auth response: %v\n", err)
	}
	fmt.Printf("Auth token: %v\n", result.Token)
	return apiClient, "Bearer " + result.Token
}

func main() {
	GetAuthToken()
}

Output:

Calling V1AuthLoginPost (url: https://api.graphiant.com)
Auth token: gr-auth-b2149f78-292a-49d2-9c2d-2b8f99754b3b-e4ed7075-9c47-4ed6-b868-1e435b8212d5

Using Graphiant Portal Client

Once Graphiant portal client object is instantiated and bearer token is retrieved, user can perform CRUD operations using Graphiant APIs.

Refer to API Documentation here: https://github.com/Graphiant-Inc/graphiant-sdk-go/blob/main/docs/DefaultAPI.md

Getting Device Summary

E.g. Below is the function to GET Edges Summary

func GetEdgeSummary() {

	apiClient, token := GetAuthToken()
	resp, _, _ := apiClient.DefaultAPI.
		V1EdgesSummaryGet(context.Background()).
		Authorization(token).
		Execute()
	summary, _ := json.MarshalIndent(resp.GetEdgesSummary(), "", "  ")
	fmt.Printf("Edge summary response : %s", summary)
}

Output:

Calling V1AuthLoginPost (url: https://api.graphiant.com)
Auth token: gr-auth-6ad8ba2a-7363-4ea0-b318-06531212e0b4-c2570bc7-cfb9-4b73-b701-8db375ea9499
Edge summary response : [
  {
    "deviceId": 30000051747,
    "discoveredLocation": "Tower Hamlets, Greater London, United Kingdom",
    "enterpriseId": 10000000996,
    "firstAppearedOn": {
      "nanos": 284291000,
      "seconds": 1749708078
    },
    "hostname": "edge-1",
    "ipDetected": "65.89.143.70",
    "isNew": true,
    "lastBootedAt": {
      "nanos": 859346000,
      "seconds": 1749776825
    },
    "location": {
      "addressLine1": "",
      "addressLine2": "",
      "city": "tower hamlets",
      "country": "united kingdom",
      "countryCode": "",
      "provinceCode": "",
      "state": "greater london",
      "stateCode": ""
    },
    "model": "QEMU-Q35",
    "overrideRegion": "us-west-2 (San Jose)",
    "portalStatus": "Ready",
    "region": "eu-west-1 (London)",
    "role": "cpe",
    "serialNum": "11c0174d-de71-4215-a050-107436689596",
    "site": "San Jose-encryption",
    "siteId": 4678,
    "status": "active",
    "swName": "25.6.1",
    "swVersion": "2506.202506021956",
    "ttConnCount": 2
  },
  {
    "deviceId": 30000051745,
    "discoveredLocation": "Tower Hamlets, Greater London, United Kingdom",
    "enterpriseId": 10000000996,
    "firstAppearedOn": {
      "nanos": 99728000,
      "seconds": 1749708076
    },
    "hostname": "edge-2",
    "ipDetected": "62.89.143.70",
    "isNew": true,
    "lastBootedAt": {
      "nanos": 585489000,
      "seconds": 1749708006
    },
    "location": {
      "addressLine1": "",
      "addressLine2": "",
      "city": "tower hamlets",
      "country": "united kingdom",
      "countryCode": "",
      "provinceCode": "",
      "state": "greater london",
      "stateCode": ""
    },
    "model": "QEMU-Q35",
    "overrideRegion": "us-east-2 (Atlanta)",
    "portalStatus": "Ready",
    "region": "eu-west-1 (London)",
    "role": "cpe",
    "serialNum": "40ed3fc5-c7e3-4730-8054-71a4ab937481",
    "site": "Chicago-encryption",
    "siteId": 4679,
    "status": "active",
    "swName": "25.3.5",
    "swVersion": "2503.202506041024",
    "ttConnCount": 2
  },
]%   

Updating Device configuration

Prerequisite before configuration push to device

Before updating a device (edge/gateway/core) configuration, From the Edge Summary output (GetEdgeSummary function), make sure portalStatus is Ready and ttConnCount is 2.

Below are the basic interface level operation examples for an Edge:

To Configure a WAN Circuit

func ConfigureWan(deviceId int64, interfaceName string, wanCircuit string) {
	apiClient, token := GetAuthToken()

	circuit := map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeCircuitsValue{
		wanCircuit: {
			Name:              openapiclient.PtrString(wanCircuit),
			ConnectionType:    openapiclient.PtrString("internet_dia"),
			Label:             openapiclient.PtrString("internet_dia_4"),
			DiaEnabled:        openapiclient.PtrBool(true),
			QosProfile:        openapiclient.PtrString("gold25"),
			LinkDownSpeedMbps: openapiclient.PtrInt32(100),
			LinkUpSpeedMbps:   openapiclient.PtrInt32(50),
		},
	}
	interfaces := map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValue{
		interfaceName: {
			Interface: &openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterface{
				AdminStatus: openapiclient.PtrBool(true),
				Circuit:     openapiclient.PtrString(wanCircuit),
				Ipv4: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGw{
					Dhcp: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGwDhcp{
						DhcpClient: openapiclient.PtrBool(true)},
				},
				Ipv6: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGw{
					Dhcp: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGwDhcp{
						DhcpClient: openapiclient.PtrBool(true)},
				},
			},
		},
	}
	configReq := openapiclient.NewV1DevicesDeviceIdConfigPutRequest()
	configReq.Edge = openapiclient.NewV1DevicesDeviceIdConfigPutRequestEdge()
	configReq.Edge.SetInterfaces(interfaces)
	configReq.Edge.SetCircuits(circuit)
	circuitsJSON, _ := json.MarshalIndent(configReq.Edge.GetCircuits(), "", "  ")
	interfacesJSON, _ := json.MarshalIndent(configReq.Edge.GetInterfaces(), "", "  ")
	fmt.Printf("Calling V1DevicesDeviceIdConfigPut : %v %s %s", deviceId, circuitsJSON, interfacesJSON)
	_, httpRes, err := apiClient.DefaultAPI.
		V1DevicesDeviceIdConfigPut(context.Background(), deviceId).
		Authorization(token).
		V1DevicesDeviceIdConfigPutRequest(*configReq).Execute()
	if err != nil {
		fmt.Printf("Error executing config put request: %v\n", err)
		return
	}
	body, _ := io.ReadAll(httpRes.Body)
	fmt.Printf("Config put response: %s, http status: %v\n", string(body), httpRes.StatusCode)
}

To Configure a LAN Interface

func ConfigureLan(deviceId int64, interfaceName string, lan string, ipv4Address string, ipv6Address string) {
	apiClient, token := GetAuthToken()

	interfaces := map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValue{
		interfaceName: {
			Interface: &openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterface{
				Lan:         openapiclient.PtrString(lan),
				AdminStatus: openapiclient.PtrBool(true),
				Ipv4: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGw{
					Address: &openapiclient.V1GlobalConfigPatchRequestSnmpsValueConfigEngineEndpointsValueEngineEndpointAddressesValue{
						Address: openapiclient.PtrString(ipv4Address),
					},
				},
				Ipv6: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGw{
					Address: &openapiclient.V1GlobalConfigPatchRequestSnmpsValueConfigEngineEndpointsValueEngineEndpointAddressesValue{
						Address: openapiclient.PtrString(ipv6Address),
					},
				},
			},
		},
	}
	configReq := openapiclient.NewV1DevicesDeviceIdConfigPutRequest()
	configReq.Edge = openapiclient.NewV1DevicesDeviceIdConfigPutRequestEdge()
	configReq.Edge.SetInterfaces(interfaces)
	interfacesJSON, _ := json.MarshalIndent(configReq.Edge.GetInterfaces(), "", "  ")

	fmt.Printf("Calling V1DevicesDeviceIdConfigPut : %v %s\n", deviceId, interfacesJSON)
	_, httpRes, err := apiClient.DefaultAPI.
		V1DevicesDeviceIdConfigPut(context.Background(), deviceId).
		Authorization(token).
		V1DevicesDeviceIdConfigPutRequest(*configReq).Execute()
	if err != nil {
		fmt.Printf("Error executing config put request: %v\n", err)
		return
	}
	body, _ := io.ReadAll(httpRes.Body)
	fmt.Printf("Config put response: %s, http status: %v\n", string(body), httpRes.StatusCode)
}

To Deconfigure LAN and WAN Circuit Configs

Note:

In order to move a WAN Circuit to LAN or vice versa, please move the interface to default LAN (ConfigureDefaultLan) and then configure a new interface type(ConfigureLan/ConfigureWan).

func ConfigureDefaultLan(deviceId int64, interfaceName string, defaultLan string) {
	apiClient, token := GetAuthToken()

	interfaces := map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValue{
		interfaceName: {
			Interface: &openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterface{
				Lan: openapiclient.PtrString(defaultLan),
			},
		},
	}
	configReq := openapiclient.NewV1DevicesDeviceIdConfigPutRequest()
	configReq.Edge = openapiclient.NewV1DevicesDeviceIdConfigPutRequestEdge()
	configReq.Edge.SetInterfaces(interfaces)
	interfacesJSON, _ := json.MarshalIndent(configReq.Edge.GetInterfaces(), "", "  ")

	fmt.Printf("Calling V1DevicesDeviceIdConfigPut : %v %s\n", deviceId, interfacesJSON)
	_, httpRes, err := apiClient.DefaultAPI.
		V1DevicesDeviceIdConfigPut(context.Background(), deviceId).
		Authorization(token).
		V1DevicesDeviceIdConfigPutRequest(*configReq).Execute()
	if err != nil {
		fmt.Printf("Error executing config put request: %v\n", err)
		return
	}
	body, _ := io.ReadAll(httpRes.Body)
	fmt.Printf("Config put response: %s, http status: %v\n", string(body), httpRes.StatusCode)
}

To configure a LAN sub-interface

func ConfigureSubInterface(deviceId int64, interfaceName string, lan string, vlan int32, ipv4Address string, ipv6Address string) {
	apiClient, token := GetAuthToken()

	interfaces := map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValue{
		interfaceName: {
			Interface: &openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterface{
				Subinterfaces: &map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterfaceSubinterfacesValue{
					fmt.Sprintf("%d", vlan): {
						Interface: &openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterfaceSubinterfacesValueInterface{
							AdminStatus: openapiclient.PtrBool(true),
							Ipv4: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGw{
								Address: &openapiclient.V1GlobalConfigPatchRequestSnmpsValueConfigEngineEndpointsValueEngineEndpointAddressesValue{
									Address: openapiclient.PtrString(ipv4Address),
								},
							},
							Ipv6: &openapiclient.V1DevicesDeviceIdConfigPutRequestCoreInterfacesValueInterfaceGwGw{
								Address: &openapiclient.V1GlobalConfigPatchRequestSnmpsValueConfigEngineEndpointsValueEngineEndpointAddressesValue{
									Address: openapiclient.PtrString(ipv6Address),
								},
							},
							Lan:  openapiclient.PtrString(lan),
							Vlan: openapiclient.PtrInt32(vlan),
						},
					},
				},
			},
		},
	}
	configReq := openapiclient.NewV1DevicesDeviceIdConfigPutRequest()
	configReq.Edge = openapiclient.NewV1DevicesDeviceIdConfigPutRequestEdge()
	configReq.Edge.SetInterfaces(interfaces)
	interfacesJSON, _ := json.MarshalIndent(configReq.Edge.GetInterfaces(), "", "  ")

	fmt.Printf("Calling V1DevicesDeviceIdConfigPut : %v %s\n", deviceId, interfacesJSON)
	_, httpRes, err := apiClient.DefaultAPI.
		V1DevicesDeviceIdConfigPut(context.Background(), deviceId).
		Authorization(token).
		V1DevicesDeviceIdConfigPutRequest(*configReq).Execute()
	if err != nil {
		fmt.Printf("Error executing config put request: %v\n", err)
		return
	}
	body, _ := io.ReadAll(httpRes.Body)
	fmt.Printf("Config put response: %s, http status: %v\n", string(body), httpRes.StatusCode)
}

To delete a subinterface

func DeleteSubInterface(deviceId int64, interfaceName string, vlan int32) {
	apiClient, token := GetAuthToken()

	interfaces := map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValue{
		interfaceName: {
			Interface: &openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterface{
				Subinterfaces: &map[string]openapiclient.V1DevicesDeviceIdConfigPutRequestEdgeInterfacesValueInterfaceSubinterfacesValue{
					fmt.Sprintf("%d", vlan): {},
				},
			},
		},
	}
	configReq := openapiclient.NewV1DevicesDeviceIdConfigPutRequest()
	configReq.Edge = openapiclient.NewV1DevicesDeviceIdConfigPutRequestEdge()
	configReq.Edge.SetInterfaces(interfaces)
	interfacesJSON, _ := json.MarshalIndent(configReq.Edge.GetInterfaces(), "", "  ")

	fmt.Printf("Calling V1DevicesDeviceIdConfigPut : %v %s\n", deviceId, interfacesJSON)
	_, httpRes, err := apiClient.DefaultAPI.
		V1DevicesDeviceIdConfigPut(context.Background(), deviceId).
		Authorization(token).
		V1DevicesDeviceIdConfigPutRequest(*configReq).Execute()
	if err != nil {
		fmt.Printf("Error executing config put request: %v\n", err)
		return
	}
	body, _ := io.ReadAll(httpRes.Body)
	fmt.Printf("Config put response: %s, http status: %v\n", string(body), httpRes.StatusCode)
}