1/*
2Copyright 2018 Comcast Cable Communications Management, LLC
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6Unless required by applicable law or agreed to in writing, software
7distributed under the License is distributed on an "AS IS" BASIS,
8WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9See the License for the specific language governing permissions and
10limitations under the License.
11*/
12
13package vinyldns
14
15import (
16	"encoding/json"
17	"fmt"
18)
19
20// Groups retrieves a list of Groups that the requester is a part of.
21func (c *Client) Groups() ([]Group, error) {
22	groups := &Groups{}
23	err := resourceRequest(c, groupsEP(c), "GET", nil, groups)
24	if err != nil {
25		return []Group{}, err
26	}
27
28	return groups.Groups, nil
29}
30
31// GroupsListAll retrieves the complete list of groups with the ListFilter criteria passed.
32// Handles paging through results on the user's behalf.
33func (c *Client) GroupsListAll(filter ListFilter) ([]Group, error) {
34	if filter.MaxItems > 100 {
35		return nil, fmt.Errorf("MaxItems must be between 1 and 100")
36	}
37
38	groups := []Group{}
39
40	for {
41		resp, err := c.groupsList(filter)
42		if err != nil {
43			return nil, err
44		}
45
46		groups = append(groups, resp.Groups...)
47		filter.StartFrom = resp.NextID
48
49		if len(filter.StartFrom) == 0 {
50			return groups, nil
51		}
52	}
53}
54
55// GroupCreate creates the Group it's passed.
56func (c *Client) GroupCreate(g *Group) (*Group, error) {
57	gJSON, err := json.Marshal(g)
58	if err != nil {
59		return nil, err
60	}
61	var resource = &Group{}
62	err = resourceRequest(c, groupsEP(c), "POST", gJSON, resource)
63	if err != nil {
64		return &Group{}, err
65	}
66
67	return resource, nil
68}
69
70// Group gets the Group whose ID it's passed.
71func (c *Client) Group(groupID string) (*Group, error) {
72	group := &Group{}
73	err := resourceRequest(c, groupEP(c, groupID), "GET", nil, group)
74	if err != nil {
75		return nil, err
76	}
77
78	return group, nil
79}
80
81// GroupDelete deletes the Group whose ID it's passed.
82func (c *Client) GroupDelete(groupID string) (*Group, error) {
83	group := &Group{}
84	err := resourceRequest(c, groupEP(c, groupID), "DELETE", nil, group)
85	if err != nil {
86		return nil, err
87	}
88
89	return group, nil
90}
91
92// GroupUpdate updates the Group whose ID it's passed.
93func (c *Client) GroupUpdate(groupID string, g *Group) (*Group, error) {
94	gJSON, err := json.Marshal(g)
95	if err != nil {
96		return nil, err
97	}
98	var resource = &Group{}
99	err = resourceRequest(c, groupEP(c, groupID), "PUT", gJSON, resource)
100	if err != nil {
101		return &Group{}, err
102	}
103
104	return resource, nil
105}
106
107// GroupAdmins returns an array of Users that are admins
108// associated with the Group whose GroupID it's passed.
109func (c *Client) GroupAdmins(groupID string) ([]User, error) {
110	admins := &GroupAdmins{}
111	err := resourceRequest(c, groupAdminsEP(c, groupID), "GET", nil, admins)
112	if err != nil {
113		return nil, err
114	}
115
116	return admins.GroupAdmins, nil
117}
118
119// GroupMembers returns an array of Users that are members
120// associated with the Group whose GroupID it's passed.
121func (c *Client) GroupMembers(groupID string) ([]User, error) {
122	members := &GroupMembers{}
123	err := resourceRequest(c, groupMembersEP(c, groupID), "GET", nil, members)
124	if err != nil {
125		return nil, err
126	}
127
128	return members.GroupMembers, nil
129}
130
131// GroupActivity returns group change activity
132// associated with the Group whose GroupID it's passed.
133func (c *Client) GroupActivity(groupID string) (*GroupChanges, error) {
134	activity := &GroupChanges{}
135	err := resourceRequest(c, groupActivityEP(c, groupID), "GET", nil, activity)
136	if err != nil {
137		return nil, err
138	}
139
140	return activity, nil
141}
142