|
| 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