-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathresource_cloudflare_pages_domain.go
80 lines (70 loc) · 2.42 KB
/
resource_cloudflare_pages_domain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package provider
import (
"context"
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceCloudflarePagesDomain() *schema.Resource {
return &schema.Resource{
Schema: resourceCloudflarePagesDomainSchema(),
CreateContext: resourceCloudflarePagesDomainCreate,
ReadContext: resourceCloudflarePagesDomainRead,
DeleteContext: resourceCloudflarePagesDomainDelete,
Description: heredoc.Doc(`
Provides a resource for managing Cloudflare Pages domains.
`),
}
}
func resourceCloudflarePagesDomainCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)
projectName := d.Get("project_name").(string)
domain := d.Get("domain").(string)
params := cloudflare.PagesDomainParameters{
AccountID: accountID,
ProjectName: projectName,
DomainName: domain,
}
r, err := client.PagesAddDomain(ctx, params)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating domain for project %q: %w", accountID, err))
}
d.SetId(r.ID)
return resourceCloudflarePagesDomainRead(ctx, d, meta)
}
func resourceCloudflarePagesDomainRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)
projectName := d.Get("project_name").(string)
domain := d.Get("domain").(string)
params := cloudflare.PagesDomainParameters{
AccountID: accountID,
ProjectName: projectName,
DomainName: domain,
}
r, err := client.GetPagesDomain(ctx, params)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating domain for project %q: %w", accountID, err))
}
d.Set("status", r.Status)
return nil
}
func resourceCloudflarePagesDomainDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get("account_id").(string)
projectName := d.Get("project_name").(string)
domain := d.Get("domain").(string)
params := cloudflare.PagesDomainParameters{
AccountID: accountID,
ProjectName: projectName,
DomainName: domain,
}
err := client.PagesDeleteDomain(ctx, params)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating domain for project %q: %w", accountID, err))
}
return nil
}