Documentation ¶
Overview ¶
This is a chef server api client. This Library can be used to write tools to interact with the chef server.
The testing can be run with `go test`, and the client can be used as per normal via `go get github.com/Pursuit92/chef` Documentation can be found on GoDoc at http://godoc.org/github.com/Pursuit92/chef
This is example code generating a new node on the chef-server.
package main import ( "encoding/json" "fmt" "io/ioutil" "log" "os" "github.com/Pursuit92/chef" ) func main() { // read a client key key, err := ioutil.ReadFile("key.pem") if err != nil { fmt.Println("Couldn't read key.pem:", err) os.Exit(1) } // build a client client, err := chef.NewClient(&chef.Config{ Name: "foo", Key: string(key), // goiardi is on port 4545 by default. chef-zero is 8889 BaseURL: "http://localhost:4545", }) if err != nil { fmt.Println("Issue setting up client:", err) os.Exit(1) } // Create a Node object // TOOD: should have a constructor for this ranjib := chef.Node{ Name: "ranjib", Environment: "_default", ChefType: "node", JsonClass: "Chef::Node", RunList: []string{"pwn"}, } // Create _, err = client.Nodes.Post(ranjib) if err != nil { log.Fatal("Couldn't create node. ", err) } // List nodes nodeList, err := client.Nodes.List() if err != nil { log.Fatal("Couldn't list nodes: ", err) } // dump the node list in Json jsonData, err := json.MarshalIndent(nodeList, "", "\t") os.Stdout.Write(jsonData) os.Stdout.WriteString("\n") // dump the ranjib node we got from server in JSON! serverNode, _ := client.Nodes.Get("ranjib") if err != nil { log.Fatal("Couldn't get node: ", err) } jsonData, err = json.MarshalIndent(serverNode, "", "\t") os.Stdout.Write(jsonData) os.Stdout.WriteString("\n") // update node ranjib.RunList = append(ranjib.RunList, "recipe[works]") jsonData, err = json.MarshalIndent(ranjib, "", "\t") os.Stdout.Write(jsonData) os.Stdout.WriteString("\n") _, err = client.Nodes.Put(ranjib) if err != nil { log.Fatal("Couldn't update node: ", err) } // Delete node ignoring errors :) client.Nodes.Delete(ranjib.Name) }
Index ¶
- Constants
- func Base64BlockEncode(content []byte, limit int) []string
- func CheckResponse(r *http.Response) error
- func GenerateSignature(priv *rsa.PrivateKey, data string) (enc []byte, err error)
- func HashStr(toHash string) string
- func JSONReader(v interface{}) (r io.Reader, err error)
- func PrivateKeyFromString(key []byte) (*rsa.PrivateKey, error)
- type AuthConfig
- type Body
- type Client
- type ClientObj
- type ClientResult
- type ClientService
- func (e *ClientService) Delete(name string) (err error)
- func (e *ClientService) Get(name string) (client ClientObj, err error)
- func (e *ClientService) List() (data map[string]string, err error)
- func (e *ClientService) Post(client ClientObj) (data *ClientResult, err error)
- func (e *ClientService) Put(n ClientObj) (node ClientObj, err error)
- type Config
- type Cookbook
- type CookbookItem
- type CookbookListResult
- type CookbookMeta
- type CookbookService
- func (c *CookbookService) Delete(name, version string) (err error)
- func (c *CookbookService) Get(name string) (data CookbookVersion, err error)
- func (c *CookbookService) GetAvailableVersions(name, numVersions string) (data CookbookListResult, err error)
- func (c *CookbookService) GetVersion(name, version string) (data Cookbook, err error)
- func (c *CookbookService) List() (CookbookListResult, error)
- func (c *CookbookService) ListAvailableVersions(numVersions string) (data CookbookListResult, err error)
- type CookbookVersion
- type CookbookVersions
- type DataBag
- type DataBagCreateResult
- type DataBagItem
- type DataBagListResult
- type DataBagService
- func (d *DataBagService) Create(databag *DataBag) (result *DataBagCreateResult, err error)
- func (d *DataBagService) CreateItem() (err error)
- func (d *DataBagService) Delete(name string) (result *DataBag, err error)
- func (d *DataBagService) DeleteItem() (err error)
- func (d *DataBagService) GetItem(databagName, databagItem string) (item DataBagItem, err error)
- func (d *DataBagService) List() (data *DataBagListResult, err error)
- func (d *DataBagService) ListItems(name string) (data *DataBagListResult, err error)
- func (d *DataBagService) UpdateItem(databagName, databagItem string, data DataBagItem) (err error)
- type Environment
- type EnvironmentResult
- type EnvironmentService
- func (e *EnvironmentService) Create(environment *Environment) (data *EnvironmentResult, err error)
- func (e *EnvironmentService) Get(name string) (data *Environment, err error)
- func (e *EnvironmentService) List() (data *EnvironmentResult, err error)
- func (e *EnvironmentService) Put(environment *Environment) (data *Environment, err error)
- type ErrorResponse
- type Node
- type NodeResult
- type NodeService
- func (e *NodeService) Delete(name string) (err error)
- func (e *NodeService) Get(name string) (node Node, err error)
- func (e *NodeService) List() (data map[string]string, err error)
- func (e *NodeService) Post(node Node) (data *NodeResult, err error)
- func (e *NodeService) Put(n Node) (node Node, err error)
- type Role
- type RoleCreateResult
- type RoleListResult
- type RoleService
- type RunList
- type Sandbox
- type SandboxItem
- type SandboxPostResponse
- type SandboxRequest
- type SandboxService
- type SearchQuery
- type SearchResult
- type SearchService
- Bugs
Constants ¶
const ChefVersion = "11.12.0"
ChefVersion that we pretend to emulate
Variables ¶
This section is empty.
Functions ¶
func Base64BlockEncode ¶
Base64BlockEncode takes a byte slice and breaks it up into a slice of base64 encoded strings
func CheckResponse ¶
CheckResponse receives a pointer to a http.Response and generates an Error via unmarshalling
func GenerateSignature ¶
func GenerateSignature(priv *rsa.PrivateKey, data string) (enc []byte, err error)
GenerateSignature will generate a signature ( sign ) the given data
func JSONReader ¶
JSONReader handles arbitrary types and synthesizes a streaming encoder for them.
func PrivateKeyFromString ¶
func PrivateKeyFromString(key []byte) (*rsa.PrivateKey, error)
PrivateKeyFromString parses an RSA private key from a string
Types ¶
type AuthConfig ¶
type AuthConfig struct { PrivateKey *rsa.PrivateKey ClientName string }
AuthConfig representing a client and a private key used for encryption
This is embedded in the Client type
func (AuthConfig) SignRequest ¶
func (ac AuthConfig) SignRequest(request *http.Request) error
SignRequest modifies headers of an http.Request
type Body ¶
Body wraps io.Reader and adds methods for calculating hashes and detecting content
func (*Body) Buffer ¶
Buffer creates a byte.Buffer copy from a io.Reader resets read on reader to 0,0
func (*Body) ContentType ¶
ContentType returns the content-type string of Body as detected by http.DetectContentType()
type Client ¶
type Client struct { Auth *AuthConfig BaseURL *url.URL Cookbooks *CookbookService DataBags *DataBagService Environments *EnvironmentService Nodes *NodeService Roles *RoleService Sandboxes *SandboxService Search *SearchService Clients *ClientService // contains filtered or unexported fields }
Client is vessel for public methods used against the chef-server
func NewClient ¶
NewClient is the client generator used to instantiate a client for talking to a chef-server It is a simple constructor for the Client struct intended as a easy interface for issuing signed requests
type ClientObj ¶
type ClientObj struct { Name string `json:"name"` ClientName string `json:"clientname"` OrgName string `json:"orgname"` Validator bool `json:"validator"` Certificate string `json:"certificate"` Admin bool `json:"admin"` PrivateKey string `json:"private_key"` }
Client represents the native Go version of the deserialized Client type
func NewClientObj ¶
NewClient is the Client constructor method
type ClientResult ¶
type ClientResult struct {
Uri string `json:"uri"`
}
type ClientService ¶
type ClientService struct {
// contains filtered or unexported fields
}
func (*ClientService) Delete ¶
func (e *ClientService) Delete(name string) (err error)
Delete removes a node on the Chef server
Chef API docs: https://docs.getchef.com/api_chef_server.html#id40
func (*ClientService) Get ¶
func (e *ClientService) Get(name string) (client ClientObj, err error)
Get gets a node from the Chef server.
Chef API docs: http://docs.opscode.com/api_chef_server.html#id28
func (*ClientService) List ¶
func (e *ClientService) List() (data map[string]string, err error)
List lists the nodes in the Chef server.
Chef API docs: http://docs.opscode.com/api_chef_server.html#id25
func (*ClientService) Post ¶
func (e *ClientService) Post(client ClientObj) (data *ClientResult, err error)
Post creates a Client on the chef server
Chef API docs: https://docs.getchef.com/api_chef_server.html#id39
func (*ClientService) Put ¶
func (e *ClientService) Put(n ClientObj) (node ClientObj, err error)
Put updates a node on the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id42
type Config ¶
type Config struct { // This should be the user ID on the chef server Name string // This is the plain text private Key for the user Key string // BaseURL is the chef server URL used to connect too. Is using orgs you should include your org in the url BaseURL string // When set to false (default) this will enable SSL Cert Verification. If you need to disable Cert Verification set to true SkipSSL bool }
Config contains the configuration options for a chef client. This is Used primarily in the NewClient() constructor in order to setup a proper client object
type Cookbook ¶
type Cookbook struct { CookbookName string `json:"cookbook_name"` Name string `json:"name"` Version string `json:"version,omitempty"` ChefType string `json:"chef_type,omitempty"` Frozen bool `json:"frozen?,omitempty"` JsonClass string `json:"json_class,omitempty"` Files []CookbookItem `json:"files,omitempty"` Templates []CookbookItem `json:"Templates,omitempty"` Attributes []CookbookItem `json:"attributes,omitempty"` Recipes []CookbookItem `json:"recipes,omitempty"` Definitions []CookbookItem `json:"definitions,omitempty"` Libraries []CookbookItem `json:"libraries,omitempty"` Providers []CookbookItem `json:"Providers,omitempty"` Resources []CookbookItem `json:"Resources,omitempty"` RootFiles []CookbookItem `json:"Templates,omitempty"` Metadata CookbookMeta `json:"Metadata,omitempty"` }
Cookbook represents the native Go version of the deserialized api cookbook
type CookbookItem ¶
type CookbookItem struct { Url string `json:"url,omitempty"` Path string `json:"path,omitempty"` Name string `json:"name,omitempty"` Checksum string `json:"checksum,omitempty"` Specificity string `json:"specificity,omitempty"` }
CookbookItem represents a object of cookbook file data
type CookbookListResult ¶
type CookbookListResult map[string]CookbookVersions
CookbookListResult is the summary info returned by chef-api when listing http://docs.opscode.com/api_chef_server.html#cookbooks
func (CookbookListResult) String ¶
func (c CookbookListResult) String() (out string)
String makes CookbookListResult implement the string result
type CookbookMeta ¶
type CookbookMeta struct { Name string `json:"cookbook_name,omitempty"` Version string `json:"version,omitempty"` Description string `json:"description,omitempty"` LongDescription string `json:"long_description,omitempty"` Maintainer string `json:"maintainer,omitempty"` MaintainerEmail string `json:"maintainer_email,omitempty"` License string `json:"license,omitempty"` Platforms map[string]string `json:"platforms,omitempty"` Depends map[string]string `json:"dependencies,omitempty"` Reccomends map[string]string `json:"recommendations,omitempty"` Suggests map[string]string `json:"suggestions,omitempty"` Conflicts map[string]string `json:"conflicting,omitempty"` Provides map[string]string `json:"providing,omitempty"` Replaces map[string]string `json:"replacing,omitempty"` // contains filtered or unexported fields }
CookbookMeta represents a Golang version of cookbook metadata
type CookbookService ¶
type CookbookService struct {
// contains filtered or unexported fields
}
CookbookService is the service for interacting with chef server cookbooks endpoint
func (*CookbookService) Delete ¶
func (c *CookbookService) Delete(name, version string) (err error)
DeleteVersion removes a version of a cook from a server
func (*CookbookService) Get ¶
func (c *CookbookService) Get(name string) (data CookbookVersion, err error)
Get retruns a CookbookVersion for a specific cookbook
GET /cookbooks/name
func (*CookbookService) GetAvailableVersions ¶
func (c *CookbookService) GetAvailableVersions(name, numVersions string) (data CookbookListResult, err error)
GetAvailable returns the versions of a coookbook available on a server
func (*CookbookService) GetVersion ¶
func (c *CookbookService) GetVersion(name, version string) (data Cookbook, err error)
GetVersion fetches a specific version of a cookbooks data from the server api
GET /cookbook/foo/1.2.3 GET /cookbook/foo/_latest Chef API docs: http://docs.opscode.com/api_chef_server.html#id5
func (*CookbookService) List ¶
func (c *CookbookService) List() (CookbookListResult, error)
List returns a CookbookListResult with the latest versions of cookbooks available on the server
func (*CookbookService) ListAvailableVersions ¶
func (c *CookbookService) ListAvailableVersions(numVersions string) (data CookbookListResult, err error)
ListVersions lists the cookbooks available on the server limited to numVersions
Chef API docs: http://docs.opscode.com/api_chef_server.html#id2
type CookbookVersion ¶
type CookbookVersion struct { Url string `json:"url,omitempty"` Version string `json:"version,omitempty"` }
CookbookVersion is the data for a specific cookbook version
type CookbookVersions ¶
type CookbookVersions struct { Url string `json:"url,omitempty"` Versions []CookbookVersion `json:"versions,omitempty"` }
CookbookVersions is the data container returned from the chef server when listing all cookbooks
type DataBag ¶
type DataBag struct { Name string `json:"name"` JsonClass string `json:"json_class"` ChefType string `json:"chef_type"` }
DataBag is a data bag
type DataBagCreateResult ¶
type DataBagCreateResult struct {
URI string `json:"uri"`
}
type DataBagListResult ¶
DataBagListResult is the list of data bags returned by chef-api when listing http://docs.getchef.com/api_chef_server.html#data
func (DataBagListResult) String ¶
func (d DataBagListResult) String() (out string)
String makes DataBagListResult implement the string result
type DataBagService ¶
type DataBagService struct {
// contains filtered or unexported fields
}
DataBagService is the service for interacting with the chef server data endpoint
func (*DataBagService) Create ¶
func (d *DataBagService) Create(databag *DataBag) (result *DataBagCreateResult, err error)
Create adds a data bag to the server
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id19
func (*DataBagService) CreateItem ¶
func (d *DataBagService) CreateItem() (err error)
CreateItem adds an item to a data bag
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id21
func (*DataBagService) Delete ¶
func (d *DataBagService) Delete(name string) (result *DataBag, err error)
Delete removes a data bag from the server
Chef API Docs: ????????????????
func (*DataBagService) DeleteItem ¶
func (d *DataBagService) DeleteItem() (err error)
DeleteItem deletes an item from a data bag
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id22
func (*DataBagService) GetItem ¶
func (d *DataBagService) GetItem(databagName, databagItem string) (item DataBagItem, err error)
GetItem gets an item from a data bag
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id23
func (*DataBagService) List ¶
func (d *DataBagService) List() (data *DataBagListResult, err error)
List returns a list of databags on the server
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id18
func (*DataBagService) ListItems ¶
func (d *DataBagService) ListItems(name string) (data *DataBagListResult, err error)
ListItems gets a list of the items in a data bag.
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id20
func (*DataBagService) UpdateItem ¶
func (d *DataBagService) UpdateItem(databagName, databagItem string, data DataBagItem) (err error)
UpdateItem updates an item in a data bag
Chef API Docs: http://docs.getchef.com/api_chef_server.html#id24
type Environment ¶
type Environment struct { Name string `json:"name"` Description string `json:"description"` ChefType string `json:"chef_type"` Attributes interface{} `json:"attributes,omitempty"` DefaultAttributes interface{} `json:"default_attributes,omitempty"` OverrideAttributes interface{} `json:"override_attributes,omitempty"` JsonClass string `json:"json_class,omitempty"` CookbookVersions map[string]string `json:"cookbook_versions"` }
Environment represents the native Go version of the deserialized Environment type
type EnvironmentResult ¶
func (EnvironmentResult) String ¶
func (e EnvironmentResult) String() (out string)
String makes EnvironmentResult implement the string result
type EnvironmentService ¶
type EnvironmentService struct {
// contains filtered or unexported fields
}
Environment has a Reader, hey presto
func (*EnvironmentService) Create ¶
func (e *EnvironmentService) Create(environment *Environment) (data *EnvironmentResult, err error)
Create an environment in the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id15
func (*EnvironmentService) Get ¶
func (e *EnvironmentService) Get(name string) (data *Environment, err error)
Get gets an environment from the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id17
func (*EnvironmentService) List ¶
func (e *EnvironmentService) List() (data *EnvironmentResult, err error)
List lists the environments in the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id14
func (*EnvironmentService) Put ¶
func (e *EnvironmentService) Put(environment *Environment) (data *Environment, err error)
Write an environment to the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id18
type ErrorResponse ¶
An ErrorResponse reports one or more errors caused by an API request. Thanks to https://github.com/google/go-github
func (*ErrorResponse) Error ¶
func (r *ErrorResponse) Error() string
type Node ¶
type Node struct { Name string `json:"name,omitempty"` Environment string `json:"chef_environment"` ChefType string `json:"chef_type,omitempty"` AutomaticAttributes map[string]interface{} `json:"automatic,omitempty"` NormalAttributes map[string]interface{} `json:"normal"` DefaultAttributes map[string]interface{} `json:"default,omitempty"` OverrideAttributes map[string]interface{} `json:"override,omitempty"` JsonClass string `json:"json_class,omitempty"` RunList []string `json:"run_list"` }
Node represents the native Go version of the deserialized Node type
type NodeResult ¶
type NodeResult struct {
Uri string `json:"uri"`
}
type NodeService ¶
type NodeService struct {
// contains filtered or unexported fields
}
func (*NodeService) Delete ¶
func (e *NodeService) Delete(name string) (err error)
Delete removes a node on the Chef server
Chef API docs: https://docs.getchef.com/api_chef_server.html#id40
func (*NodeService) Get ¶
func (e *NodeService) Get(name string) (node Node, err error)
Get gets a node from the Chef server.
Chef API docs: http://docs.opscode.com/api_chef_server.html#id28
func (*NodeService) List ¶
func (e *NodeService) List() (data map[string]string, err error)
List lists the nodes in the Chef server.
Chef API docs: http://docs.opscode.com/api_chef_server.html#id25
func (*NodeService) Post ¶
func (e *NodeService) Post(node Node) (data *NodeResult, err error)
Post creates a Node on the chef server
Chef API docs: https://docs.getchef.com/api_chef_server.html#id39
func (*NodeService) Put ¶
func (e *NodeService) Put(n Node) (node Node, err error)
Put updates a node on the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id42
type Role ¶
type Role struct { Name string `json:"name"` ChefType string `json:"chef_type"` Description string `json:"description"` RunList RunList `json:"run_list"` DefaultAttributes interface{} `json:"default_attributes,omitempty"` OverrideAttributes interface{} `json:"override_attributes,omitempty"` JsonClass string `json:"json_class,omitempty"` }
Role represents the native Go version of the deserialized Role type
type RoleCreateResult ¶
func (RoleCreateResult) String ¶
func (e RoleCreateResult) String() (out string)
String makes RoleCreateResult implement the string result
type RoleListResult ¶
func (RoleListResult) String ¶
func (e RoleListResult) String() (out string)
String makes RoleListResult implement the string result
type RoleService ¶
type RoleService struct {
// contains filtered or unexported fields
}
func (*RoleService) Create ¶
func (e *RoleService) Create(role *Role) (data *RoleCreateResult, err error)
Create a new role in the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id32
func (*RoleService) Get ¶
func (e *RoleService) Get(name string) (data *Role, err error)
Get gets a role from the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id34
func (*RoleService) List ¶
func (e *RoleService) List() (data *RoleListResult, err error)
List lists the roles in the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id31
func (*RoleService) Put ¶
func (e *RoleService) Put(role *Role) (data *Role, err error)
Update a role in the Chef server.
Chef API docs: http://docs.getchef.com/api_chef_server.html#id35
type Sandbox ¶
type Sandbox struct { ID string `json:"guid"` Name string `json:"name"` CreationTime time.Time `json:"create_time"` Completed bool `json:"is_completed"` Checksums []string }
Sandbox Is the structure of an actul sandbox that has been created and returned by the final PUT to the sandbox ID
type 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 SandboxPostResponse ¶
type SandboxPostResponse struct { ID string `json:"sandbox_id"` Uri string `json:"uri"` Checksums map[string]SandboxItem }
SandboxPostResponse is the struct returned from the chef-server for Post Requests to /sandbox
type SandboxRequest ¶
type SandboxRequest struct {
Checksums map[string]interface{} `json:"checksums"`
}
SandboxRequest is the desired chef-api structure for a Post body
type SandboxService ¶
type SandboxService struct {
// contains filtered or unexported fields
}
SandboxService is the chef-client Sandbox service used as the entrypoint and caller for Sandbox methods
func (SandboxService) Post ¶
func (s SandboxService) Post(sums []string) (data SandboxPostResponse, err error)
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
type SearchQuery ¶
type SearchQuery struct { // The index you want to search Index string // The query you want to execute. This is the 'chef' query ex: 'chef_environment:prod' Query string // Sort order you want the search results returned SortBy string // Starting position for search Start int // Number of rows to return Rows int }
SearchQuery Is the struct for holding a query request
func (SearchQuery) Do ¶
func (q SearchQuery) Do(client *Client) (res SearchResult, err error)
Do will execute the search query on the client
func (SearchQuery) String ¶
func (q SearchQuery) String() string
String implements the Stringer Interface for the SearchQuery
type SearchResult ¶
search result will return a slice of interface{} of chef-like objects (roles/nodes/etc)
type SearchService ¶
type SearchService struct {
// contains filtered or unexported fields
}
func (SearchService) Exec ¶
func (e SearchService) Exec(idx, statement string) (res SearchResult, err error)
Exec runs the query on the index passed in. This is a helper method. If you wnat more controll over the query use NewQuery and its Do() method. BUG(spheromak): Should we use exec or SearchQuery.Do() or have both ?
func (SearchService) Indexes ¶
func (e SearchService) Indexes() (data map[string]string, err error)
List lists the nodes in the Chef server.
Chef API docs: http://docs.opscode.com/api_chef_server.html#id25
func (SearchService) NewQuery ¶
func (e SearchService) NewQuery(idx, statement string) (query SearchQuery, err error)
NewSearch is a constructor for a SearchQuery struct. This is used by other search service methods to perform search requests on the server
Notes ¶
Bugs ¶
tightly coupled
This is now both a *response* decoder and handles upload.. gettin smelly
Should we use exec or SearchQuery.Do() or have both ?