Skip to content

Commit

Permalink
Add _status and universe endpoint support [minor]
Browse files Browse the repository at this point in the history
Add the status and organization endpoint examples
  • Loading branch information
MarkGibbons committed Jan 19, 2020
1 parent 3b96bdd commit 4f79680
Show file tree
Hide file tree
Showing 17 changed files with 386 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Status testing

BASE=$(dirname $0)

. ${BASE}/setup
. ${BASE}/creds

go run ${BASE}/../cmd/status/status.go ${CHEFUSER} ${KEYFILE} ${CHEFGLOBALURL} true
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Universe testing

BASE=$(dirname $0)

. ${BASE}/setup
. ${BASE}/creds

go run ${BASE}/../cmd/universe/universe.go ${CHEFUSER} ${KEYFILE} ${CHEFORGANIZATIONURL} true
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Test the go-chef/chef chef server api /_status endpoints against a live server
//
package main

import (
"fmt"
"chefapi_test/testapi"
)


// main Exercise the chef server api
func main() {
// Create a client for access
client := testapi.Client()

status, err := client.Status.Get()
if err != nil {
fmt.Println("Issue getting status information", err)
}
fmt.Printf("List status: %+v", status)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Test the go-chef/chef chef server api /universe endpoints against a live server
//
package main

import (
"fmt"
"chefapi_test/testapi"
)


// main Exercise the chef server api
func main() {
// Create a client for access
client := testapi.Client()

universe, err := client.Universe.Get()
if err != nil {
fmt.Println("Issue getting universe information", err)
}
fmt.Printf("List universe: %+v", universe)
}
2 changes: 0 additions & 2 deletions examples/chefapi_examples/spec/spec_helper.rb

This file was deleted.

22 changes: 0 additions & 22 deletions examples/chefapi_examples/spec/unit/recipes/default_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Inspec tests for the node chef api go module
# Inspec tests for the status chef api go module
#

describe command('/go/src/chefapi_test/bin/node') do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Inspec tests for the status chef api go module
#

describe command('/go/src/chefapi_test/bin/status') do
its('stderr') { should_not match(/error|no such file|cannot find|not used|undefined/) }
its('stdout') { should match(/List status: \{Status:pong Upstreams:map\[(?=.*chef_solr:pong)(?=.*chef_sql:pong)(?=.*chef_index:pong)(?=.*oc_chef_action:pong)(?=.*oc_chef_authz:pong).*\].*Keygen:map\[(?=.*keys:10)(?=.*max:10)(?=.*max_workers:1)(?=.*cur_max_workers:1)(?=.*inflight:0)(?=.*avail_workers:1)(?=.*start_size:0).*\]\}/) }
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Inspec tests for the universe chef api go module
#

describe command('/go/src/chefapi_test/bin/universe') do
its('stderr') { should_not match(/error|no such file|cannot find|not used|undefined/) }
its('stdout') { should match(%r{^List universe: \{Books:map\[sampbook:\{Versions:map\[(?=.*0.2.0:\{LocationPath:https:\/\/localhost\/organizations\/test\/cookbooks\/sampbook\/0.2.0 LocationType:chef_server Dependencies:map\[\]\})(?=.*0.1.0:\{LocationPath:https:\/\/localhost\/organizations\/test\/cookbooks\/sampbook\/0.1.0 LocationType:chef_server Dependencies:map\[\]\}).*\]\}\]\}}) }
end
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ require (
github.com/cenkalti/backoff v2.1.1+incompatible
github.com/ctdk/goiardi v0.11.10
github.com/davecgh/go-spew v1.1.1
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a
github.com/google/go-cmp v0.4.0
github.com/gopherjs/gopherjs v0.0.0-20191106031601-ce3c9ade29de // indirect
github.com/smartystreets/assertions v1.0.1 // indirect
github.com/smartystreets/goconvey v1.6.4
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.2.7 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ github.com/ctdk/goiardi v0.11.10/go.mod h1:Pr6Cj6Wsahw45myttaOEZeZ0LE7p1qzWmzgsB
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gopherjs/gopherjs v0.0.0-20191106031601-ce3c9ade29de h1:F7WD09S8QB4LrkEpka0dFPLSotH11HRpCsLIbIcJ7sU=
github.com/gopherjs/gopherjs v0.0.0-20191106031601-ce3c9ade29de/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v1.0.1 h1:voD4ITNjPL5jjBfgR/r8fPIIBrliWrWHeiJApdr3r4w=
github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -25,6 +33,9 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4 changes: 4 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type Client struct {
Roles *RoleService
Sandboxes *SandboxService
Search *SearchService
Status *StatusService
Universe *UniverseService
Users *UserService
}

Expand Down Expand Up @@ -171,6 +173,8 @@ func NewClient(cfg *Config) (*Client, error) {
c.Roles = &RoleService{client: c}
c.Sandboxes = &SandboxService{client: c}
c.Search = &SearchService{client: c}
c.Status = &StatusService{client: c}
c.Universe = &UniverseService{client: c}
c.Users = &UserService{client: c}
return c, nil
}
Expand Down
20 changes: 20 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package chef

type StatusService struct {
client *Client
}

// Status represents the body of the returned information.
type Status struct {
Status string `json:"status"`
Upstreams map[string]string `json:"upstreams"`
Keygen map[string]int `json:"keygen"`
}

// Status gets license information.
//
// https://docs.chef.io/api_chef_server.html#license
func (e *StatusService) Get() (data Status, err error) {
err = e.client.magicRequestDecoder("GET", "_status", nil, &data)
return
}
81 changes: 81 additions & 0 deletions status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package chef

import (
"encoding/json"
"fmt"
// "github.com/google/go-cmp/cmp"
"io"
"log"
"net/http"
"os"
"reflect"
"testing"
)

var (
testStatusJSON = "test/status.json"
)

func TestStatusFromJSONDecoder(t *testing.T) {
if file, err := os.Open(testStatusJSON); err != nil {
t.Error("Unexpected error '", err, "' during os.Open on", testStatusJSON)
} else {
dec := json.NewDecoder(file)
var g Status
if err := dec.Decode(&g); err == io.EOF {
log.Fatal(g)
} else if err != nil {
log.Fatal(err)
}
}
}

func TestStatusGet(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/_status", func(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body)
var request Status
dec.Decode(&request)
switch {
case r.Method == "GET":
fmt.Fprintf(w, `{
"status": "pong",
"upstreams": {
"chef_elasticsearch": "pong",
"chef_sql": "pong",
"chef_index": "pong",
"oc_chef_authz": "pong",
"data_collector": "pong"
},
"keygen": {
"keys": 10,
"max": 10,
"max_workers": 2,
"cur_max_workers": 2,
"inflight": 0,
"avail_workers": 2,
"start_size": 0
}
}`)
}

})

wantStatus := Status{
Status: "pong",
Upstreams: map[string]string{"chef_elasticsearch": "pong", "chef_index": "pong", "chef_sql": "pong", "data_collector": "pong", "oc_chef_authz": "pong"},
Keygen: map[string]int{"avail_workers": 2, "cur_max_workers": 2, "inflight": 0, "keys": 10, "max": 10, "max_workers": 2, "start_size": 0},
}

status, err := client.Status.Get()
if err != nil {
t.Errorf("Status.Get returned error: %s", err.Error())
}

if !reflect.DeepEqual(status, wantStatus) {
t.Errorf("Status.Get returned %+v, want %+v, error %+v", status, wantStatus, err)
}

}
19 changes: 19 additions & 0 deletions test/status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"status": "pong",
"upstreams": {
"chef_elasticsearch": "pong",
"chef_sql": "pong",
"chef_index": "pong",
"oc_chef_authz": "pong",
"data_collector": "pong"
},
"keygen": {
"keys": 10,
"max": 10,
"max_workers": 2,
"cur_max_workers": 2,
"inflight": 0,
"avail_workers": 2,
"start_size": 0
}
}
70 changes: 70 additions & 0 deletions universe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package chef

