Skip to content

Instantly share code, notes, and snippets.

@theckman
Last active December 29, 2020 17:08
Show Gist options
  • Save theckman/f27168a7f4d585a7fff9 to your computer and use it in GitHub Desktop.
Save theckman/f27168a7f4d585a7fff9 to your computer and use it in GitHub Desktop.
Simple usage of Chef partial search in Golang
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/go-chef/chef"
)
func kaboom(message string) {
fmt.Fprint(os.Stderr, message)
os.Exit(1)
}
func main() {
clientKey, err := ioutil.ReadFile("/etc/chef/client.pem")
if err != nil {
kaboom(fmt.Sprintf("error reading client PEM file: %s\n", err.Error()))
}
hostname, _ := os.Hostname()
client, err := chef.NewClient(&chef.Config{
Name: hostname,
Key: string(clientKey),
BaseURL: "http://localhost:4545",
})
if err != nil {
kaboom(fmt.Sprintf("error building Chef client: %s\n", err.Error()))
}
params := make(map[string]interface{})
params["ec2.placement_availability_zone"] = []string{"ec2", "placement_availability_zone"}
search := fmt.Sprintf("name:%s", hostname)
res, err := client.Search.PartialExec("node", search, params)
fmt.Println(err)
fmt.Printf("%+v\n", res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment