~xenrox/terraform-provider-mailman

92d8556df029907f4c2dc63f1da2fae7a16341fa — Thorben Günther 1 year, 1 month ago 7cc4d00
Implement create for domain resource
4 files changed, 113 insertions(+), 10 deletions(-)

M mailman/domain.go
M mailman/mailman_client.go
M provider/provider.go
M provider/resource_mailman_domain.go
M mailman/domain.go => mailman/domain.go +7 -0
@@ 17,3 17,10 @@ func (client *Client) GetDomain(mailHost string) (*Domain, error) {

	return domain, nil
}

func (client *Client) NewDomain(domain *Domain) error {

	err := client.post("/domains", domain)

	return err
}

M mailman/mailman_client.go => mailman/mailman_client.go +40 -9
@@ 1,6 1,7 @@
package mailman

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"


@@ 46,26 47,56 @@ func (client *Client) get(path string, resource interface{}) error {
		return nil
	}

	req.SetBasicAuth(client.credentials.Username, client.credentials.Password)
	resp, err := client.httpClient.Do(req)
	responseBody, err := client.sendRequest(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode > 400 {
		return fmt.Errorf("API error code: %d", resp.StatusCode)
	err = json.Unmarshal(responseBody, &resource)
	if err != nil {
		return err
	}

	responseBody, err := ioutil.ReadAll(resp.Body)
	return nil
}

func (client *Client) post(path string, requestBody interface{}) error {
	resourceURL := client.baseURL + path

	payload, err := json.Marshal(requestBody)
	if err != nil {
		return err
	}

	err = json.Unmarshal(responseBody, &resource)
	req, err := http.NewRequest(http.MethodPost, resourceURL, bytes.NewBuffer(payload))
	if err != nil {
		return err
		return nil
	}

	return nil
	req.Header.Add("Content-Type", "application/json")

	_, err = client.sendRequest(req)

	return err
}

func (client *Client) sendRequest(req *http.Request) ([]byte, error) {
	req.SetBasicAuth(client.credentials.Username, client.credentials.Password)

	resp, err := client.httpClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode > 400 {
		return nil, fmt.Errorf("API error code: %d", resp.StatusCode)
	}

	responseBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	return responseBody, nil
}

M provider/provider.go => provider/provider.go +3 -1
@@ 29,7 29,9 @@ func MailmanProvider() *schema.Provider {
				DefaultFunc: schema.EnvDefaultFunc("MAILMAN_URL", nil),
			},
		},
		ResourcesMap: map[string]*schema.Resource{},
		ResourcesMap: map[string]*schema.Resource{
			"mailman_domain": resourceMailmanDomain(),
		},
		DataSourcesMap: map[string]*schema.Resource{
			"mailman_domain": dataSourceMailmanDomain(),
		},

M provider/resource_mailman_domain.go => provider/resource_mailman_domain.go +63 -0
@@ 5,6 5,69 @@ import (
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceMailmanDomain() *schema.Resource {
	return &schema.Resource{
		Create: resourceMailmanDomainCreate,
		Read:   resourceMailmanDomainRead,
		Update: resourceMailmanDomainUpdate,
		Delete: resourceMailmanDomainDelete,
		Schema: map[string]*schema.Schema{
			"mail_host": {
				Type:     schema.TypeString,
				Required: true,
			},
			"alias_domain": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"description": {
				Type:     schema.TypeString,
				Optional: true,
			},
		},
	}
}

func resourceMailmanDomainCreate(data *schema.ResourceData, meta interface{}) error {
	client := meta.(*mailman.Client)

	domain := &mailman.Domain{
		MailHost:    data.Get("mail_host").(string),
		AliasDomain: data.Get("alias_domain").(string),
		Description: data.Get("description").(string),
	}

	err := client.NewDomain(domain)
	if err != nil {
		return err
	}

	setDomainData(data, domain)

	return resourceMailmanDomainRead(data, meta)
}

func resourceMailmanDomainRead(data *schema.ResourceData, meta interface{}) error {
	client := meta.(*mailman.Client)

	domain, err := client.GetDomain(data.Id())
	if err != nil {
		return err
	}

	setDomainData(data, domain)

	return nil
}

func resourceMailmanDomainUpdate(data *schema.ResourceData, meta interface{}) error {
	return nil
}

func resourceMailmanDomainDelete(data *schema.ResourceData, meta interface{}) error {
	return nil
}

func setDomainData(data *schema.ResourceData, domain *mailman.Domain) {
	data.SetId(domain.MailHost)