Skip to content

Commit

Permalink
Merge pull request #10 from go-chef/master
Browse files Browse the repository at this point in the history
Merging changes from the upstream project
  • Loading branch information
mudash authored Mar 23, 2020
2 parents 708d096 + 5994b66 commit ba4b81e
Show file tree
Hide file tree
Showing 129 changed files with 4,147 additions and 482 deletions.
52 changes: 52 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Golang CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2
workflows:
version: 2
merge-to-master:
jobs:
- tester
- update_tag:
requires:
- tester
filters:
branches:
only: /master/
jobs:
update_tag:
docker:
- image: circleci/golang:1.13.1
working_directory: /go/src/github.com/go-chef/chefi
steps:
- add_ssh_keys:
fingerprints:
- ${TAG_TOKEN}
- checkout
- run: curl -s https://api.github.com/repos/pantheon-systems/autotag/releases/latest | grep browser_download | grep Linux | cut -d '"' -f 4 | xargs curl -o ./autotag -L && chmod 755 ./autotag
- run: ./autotag
- run: git push --tags origin
tester:
docker:
- image: circleci/golang:1.9
#### TEMPLATE_NOTE: go expects specific checkout path representing url
#### expecting it in the form of
#### /go/src/github.com/circleci/go-tool
#### /go/src/bitbucket.org/circleci/go-tool
working_directory: /go/src/github.com/go-chef/chef
steps:
- add_ssh_keys:
fingerprints:
- 35:v45:41:ba:cf:3f:7f:d5:00:0f:11:6b:4d:c0:a1:90
- checkout
# specify any bash command here prefixed with `run: `
- run: pwd
- run: env
- run: ls -Rl
- run: go get
- run: go get github.com/ctdk/goiardi/chefcrypto
- run: go get github.com/r3labs/diff
- run: go get github.com/smartystreets/goconvey/convey
- run: go get github.com/stretchr/testify/assert
- run: go vet
- run: go test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ _testmain.go

# vim
*.swp

# cookbook
*.kitchen
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ This is a Library that you can use to write tools to interact with the chef serv

go get -t github.com/go-chef/chef
go test -v github.com/go-chef/chef
examples::chefapi_tester kitchen verify

## 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`

## Usage
This example is setting up a basic client that you can use to interact with all the service endpoints (clients, nodes, cookbooks, etc.)
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.

```go
Expand Down Expand Up @@ -79,6 +80,7 @@ improved, but is still an ongoing concern.
|AJ Christensen |[@fujin](https://github.com/fujin)
|Brad Beam |[@bradbeam](https://github.com/bradbeam)
|Kraig Amador |[@bigkraig](https://github.com/bigkraig)
|Mark Gibbons |[@mark](https://github.com/markgibbons)

## COPYRIGHT

Expand Down
155 changes: 155 additions & 0 deletions association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package chef

// import "fmt"
import "errors"

type AssociationService struct {
client *Client
}

// Chef API docs: https://docs.chef.io/api_chef_server.html#association-requests
// https://github.com/chef/chef-server/blob/master/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_org_invites.erl Invitation implementation
// https://github.com/chef/chef-server/blob/master/src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_org_associations.erl user org associations

// Association represents the response from creating an invitation to join an organization
// POST /organization/NAME/association_requests
type Association struct {
Uri string `json:"uri"` // the last part of the uri is the invitation id
OrganizationUser struct {
UserName string `json:"username,omitempty"`
} `json:"organization_user"`
Organization struct {
Name string `json:"name,omitempty"`
} `json:"organization"`
User struct {
Email string `json:"email,omitempty"`
FirstName string `json:"first_name,omitempty"`
} `json:"user"`
}

// RescindInvite respresents the response from deleting an invitation
// DELETE /organization/NAME/association_requests/ID
type RescindInvite struct {
Id string `json:"id,omitempty"`
Orgname string `json:"orgname,omitempty"`
Username string `json:"username,omitempty"`
}

// Invite represents an entry in the array of responses listing the outstanding invitations
// GET /organization/NAME/association_requests
type Invite struct {
Id string `json:"id,omitempty"`
UserName string `json:"username,omitempty"`
}

// Request represents the body of the request to invite a user to an organization
// POST /organization/NAME/association_requests
type Request struct {
User string `json:"user"`
}

// AddNow represents the body of the request to add a user to an organization
// POST /organization/NAME/users
type AddNow struct {
Username string `json:"username"`
}

// Invite represents an entry in the array of responses listing the users in an organization
// GET /organization/NAME/association_requests
type OrgUserListEntry struct {
User struct {
Username string `json:"username,omitempty"`
} `json:"user,omitempty"`
}

// OrgUser represents the detailed information about a user in an organization
// GET /organization/NAME/user/NAME
// DELETE /organization/NAME/user/NAME
type OrgUser struct {
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
DisplayName string `json:"display_name,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
PublicKey string `json:"public_key,omitempty"`
}

// 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)
return
}

// Invite creates an invitation for a user to join an organization on the chef server
func (e *AssociationService) Invite(invite Request) (data Association, err error) {
body, err := JSONReader(invite)
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "association_requests/", 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)
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)
if err != nil {
return
}
// Find an invite for the user or return err
for _, in := range invitelist {
if in.UserName == user {
id = in.Id
}
}
if id == "" {
err = errors.New("User request not found")
}
return
}

// AcceptInvite Accepts an invitation
// TODO: Gets a 405, code is in knife is it part of erchef?
func (e *AssociationService) AcceptInvite(id string) (data string, err error) {
body, err := JSONReader("{ \"accept\" }")
if err != nil {
return
}
err = e.client.magicRequestDecoder("PUT", "association_requests/"+id, 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)
return
}

// Add a user immediately
func (e *AssociationService) Add(addme AddNow) (err error) {
body, err := JSONReader(addme)
if err != nil {
return
}
err = e.client.magicRequestDecoder("POST", "users", 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)
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)
return
}
Loading

0 comments on commit ba4b81e

Please sign in to comment.