forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.go
69 lines (57 loc) · 2.21 KB
/
sandbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package chef
import (
"fmt"
"strings"
"time"
)
// SandboxService is the chef-client Sandbox service used as the entrypoint and caller for Sandbox methods
type SandboxService struct {
client *Client
}
// SandboxRequest is the desired chef-api structure for a Post body
type SandboxRequest struct {
Checksums map[string]interface{} `json:"checksums"`
}
// SandboxPostResponse is the struct returned from the chef-server for Post Requests to /sandbox
type SandboxPostResponse struct {
ID string `json:"sandbox_id"`
Uri string `json:"uri"`
Checksums map[string]SandboxItem
}
// A SandbooxItem is embeddedinto the response from the chef-server and the actual sandbox It is the Url and state for a specific Item.
type SandboxItem struct {
Url string `json:"url"`
Upload bool `json:"needs_upload"`
}
// Sandbox Is the structure of an actul sandbox that has been created and returned by the final PUT to the sandbox ID
type Sandbox struct {
ID string `json:"guid"`
Name string `json:"name"`
CreationTime time.Time `json:"create_time"`
Completed bool `json:"is_completed"`
Checksums []string
}
// Post creates a new sandbox on the chef-server. Deviates from the Chef-server api in that it takes a []string of sums for the sandbox instead of the IMO rediculous hash of nulls that the API wants. We convert it to the right structure under the hood for the chef-server api.
// http://docs.getchef.com/api_chef_server.html#id38
func (s SandboxService) Post(sums []string) (data SandboxPostResponse, err error) {
smap := make(map[string]interface{})
for _, hashstr := range sums {
smap[hashstr] = nil
}
sboxReq := SandboxRequest{Checksums: smap}
body, err := JSONReader(sboxReq)
if err != nil {
return
}
err = s.client.magicRequestDecoder("POST", "/sandboxes", body, &data)
return
}
// Put is used to commit a sandbox ID to the chef server. To singal that the sandox you have Posted is now uploaded.
func (s SandboxService) Put(id string) (box Sandbox, err error) {
body := strings.NewReader(`{"is_completed":true}"}`)
if id == "" {
return box, fmt.Errorf("must supply sandbox id to PUT request.")
}
err = s.client.magicRequestDecoder("PUT", "/sandboxes/"+id, body, &box)
return
}