github.com/go-chef/chef@v0.30.1/doc.go (about) 1 /* 2 This is a Chef Infra Server API client. This Library can be used to write tools to 3 interact with the chef server. 4 5 The testing can be run with go test, and the client can be used as per normal via: 6 7 go get github.com/go-chef/chef 8 9 Documentation can be found on GoDoc at http://godoc.org/github.com/go-chef/chef 10 11 This is an example code generating a new node on a Chef Infra Server: 12 13 package main 14 15 import ( 16 "encoding/json" 17 "fmt" 18 "log" 19 "os" 20 21 chef "github.com/go-chef/chef 22 ) 23 24 func main() { 25 // read a client key 26 key, err := os.ReadFile("key.pem") 27 if err != nil { 28 fmt.Println("Couldn't read key.pem:", err) 29 os.Exit(1) 30 } 31 32 // build a client 33 client, err := chef.NewClient(&chef.Config{ 34 Name: "foo", 35 Key: string(key), 36 // goiardi is on port 4545 by default. chef-zero is 8889 37 BaseURL: "http://localhost:4545", 38 }) 39 if err != nil { 40 fmt.Println("unable to setup client:", err) 41 os.Exit(1) 42 } 43 44 // create a Node object 45 ranjib := chef.NewNode("ranjib") 46 47 // create the node on the Chef Infra Server 48 _, err = client.Nodes.Post(ranjib) 49 if err != nil { 50 log.Fatal("couldn't create node. ", err) 51 } 52 53 // list nodes 54 nodeList, err := client.Nodes.List() 55 if err != nil { 56 log.Fatal("couldn't list nodes: ", err) 57 } 58 59 // dump the node list in Json 60 jsonData, err := json.MarshalIndent(nodeList, "", "\t") 61 if err != nil { 62 log.Fatal("couldn't marshal nodes list: ", err) 63 } 64 fmt.Println(jsonData) 65 66 // dump the ranjib node we got from server in JSON! 67 serverNode, _ := client.Nodes.Get("ranjib") 68 if err != nil { 69 log.Fatal("couldn't get node: ", err) 70 } 71 jsonData, err = json.MarshalIndent(serverNode, "", "\t") 72 if err != nil { 73 log.Fatal("couldn't marshal node: ", err) 74 } 75 fmt.Println(jsonData) 76 77 // update node 78 ranjib.RunList = append(ranjib.RunList, "recipe[works]") 79 jsonData, err = json.MarshalIndent(ranjib, "", "\t") 80 if err != nil { 81 log.Fatal("couldn't marshal node: ", err) 82 } 83 fmt.Println(jsonData) 84 85 _, err = client.Nodes.Put(ranjib) 86 if err != nil { 87 log.Fatal("couldn't update node: ", err) 88 } 89 90 // delete node 91 client.Nodes.Delete(ranjib.Name) 92 if err != nil { 93 fmt.Println("unable to delete node:", err) 94 os.Exit(1) 95 } 96 } 97 */ 98 package chef