github.com/go-chef/chef@v0.30.1/sandbox.go (about)

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // SandboxService is the chef-client Sandbox service used as the entrypoint and caller for Sandbox methods
     9  type SandboxService struct {
    10  	client *Client
    11  }
    12  
    13  // SandboxRequest is the desired chef-api structure for a Post body
    14  type SandboxRequest struct {
    15  	Checksums map[string]interface{} `json:"checksums"`
    16  }
    17  
    18  // SandboxPostResponse is the struct returned from the chef-server for Post Requests to /sandboxes
    19  type SandboxPostResponse struct {
    20  	ID        string `json:"sandbox_id"`
    21  	Uri       string `json:"uri"`
    22  	Checksums map[string]SandboxItem
    23  }
    24  
    25  // A SandboxItem is embedded into the response from the chef-server and the actual sandbox is the Url and state for a specific Item.
    26  type SandboxItem struct {
    27  	Url    string `json:"url"`
    28  	Upload bool   `json:"needs_upload"`
    29  }
    30  
    31  // Sandbox Is the structure of an actual sandbox that has been created and returned by the final PUT to the sandbox ID
    32  type Sandbox struct {
    33  	ID           string    `json:"guid"`
    34  	Name         string    `json:"name"`
    35  	CreationTime time.Time `json:"create_time"`
    36  	Completed    bool      `json:"is_completed"`
    37  	Checksums    []string
    38  }
    39  
    40  // 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.
    41  // http://docs.getchef.com/api_chef_server.html#id38
    42  func (s SandboxService) Post(sums []string) (data SandboxPostResponse, err error) {
    43  	smap := make(map[string]interface{})
    44  	for _, hashstr := range sums {
    45  		smap[hashstr] = nil
    46  	}
    47  	sboxReq := SandboxRequest{Checksums: smap}
    48  
    49  	body, err := JSONReader(sboxReq)
    50  	if err != nil {
    51  		return
    52  	}
    53  
    54  	err = s.client.magicRequestDecoder("POST", "sandboxes", body, &data)
    55  	return
    56  }
    57  
    58  // Put is used to commit a sandbox ID to the chef server. To signal that the sandbox you have Posted is now uploaded.
    59  func (s SandboxService) Put(id string) (box Sandbox, err error) {
    60  	answer := make(map[string]bool)
    61  	answer["is_completed"] = true
    62  	body, err := JSONReader(answer)
    63  
    64  	if id == "" {
    65  		return box, fmt.Errorf("must supply sandbox id to PUT request.")
    66  	}
    67  
    68  	err = s.client.magicRequestDecoder("PUT", "sandboxes/"+id, body, &box)
    69  	return
    70  }