~xenrox/terraform-provider-mailman

e04a26d796ba53f051994a948884c34192295e6c — Thorben Günther 1 year, 1 month ago 92d8556
domain resource: Add delete and skeleton for update
3 files changed, 59 insertions(+), 3 deletions(-)

M mailman/domain.go
M mailman/mailman_client.go
M provider/resource_mailman_domain.go
M mailman/domain.go => mailman/domain.go +16 -0
@@ 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
}

M mailman/mailman_client.go => mailman/mailman_client.go +34 -1
@@ 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)
	}


M provider/resource_mailman_domain.go => provider/resource_mailman_domain.go +9 -2
@@ 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) {