From e04a26d796ba53f051994a948884c34192295e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 9 Jul 2021 13:51:27 +0200 Subject: [PATCH] domain resource: Add delete and skeleton for update --- mailman/domain.go | 16 +++++++++++++ mailman/mailman_client.go | 35 ++++++++++++++++++++++++++++- provider/resource_mailman_domain.go | 11 +++++++-- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/mailman/domain.go b/mailman/domain.go index 468006b..6e97189 100644 --- a/mailman/domain.go +++ b/mailman/domain.go @@ -24,3 +24,19 @@ func (client *Client) NewDomain(domain *Domain) error { return err } + +func (client *Client) UpdateDomain(domain *Domain) error { + path := "/domains/" + domain.MailHost + + err := client.put(path, domain) + + return err +} + +func (client *Client) DeleteDomain(mailHost string) error { + path := "/domains/" + mailHost + + err := client.delete(path) + + return err +} diff --git a/mailman/mailman_client.go b/mailman/mailman_client.go index 1db07d0..8e7fec0 100644 --- a/mailman/mailman_client.go +++ b/mailman/mailman_client.go @@ -80,6 +80,39 @@ func (client *Client) post(path string, requestBody interface{}) error { return err } +func (client *Client) put(path string, requestBody interface{}) error { + resourceURL := client.baseURL + path + + payload, err := json.Marshal(requestBody) + if err != nil { + return err + } + + req, err := http.NewRequest(http.MethodPut, resourceURL, bytes.NewBuffer(payload)) + if err != nil { + return nil + } + + req.Header.Add("Content-Type", "application/json") + + _, err = client.sendRequest(req) + + return err +} + +func (client *Client) delete(path string) error { + resourceURL := client.baseURL + path + + req, err := http.NewRequest(http.MethodDelete, resourceURL, nil) + if err != nil { + return nil + } + + _, err = client.sendRequest(req) + + return err +} + func (client *Client) sendRequest(req *http.Request) ([]byte, error) { req.SetBasicAuth(client.credentials.Username, client.credentials.Password) @@ -89,7 +122,7 @@ func (client *Client) sendRequest(req *http.Request) ([]byte, error) { } defer resp.Body.Close() - if resp.StatusCode > 400 { + if resp.StatusCode >= 400 { return nil, fmt.Errorf("API error code: %d", resp.StatusCode) } diff --git a/provider/resource_mailman_domain.go b/provider/resource_mailman_domain.go index b184481..1d31ca5 100644 --- a/provider/resource_mailman_domain.go +++ b/provider/resource_mailman_domain.go @@ -1,6 +1,8 @@ package provider import ( + "fmt" + "git.xenrox.net/~xenrox/terraform-provider-mailman/mailman" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -61,11 +63,16 @@ func resourceMailmanDomainRead(data *schema.ResourceData, meta interface{}) erro } func resourceMailmanDomainUpdate(data *schema.ResourceData, meta interface{}) error { - return nil + // TODO: Implement after user resources + return fmt.Errorf("not implemented yet") } func resourceMailmanDomainDelete(data *schema.ResourceData, meta interface{}) error { - return nil + client := meta.(*mailman.Client) + + err := client.DeleteDomain(data.Id()) + + return err } func setDomainData(data *schema.ResourceData, domain *mailman.Domain) { -- 2.44.0