Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base url use #major #209

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@ Apache License
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ This is a Library that you can use to write tools to interact with the chef serv
go test -v github.com/go-chef/chef
examples::chefapi_tester kitchen verify

## Chef Server URL

The API calls expect the client configuration to be set up with the appropriate base URL. Most of the API calls are intended to be made relative to a base URL that specifies a chef server organization. The specified URL will look something like "https://chef-server.example/organizations/orgname". The association, license, organization and user endpoints use the base URL without a specified organization similar to "https://chef-server.example". If the StetURL variable in the client config is set to false, the default, the global URL will be computed from a URL that specifies the organization. The default makes it possible to make global calls and calls for one organization using the same client configuration.If you want to call the API for multiple organizations new clients need to be created for each organization.

## SSL

If you run into an SSL verification problem when trying to connect to a ssl server with self signed certs setup your config object with `SkipSSL: true`
If you run into an SSL verification problem when trying to connect to a chef server with self signed certs you can setup your config object with `SkipSSL: true`.
You may also add self signed certificates and missing root CAs to the ROOTCAS field in the chef client cfg. See the testapi/testapi.go file for example code.

## Usage
Typically using this api client follows this pattern:

* Create a client structure using NewClient. Specify the chef server URL and an organization.
* Make api calls using client.EndPoint.method function calls. Some calls take parameters and some require JSON bodies. The functions generally turn the JSON returnedfrom the chef server into golang structures.

