-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathresource_device_authorization.go
97 lines (80 loc) · 3.05 KB
/
resource_device_authorization.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
// Copyright (c) David Bond, Tailscale Inc, & Contributors
// SPDX-License-Identifier: MIT
package tailscale
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
tsclient "github.com/tailscale/tailscale-client-go/v2"
)
func resourceDeviceAuthorization() *schema.Resource {
return &schema.Resource{
Description: "The device_authorization resource is used to approve new devices before they can join the tailnet. See https://tailscale.com/kb/1099/device-authorization/ for more details.",
ReadContext: resourceDeviceAuthorizationRead,
CreateContext: resourceDeviceAuthorizationCreate,
UpdateContext: resourceDeviceAuthorizationUpdate,
DeleteContext: resourceDeviceAuthorizationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"device_id": {
Type: schema.TypeString,
Required: true,
Description: "The device to set as authorized",
},
"authorized": {
Type: schema.TypeBool,
Required: true,
Description: "Whether or not the device is authorized",
},
},
}
}
func resourceDeviceAuthorizationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tsclient.Client)
deviceID := d.Id()
device, err := client.Devices().Get(ctx, deviceID)
if err != nil {
return diagnosticsError(err, "Failed to fetch device")
}
d.SetId(device.ID)
d.Set("device_id", device.ID)
d.Set("authorized", device.Authorized)
return nil
}
func resourceDeviceAuthorizationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)
authorized := d.Get("authorized").(bool)
if authorized {
if err := client.Devices().SetAuthorized(ctx, deviceID, true); err != nil {
return diagnosticsError(err, "Failed to authorize device")
}
}
d.SetId(deviceID)
return resourceDeviceAuthorizationRead(ctx, d, m)
}
func resourceDeviceAuthorizationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)
device, err := client.Devices().Get(ctx, deviceID)
if err != nil {
return diagnosticsError(err, "Failed to fetch device")
}
// Currently, the Tailscale API only supports authorizing a device, but not un-authorizing one. So if the device
// data from the API states it is authorized then we can't do anything else here.
if device.Authorized {
d.Set("authorized", true)
return nil
}
if err = client.Devices().SetAuthorized(ctx, deviceID, true); err != nil {
return diagnosticsError(err, "Failed to authorize device")
}
d.Set("authorized", true)
return resourceDeviceAuthorizationRead(ctx, d, m)
}
func resourceDeviceAuthorizationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Since authorization cannot be removed at this point, deleting the resource will do nothing.
return nil
}