type UniverseService struct {
client *Client
}

// Universe represents the body of the returned information.
type Universe struct {
Books map[string]UniverseBook
}

type UniverseBook struct {
Versions map[string]UniverseVersion
}

type UniverseVersion struct {
LocationPath string
LocationType string
Dependencies map[string]string
}

// Universe gets available cookbook version information.
//
// https://docs.chef.io/api_chef_server.html#universe
func (e *UniverseService) Get() (universe Universe, err error) {
var data map[string]interface{}
err = e.client.magicRequestDecoder("GET", "universe", nil, &data)
unpackUniverse(&universe, &data)
return
}

func unpackUniverse(universe *Universe, data *map[string]interface{}) {
(*universe).Books = make(map[string]UniverseBook)
for bookn, versions := range *data {
ub := UniverseBook{}
ub.Versions = make(map[string]UniverseVersion)
switch versions.(type) {
case map[string]interface{}:
for vname, version := range versions.(map[string]interface{}) {
uv := UniverseVersion{}
switch version.(type) {
case map[string]interface{}:
for aname, attr := range version.(map[string]interface{}) {
deps := make(map[string]string)
switch aname {
case "dependencies":
for dname, dep := range attr.(map[string]interface{}) {
switch dep.(type) {
case string:
deps[dname] = dep.(string)
default:
}
}
uv.Dependencies = deps
case "location_path":
uv.LocationPath = attr.(string)
case "location_type":
uv.LocationType = attr.(string)
}
}
default:
}
ub.Versions[vname] = uv
}
default:
}
(*universe).Books[bookn] = ub
}
return
}
Loading

0 comments on commit 4f79680

Please sign in to comment.