This example is setting up a basic client that you can use to interact with all the service endpoints (clients, nodes, cookbooks, etc. At [@chefapi](https://docs.chef.io/api_chef_server.html))
More usage examples can be found in the [examples](examples) directory.

Expand Down
4 changes: 2 additions & 2 deletions acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewACL(acltype string, actors, groups ACLitem) (acl *ACL) {
// Chef API docs: lol
func (a *ACLService) Get(subkind string, name string) (acl ACL, err error) {
url := fmt.Sprintf("%s/%s/_acl", subkind, name)
err = a.client.magicRequestDecoder("GET", url, nil, &acl)
err = a.client.magicRequestDecoder("GET", url, UseOrg, nil, &acl)
return
}

Expand All @@ -48,6 +48,6 @@ func (a *ACLService) Put(subkind, name string, acltype string, item *ACL) (err e
return
}

err = a.client.magicRequestDecoder("PUT", url, body, nil)
err = a.client.magicRequestDecoder("PUT", url, UseOrg, body, nil)
return
}
18 changes: 9 additions & 9 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type OrgUser struct {

// ListInvites gets a list of the pending invitations for an organization.
func (e *AssociationService) ListInvites() (invitelist []Invite, err error) {
err = e.client.magicRequestDecoder("GET", "association_requests", nil, &invitelist)
err = e.client.magicRequestDecoder("GET", "association_requests", UseGlobal, nil, &invitelist)
return
}

Expand All @@ -86,20 +86,20 @@ func (e *AssociationService) Invite(invite Request) (data Association, err error
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "association_requests/", body, &data)
err = e.client.magicRequestDecoder("POST", "association_requests/", UseGlobal, body, &data)
return
}

// DeleteInvite removes a pending invitation to an organization
func (e *AssociationService) DeleteInvite(id string) (rescind RescindInvite, err error) {
err = e.client.magicRequestDecoder("DELETE", "association_requests/"+id, nil, &rescind)
err = e.client.magicRequestDecoder("DELETE", "association_requests/"+id, UseGlobal, nil, &rescind)
return
}

// InviteID Finds an invitation id for a user
func (e *AssociationService) InviteId(user string) (id string, err error) {
var invitelist []Invite
err = e.client.magicRequestDecoder("GET", "association_requests", nil, &invitelist)
err = e.client.magicRequestDecoder("GET", "association_requests", UseGlobal, nil, &invitelist)
if err != nil {
return
}
Expand All @@ -122,13 +122,13 @@ func (e *AssociationService) AcceptInvite(id string) (data string, err error) {
if err != nil {
return
}
err = e.client.magicRequestDecoder("PUT", "association_requests/"+id, body, &data)
err = e.client.magicRequestDecoder("PUT", "association_requests/"+id, UseGlobal, body, &data)
return
}

// List gets a list of the users in an organization
func (e *AssociationService) List() (data []OrgUserListEntry, err error) {
err = e.client.magicRequestDecoder("GET", "users", nil, &data)
err = e.client.magicRequestDecoder("GET", "users", UseGlobal, nil, &data)
return
}

Expand All @@ -138,18 +138,18 @@ func (e *AssociationService) Add(addme AddNow) (err error) {
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "users", body, nil)
err = e.client.magicRequestDecoder("POST", "users", UseGlobal, body, nil)
return
}

// Get the details of a user in an organization
func (e *AssociationService) Get(name string) (data OrgUser, err error) {
err = e.client.magicRequestDecoder("GET", "users/"+name, nil, &data)
err = e.client.magicRequestDecoder("GET", "users/"+name, UseGlobal, nil, &data)
return
}

// Delete removes a user from an organization
func (e *AssociationService) Delete(name string) (data OrgUser, err error) {
err = e.client.magicRequestDecoder("DELETE", "users/"+name, nil, &data)
err = e.client.magicRequestDecoder("DELETE", "users/"+name, UseGlobal, nil, &data)
return
}
2 changes: 1 addition & 1 deletion authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ type Authenticate struct {
// https://docs.chef.io/api_chef_server.html#authenticate-user
func (e *AuthenticateUserService) Authenticate(authenticate_request Authenticate) (err error) {
body, err := JSONReader(authenticate_request)
err = e.client.magicRequestDecoder("POST", "authenticate_user", body, nil)
err = e.client.magicRequestDecoder("POST", "authenticate_user", UseOrg, body, nil)
return
}
20 changes: 10 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c ApiClientListResult) String() (out string) {
//
// Chef API docs: https://docs.chef.io/api_chef_server/#get-11
func (e *ApiClientService) List() (data ApiClientListResult, err error) {
err = e.client.magicRequestDecoder("GET", "clients", nil, &data)
err = e.client.magicRequestDecoder("GET", "clients", UseOrg, nil, &data)
return
}

Expand All @@ -59,7 +59,7 @@ func (e *ApiClientService) Create(client ApiNewClient) (data *ApiClientCreateRes
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "clients", body, &data)
err = e.client.magicRequestDecoder("POST", "clients", UseOrg, body, &data)
return
}

Expand All @@ -68,7 +68,7 @@ func (e *ApiClientService) Create(client ApiNewClient) (data *ApiClientCreateRes
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
func (e *ApiClientService) Delete(name string) (err error) {
url := fmt.Sprintf("clients/%s", name)
err = e.client.magicRequestDecoder("DELETE", url, nil, nil)
err = e.client.magicRequestDecoder("DELETE", url, UseOrg, nil, nil)
return
}

Expand All @@ -77,7 +77,7 @@ func (e *ApiClientService) Delete(name string) (err error) {
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-name
func (e *ApiClientService) Get(name string) (client ApiClient, err error) {
url := fmt.Sprintf("clients/%s", name)
err = e.client.magicRequestDecoder("GET", url, nil, &client)
err = e.client.magicRequestDecoder("GET", url, UseOrg, nil, &client)
return
}

Expand All @@ -90,7 +90,7 @@ func (e *ApiClientService) Update(name string, client ApiNewClient) (data *ApiCl
if err != nil {
return
}
err = e.client.magicRequestDecoder("PUT", url, body, &data)
err = e.client.magicRequestDecoder("PUT", url, UseOrg, body, &data)
return
}

Expand All @@ -99,7 +99,7 @@ func (e *ApiClientService) Update(name string, client ApiNewClient) (data *ApiCl
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-client-keys
func (e *ApiClientService) ListKeys(name string) (data []KeyItem, err error) {
url := fmt.Sprintf("clients/%s/keys", name)
err = e.client.magicRequestDecoder("GET", url, nil, &data)
err = e.client.magicRequestDecoder("GET", url, UseOrg, nil, &data)
return
}

Expand All @@ -115,7 +115,7 @@ func (e *ApiClientService) ListKeys(name string) (data []KeyItem, err error) {
func (e *ApiClientService) AddKey(name string, keyadd AccessKey) (key KeyItem, err error) {
url := fmt.Sprintf("clients/%s/keys", name)
body, err := JSONReader(keyadd)
err = e.client.magicRequestDecoder("POST", url, body, &key)
err = e.client.magicRequestDecoder("POST", url, UseOrg, body, &key)
return
}

Expand All @@ -129,7 +129,7 @@ func (e *ApiClientService) AddKey(name string, keyadd AccessKey) (key KeyItem, e
// Chef API docs: https://docs.chef.io/api_chef_server/#clientskeys
func (e *ApiClientService) DeleteKey(name string, keyname string) (key AccessKey, err error) {
url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
err = e.client.magicRequestDecoder("DELETE", url, nil, &key)
err = e.client.magicRequestDecoder("DELETE", url, UseOrg, nil, &key)
return
}

Expand All @@ -138,7 +138,7 @@ func (e *ApiClientService) DeleteKey(name string, keyname string) (key AccessKey
// Chef API docs: https://docs.chef.io/api_chef_server.html#clients-client-keys-key
func (e *ApiClientService) GetKey(name string, keyname string) (key AccessKey, err error) {
url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
err = e.client.magicRequestDecoder("GET", url, nil, &key)
err = e.client.magicRequestDecoder("GET", url, UseOrg, nil, &key)
return
}

Expand All @@ -153,6 +153,6 @@ func (e *ApiClientService) GetKey(name string, keyname string) (key AccessKey, e
func (e *ApiClientService) UpdateKey(name string, keyname string, keyupd AccessKey) (key AccessKey, err error) {
url := fmt.Sprintf("clients/%s/keys/%s", name, keyname)
body, err := JSONReader(keyupd)
err = e.client.magicRequestDecoder("PUT", url, body, &key)
err = e.client.magicRequestDecoder("PUT", url, UseOrg, body, &key)
return
}
8 changes: 4 additions & 4 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c ContainerListResult) String() (out string) {
//
// Chef API docs: https://docs.chef.io/api_chef_server/containers
func (e *ContainerService) List() (data ContainerListResult, err error) {
err = e.client.magicRequestDecoder("GET", "containers", nil, &data)
err = e.client.magicRequestDecoder("GET", "containers", UseOrg, nil, &data)
return
}

Expand All @@ -45,7 +45,7 @@ func (e *ContainerService) Create(container Container) (data *ContainerCreateRes
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "containers", body, &data)
err = e.client.magicRequestDecoder("POST", "containers", UseOrg, body, &data)
return
}

Expand All @@ -54,7 +54,7 @@ func (e *ContainerService) Create(container Container) (data *ContainerCreateRes
// Chef API docs: https://docs.chef.io/api_chef_server.html#container
func (e *ContainerService) Delete(name string) (err error) {
url := fmt.Sprintf("containers/%s", name)
err = e.client.magicRequestDecoder("DELETE", url, nil, nil)
err = e.client.magicRequestDecoder("DELETE", url, UseOrg, nil, nil)
return
}

Expand All @@ -63,6 +63,6 @@ func (e *ContainerService) Delete(name string) (err error) {
// Chef API docs: https://docs.chef.io/api_chef_server.html#containers
func (e *ContainerService) Get(name string) (container Container, err error) {
url := fmt.Sprintf("containers/%s", name)
err = e.client.magicRequestDecoder("GET", url, nil, &container)
err = e.client.magicRequestDecoder("GET", url, UseOrg, nil, &container)
return
}
14 changes: 7 additions & 7 deletions cookbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,18 @@ func versionParams(path, numVersions string) string {
return path
}

// Get retruns a CookbookVersion for a specific cookbook
// Get returns a CookbookVersion for a specific cookbook
// GET /cookbooks/name
func (c *CookbookService) Get(name string) (data CookbookVersion, err error) {
path := fmt.Sprintf("cookbooks/%s", name)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
err = c.client.magicRequestDecoder("GET", path, UseOrg, nil, &data)
return
}

// GetAvailable returns the versions of a coookbook available on a server
func (c *CookbookService) GetAvailableVersions(name, numVersions string) (data CookbookListResult, err error) {
path := versionParams(fmt.Sprintf("cookbooks/%s", name), numVersions)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
err = c.client.magicRequestDecoder("GET", path, UseOrg, nil, &data)
return
}

Expand All @@ -133,23 +133,23 @@ func (c *CookbookService) GetAvailableVersions(name, numVersions string) (data C
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-name-version
func (c *CookbookService) GetVersion(name, version string) (data Cookbook, err error) {
url := fmt.Sprintf("cookbooks/%s/%s", name, version)
err = c.client.magicRequestDecoder("GET", url, nil, &data)
err = c.client.magicRequestDecoder("GET", url, UseOrg, nil, &data)
return
}

// ListVersions lists the cookbooks available on the server limited to numVersions
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-name
func (c *CookbookService) ListAvailableVersions(numVersions string) (data CookbookListResult, err error) {
path := versionParams("cookbooks", numVersions)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
err = c.client.magicRequestDecoder("GET", path, UseOrg, nil, &data)
return
}

// ListAllRecipes lists the names of all recipes in the most recent cookbook versions
// Chef API docs: https://docs.chef.io/api_chef_server.html#cookbooks-recipes
func (c *CookbookService) ListAllRecipes() (data CookbookRecipesResult, err error) {
path := "cookbooks/_recipes"
err = c.client.magicRequestDecoder("GET", path, nil, &data)
err = c.client.magicRequestDecoder("GET", path, UseOrg, nil, &data)
return
}

Expand All @@ -161,6 +161,6 @@ func (c *CookbookService) List() (CookbookListResult, error) {
// DeleteVersion removes a version of a cook from a server
func (c *CookbookService) Delete(name, version string) (err error) {
path := fmt.Sprintf("cookbooks/%s/%s", name, version)
err = c.client.magicRequestDecoder("DELETE", path, nil, nil)
err = c.client.magicRequestDecoder("DELETE", path, UseOrg, nil, nil)
return
}
6 changes: 3 additions & 3 deletions cookbook_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,22 @@ type CBAMeta struct {
// List lists the Cookbook_Artifacts in the Chef server.
// GET /cookbook_artifacts
func (c *CBAService) List() (data CBAGetResponse, err error) {
err = c.client.magicRequestDecoder("GET", "cookbook_artifacts", nil, &data)
err = c.client.magicRequestDecoder("GET", "cookbook_artifacts", UseOrg, nil, &data)
return
}

// Get returns details for a specific cookbook artifact
// GET /cookbook_artifacts/name
func (c *CBAService) Get(name string) (data CBAGetResponse, err error) {
path := fmt.Sprintf("cookbook_artifacts/%s", name)
err = c.client.magicRequestDecoder("GET", path, nil, &data)
err = c.client.magicRequestDecoder("GET", path, UseOrg, nil, &data)
return
}

// GetVersion fetches a specific version of a cookbook_artifact from the server api
// GET /cookbook_artifact/foo/1ef062de1bc4cb14e4a78fb739e104eb9508473e
func (c *CBAService) GetVersion(name, id string) (data CBADetail, err error) {
url := fmt.Sprintf("cookbook_artifacts/%s/%s", name, id)
err = c.client.magicRequestDecoder("GET", url, nil, &data)
err = c.client.magicRequestDecoder("GET", url, UseOrg, nil, &data)
return
}
2 changes: 1 addition & 1 deletion cookbook_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *CookbookService) downloadCookbookFile(item CookbookItem, localPath stri
return nil
}

request, err := c.client.NewRequest("GET", item.Url, nil)
request, err := c.client.NewRequest("GET", item.Url, false, nil)
if err != nil {
return err
}
Expand Down
Loading