From 85b93a5d4f992c2b32204876c0b5f92f3fb4a85d Mon Sep 17 00:00:00 2001 From: Alexandre Garand Date: Wed, 3 Jul 2019 11:13:08 +0200 Subject: [PATCH] add route resource without test --- mailgun/resource_mailgun_route.go | 133 ++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 mailgun/resource_mailgun_route.go diff --git a/mailgun/resource_mailgun_route.go b/mailgun/resource_mailgun_route.go new file mode 100644 index 0000000..44fff8d --- /dev/null +++ b/mailgun/resource_mailgun_route.go @@ -0,0 +1,133 @@ +package mailgun + +import ( + "context" + "fmt" + "github.com/hashicorp/terraform/helper/schema" + "github.com/mailgun/mailgun-go" + "log" + "time" +) + +func resourceMailgunRoute() *schema.Resource { + return &schema.Resource{ + Create: CreateRoute, + Update: UpdateRoute, + Delete: DeleteRoute, + Read: ReadRoute, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "created_at": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "priority": &schema.Schema{ + Type: schema.TypeInt, + Required: true, + }, + + "expression": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "description": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "actions": &schema.Schema{ + Type: schema.TypeList, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func CreateRoute(d *schema.ResourceData, meta interface{}) error { + mg := meta.(*mailgun.MailgunImpl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + log.Printf("[DEBUG] creating mailgun route: %s", d.Id()) + + creationResponse, err := mg.CreateRoute(ctx, mailgun.Route{ + Priority: d.Get("priority").(int), + Description: d.Get("description").(string), + Expression: d.Get("expression").(string), + Actions: interfaceToStringTab(d.Get("actions")), + }) + + if err != nil { + return fmt.Errorf("Error creating mailgun route: %s", err.Error()) + } + + d.SetId(creationResponse.Id) + + return ReadRoute(d, meta) +} + +func UpdateRoute(d *schema.ResourceData, meta interface{}) error { + mg := meta.(*mailgun.MailgunImpl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + log.Printf("[DEBUG] updating mailgun route: %s", d.Id()) + + _, err := mg.UpdateRoute(ctx, d.Id(), mailgun.Route{ + Priority: d.Get("priority").(int), + Description: d.Get("description").(string), + Expression: d.Get("expression").(string), + Actions: interfaceToStringTab(d.Get("actions")), + }) + + if err != nil { + return fmt.Errorf("Error updating mailgun route: %s", err.Error()) + } + + return ReadDomain(d, meta) +} + +func DeleteRoute(d *schema.ResourceData, meta interface{}) error { + mg := meta.(*mailgun.MailgunImpl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + log.Printf("[DEBUG] Deleting mailgun route: %s", d.Id()) + + err := mg.DeleteRoute(ctx, d.Id()) + + return err +} + +func ReadRoute(d *schema.ResourceData, meta interface{}) error { + mg := meta.(*mailgun.MailgunImpl) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + defer cancel() + + route, err := mg.GetRoute(ctx, d.Id()) + + if err != nil { + return fmt.Errorf("Error Getting mailgun route Details for %s: Error: %s", d.Id(), err) + } + + d.Set("priority", route.Priority) + d.Set("description", route.Description) + d.Set("expression", route.Expression) + d.Set("actions", route.Actions) + d.Set("created_at", route.CreatedAt) + d.Set("id", route.Id) + + d.SetId(route.Id) + + return nil +} -- 2.41.0