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

     1  package chef
     2  
     3  import "fmt"
     4  
     5  type RoleService struct {
     6  	client *Client
     7  }
     8  
     9  // Role represents the native Go version of the deserialized Role type
    10  type Role struct {
    11  	Name               string      `json:"name"`
    12  	ChefType           string      `json:"chef_type,omitempty"`
    13  	DefaultAttributes  interface{} `json:"default_attributes,omitempty"`
    14  	Description        string      `json:"description"`
    15  	EnvRunList         EnvRunList  `json:"env_run_lists,omitempty"`
    16  	JsonClass          string      `json:"json_class,omitempty"`
    17  	OverrideAttributes interface{} `json:"override_attributes,omitempty"`
    18  	RunList            RunList     `json:"run_list"`
    19  }
    20  
    21  type RoleCreateResult map[string]string
    22  type RoleEnvironmentsResult []string
    23  type RoleListResult map[string]string
    24  
    25  // String makes RoleListResult implement the string result
    26  func (e RoleListResult) String() (out string) {
    27  	return strMapToStr(e)
    28  }
    29  
    30  // String makes RoleCreateResult implement the string result
    31  func (e RoleCreateResult) String() (out string) {
    32  	return strMapToStr(e)
    33  }
    34  
    35  // List lists the roles in the Chef server.
    36  //
    37  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles
    38  func (e *RoleService) List() (data *RoleListResult, err error) {
    39  	err = e.client.magicRequestDecoder("GET", "roles", nil, &data)
    40  	return
    41  }
    42  
    43  // Create a new role in the Chef server.
    44  //
    45  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles
    46  func (e *RoleService) Create(role *Role) (data *RoleCreateResult, err error) {
    47  	body, err := JSONReader(role)
    48  	if err != nil {
    49  		return
    50  	}
    51  
    52  	// BUG(fujiN): This is now both a *response* decoder and handles upload.. gettin smelly
    53  	err = e.client.magicRequestDecoder("POST", "roles", body, &data)
    54  
    55  	return
    56  }
    57  
    58  // Delete a role from the Chef server.
    59  //
    60  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles-name
    61  func (e *RoleService) Delete(name string) (err error) {
    62  	path := fmt.Sprintf("roles/%s", name)
    63  	err = e.client.magicRequestDecoder("DELETE", path, nil, nil)
    64  	return
    65  }
    66  
    67  // Get gets a role from the Chef server.
    68  //
    69  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles-name
    70  func (e *RoleService) Get(name string) (data *Role, err error) {
    71  	path := fmt.Sprintf("roles/%s", name)
    72  	err = e.client.magicRequestDecoder("GET", path, nil, &data)
    73  	return
    74  }
    75  
    76  // Update a role in the Chef server.
    77  //
    78  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles-name
    79  // Trying to rename a role by specifying a new name in the body returns a 400
    80  func (e *RoleService) Put(role *Role) (data *Role, err error) {
    81  	path := fmt.Sprintf("roles/%s", role.Name)
    82  	body, err := JSONReader(role)
    83  	if err != nil {
    84  		return
    85  	}
    86  
    87  	err = e.client.magicRequestDecoder("PUT", path, body, &data)
    88  	return
    89  }
    90  
    91  // Get a list of environments that have environment specific run-lists for the given role
    92  //
    93  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles-name-environments
    94  func (e *RoleService) GetEnvironments(role string) (data RoleEnvironmentsResult, err error) {
    95  	path := fmt.Sprintf("roles/%s/environments", role)
    96  	err = e.client.magicRequestDecoder("GET", path, nil, &data)
    97  	return
    98  }
    99  
   100  // Get the environment-specific run-list for  a role
   101  //
   102  // Chef API docs: https://docs.chef.io/api_chef_server.html#roles-name-environments-name
   103  func (e *RoleService) GetEnvironmentRunlist(role string, environment string) (data EnvRunList, err error) {
   104  	path := fmt.Sprintf("roles/%s/environments/%s", role, environment)
   105  	err = e.client.magicRequestDecoder("GET", path, nil, &data)
   106  	return
   107  }