-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_reseller.go
156 lines (119 loc) · 3.9 KB
/
package_reseller.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package directadmin
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"github.com/google/go-querystring/query"
"github.com/spf13/cast"
)
type ResellerPackage struct {
Package
OversellEnabled bool `json:"oversellEnabled" url:"oversellEnabled"`
UserQuota int `json:"userQuota" url:"userQuota"`
}
// CreateResellerPackage (admin) creates the provided package
func (c *AdminContext) CreateResellerPackage(pack ResellerPackage) error {
var response apiGenericResponse
body, err := query.Values(pack.translate())
if err != nil {
return err
}
if _, err = c.makeRequestOld(http.MethodPost, "MANAGE_RESELLER_PACKAGES?add=yes", body, &response); err != nil {
return err
}
if response.Success != "Saved" {
return fmt.Errorf("failed to create package: %v", response.Result)
}
return nil
}
// DeleteResellerPackages (admin) deletes all the specified packs for the session user
func (c *AdminContext) DeleteResellerPackages(packs ...string) error {
var response apiGenericResponse
body := url.Values{}
body.Set("delete", "yes")
for index, pack := range packs {
body.Set("delete"+cast.ToString(index), pack)
body.Set("select"+cast.ToString(index), pack)
}
if _, err := c.makeRequestOld(http.MethodPost, "MANAGE_USER_PACKAGES", body, &response); err != nil {
return err
}
if response.Success != "Deleted" {
return fmt.Errorf("failed to delete pack: %v", response.Result)
}
return nil
}
// GetResellerPackage (admin) returns the single specified package
func (c *AdminContext) GetResellerPackage(packageName string) (ResellerPackage, error) {
var rawPack rawResellerPackage
if _, err := c.makeRequestOld(http.MethodGet, "API_PACKAGES_USER?package="+packageName, nil, &rawPack); err != nil {
return ResellerPackage{}, fmt.Errorf("failed to get package info for %v: %v", packageName, err)
}
rawPack.Name = packageName
return rawPack.translate(), nil
}
// GetResellerPackages (admin) returns all packages belonging to the session user
func (c *AdminContext) GetResellerPackages() ([]ResellerPackage, error) {
var packageList []string
var packages []ResellerPackage
if _, err := c.makeRequestOld(http.MethodGet, "API_PACKAGES_USER", nil, &packageList); err != nil {
return nil, err
}
var errs []error
var wg sync.WaitGroup
var mu sync.Mutex
wg.Add(len(packageList))
for _, packageName := range packageList {
// convert to local variable to prevent variable overwrite
packageToProcess := packageName
go func(packageName string) {
defer wg.Done()
pack, err := c.GetResellerPackage(packageName)
if err != nil {
mu.Lock()
errs = append(errs, err)
mu.Unlock()
return
}
mu.Lock()
packages = append(packages, pack)
mu.Unlock()
}(packageToProcess)
}
wg.Wait()
if len(errs) > 0 {
counter := 0
var errStrings []string
for _, err := range errs {
counter++
errStrings = append(errStrings, "error "+cast.ToString(counter)+": "+err.Error())
}
return nil, errors.New(strings.Join(errStrings, "; "))
}
if len(packages) == 0 {
return nil, errors.New("no packages could be found")
}
return packages, nil
}
// RenameResellerPackage (admin) renames the provided package
func (c *AdminContext) RenameResellerPackage(oldPackName string, newPackName string) error {
var response apiGenericResponse
body := url.Values{}
body.Set("old_package", oldPackName)
body.Set("new_package", newPackName)
if _, err := c.makeRequestOld(http.MethodPost, "MANAGE_USER_PACKAGES?action=rename", body, &response); err != nil {
return err
}
if response.Success != "Saved" {
return fmt.Errorf("failed to rename package: %v", response.Result)
}
return nil
}
// UpdateResellerPackage (admin) accepts a Package object and updates the version on DA with it
func (c *AdminContext) UpdateResellerPackage(pack ResellerPackage) error {
// DA's update functionality is virtually identical to create, so we'll just use that
return c.CreateResellerPackage(pack)
}