Skip to content

Commit

Permalink
Support suppressions unsubscribe group
Browse files Browse the repository at this point in the history
Provide a resource to manage suppresions unsubscribe group.
  • Loading branch information
kenzo0107 committed Jan 19, 2024
1 parent edc306b commit d7850be
Show file tree
Hide file tree
Showing 12 changed files with 562 additions and 7 deletions.
34 changes: 34 additions & 0 deletions docs/data-sources/unsubscribe_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sendgrid_unsubscribe_group Data Source - terraform-provider-sendgrid"
subcategory: ""
description: |-
Provides a subuser resource.
Suppression groups, or unsubscribe groups, are specific types or categories of emails from which you would like your recipients to be able to unsubscribe. For example: Daily Newsletters, Invoices, and System Alerts are all potential suppression groups.
Visit the main documentation to [learn more about suppression/unsubscribe groups](https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/).
---

# sendgrid_unsubscribe_group (Data Source)

Provides a subuser resource.

Suppression groups, or unsubscribe groups, are specific types or categories of emails from which you would like your recipients to be able to unsubscribe. For example: Daily Newsletters, Invoices, and System Alerts are all potential suppression groups.

Visit the main documentation to [learn more about suppression/unsubscribe groups](https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/).



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) The ID of the verified sender.

### Read-Only

- `description` (String) This will display to the user as the email address that sent this email. We will send the verification email to the address you enter in this field. If you have not received your verification email after some time, please refer back to the Sender settings and confirm that the "From" email is a valid address.
- `is_default` (Boolean) This is a user-friendly name that is displayed to your recipient when they receive their email.
- `name` (String) A label for your sender identity to help you identify it more quickly. This label is not visible to your recipients.
5 changes: 2 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ description: |-
```terraform
terraform {
required_providers {
hashicups = {
source = "kenzo0107/sendgrid"
sendgrid = {
source = "registry.terraform.io/kenzo0107/sendgrid"
}
}
}
provider "sendgrid" {
api_key = "<your api key>"
}
```

Expand Down
43 changes: 43 additions & 0 deletions docs/resources/unsubscribe_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sendgrid_unsubscribe_group Resource - terraform-provider-sendgrid"
subcategory: ""
description: |-
Provides a subuser resource.
Suppression groups, or unsubscribe groups, are specific types or categories of emails from which you would like your recipients to be able to unsubscribe. For example: Daily Newsletters, Invoices, and System Alerts are all potential suppression groups.
Visit the main documentation to learn more about suppression/unsubscribe groups https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/.
---

# sendgrid_unsubscribe_group (Resource)

Provides a subuser resource.

Suppression groups, or unsubscribe groups, are specific types or categories of emails from which you would like your recipients to be able to unsubscribe. For example: Daily Newsletters, Invoices, and System Alerts are all potential suppression groups.

Visit the main documentation to [learn more about suppression/unsubscribe groups](https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/).

## Example Usage

