Skip to content

Commit

Permalink
Add addon repository calls
Browse files Browse the repository at this point in the history
  • Loading branch information
phorick committed Jul 17, 2024
1 parent d473154 commit 983f165
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 1 deletion.
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
libraryVersion = "6.24.0"
libraryVersion = "6.25.0"
defaultBasePath = "https://api.transip.nl/v6"
userAgent = "go-client-gotransip/" + libraryVersion
)
Expand Down
31 changes: 31 additions & 0 deletions email/mailaddon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package email

// MailAddon is the struct used to unmarshall the addon response.
type MailAddon struct {
// ID is the ID of the addon
ID int `json:"id"`
// DiskSpace is the amount of disk space the addon provides
DiskSpace int `json:"diskSpace"`
// Mailboxes is the amount of extra mailboxes the addon provides
Mailboxes int `json:"mailboxes"`
// LinkedMailBox is the mailbox the addon is currently linked to, if any
LinkedMailBox string `json:"linkedMailBox"`
// CanBeLinked is whether this Addon is allowed to be linked
CanBeLinked bool `json:"canBeLinked"`
}

// mailAddonWrapper struct contains a MailAddon in it,
// this is solely used for unmarshalling/marshalling
type mailAddonWrapper struct {
MailAddons []MailAddon `json:"addons"`
}

// LinkAddonRequest is used to generate a json body that contains the Action, AddonID, and Mailbox properties
type LinkAddonRequest struct {
// Action could be linkmailbox or unlinkmailbox
Action string `json:"action"`
// AddonID is the id of the addon to link
AddonID int `json:"addonId"`
// Mailbox is the email address associated with the mailbox
Mailbox string `json:"mailbox"`
}
38 changes: 38 additions & 0 deletions email/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package email

import (
"errors"
"fmt"
"net/mail"
"strings"
Expand Down Expand Up @@ -155,3 +156,40 @@ func (r *Repository) DeleteMaillist(domainName string, maillistID int) error {

return r.Client.Delete(restRequest)
}

// GetAddonsByDomainName gets a list of mail addons associated with a domain
func (r *Repository) GetAddonsByDomainName(domainName string) ([]MailAddon, error) {
var response mailAddonWrapper
restRequest := rest.Request{Endpoint: fmt.Sprintf("/email/%s/mail-addons", domainName)}
err := r.Client.Get(restRequest, &response)

return response.MailAddons, err
}

// LinkMailaddon Links an addon to a mailbox
func (r *Repository) LinkMailaddon(addonID int, mailbox string) error {
components := strings.Split(mailbox, "@")
if len(components) != 2 {
return errors.New("invalid mailbox")
}
domainName := components[1]

linkAddonRequest := LinkAddonRequest{Action: "linkmailbox", AddonID: addonID, Mailbox: mailbox}
err := r.Client.Patch(rest.Request{Endpoint: fmt.Sprintf("/email/%s/mail-addons", domainName), Body: linkAddonRequest})

return err
}

// UnlinkMailaddon Unlinks an addon from a mailbox
func (r *Repository) UnlinkMailaddon(addonID int, mailbox string) error {
components := strings.Split(mailbox, "@")
if len(components) != 2 {
return errors.New("invalid mailbox")
}
domainName := components[1]

linkAddonRequest := LinkAddonRequest{Action: "unlinkmailbox", AddonID: addonID, Mailbox: mailbox}
err := r.Client.Patch(rest.Request{Endpoint: fmt.Sprintf("/email/%s/mail-addons", domainName), Body: linkAddonRequest})

return err
}
67 changes: 67 additions & 0 deletions email/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,70 @@ func TestRepository_DeleteMaillist(t *testing.T) {
err := repo.DeleteMaillist("example.com", 1)
require.NoError(t, err)
}

func TestRepository_LinkAddon(t *testing.T) {
const expectedRequestBody = `{"action":"linkmailbox","addonId":7,"mailbox":"[email protected]"}`
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

err := repo.LinkMailaddon(7, "[email protected]")
require.NoError(t, err)
}

func TestRepository_LinkAddonInvalidMailbox(t *testing.T) {
const expectedRequestBody = `{"action":"linkmailbox","addonId":7,"mailbox":"[email protected]"}`
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

err := repo.LinkMailaddon(7, "testexample.com")
require.Error(t, err)
expectedErrorMessage := "invalid mailbox"
assert.EqualErrorf(t, err, expectedErrorMessage, "Error should be: %v, got: %v", expectedErrorMessage, err)
}

func TestRepository_UnlinkAddon(t *testing.T) {
const expectedRequestBody = `{"action":"unlinkmailbox","addonId":7,"mailbox":"[email protected]"}`
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

err := repo.UnlinkMailaddon(7, "[email protected]")
require.NoError(t, err)
}

func TestRepository_UnlinkAddonInvalidMailbox(t *testing.T) {
const expectedRequestBody = `{"action":"unlinkmailbox","addonId":7,"mailbox":"[email protected]"}`
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "PATCH", StatusCode: 204, ExpectedRequest: expectedRequestBody}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

err := repo.UnlinkMailaddon(7, "testexample.com")
require.Error(t, err)
expectedErrorMessage := "invalid mailbox"
assert.EqualErrorf(t, err, expectedErrorMessage, "Error should be: %v, got: %v", expectedErrorMessage, err)
}

