Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing importer logic #442

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/resources/device_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ resource "tailscale_device_key" "example_key" {
### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# Device key can be imported using the device id, e.g.,
terraform import tailscale_device_key.sample 123456789
```
9 changes: 9 additions & 0 deletions docs/resources/device_subnet_routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ resource "tailscale_device_subnet_routes" "sample_exit_node" {
### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# Device subnet rules can be imported using the device id, e.g.,
terraform import tailscale_device_subnet_routes.sample 123456789
```
9 changes: 9 additions & 0 deletions docs/resources/device_tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ resource "tailscale_device_tags" "sample_tags" {
### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# Device tags can be imported using the device id, e.g.,
terraform import tailscale_device_tags.sample 123456789
```
9 changes: 9 additions & 0 deletions docs/resources/dns_nameservers.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ resource "tailscale_dns_nameservers" "sample_nameservers" {
### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# ID doesn't matter.
terraform import tailscale_dns_nameservers.sample dns_nameservers
```
9 changes: 9 additions & 0 deletions docs/resources/dns_search_paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ resource "tailscale_dns_search_paths" "sample_search_paths" {
### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# ID doesn't matter.
terraform import tailscale_dns_search_paths.sample dns_search_paths
```
2 changes: 2 additions & 0 deletions examples/resources/tailscale_device_key/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Device key can be imported using the device id, e.g.,
terraform import tailscale_device_key.sample 123456789
2 changes: 2 additions & 0 deletions examples/resources/tailscale_device_subnet_routes/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Device subnet rules can be imported using the device id, e.g.,
terraform import tailscale_device_subnet_routes.sample 123456789
2 changes: 2 additions & 0 deletions examples/resources/tailscale_device_tags/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Device tags can be imported using the device id, e.g.,
terraform import tailscale_device_tags.sample 123456789
2 changes: 2 additions & 0 deletions examples/resources/tailscale_dns_nameservers/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ID doesn't matter.
terraform import tailscale_dns_nameservers.sample dns_nameservers
2 changes: 2 additions & 0 deletions examples/resources/tailscale_dns_search_paths/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ID doesn't matter.
terraform import tailscale_dns_search_paths.sample dns_search_paths
6 changes: 5 additions & 1 deletion tailscale/resource_device_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceDeviceKey() *schema.Resource {
CreateContext: resourceDeviceKeyCreate,
DeleteContext: resourceDeviceKeyDelete,
UpdateContext: resourceDeviceKeyUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"device_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -67,13 +70,14 @@ func resourceDeviceKeyDelete(ctx context.Context, d *schema.ResourceData, m inte

func resourceDeviceKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)
deviceID := d.Id()

device, err := client.Devices().Get(ctx, deviceID)
if err != nil {
return diagnosticsError(err, "Failed to fetch devices")
}

d.Set("device_id", device.ID)
if err = d.Set("key_expiry_disabled", device.KeyExpiryDisabled); err != nil {
return diagnosticsError(err, "failed to set key_expiry_disabled field")
}
Expand Down
6 changes: 5 additions & 1 deletion tailscale/resource_device_subnet_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func resourceDeviceSubnetRoutes() *schema.Resource {
CreateContext: resourceDeviceSubnetRoutesCreate,
UpdateContext: resourceDeviceSubnetRoutesUpdate,
DeleteContext: resourceDeviceSubnetRoutesDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"device_id": {
Type: schema.TypeString,
Expand All @@ -46,13 +49,14 @@ func resourceDeviceSubnetRoutes() *schema.Resource {

func resourceDeviceSubnetRoutesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)
deviceID := d.Id()

routes, err := client.Devices().SubnetRoutes(ctx, deviceID)
if err != nil {
return diagnosticsError(err, "Failed to fetch device subnet routes")
}

d.Set("device_id", deviceID)
if err = d.Set("routes", routes.Enabled); err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 5 additions & 2 deletions tailscale/resource_device_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func resourceDeviceTags() *schema.Resource {
CreateContext: resourceDeviceTagsSet,
UpdateContext: resourceDeviceTagsSet,
DeleteContext: deleteContext,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"device_id": {
Type: schema.TypeString,
Expand All @@ -50,14 +53,14 @@ func resourceDeviceTags() *schema.Resource {

func resourceDeviceTagsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tsclient.Client)
deviceID := d.Get("device_id").(string)
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("tags", device.Tags)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions tailscale/resource_dns_nameservers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceDNSNameservers() *schema.Resource {
CreateContext: resourceDNSNameserversCreate,
UpdateContext: resourceDNSNameserversUpdate,
DeleteContext: resourceDNSNameserversDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"nameservers": {
Type: schema.TypeList,
Expand Down
3 changes: 3 additions & 0 deletions tailscale/resource_dns_search_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func resourceDNSSearchPaths() *schema.Resource {
UpdateContext: resourceDNSSearchPathsUpdate,
DeleteContext: resourceDNSSearchPathsDelete,
CreateContext: resourceDNSSearchPathsCreate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"search_paths": {
Type: schema.TypeList,
Expand Down