forked from cloudflare/terraform-provider-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_cloudflare_argo_tunnel.go
109 lines (84 loc) · 3.18 KB
/
resource_cloudflare_argo_tunnel.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package provider
import (
"context"
"fmt"
"strings"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
)
const argoTunnelCNAME = "cfargotunnel.com"
func resourceCloudflareArgoTunnel() *schema.Resource {
return &schema.Resource{
Schema: resourceCloudflareArgoTunnelSchema(),
CreateContext: resourceCloudflareArgoTunnelCreate,
ReadContext: resourceCloudflareArgoTunnelRead,
DeleteContext: resourceCloudflareArgoTunnelDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceCloudflareArgoTunnelImport,
},
}
}
func resourceCloudflareArgoTunnelCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accID := d.Get("account_id").(string)
name := d.Get("name").(string)
secret := d.Get("secret").(string)
tunnel, err := client.CreateArgoTunnel(ctx, accID, name, secret)
if err != nil {
return diag.FromErr(errors.Wrap(err, fmt.Sprintf("failed to create Argo Tunnel")))
}
d.SetId(tunnel.ID)
return resourceCloudflareArgoTunnelRead(ctx, d, meta)
}
func resourceCloudflareArgoTunnelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accID := d.Get("account_id").(string)
tunnel, err := client.ArgoTunnel(ctx, accID, d.Id())
if err != nil {
return diag.FromErr(fmt.Errorf("failed to fetch Argo Tunnel: %w", err))
}
token, err := client.TunnelToken(context.Background(), cloudflare.TunnelTokenParams{
AccountID: accID,
ID: tunnel.ID,
})
if err != nil {
fmt.Sprintf("[WARN] Unable to set the tunnel_token in state because it's not found in API")
d.Set("tunnel_token", "")
return nil
}
d.Set("cname", fmt.Sprintf("%s.%s", tunnel.ID, argoTunnelCNAME))
return nil
}
func resourceCloudflareArgoTunnelDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accID := d.Get("account_id").(string)
cleanupErr := client.CleanupArgoTunnelConnections(ctx, accID, d.Id())
if cleanupErr != nil {
return diag.FromErr(errors.Wrap(cleanupErr, fmt.Sprintf("failed to clean up Argo Tunnel connections")))
}
deleteErr := client.DeleteArgoTunnel(ctx, accID, d.Id())
if deleteErr != nil {
return diag.FromErr(errors.Wrap(deleteErr, fmt.Sprintf("failed to delete Argo Tunnel")))
}
d.SetId("")
return nil
}
func resourceCloudflareArgoTunnelImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*cloudflare.API)
attributes := strings.Split(d.Id(), "/")
if len(attributes) != 2 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/argoTunnelUUID\"", d.Id())
}
accID, tunnelID := attributes[0], attributes[1]
tunnel, err := client.ArgoTunnel(ctx, accID, tunnelID)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch Argo Tunnel %s", tunnelID))
}
d.Set("account_id", accID)
d.Set("name", tunnel.Name)
d.SetId(tunnel.ID)
resourceCloudflareArgoTunnelRead(ctx, d, meta)
return []*schema.ResourceData{d}, nil
}