forked from cvbarros/go-teamcity
/
group.go
80 lines (66 loc) · 1.83 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package teamcity
import (
"fmt"
"net/http"
"github.com/dghubble/sling"
)
// Group is the model for group entities in TeamCity
type Group struct {
Key string `json:"key,omitempty" xml:"key"`
Description string `json:"description,omitempty" xml:"description"`
Name string `json:"name,omitempty" xml:"name"`
}
// NewGroup returns an instance of a Group. A non-empty Key and Name is required.
// Description can be an empty string and will be omitted.
func NewGroup(key string, name string, description string) (*Group, error) {
if key == "" {
return nil, fmt.Errorf("Key is required")
}
if name == "" {
return nil, fmt.Errorf("Name is required")
}
return &Group{
Key: key,
Name: name,
Description: description,
}, nil
}
// GroupService has operations for handling groups
type GroupService struct {
sling *sling.Sling
httpClient *http.Client
restHelper *restHelper
}
func newGroupService(base *sling.Sling, httpClient *http.Client) *GroupService {
sling := base.Path("userGroups/")
return &GroupService{
httpClient: httpClient,
sling: sling,
restHelper: newRestHelperWithSling(httpClient, sling),
}
}
// Create - Creates a new group
func (s *GroupService) Create(group *Group) (*Group, error) {
var created Group
err := s.restHelper.post("", group, &created, "group")
if err != nil {
return nil, err
}
return &created, nil
}
// GetByKey - Get a group by its group key
func (s *GroupService) GetByKey(key string) (*Group, error) {
var out Group
locator := LocatorKey(key).String()
err := s.restHelper.get(locator, &out, "group")
if err != nil {
return nil, err
}
return &out, err
}
// Delete - Deletes a group by its group key
func (s *GroupService) Delete(key string) error {
locator := LocatorKey(key).String()
err := s.restHelper.delete(locator, "group")
return err
}