Skip to content

Commit b4c6a69

Browse files
authored
Merge pull request #5 from octoenergy/link-branding-validation
[Feat] Link branding validation
2 parents dc09ebf + 60101e3 commit b4c6a69

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# sendgrid_link_branding_validation
2+
3+
Provide a resource to manage a link branding validation.
4+
5+
## Example Usage
6+
7+
```hcl
8+
9+
resource "sendgrid_link_branding_validation" "foo" {
10+
link_branding_id = sendgrid_link_branding.foo.id
11+
}
12+
13+
```
14+
15+
## Argument Reference
16+
17+
The following arguments are supported:
18+
19+
* `link_branding_id` - (Required) Id of the link branding to validate.
20+
21+
## Attributes Reference
22+
23+
In addition to all arguments above, the following attributes are exported:
24+
25+
* `valid` - Indicates if this is a valid link branding or not.
26+

sendgrid/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func Provider() *schema.Provider {
9191
"sendgrid_domain_authentication": resourceSendgridDomainAuthentication(),
9292
"sendgrid_domain_authentication_validation": resourceSendgridDomainAuthenticationValidation(),
9393
"sendgrid_link_branding": resourceSendgridLinkBranding(),
94+
"sendgrid_link_branding_validation": resourceSendgridLinkBrandingValidation(),
9495
"sendgrid_sso_integration": resourceSendgridSSOIntegration(),
9596
"sendgrid_sso_certificate": resourceSendgridSSOCertificate(),
9697
"sendgrid_teammate": resourceSendgridTeammate(),
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Provide a resource to manage a link branding validation.
3+
Example Usage
4+
```hcl
5+
6+
resource "sendgrid_link_branding_validation" "foo" {
7+
link_branding_id = sendgrid_link_branding.foo.id
8+
}
9+
10+
```
11+
*/
12+
package sendgrid
13+
14+
import (
15+
"context"
16+
"fmt"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
sendgrid "github.com/octoenergy/terraform-provider-sendgrid/sdk"
21+
)
22+
23+
// https://docs.sendgrid.com/api-reference/link-branding/validate-a-branded-link
24+
func resourceSendgridLinkBrandingValidation() *schema.Resource { //nolint:funlen
25+
return &schema.Resource{
26+
CreateContext: resourceSendgridLinkBrandingValidationCreate,
27+
ReadContext: resourceSendgridLinkBrandingValidationRead,
28+
UpdateContext: resourceSendgridLinkBrandingValidationUpdate,
29+
DeleteContext: resourceSendgridLinkBrandingValidationDelete,
30+
Importer: &schema.ResourceImporter{
31+
StateContext: schema.ImportStatePassthroughContext,
32+
},
33+
34+
Schema: map[string]*schema.Schema{
35+
"link_branding_id": {
36+
Type: schema.TypeString,
37+
Description: "Id of the link branding to validate.",
38+
Required: true,
39+
},
40+
41+
"valid": {
42+
Type: schema.TypeBool,
43+
Description: "Indicates if this is a valid link branding or not.",
44+
Computed: true,
45+
},
46+
},
47+
}
48+
}
49+
50+
func resourceSendgridLinkBrandingValidationCreate(
51+
ctx context.Context,
52+
d *schema.ResourceData,
53+
m interface{},
54+
) diag.Diagnostics {
55+
return validateLinkBranding(ctx, d, m)
56+
}
57+
58+
func validateLinkBranding(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
59+
c := m.(*sendgrid.Client)
60+
61+
if err := c.ValidateLinkBranding(ctx, d.Get("link_branding_id").(string)); err.Err != nil || err.StatusCode != 200 {
62+
if err.Err != nil {
63+
return diag.FromErr(err.Err)
64+
}
65+
return diag.Errorf("unable to validate domain DNS configuration")
66+
}
67+
68+
return resourceSendgridLinkBrandingValidationRead(ctx, d, m)
69+
}
70+
71+
func resourceSendgridLinkBrandingValidationRead( //nolint:funlen,cyclop
72+
ctx context.Context,
73+
d *schema.ResourceData,
74+
m interface{},
75+
) diag.Diagnostics {
76+
c := m.(*sendgrid.Client)
77+
78+
link, err := c.ReadLinkBranding(ctx, d.Get("link_branding_id").(string))
79+
if err.Err != nil {
80+
return diag.FromErr(err.Err)
81+
}
82+
83+
//nolint:errcheck
84+
d.Set("valid", link.Valid)
85+
d.SetId(fmt.Sprint(link.ID))
86+
return nil
87+
}
88+
89+
func resourceSendgridLinkBrandingValidationUpdate(
90+
ctx context.Context,
91+
d *schema.ResourceData,
92+
m interface{},
93+
) diag.Diagnostics {
94+
return validateLinkBranding(ctx, d, m)
95+
}
96+
97+
func resourceSendgridLinkBrandingValidationDelete(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics {
98+
return nil
99+
}

0 commit comments

Comments
 (0)