]> granicus.if.org Git - icinga2/commitdiff
REST API Docs: Add Golang client code example
authorMichael Friedrich <michael.friedrich@icinga.com>
Tue, 26 Mar 2019 08:46:36 +0000 (09:46 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Thu, 28 Mar 2019 08:52:45 +0000 (09:52 +0100)
(cherry picked from commit 4296622f526dfe17ffe9155430f38f39616288a4)

doc/02-getting-started.md
doc/12-icinga2-api.md

index df2634c48086a9695c797fc7955a6e1e979d7bfd..1ea7c5c15baac03385ff62d042eb956c2b17090a 100644 (file)
@@ -329,7 +329,7 @@ yum install nagios-plugins-all
 ```
 
 The packages for RHEL/CentOS depend on other packages which are distributed
-as part of the [EPEL repository](#package-repositories-rhel-epel).
+as part of the [EPEL repository](02-getting-started.md#package-repositories-rhel-epel).
 
 Fedora:
 
index 1f7de10f316b4cc7eefb015b7cc4757f04283dcb..14e4ad272a70d3f98535ff34282bb7b1b0fe5458 100644 (file)
@@ -2048,9 +2048,13 @@ Add the `--connect` parameter to debug and evaluate expressions via the API.
 
 ### API Clients Programmatic Examples <a id="icinga2-api-clients-programmatic-examples"></a>
 
-The programmatic examples use HTTP basic authentication and SSL certificate
-verification. The CA file is expected in `pki/icinga2-ca.crt`
-but you may adjust the examples for your likings.
+The following languages are covered:
+
+* [Python](12-icinga2-api.md#icinga2-api-clients-programmatic-examples-python)
+* [Ruby](12-icinga2-api.md#icinga2-api-clients-programmatic-examples-ruby)
+* [PHP](12-icinga2-api.md#icinga2-api-clients-programmatic-examples-php)
+* [Perl](12-icinga2-api.md#icinga2-api-clients-programmatic-examples-perl)
+* [Golang](12-icinga2-api.md#icinga2-api-clients-programmatic-examples-golang)
 
 The [request method](icinga2-api-requests) is `POST` using
 [X-HTTP-Method-Override: GET](12-icinga2-api.md#icinga2-api-requests-method-override)
@@ -2260,3 +2264,72 @@ if ($status == 200) {
 
 $ perl icinga2-api-example.pl
 ```
+
+
+#### Example API Client in Golang <a id="icinga2-api-clients-programmatic-examples-golang"></a>
+
+Requires the Golang build chain.
+
+```
+$ vim icinga2-api-example.go
+
+package main
+
+import (
+       "bytes"
+       "crypto/tls"
+       "log"
+       "io/ioutil"
+       "net/http"
+)
+
+func main() {
+       var urlBase= "https://localhost:5665"
+       var apiUser= "root"
+       var apiPass= "icinga"
+
+       urlEndpoint := urlBase + "/v1/objects/services"
+
+       tr := &http.Transport{
+               TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+       }
+       httpClient := &http.Client{Transport: tr}
+
+       var requestBody = []byte(`{
+               "attrs": [ "name", "state", "last_check_result" ],
+               "joins": [ "host.name", "host.state", "host.last_check_result" ],
+               "filter": "match(\"ping*\", service.name)"
+       }`)
+
+       req, err := http.NewRequest("POST", urlEndpoint, bytes.NewBuffer(requestBody))
+       req.Header.Set("Accept", "application/json")
+       req.Header.Set("X-HTTP-Method-Override", "GET")
+
+       req.SetBasicAuth(apiUser, apiPass)
+
+       resp, err := httpClient.Do(req)
+       if err != nil {
+               log.Fatal("Server error:", err)
+               return
+       }
+       defer resp.Body.Close()
+
+       log.Print("Response status:", resp.Status)
+
+       bodyBytes, _ := ioutil.ReadAll(resp.Body)
+       bodyString := string(bodyBytes)
+
+       if resp.StatusCode == http.StatusOK {
+               log.Print("Result: " + bodyString)
+       } else {
+               log.Fatal(bodyString)
+       }
+}
+```
+
+Build the binary:
+
+```
+go build icinga2-api-example.go
+./icinga2-api-example
+```