opentofu/vendor/github.com/lrsmith/go-icinga2-api/iapi/hostgroups.go
Len Smith 015e96d0dd Initial check in for Icinga2 Provider/Resource (#8306)
* Initial checkin for PR request

* Added an argument to provider to allow control over whether or not TLS Certs will skip verification. Controllable via provider or env variable being set

* Initial check-in to use refactored module

* Checkin of very MVP for creating/deleting host test which works and validates basic host creation and deletion

* Check in with support for creating hosts with variables working

* Checking in work to date

* Remove code that causes travis CI to fail while I debug

* Adjust create to accept multivale

* Back on track. Working basic tests. go-icinga2-api needs more test too

* Squashing

* Back on track. Working basic tests. go-icinga2-api needs more test too

* Check in refactored hostgroup support

* Check in refactored check_command, hosts, and hsotgroup with a few test

* Checking in service code

* Add in dependency for icinga2 provider

* Add documentation. Refactor, fix and extend based on feedback from Hashicorp

* Added checking and validation around invalid URL and unavailable server
2016-12-12 15:28:26 +00:00

89 lines
1.9 KiB
Go

package iapi
import (
"encoding/json"
"errors"
"fmt"
)
// GetHostgroup ...
//
func (server *Server) GetHostgroup(name string) ([]HostgroupStruct, error) {
var hostgroups []HostgroupStruct
results, err := server.NewAPIRequest("GET", "/objects/hostgroups/"+name, nil)
if err != nil {
return nil, err
}
// Contents of the results is an interface object. Need to convert it to json first.
jsonStr, marshalErr := json.Marshal(results.Results)
if marshalErr != nil {
return nil, marshalErr
}
// then the JSON can be pushed into the appropriate struct.
// Note : Results is a slice so much push into a slice.
if unmarshalErr := json.Unmarshal(jsonStr, &hostgroups); unmarshalErr != nil {
return nil, unmarshalErr
}
if len(hostgroups) == 0 {
return nil, nil
}
if len(hostgroups) != 1 {
return nil, errors.New("Found more than one matching hostgroup.")
}
return hostgroups, err
}
// CreateHostgroup ...
func (server *Server) CreateHostgroup(name, displayName string) ([]HostgroupStruct, error) {
var newAttrs HostgroupAttrs
newAttrs.DisplayName = displayName
var newHostgroup HostgroupStruct
newHostgroup.Name = name
newHostgroup.Type = "Hostgroup"
newHostgroup.Attrs = newAttrs
payloadJSON, marshalErr := json.Marshal(newHostgroup)
if marshalErr != nil {
return nil, marshalErr
}
results, err := server.NewAPIRequest("PUT", "/objects/hostgroups/"+name, []byte(payloadJSON))
if err != nil {
return nil, err
}
if results.Code == 200 {
hostgroups, err := server.GetHostgroup(name)
return hostgroups, err
}
return nil, fmt.Errorf("%s", results.ErrorString)
}
// DeleteHostgroup ...
func (server *Server) DeleteHostgroup(name string) error {
results, err := server.NewAPIRequest("DELETE", "/objects/hostgroups/"+name, nil)
if err != nil {
return err
}
if results.Code == 200 {
return nil
} else {
return fmt.Errorf("%s", results.ErrorString)
}
}