add description for functions

This commit is contained in:
gi8 2021-03-11 10:42:25 +01:00
parent 409e6cd1bb
commit 4753e58c17

View file

@ -30,9 +30,11 @@ var baseURL string
var token string var token string
var configName string var configName string
// bluecatLogin to be able to talk to the bluecat API, you must login to generate a bluecat API token
func bluecatLogin(bluecatURL, username, password string, bluecatConfigName string) error { func bluecatLogin(bluecatURL, username, password string, bluecatConfigName string) error {
baseURL = bluecatURL baseURL = bluecatURL
configName = bluecatConfigName configName = bluecatConfigName
queryArgs := map[string]string{ queryArgs := map[string]string{
"username": username, "username": username,
"password": password, "password": password,
@ -59,6 +61,7 @@ func bluecatLogin(bluecatURL, username, password string, bluecatConfigName strin
return nil return nil
} }
// bluecatLogout logout to invoke bluecat API token
func bluecatLogout() error { func bluecatLogout() error {
if len(token) == 0 { if len(token) == 0 {
return nil return nil
@ -90,6 +93,7 @@ func bluecatLogout() error {
return nil return nil
} }
// bluecatLookupConfID search configuration by id
func bluecatLookupConfID() (uint, error) { func bluecatLookupConfID() (uint, error) {
queryArgs := map[string]string{ queryArgs := map[string]string{
"parentId": strconv.Itoa(0), "parentId": strconv.Itoa(0),
@ -106,11 +110,12 @@ func bluecatLookupConfID() (uint, error) {
var conf entityResponse var conf entityResponse
err = json.NewDecoder(resp.Body).Decode(&conf) err = json.NewDecoder(resp.Body).Decode(&conf)
if err != nil { if err != nil {
return 0, fmt.Errorf("bluecat: %w", err) return 0, fmt.Errorf("bluecat: cannot get entity by name. %w", err)
} }
return conf.ID, nil return conf.ID, nil
} }
// bluecatLookupViewID search view by name
func bluecatLookupViewID(viewName string) (uint, error) { func bluecatLookupViewID(viewName string) (uint, error) {
confID, err := bluecatLookupConfID() confID, err := bluecatLookupConfID()
if err != nil { if err != nil {
@ -138,30 +143,34 @@ func bluecatLookupViewID(viewName string) (uint, error) {
return view.ID, nil return view.ID, nil
} }
// bluecatLookupParentZoneID search parent zone id
func bluecatLookupParentZoneID(viewID uint, fqdn string) (uint, string, error) { func bluecatLookupParentZoneID(viewID uint, fqdn string) (uint, string, error) {
parentViewID := viewID parentViewID := viewID
name := "" name := ""
if fqdn != "" { if fqdn == "" {
zones := strings.Split(strings.Trim(fqdn, "."), ".") return parentViewID, name, nil
last := len(zones) - 1 }
name = zones[0]
for i := last; i > -1; i-- { zones := strings.Split(strings.Trim(fqdn, "."), ".")
zoneID, err := bluecatGetZone(parentViewID, zones[i]) last := len(zones) - 1
if err != nil || zoneID == 0 { name = zones[0]
return parentViewID, name, err
} for i := last; i > -1; i-- {
if i > 0 { zoneID, err := bluecatGetZone(parentViewID, zones[i])
name = strings.Join(zones[0:i], ".") if err != nil || zoneID == 0 {
} return parentViewID, name, err
parentViewID = zoneID
} }
if i > 0 {
name = strings.Join(zones[0:i], ".")
}
parentViewID = zoneID
} }
return parentViewID, name, nil return parentViewID, name, nil
} }
// bluecatGetZone search zone by name
func bluecatGetZone(parentID uint, name string) (uint, error) { func bluecatGetZone(parentID uint, name string) (uint, error) {
queryArgs := map[string]string{ queryArgs := map[string]string{
"parentId": strconv.FormatUint(uint64(parentID), 10), "parentId": strconv.FormatUint(uint64(parentID), 10),
@ -189,6 +198,7 @@ func bluecatGetZone(parentID uint, name string) (uint, error) {
return zone.ID, nil return zone.ID, nil
} }
// bluecatDeploy
func bluecatDeploy(entityID uint) error { func bluecatDeploy(entityID uint) error {
queryArgs := map[string]string{ queryArgs := map[string]string{
"entityId": strconv.FormatUint(uint64(entityID), 10), "entityId": strconv.FormatUint(uint64(entityID), 10),
@ -203,6 +213,7 @@ func bluecatDeploy(entityID uint) error {
return nil return nil
} }
// bluecatSendRequest send a API request to bluecat
func bluecatSendRequest(method, resource string, payload interface{}, queryArgs map[string]string) (*http.Response, error) { func bluecatSendRequest(method, resource string, payload interface{}, queryArgs map[string]string) (*http.Response, error) {
url := fmt.Sprintf("%s/Services/REST/v1/%s", baseURL, resource) url := fmt.Sprintf("%s/Services/REST/v1/%s", baseURL, resource)