ftgapi/ftgapi.go

99 lines
1.8 KiB
Go

package ftgapi
import (
"bytes"
"crypto/tls"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
func (f *FortigateAPI) query(method string, endpoint string, postData []byte) []byte {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
spaceClient := http.Client{
Timeout: time.Second * 2, // Timeout after 2 seconds
}
req, err := http.NewRequest(method, f.URL+"/api/v2/"+endpoint+"/?access_token="+f.APIKey, bytes.NewBuffer(postData))
if err != nil {
log.Fatal(err)
}
// req.Header.Set("Authorization", "Basic "+bauth)
res, getErr := spaceClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
return body
}
func (f *FortigateAPI) get(endpoint string) interface{} {
var apirequest FTGAPIRequest
response := f.query("GET", endpoint, nil)
err := json.Unmarshal([]byte(response), &apirequest)
if err != nil {
log.Fatal(err)
}
resultstr, _ := json.Marshal(apirequest.Results)
//log.Println(string(response))
switch endpoint {
case "monitor/web-ui/state":
generic := FTGMonitorWebUIState{}
json.Unmarshal(resultstr, &generic)
return generic
}
return nil
}
func (f *FortigateAPI) GetMonitorWebUIState() FTGMonitorWebUIState {
return f.get("monitor/web-ui/state").(FTGMonitorWebUIState)
}
type FTGMonitorWebUIState struct {
Model_name string
Model_number string
Hostname string
Model string
Model_subtype string
Model_level string
}
type FTGAPIRequest struct {
Path string
Name string
Action string
Serial string
Version string
Build int
Status string
Http_status int
Results interface{}
}
type FortigateAPI struct {
URL string
APIKey string
}