```terraform
resource "sendgrid_unsubscribe_group" "example" {
name = "dummy"
description = "dummy"
is_default = false
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of your suppression group.

### Optional

- `description` (String) A brief description of your suppression group.
- `is_default` (Boolean) Indicates if you would like this to be your default suppression group.

### Read-Only

- `id` (String) The user ID of the unsubscribe group.
5 changes: 2 additions & 3 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
terraform {
required_providers {
hashicups = {
source = "kenzo0107/sendgrid"
sendgrid = {
source = "registry.terraform.io/kenzo0107/sendgrid"
}
}
}

provider "sendgrid" {
api_key = "<your api key>"
}
5 changes: 5 additions & 0 deletions examples/resources/sendgrid_unsubscribe_group/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "sendgrid_unsubscribe_group" "example" {
name = "dummy"
description = "dummy"
is_default = false
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform-plugin-framework v1.3.5
github.com/hashicorp/terraform-plugin-go v0.18.0
github.com/hashicorp/terraform-plugin-testing v1.5.1
github.com/kenzo0107/sendgrid v0.0.12
github.com/kenzo0107/sendgrid v0.0.13
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ github.com/kenzo0107/sendgrid v0.0.9 h1:1F9TAn9zsSSivkTfAw1Ghp+IN8+0NmcLoJodMyjV
github.com/kenzo0107/sendgrid v0.0.9/go.mod h1:QjjiRx0jvq2ldgR7U9pnoW+F3wf/E9pVUed04pReuPU=
github.com/kenzo0107/sendgrid v0.0.12 h1:MFiauMa3VkOofA1grZ8iqVdj9X/crE+B3Z42U9+8z5E=
github.com/kenzo0107/sendgrid v0.0.12/go.mod h1:QjjiRx0jvq2ldgR7U9pnoW+F3wf/E9pVUed04pReuPU=
github.com/kenzo0107/sendgrid v0.0.13 h1:tuEXgYvKMcqLImrXeVgfpxY90eBmZUNZBJOM1I7r6hQ=
github.com/kenzo0107/sendgrid v0.0.13/go.mod h1:QjjiRx0jvq2ldgR7U9pnoW+F3wf/E9pVUed04pReuPU=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (p *sendgridProvider) Resources(ctx context.Context) []func() resource.Reso
newSenderAuthenticationResource,
newLinkBrandingResource,
newSenderVerificationResource,
newUnsubscribeGroupResource,
}
}

Expand All @@ -136,6 +137,7 @@ func (p *sendgridProvider) DataSources(ctx context.Context) []func() datasource.
newSenderAuthenticationDataSource,
newLinkBrandingDataSource,
newSenderVerificationDataSource,
newUnsubscribeGroupDataSource,
}
}

Expand Down
118 changes: 118 additions & 0 deletions internal/provider/unsubscribe_group_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/kenzo0107/sendgrid"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &unsubscribeGroupDataSource{}
_ datasource.DataSourceWithConfigure = &unsubscribeGroupDataSource{}
)

func newUnsubscribeGroupDataSource() datasource.DataSource {
return &unsubscribeGroupDataSource{}
}

type unsubscribeGroupDataSource struct {
client *sendgrid.Client
}

type unsubscribeGroupDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
IsDefault types.Bool `tfsdk:"is_default"`
}

func (d *unsubscribeGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_unsubscribe_group"
}

func (d *unsubscribeGroupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*sendgrid.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *sendgrid.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.client = client
}

func (d *unsubscribeGroupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `
Provides a subuser resource.
Suppression groups, or unsubscribe groups, are specific types or categories of emails from which you would like your recipients to be able to unsubscribe. For example: Daily Newsletters, Invoices, and System Alerts are all potential suppression groups.
Visit the main documentation to [learn more about suppression/unsubscribe groups](https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/).
`,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the unsubscribe group.",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of your suppression group.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "A brief description of your suppression group.",
Computed: true,
},
"is_default": schema.BoolAttribute{
MarkdownDescription: "Indicates if you would like this to be your default suppression group.",
Computed: true,
},
},
}
}

func (d *unsubscribeGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var s unsubscribeGroupDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &s)...)
if resp.Diagnostics.HasError() {
return
}

groupID := s.ID.ValueString()
id, _ := strconv.ParseInt(groupID, 10, 64)
o, err := d.client.GetSuppressionGroup(ctx, id)
if err != nil {
resp.Diagnostics.AddError(
"Reading unsubscribe group",
fmt.Sprintf("Unable to get unsubscribe group, got error: %s", err),
)
return
}

s.ID = types.StringValue(groupID)
s.Name = types.StringValue(o.Name)
s.Description = types.StringValue(o.Description)
s.IsDefault = types.BoolValue(o.IsDefault)

resp.Diagnostics.Append(resp.State.Set(ctx, &s)...)
if resp.Diagnostics.HasError() {
return
}
}
50 changes: 50 additions & 0 deletions internal/provider/unsubscribe_group_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccUnsubscribeGroupDataSource(t *testing.T) {
resourceName := "data.sendgrid_unsubscribe_group.test"

name := fmt.Sprintf("test-acc-%s", acctest.RandString(16))
description := fmt.Sprintf("test-acc-%s", acctest.RandString(16))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
Config: testAccUnsubscribeGroupDataSourceConfig(name, description, false),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", description),
resource.TestCheckResourceAttr(resourceName, "is_default", "false"),
),
},
},
})
}

func testAccUnsubscribeGroupDataSourceConfig(name, description string, is_default bool) string {
return fmt.Sprintf(`
resource "sendgrid_unsubscribe_group" "test" {
name = "%s"
description = "%s"
is_default = %t
}
data "sendgrid_unsubscribe_group" "test" {
id = sendgrid_unsubscribe_group.test.id
}
`, name, description, is_default)
}
Loading

0 comments on commit d7850be

Please sign in to comment.