-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add _status and universe endpoint support [minor]
Add the status and organization endpoint examples
- Loading branch information
1 parent
3b96bdd
commit 4f79680
Showing
17 changed files
with
386 additions
and
26 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
examples/chefapi_examples/files/default/go/src/chefapi_test/bin/status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
examples/chefapi_examples/files/default/go/src/chefapi_test/bin/universe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
22 changes: 22 additions & 0 deletions
22
examples/chefapi_examples/files/default/go/src/chefapi_test/cmd/status/status.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/chefapi_examples/files/default/go/src/chefapi_test/cmd/universe/universe.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
examples/chefapi_examples/spec/unit/recipes/default_spec.rb
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
examples/chefapi_examples/test/integration/default/inspec/node_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
examples/chefapi_examples/test/integration/default/inspec/status_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
examples/chefapi_examples/test/integration/default/inspec/universe_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.