Skip to content

Commit

Permalink
Implement additional WordPress endpoints
Browse files Browse the repository at this point in the history
Fix DNS record updates
Change all instances of `Dns` to `DNS`
  • Loading branch information
wolveix committed Feb 15, 2024
1 parent 1d654dd commit e3304bd
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 71 deletions.
68 changes: 35 additions & 33 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
"github.com/spf13/cast"
)

type DnsRecord struct {
type DNSRecord struct {
Name string `json:"name"`
Ttl int `json:"ttl"`
Type string `json:"type"`
Value string `json:"value"`
}

// CheckDnsRecordExists (user) checks if the given dns record exists on the server
// CheckDNSRecordExists (user) checks if the given dns record exists on the server
//
// checkField can be either "name" or "value"
func (c *UserContext) CheckDnsRecordExists(checkField string, domain string, dnsRecord DnsRecord) error {
func (c *UserContext) CheckDNSRecordExists(checkField string, domain string, dnsRecord DNSRecord) error {
body := url.Values{
"check": {checkField},
"domain": {domain},
Expand All @@ -37,18 +37,19 @@ func (c *UserContext) CheckDnsRecordExists(checkField string, domain string, dns
return c.checkObjectExists(body)
}

// CreateDnsRecord (user) creates the provided dns record for the given domain
func (c *UserContext) CreateDnsRecord(domain string, dnsRecord DnsRecord) error {
// CreateDNSRecord (user) creates the provided dns record for the given domain
func (c *UserContext) CreateDNSRecord(domain string, dnsRecord DNSRecord) error {
var response apiGenericResponse

rawDnsRecordData := dnsRecord.translate()
rawDNSRecordData := dnsRecord.translate()

body := url.Values{}
body.Set("domain", domain)
body.Set("name", rawDnsRecordData.Name)
body.Set("ttl", rawDnsRecordData.Ttl)
body.Set("type", rawDnsRecordData.Type)
body.Set("value", rawDnsRecordData.Value)
body := url.Values{
"domain": {domain},
"name": {rawDNSRecordData.Name},
"ttl": {cast.ToString(rawDNSRecordData.Ttl)},
"type": {rawDNSRecordData.Type},
"value": {rawDNSRecordData.Value},
}

if _, err := c.api.makeRequest(http.MethodPost, "API_DNS_CONTROL?action=add&action_pointers=yes", c.credentials, body, &response); err != nil {
return err
Expand All @@ -61,8 +62,8 @@ func (c *UserContext) CreateDnsRecord(domain string, dnsRecord DnsRecord) error
return nil
}

// DeleteDnsRecords (user) deletes all the specified dnss for the session user
func (c *UserContext) DeleteDnsRecords(dnsRecords ...DnsRecord) error {
// DeleteDNSRecords (user) deletes all the specified dnss for the session user
func (c *UserContext) DeleteDNSRecords(dnsRecords ...DNSRecord) error {
var response apiGenericResponse

body := url.Values{}
Expand All @@ -89,18 +90,18 @@ func (c *UserContext) DeleteDnsRecords(dnsRecords ...DnsRecord) error {
return nil
}

// GetDnsRecords (user) returns the given domain's dns records
func (c *UserContext) GetDnsRecords(domain string) ([]DnsRecord, error) {
var dnsRecords []DnsRecord
rawDnsRecords := struct {
DnsRecords []rawDnsRecord `json:"records"`
// GetDNSRecords (user) returns the given domain's dns records
func (c *UserContext) GetDNSRecords(domain string) ([]DNSRecord, error) {
var dnsRecords []DNSRecord
rawDNSRecords := struct {
DNSRecords []rawDNSRecord `json:"records"`
}{}

if _, err := c.api.makeRequest(http.MethodGet, "API_DNS_CONTROL?domain="+domain, c.credentials, nil, &rawDnsRecords); err != nil {
if _, err := c.api.makeRequest(http.MethodGet, "API_DNS_CONTROL?domain="+domain, c.credentials, nil, &rawDNSRecords); err != nil {
return nil, err
}

for _, dnsRecord := range rawDnsRecords.DnsRecords {
for _, dnsRecord := range rawDNSRecords.DNSRecords {
dnsRecords = append(dnsRecords, dnsRecord.translate())
}

Expand All @@ -111,26 +112,27 @@ func (c *UserContext) GetDnsRecords(domain string) ([]DnsRecord, error) {
return dnsRecords, nil
}

// UpdateDnsRecord (user) updates the given dns record for the given domain
func (c *UserContext) UpdateDnsRecord(domain string, originalDnsRecord DnsRecord, updatedDnsRecord DnsRecord) error {
// UpdateDNSRecord (user) updates the given dns record for the given domain
func (c *UserContext) UpdateDNSRecord(domain string, originalDNSRecord DNSRecord, updatedDNSRecord DNSRecord) error {
var response apiGenericResponse

rawDnsRecordData := updatedDnsRecord.translate()
rawDNSRecordData := updatedDNSRecord.translate()

body := url.Values{}
body.Set("domain", domain)
body.Set("name", rawDnsRecordData.Name)
body.Set("ttl", rawDnsRecordData.Ttl)
body.Set("type", rawDnsRecordData.Type)
body.Set("value", rawDnsRecordData.Value)
body.Set(strings.ToLower(originalDnsRecord.Type)+"recs0", fmt.Sprintf("name=%v&value=%v", originalDnsRecord.Name, originalDnsRecord.Value))
body := url.Values{
"domain": {domain},
"name": {rawDNSRecordData.Name},
"ttl": {cast.ToString(rawDNSRecordData.Ttl)},
"type": {rawDNSRecordData.Type},
"value": {rawDNSRecordData.Value},
strings.ToLower(originalDNSRecord.Type) + "recs0": {fmt.Sprintf("name=%v&value=%v", originalDNSRecord.Name, originalDNSRecord.Value)},
}

if _, err := c.api.makeRequest(http.MethodPost, "API_DNS_CONTROL?action=edit&action_pointers=yes", c.credentials, body, &response); err != nil {
return err
}

if response.Success != "Record Added" {
return fmt.Errorf("failed to create dns record: %v", response.Result)
if response.Success != "Record Edited" {
return fmt.Errorf("failed to update dns record: %v", response.Result)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions dns_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package directadmin

import "github.com/spf13/cast"

type rawDnsRecord struct {
type rawDNSRecord struct {
Name string `json:"name"`
Ttl string `json:"ttl"`
Type string `json:"type"`
Value string `json:"value"`
}

func (d *DnsRecord) translate() rawDnsRecord {
return rawDnsRecord{
func (d *DNSRecord) translate() rawDNSRecord {
return rawDNSRecord{
Name: d.Name,
Ttl: cast.ToString(d.Ttl),
Type: d.Type,
Value: d.Value,
}
}

func (d *rawDnsRecord) translate() DnsRecord {
return DnsRecord{
func (d *rawDNSRecord) translate() DNSRecord {
return DNSRecord{
Name: d.Name,
Ttl: cast.ToInt(d.Ttl),
Type: d.Type,
Expand Down
2 changes: 1 addition & 1 deletion package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Package struct {
CatchallEnabled bool `json:"catchallEnabled" yaml:"catchallEnabled"`
CgiEnabled bool `json:"cgiEnabled" yaml:"cgiEnabled"`
CronEnabled bool `json:"cronEnabled" yaml:"cronEnabled"`
DnsControlEnabled bool `json:"dnsControlEnabled" yaml:"dnsControlEnabled"`
DNSControlEnabled bool `json:"dnsControlEnabled" yaml:"dnsControlEnabled"`
DomainPointerQuota int `json:"domainPointerQuota" yaml:"domainPointerQuota"`
DomainQuota int `json:"domainQuota" yaml:"domainQuota"`
EmailAutoresponderQuota int `json:"emailAutoresponderQuota" yaml:"emailAutoresponderQuota"`
Expand Down
6 changes: 3 additions & 3 deletions package_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type rawPackage struct {
CatchallEnabled string `json:"catchall" url:"catchall"`
CgiEnabled string `json:"cgi" url:"cgi"`
CronEnabled string `json:"cron" url:"cron"`
DnsControlEnabled string `json:"dnscontrol" url:"dnscontrol"`
DNSControlEnabled string `json:"dnscontrol" url:"dnscontrol"`
DomainPointerQuota string `json:"domainptr" url:"domainptr"`
DomainQuota string `json:"vdomains" url:"vdomains"`
EmailAutoresponderQuota string `json:"nemailr" url:"nemailr"`
Expand Down Expand Up @@ -51,7 +51,7 @@ func (p *Package) translate() (pack rawPackage) {
CgiEnabled: reverseParseOnOff(p.CgiEnabled, false),
CpuQuota: reverseParseNum(p.CpuQuota, true),
CronEnabled: reverseParseOnOff(p.CronEnabled, false),
DnsControlEnabled: reverseParseOnOff(p.DnsControlEnabled, false),
DNSControlEnabled: reverseParseOnOff(p.DNSControlEnabled, false),
DomainPointerQuota: reverseParseNum(p.DomainPointerQuota, false),
DomainQuota: reverseParseNum(p.DomainQuota, false),
EmailAutoresponderQuota: reverseParseNum(p.EmailAutoresponderQuota, false),
Expand Down Expand Up @@ -96,7 +96,7 @@ func (p *rawPackage) translate() Package {
CatchallEnabled: parseOnOff(p.CatchallEnabled),
CgiEnabled: parseOnOff(p.CgiEnabled),
CronEnabled: parseOnOff(p.CronEnabled),
DnsControlEnabled: parseOnOff(p.DnsControlEnabled),
DNSControlEnabled: parseOnOff(p.DNSControlEnabled),
DomainPointerQuota: parseNum(p.DomainPointerQuota),
DomainQuota: parseNum(p.DomainQuota),
EmailAutoresponderQuota: parseNum(p.EmailAutoresponderQuota),
Expand Down
2 changes: 1 addition & 1 deletion user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type UserConfig struct {
Creator string `json:"creator" yaml:"creator"`
CronEnabled bool `json:"cronEnabled" yaml:"cronEnabled"`
Domain string `json:"domain" yaml:"domain"`
DnsEnabled bool `json:"dnsEnabled" yaml:"dnsEnabled"`
DNSEnabled bool `json:"dnsEnabled" yaml:"dnsEnabled"`
Email string `json:"email" yaml:"email"`
GitEnabled bool `json:"gitEnabled" yaml:"gitEnabled"`
IpAddresses []string `json:"ipAddresses" yaml:"ipAddresses"`
Expand Down
4 changes: 2 additions & 2 deletions user_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type rawUserConfig struct {
Creator string `json:"creator"`
Cron string `json:"cron"`
DateCreated string `json:"date_created"`
Dnscontrol string `json:"dnscontrol"`
DNScontrol string `json:"dnscontrol"`
Domain string `json:"domain"`
Email string `json:"email"`
Ftp string `json:"ftp"`
Expand Down Expand Up @@ -72,7 +72,7 @@ func (r *rawUserConfig) parse() (UserConfig, error) {
Creator: r.Creator,
CronEnabled: parseOnOff(r.Cron),
Domain: r.Domain,
DnsEnabled: parseOnOff(r.Dnscontrol),
DNSEnabled: parseOnOff(r.DNScontrol),
Email: r.Email,
GitEnabled: parseOnOff(r.Git),
IpAddresses: strings.Split(r.Ips, ", "),
Expand Down
126 changes: 100 additions & 26 deletions wordpress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,74 @@ package directadmin

import (
"fmt"
"github.com/spf13/cast"
"net/http"
"strings"
"time"
)

type WordPressInstall struct {
AdminEmail string `json:"adminEmail" yaml:"adminEmail"`
AdminName string `json:"adminName" yaml:"adminName"`
AdminPass string `json:"adminPass" yaml:"adminPass"`
DbName string `json:"dbName" yaml:"dbName"`
DbPass string `json:"dbPass" yaml:"dbPass"`
DbPrefix string `json:"dbPrefix" yaml:"dbPrefix"`
DbUser string `json:"dbUser" yaml:"dbUser"`
FilePath string `json:"filePath" yaml:"filePath"`
Title string `json:"title" yaml:"title"`
}
type (
WordPressInstall struct {
AdminEmail string `json:"adminEmail" yaml:"adminEmail"`
AdminName string `json:"adminName" yaml:"adminName"`
AdminPass string `json:"adminPass" yaml:"adminPass"`
DbName string `json:"dbName" yaml:"dbName"`
DbPass string `json:"dbPass" yaml:"dbPass"`
DbPrefix string `json:"dbPrefix" yaml:"dbPrefix"`
DbUser string `json:"dbUser" yaml:"dbUser"`
FilePath string `json:"filePath" yaml:"filePath"`
Title string `json:"title" yaml:"title"`
}

WordPressInstallQuick struct {
AdminEmail string `json:"adminEmail" yaml:"adminEmail"`
FilePath string `json:"filePath" yaml:"filePath"`
Title string `json:"title" yaml:"title"`
}

WordPressLocation struct {
FilePath string `json:"filePath"`
Host string `json:"host"`
Id string `json:"id"`
WebPath string `json:"webPath"`
Wordpress struct {
AutoUpdateMajor bool `json:"autoUpdateMajor"`
AutoUpdateMinor bool `json:"autoUpdateMinor"`
Error string `json:"error"`
SiteURL string `json:"siteURL"`
Template string `json:"template"`
Title string `json:"title"`
Version string `json:"version"`
} `json:"wordpress"`
}

type WordPressLocation struct {
FilePath string `json:"filePath"`
Host string `json:"host"`
Id string `json:"id"`
WebPath string `json:"webPath"`
Wordpress struct {
AutoUpdateMajor bool `json:"autoUpdateMajor"`
AutoUpdateMinor bool `json:"autoUpdateMinor"`
Error string `json:"error"`
SiteURL string `json:"siteURL"`
Template string `json:"template"`
Title string `json:"title"`
Version string `json:"version"`
} `json:"wordpress"`
WordPressUser struct {
Id int `json:"id"`
DisplayName string `json:"displayName"`
Email string `json:"email"`
Login string `json:"login"`
Registered time.Time `json:"registered"`
Roles []string `json:"roles"`
}
)

// ChangeWordPressUserPassword (user) changes the password of the given wordpress user.
func (c *UserContext) ChangeWordPressUserPassword(locationId string, userId int, password string) error {
var passwordObject struct {
Password string `json:"password"`
}

if password == "" {
return fmt.Errorf("password cannot be empty")
}

passwordObject.Password = password

if _, err := c.api.makeRequestN(http.MethodPost, "wordpress/locations/"+locationId+"/users/"+cast.ToString(userId)+"/change-password", c.credentials, passwordObject, nil); err != nil {
return fmt.Errorf("failed to change wordpress user password: %v", err)
}

return nil
}

func (c *UserContext) CreateWordPressInstall(install WordPressInstall, createDatabase bool) error {
Expand All @@ -56,7 +94,7 @@ func (c *UserContext) CreateWordPressInstall(install WordPressInstall, createDat
install.DbPrefix = install.DbPrefix + "_"
}

// remove / from the beginning of FilePath if it's there'
// remove / from the beginning of FilePath if it's there
if install.FilePath[0] == '/' {
install.FilePath = install.FilePath[1:]
}
Expand All @@ -73,6 +111,20 @@ func (c *UserContext) CreateWordPressInstall(install WordPressInstall, createDat
return nil
}

// CreateWordPressInstallQuick (user) creates a new wordpress install and automatically creates a database
func (c *UserContext) CreateWordPressInstallQuick(install WordPressInstallQuick) error {
// remove / from the beginning of FilePath if it's there
if install.FilePath[0] == '/' {
install.FilePath = install.FilePath[1:]
}

if _, err := c.api.makeRequestN(http.MethodPost, "wordpress/install-quick", c.credentials, install, nil); err != nil {
return err
}

return nil
}

func (c *UserContext) DeleteWordPressInstall(id string) error {
if _, err := c.api.makeRequestN(http.MethodDelete, "wordpress/locations/"+id, c.credentials, nil, nil); err != nil {
return err
Expand All @@ -90,3 +142,25 @@ func (c *UserContext) GetWordPressInstalls() ([]*WordPressLocation, error) {

return wordpressInstalls, nil
}

func (c *UserContext) GetWordPressSSOLink(locationId string, userId int) (string, error) {
var ssoObject struct {
URL string `json:"url"`
}

if _, err := c.api.makeRequestN(http.MethodPost, "wordpress/locations/"+locationId+"/users/"+cast.ToString(userId)+"/sso-login", c.credentials, nil, &ssoObject); err != nil {
return "", fmt.Errorf("failed to get wordpress installs: %v", err)
}

return ssoObject.URL, nil
}

func (c *UserContext) GetWordPressUsers(locationId string) ([]*WordPressUser, error) {
var wordpressUsers []*WordPressUser

if _, err := c.api.makeRequestN(http.MethodGet, "wordpress/locations/"+locationId+"/users", c.credentials, nil, &wordpressUsers); err != nil {
return nil, fmt.Errorf("failed to get wordpress users: %v", err)
}

return wordpressUsers, nil
}

0 comments on commit e3304bd

Please sign in to comment.