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	"fmt"
17	"strings"
18)
19
20func zonesEP(c *Client) string {
21	return concatStrs("", c.Host, "/zones")
22}
23
24func zonesListEP(c *Client, f ListFilter) string {
25	query := buildQuery(f, "nameFilter")
26
27	return concatStrs("", zonesEP(c), query)
28}
29
30func zoneEP(c *Client, id string) string {
31	return concatStrs("", zonesEP(c), "/", id)
32}
33
34func zoneNameEP(c *Client, name string) string {
35	return concatStrs("", zonesEP(c), "/name/", name)
36}
37
38func zoneChangesEP(c *Client, id string, f ListFilter) string {
39	query := buildQuery(f, "nameFilter")
40
41	return concatStrs("", zoneEP(c, id), "/changes", query)
42}
43
44func zoneSyncEP(c *Client, id string) string {
45	return concatStrs("", zoneEP(c, id), "/sync")
46}
47
48func recordSetsEP(c *Client, zoneID string) string {
49	return concatStrs("", zoneEP(c, zoneID), "/recordsets")
50}
51
52func recordSetsListEP(c *Client, zoneID string, f ListFilter) string {
53	query := buildQuery(f, "recordNameFilter")
54
55	return concatStrs("", recordSetsEP(c, zoneID), query)
56}
57
58func recordSetsGlobalListEP(c *Client, f GlobalListFilter) string {
59	query := buildGlobalListQuery(f)
60	base := concatStrs("", c.Host, "/recordsets")
61
62	return concatStrs("", base, query)
63}
64
65func recordSetEP(c *Client, zoneID, recordSetID string) string {
66	return concatStrs("", recordSetsEP(c, zoneID), "/", recordSetID)
67}
68
69func recordSetChangesEP(c *Client, zoneID string, f ListFilter) string {
70	query := buildQuery(f, "nameFilter")
71
72	return concatStrs("", zoneEP(c, zoneID), "/recordsetchanges", query)
73}
74
75func recordSetChangeEP(c *Client, zoneID, recordSetID, changeID string) string {
76	return concatStrs("", recordSetEP(c, zoneID, recordSetID), "/changes/", changeID)
77}
78
79func groupsEP(c *Client) string {
80	return concatStrs("", c.Host, "/groups")
81}
82
83func groupsListEP(c *Client, f ListFilter) string {
84	query := buildQuery(f, "groupNameFilter")
85
86	return concatStrs("", groupsEP(c), query)
87}
88
89func groupEP(c *Client, groupID string) string {
90	return concatStrs("", groupsEP(c), "/", groupID)
91}
92
93func groupAdminsEP(c *Client, groupID string) string {
94	return concatStrs("", groupEP(c, groupID), "/admins")
95}
96
97func groupMembersEP(c *Client, groupID string) string {
98	return concatStrs("", groupEP(c, groupID), "/members")
99}
100
101func groupActivityEP(c *Client, groupID string) string {
102	return concatStrs("", groupEP(c, groupID), "/activity")
103}
104
105func batchRecordChangesEP(c *Client) string {
106	return concatStrs("", zonesEP(c), "/batchrecordchanges")
107}
108
109func batchRecordChangeEP(c *Client, changeID string) string {
110	return concatStrs("", batchRecordChangesEP(c), "/", changeID)
111}
112
113func buildQuery(f ListFilter, nameFilterName string) string {
114	params := []string{}
115	query := "?"
116
117	if f.NameFilter != "" {
118		params = append(params, fmt.Sprintf("%s=%s", nameFilterName, f.NameFilter))
119	}
120
121	if f.StartFrom != "" {
122		params = append(params, fmt.Sprintf("startFrom=%s", f.StartFrom))
123	}
124
125	if f.MaxItems != 0 {
126		params = append(params, fmt.Sprintf("maxItems=%d", f.MaxItems))
127	}
128
129	if len(params) == 0 {
130		query = ""
131	}
132
133	return query + strings.Join(params, "&")
134}
135
136func buildGlobalListQuery(f GlobalListFilter) string {
137	params := []string{}
138	query := "?"
139
140	if f.RecordNameFilter != "" {
141		params = append(params, fmt.Sprintf("%s=%s", "recordNameFilter", f.RecordNameFilter))
142	}
143
144	if f.RecordTypeFilter != "" {
145		params = append(params, fmt.Sprintf("%s=%s", "recordTypeFilter", f.RecordTypeFilter))
146	}
147
148	if f.RecordOwnerGroupFilter != "" {
149		params = append(params, fmt.Sprintf("%s=%s", "recordOwnerGroupFilter", f.RecordOwnerGroupFilter))
150	}
151
152	if f.NameSort != "" {
153		params = append(params, fmt.Sprintf("%s=%s", "nameSort", f.NameSort))
154	}
155
156	if f.StartFrom != "" {
157		params = append(params, fmt.Sprintf("startFrom=%s", f.StartFrom))
158	}
159
160	if f.MaxItems != 0 {
161		params = append(params, fmt.Sprintf("maxItems=%d", f.MaxItems))
162	}
163
164	if len(params) == 0 {
165		query = ""
166	}
167
168	return query + strings.Join(params, "&")
169}
170