Browse Source

Adds AdminService and functions for updating LDAP mappings

Closes #479.

Change-Id: Ib4aba6a5ccea430abdb4e9b1b2351870bf4ea79f
Andy Lindeman 9 years ago
committed by Glenn Lewis
parent
commit
69894f9ef7
3 changed files with 182 additions and 0 deletions
  1. +100
    -0
      github/admin.go
  2. +80
    -0
      github/admin_test.go
  3. +2
    -0
      github/github.go

+ 100
- 0
github/admin.go View File

@ -0,0 +1,100 @@
// Copyright 2016 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import "fmt"
// AdminService handles communication with the admin related methods of the
// GitHub API. These API routes are normally only accessible for GitHub
// Enterprise installations.
//
// GitHub API docs: https://developer.github.com/v3/enterprise/
type AdminService service
// TeamLDAPMapping represents the mapping between a GitHub team and an LDAP
// group.
type TeamLDAPMapping struct {
ID *int `json:"id,omitempty"`
LDAPDN *string `json:"ldap_dn,omitempty"`
URL *string `json:"url,omitempty"`
Name *string `json:"name,omitempty"`
Slug *string `json:"slug,omitempty"`
Description *string `json:"description,omitempty"`
Privacy *string `json:"privacy,omitempty"`
Permission *string `json:"permission,omitempty"`
MembersURL *string `json:"members_url,omitempty"`
RepositoriesURL *string `json:"repositories_url,omitempty"`
}
func (m TeamLDAPMapping) String() string {
return Stringify(m)
}
// UserLDAPMapping represents the mapping between a GitHub user and an LDAP
// user.
type UserLDAPMapping struct {
ID *int `json:"id,omitempty"`
LDAPDN *string `json:"ldap_dn,omitempty"`
Login *string `json:"login,omitempty"`
AvatarURL *string `json:"avatar_url,omitempty"`
GravatarID *string `json:"gravatar_id,omitempty"`
Type *string `json:"type,omitempty"`
SiteAdmin *bool `json:"site_admin,omitempty"`
URL *string `json:"url,omitempty"`
EventsURL *string `json:"events_url,omitempty"`
FollowingURL *string `json:"following_url,omitempty"`
FollowersURL *string `json:"followers_url,omitempty"`
GistsURL *string `json:"gists_url,omitempty"`
OrganizationsURL *string `json:"organizations_url,omitempty"`
ReceivedEventsURL *string `json:"received_events_url,omitempty"`
ReposURL *string `json:"repos_url,omitempty"`
StarredURL *string `json:"starred_url,omitempty"`
SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
}
func (m UserLDAPMapping) String() string {
return Stringify(m)
}
// UpdateUserLDAPMapping updates the mapping between a GitHub user and an LDAP user.
//
// GitHub API docs: https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-user
func (s *AdminService) UpdateUserLDAPMapping(user string, mapping *UserLDAPMapping) (*UserLDAPMapping, *Response, error) {
u := fmt.Sprintf("admin/ldap/users/%v/mapping", user)
req, err := s.client.NewRequest("PATCH", u, mapping)
if err != nil {
return nil, nil, err
}
m := new(UserLDAPMapping)
resp, err := s.client.Do(req, m)
if err != nil {
return nil, resp, err
}
return m, resp, err
}
// UpdateTeamLDAPMapping updates the mapping between a GitHub team and an LDAP group.
//
// GitHub API docs: https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-team
func (s *AdminService) UpdateTeamLDAPMapping(team int, mapping *TeamLDAPMapping) (*TeamLDAPMapping, *Response, error) {
u := fmt.Sprintf("admin/ldap/teams/%v/mapping", team)
req, err := s.client.NewRequest("PATCH", u, mapping)
if err != nil {
return nil, nil, err
}
m := new(TeamLDAPMapping)
resp, err := s.client.Do(req, m)
if err != nil {
return nil, resp, err
}
return m, resp, err
}

+ 80
- 0
github/admin_test.go View File

@ -0,0 +1,80 @@
// Copyright 2016 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestAdminService_UpdateUserLDAPMapping(t *testing.T) {
setup()
defer teardown()
input := &UserLDAPMapping{
LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"),
}
mux.HandleFunc("/admin/ldap/users/u/mapping", func(w http.ResponseWriter, r *http.Request) {
v := new(UserLDAPMapping)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "PATCH")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
fmt.Fprint(w, `{"id":1,"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`)
})
mapping, _, err := client.Admin.UpdateUserLDAPMapping("u", input)
if err != nil {
t.Errorf("Admin.UpdateUserLDAPMapping returned error: %v", err)
}
want := &UserLDAPMapping{
ID: Int(1),
LDAPDN: String("uid=asdf,ou=users,dc=github,dc=com"),
}
if !reflect.DeepEqual(mapping, want) {
t.Errorf("Admin.UpdateUserLDAPMapping returned %+v, want %+v", mapping, want)
}
}
func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) {
setup()
defer teardown()
input := &TeamLDAPMapping{
LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"),
}
mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) {
v := new(TeamLDAPMapping)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "PATCH")
if !reflect.DeepEqual(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
fmt.Fprint(w, `{"id":1,"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`)
})
mapping, _, err := client.Admin.UpdateTeamLDAPMapping(1, input)
if err != nil {
t.Errorf("Admin.UpdateTeamLDAPMapping returned error: %v", err)
}
want := &TeamLDAPMapping{
ID: Int(1),
LDAPDN: String("cn=Enterprise Ops,ou=teams,dc=github,dc=com"),
}
if !reflect.DeepEqual(mapping, want) {
t.Errorf("Admin.UpdateTeamLDAPMapping returned %+v, want %+v", mapping, want)
}
}

+ 2
- 0
github/github.go View File

@ -117,6 +117,7 @@ type Client struct {
// Services used for talking to different parts of the GitHub API.
Activity *ActivityService
Admin *AdminService
Authorizations *AuthorizationsService
Gists *GistsService
Git *GitService
@ -189,6 +190,7 @@ func NewClient(httpClient *http.Client) *Client {
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
c.common.client = c
c.Activity = (*ActivityService)(&c.common)
c.Admin = (*AdminService)(&c.common)
c.Authorizations = (*AuthorizationsService)(&c.common)
c.Gists = (*GistsService)(&c.common)
c.Git = (*GitService)(&c.common)


Loading…
Cancel
Save