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

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  )
     7  
     8  // Environment has a Reader, hey presto
     9  type EnvironmentService struct {
    10  	client *Client
    11  }
    12  
    13  type EnvironmentResult map[string]string
    14  
    15  // Environment represents the native Go version of the deserialized Environment type
    16  type Environment struct {
    17  	Name               string            `json:"name"`
    18  	Description        string            `json:"description"`
    19  	ChefType           string            `json:"chef_type,omitempty"`
    20  	Attributes         interface{}       `json:"attributes,omitempty"`
    21  	DefaultAttributes  interface{}       `json:"default_attributes,omitempty"`
    22  	OverrideAttributes interface{}       `json:"override_attributes,omitempty"`
    23  	JsonClass          string            `json:"json_class,omitempty"`
    24  	CookbookVersions   map[string]string `json:"cookbook_versions"`
    25  }
    26  
    27  type EnvironmentCookbookResult map[string]CookbookVersions
    28  
    29  type EnvironmentRecipesResult []string
    30  
    31  func strMapToStr(e map[string]string) (out string) {
    32  	keys := make([]string, len(e))
    33  	for k, _ := range e {
    34  		keys = append(keys, k)
    35  	}
    36  	sort.Strings(keys)
    37  	for _, k := range keys {
    38  		if k == "" {
    39  			continue
    40  		}
    41  		out += fmt.Sprintf("%s => %s\n", k, e[k])
    42  	}
    43  	return
    44  }
    45  
    46  // String makes EnvironmentResult implement the string result
    47  func (e EnvironmentResult) String() (out string) {
    48  	return strMapToStr(e)
    49  }
    50  
    51  // List lists the environments in the Chef server.
    52  //
    53  // Chef API docs: https://docs.chef.io/api_chef_server.html#environments
    54  func (e *EnvironmentService) List() (data *EnvironmentResult, err error) {
    55  	err = e.client.magicRequestDecoder("GET", "environments", nil, &data)
    56  	return
    57  }
    58  
    59  // Create an environment in the Chef server.
    60  //
    61  // Chef API docs: https://docs.chef.io/api_chef_server.html#environments
    62  func (e *EnvironmentService) Create(environment *Environment) (data *EnvironmentResult, err error) {
    63  	body, err := JSONReader(environment)
    64  	if err != nil {
    65  		return
    66  	}
    67  
    68  	err = e.client.magicRequestDecoder("POST", "environments", body, &data)
    69  	return
    70  }
    71  
    72  // Delete an environment from the Chef server.
    73  //
    74  // Chef API docs: https://docs.chef.io/api_chef_server/#delete-9
    75  func (e *EnvironmentService) Delete(name string) (data *Environment, err error) {
    76  	path := fmt.Sprintf("environments/%s", name)
    77  	err = e.client.magicRequestDecoder("DELETE", path, nil, &data)
    78  	return
    79  }
    80  
    81  // Get gets an environment from the Chef server.
    82  //
    83  // Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name
    84  func (e *EnvironmentService) Get(name string) (data *Environment, err error) {
    85  	path := fmt.Sprintf("environments/%s", name)
    86  	err = e.client.magicRequestDecoder("GET", path, nil, &data)
    87  	return
    88  }
    89  
    90  // Write an environment to the Chef server.
    91  //
    92  // Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name
    93  // TODO: Fix the name restriction. The parms should be name, environment
    94  func (e *EnvironmentService) Put(environment *Environment) (data *Environment, err error) {
    95  	path := fmt.Sprintf("environments/%s", environment.Name)
    96  	body, err := JSONReader(environment)
    97  	if err != nil {
    98  		return
    99  	}
   100  
   101  	err = e.client.magicRequestDecoder("PUT", path, body, &data)
   102  	return
   103  }
   104  
   105  // Get the versions of a cookbook for this environment from the Chef server.
   106  //
   107  // Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name-cookbooks
   108  func (e *EnvironmentService) ListCookbooks(name string, numVersions string) (data EnvironmentCookbookResult, err error) {
   109  	path := versionParams(fmt.Sprintf("environments/%s/cookbooks", name), numVersions)
   110  	err = e.client.magicRequestDecoder("GET", path, nil, &data)
   111  	return
   112  }
   113  
   114  // ListRecipes get the recipes list of recipes available to a given environment.
   115  //
   116  // Chef API docs: https://docs.chef.io/api_chef_server/#get-33
   117  func (e *EnvironmentService) ListRecipes(name string) (data EnvironmentRecipesResult, err error) {
   118  	path := fmt.Sprintf("environments/%s/recipes", name)
   119  	err = e.client.magicRequestDecoder("GET", path, nil, &data)
   120  	return
   121  }
   122  
   123  // Get a hash of cookbooks and cookbook versions (including all dependencies) that
   124  // are required by the run_list array. Version constraints may be specified using
   125  // the @ symbol after the cookbook name as a delimiter. Version constraints may also
   126  // be present when the cookbook_versions attributes is specified for an environment
   127  // or when dependencies are specified by a cookbook.
   128  //
   129  // Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks
   130  // TODO: Write this
   131  
   132  // Get a list of cookbooks and cookbook versions that are available to the specified environment.
   133  //
   134  // Chef API docs: https://docs.chef.io/api_chef_server.html#environments-name-cookbooks
   135  // TODO: Write this