func TestRepository_GetMailAddonsByDomainName(t *testing.T) {

const apiResponse = `{"addons": [{"id": 282154,"diskSpace": 1024,"mailboxes": 5,"linkedMailBox": "[email protected]","canBeLinked": false}]}`
server := testutil.MockServer{T: t, ExpectedURL: "/email/example.com/mail-addons", ExpectedMethod: "GET", StatusCode: 200, Response: apiResponse}
client, tearDown := server.GetClient()
defer tearDown()
repo := Repository{Client: *client}

all, err := repo.GetAddonsByDomainName("example.com")
require.NoError(t, err)
require.Equal(t, 1, len(all))

assert.Equal(t, 282154, all[0].ID)
assert.Equal(t, "[email protected]", all[0].LinkedMailBox)
assert.Equal(t, false, all[0].CanBeLinked)
assert.Equal(t, 5, all[0].Mailboxes)
assert.Equal(t, 1024, all[0].DiskSpace)
}
77 changes: 77 additions & 0 deletions examples/email/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"fmt"
"github.com/transip/gotransip/v6"
"github.com/transip/gotransip/v6/email"
"log"
"strings"
)

func main() {
// Create a new client with the default demo client config, using the demo token
client, err := gotransip.NewClient(gotransip.DemoClientConfiguration)
if err != nil {
panic(err)
}

emailRepo := email.Repository{Client: client}

//transipdemo.net
log.Println("Getting a list of email boxes")
mailboxes, err := emailRepo.GetMailboxesByDomainName("transipdemo.net")
if err != nil {
panic(err)
}
fmt.Println(strings.Repeat("-", 50))
for _, mailbox := range mailboxes {
alias := mailbox.LocalPart
if len(alias) == 0 {
alias = "*"
}
fmt.Printf("Mailbox Identifier: %s, Available Disk Space: %d Status is %s \n", mailbox.Identifier, mailbox.AvailableDiskSpace, mailbox.Status)
}
fmt.Println(strings.Repeat("-", 50))

log.Println("Getting a list of email forwards")
forwards, err := emailRepo.GetMailforwardsByDomainName("transipdemo.net")
if err != nil {
panic(err)
}
fmt.Println(strings.Repeat("-", 50))
for _, forward := range forwards {
alias := forward.LocalPart
if len(alias) == 0 {
alias = "*"
}
fmt.Printf("Forward ID %d Forwards to: '%s' whene receiving mail on %s@%s. Status is %s \n", forward.ID, forward.ForwardTo, alias, forward.Domain, forward.Status)
}

fmt.Println(strings.Repeat("-", 50))

log.Println("Getting a list of email lists")
maillists, err := emailRepo.GetMaillistsByDomainName("transipdemo.net")
if err != nil {
panic(err)
}
fmt.Println(strings.Repeat("-", 50))
for _, list := range maillists {
fmt.Printf("List ID %d Name: '%s'. Email Address: %s \n", list.ID, list.Name, list.EmailAddress)
}

fmt.Println(strings.Repeat("-", 50))

log.Println("Getting a list of email addons")
addons, err := emailRepo.GetAddonsByDomainName("transipdemo.net")
if err != nil {
panic(err)
}

// Simple loop to print your addons
// For more info about the email api, see: https://api.transip.nl/rest/docs.html#email
fmt.Println(strings.Repeat("-", 50))
for _, addon := range addons {
fmt.Printf("Addon ID %d with disk space: %d, additional mailboxes: '%d' \n", addon.ID, addon.DiskSpace, addon.Mailboxes)
}
fmt.Println(strings.Repeat("-", 50))
}

0 comments on commit 983f165

Please sign in to comment.