Skip to content

Commit

Permalink
Utilize new API endpoints for user config and usage
Browse files Browse the repository at this point in the history
Support retrieving reseller's users with config and/or data (rather than needing both)
Up debug body limit to 32768
  • Loading branch information
wolveix committed Jun 2, 2024
1 parent e32cb25 commit f2f78e5
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 344 deletions.
11 changes: 0 additions & 11 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,6 @@ func (c *AdminContext) GetResellers() ([]string, error) {
return users, nil
}

// TODO: finish implementation
func (c *AdminContext) GetResellersWithUsage() ([]string, error) {
var users []string

if _, err := c.makeRequestOld(http.MethodGet, "RESELLER_SHOW", nil, &users); err != nil {
return nil, err
}

return users, nil
}

func (c *AdminContext) MoveUserToReseller(username string, reseller string) error {
if _, err := c.makeRequestNew(http.MethodPost, "change-user-creator", convertAccount{Account: username, Creator: reseller}, nil); err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions directadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
AccountRoleAdmin = "Admin"
AccountRoleReseller = "Reseller"
AccountRoleUser = "User"
AccountRoleAdmin = "admin"
AccountRoleReseller = "reseller"
AccountRoleUser = "user"
)

type API struct {
Expand Down
6 changes: 4 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/goccy/go-json"
"github.com/spf13/cast"
)

type httpDebug struct {
Expand Down Expand Up @@ -55,8 +56,8 @@ func (c *UserContext) makeRequest(req *http.Request) ([]byte, error) {
}

if c.api.debug {
if len(responseBytes) > 4096 {
debug.Body = "body too long for debug"
if len(responseBytes) > 32768 {
debug.Body = "body too long for debug: " + cast.ToString(len(responseBytes))
} else {
debug.Body = string(responseBytes)
}
Expand Down Expand Up @@ -158,6 +159,7 @@ func (c *UserContext) makeRequestOld(method string, endpoint string, body url.Va
if resp != nil {
if object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
fmt.Printf("ERROR HERE: %s\n", string(resp))
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
} else if err = json.Unmarshal(resp, &genericResponse); err == nil && genericResponse.Error != "" {
Expand Down
60 changes: 29 additions & 31 deletions reseller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *ResellerContext) CreateUser(user UserConfig, password string, emailUser
body.Set("add", "Submit")
body.Set("domain", user.Domain)
body.Set("email", user.Email)
body.Set("ip", user.IpAddresses[0])
body.Set("ip", user.IP)
body.Set("package", user.Package)
body.Set("passwd", password)
body.Set("passwd2", password)
Expand Down Expand Up @@ -100,8 +100,9 @@ func (c *ResellerContext) GetMyUsers() ([]string, error) {
return users, nil
}

// GetMyUsersWithUsage (reseller) returns all users belonging to the session user, along with their UserConfig and UserUsage data
func (c *ResellerContext) GetMyUsersWithUsage() ([]User, error) {
// GetMyUsersWithData (reseller) returns all users belonging to the session user, along with the toggled data (config
// and/or usage)
func (c *ResellerContext) GetMyUsersWithData(retrieveConfig bool, retrieveUsage bool) ([]User, error) {
var err error
var usernames []string
var users []User
Expand All @@ -123,27 +124,34 @@ func (c *ResellerContext) GetMyUsersWithUsage() ([]User, error) {
go func(username string) {
defer wg.Done()

config, err := c.GetUserConfig(username)
if err != nil {
mu.Lock()
errs = append(errs, fmt.Errorf("failed to get user config: %v", err))
mu.Unlock()
return
var user User

if retrieveConfig {
config, err := c.GetUserConfig(username)
if err != nil {
mu.Lock()
errs = append(errs, fmt.Errorf("failed to get user config for %v: %v", username, err))
mu.Unlock()
return
}

user.Config = *config
}

usage, err := c.GetUserUsage(username)
if err != nil {
mu.Lock()
errs = append(errs, fmt.Errorf("failed to get user usage: %v", err))
mu.Unlock()
return
if retrieveUsage {
usage, err := c.GetUserUsage(username)
if err != nil {
mu.Lock()
errs = append(errs, fmt.Errorf("failed to get user usage for %v: %v", username, err))
mu.Unlock()
return
}

user.Usage = *usage
}

mu.Lock()
users = append(users, User{
Config: *config,
Usage: *usage,
})
users = append(users, user)
mu.Unlock()
}(userToProcess)
}
Expand Down Expand Up @@ -172,15 +180,8 @@ func (c *ResellerContext) GetMyUsersWithUsage() ([]User, error) {
// GetUserConfig (reseller) returns the given user's config
func (c *ResellerContext) GetUserConfig(username string) (*UserConfig, error) {
var config UserConfig
var err error
var rawConfig rawUserConfig

if _, err := c.makeRequestOld(http.MethodGet, "API_SHOW_USER_CONFIG?user="+username, nil, &rawConfig); err != nil {
return nil, err
}

config, err = rawConfig.parse()
if err != nil {
if _, err := c.makeRequestNew(http.MethodGet, "users/"+username+"/config", nil, &config); err != nil {
return nil, err
}

Expand All @@ -189,15 +190,12 @@ func (c *ResellerContext) GetUserConfig(username string) (*UserConfig, error) {

// GetUserUsage (reseller) returns the given user's usage.
func (c *ResellerContext) GetUserUsage(username string) (*UserUsage, error) {
var rawUsage rawUserUsage
var usage UserUsage

if _, err := c.makeRequestOld(http.MethodGet, "API_SHOW_USER_USAGE?bytes=yes&user="+username, nil, &rawUsage); err != nil {
if _, err := c.makeRequestNew(http.MethodGet, "users/"+username+"/usage", nil, &usage); err != nil {
return nil, err
}

usage = rawUsage.parse()

return &usage, nil
}

Expand Down
Loading

0 comments on commit f2f78e5

Please sign in to comment.