Skip to content

Commit

Permalink
tailscale: update state after modification functions for resources
Browse files Browse the repository at this point in the history
Add calls to appropriate `Read` functions at the end of `Create` and
`Update` functions to follow recommended best practices from HashiCorp.
This is done to prevent scenarios where Terraform could otherwise have
stale state after resource creation or update (from default or computed
values being populated server side on the object as an example) which
can result in unexpected drift in subsequent plan / apply operations.

Fixes tailscale/corp#19698

Signed-off-by: Mario Minardi <mario@tailscale.com>
  • Loading branch information
mpminardi committed May 1, 2024
1 parent 380cf9a commit b22944a
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions tailscale/resource_device_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func resourceDeviceAuthorizationCreate(ctx context.Context, d *schema.ResourceDa
}

d.SetId(deviceID)
return nil
return resourceDeviceAuthorizationRead(ctx, d, m)
}

func resourceDeviceAuthorizationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -109,7 +109,7 @@ func resourceDeviceAuthorizationUpdate(ctx context.Context, d *schema.ResourceDa
}

d.Set("authorized", true)
return nil
return resourceDeviceAuthorizationRead(ctx, d, m)
}

func resourceDeviceAuthorizationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
4 changes: 2 additions & 2 deletions tailscale/resource_device_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func resourceDeviceKeyCreate(ctx context.Context, d *schema.ResourceData, m inte
}

d.SetId(deviceID)
return nil
return resourceDeviceKeyRead(ctx, d, m)
}

func resourceDeviceKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down Expand Up @@ -106,5 +106,5 @@ func resourceDeviceKeyUpdate(ctx context.Context, d *schema.ResourceData, m inte
return diagnosticsError(err, "failed to update device key")
}

return nil
return resourceDeviceKeyRead(ctx, d, m)
}
4 changes: 2 additions & 2 deletions tailscale/resource_device_subnet_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func resourceDeviceSubnetRoutesCreate(ctx context.Context, d *schema.ResourceDat
}

d.SetId(createUUID())
return nil
return resourceDeviceSubnetRoutesRead(ctx, d, m)
}

func resourceDeviceSubnetRoutesUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -82,7 +82,7 @@ func resourceDeviceSubnetRoutesUpdate(ctx context.Context, d *schema.ResourceDat
return diagnosticsError(err, "Failed to set device subnet routes")
}

return nil
return resourceDeviceSubnetRoutesRead(ctx, d, m)
}

func resourceDeviceSubnetRoutesDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
4 changes: 2 additions & 2 deletions tailscale/resource_device_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func resourceDeviceTagsCreate(ctx context.Context, d *schema.ResourceData, m int
}

d.SetId(deviceID)
return nil
return resourceDeviceTagsRead(ctx, d, m)
}

func resourceDeviceTagsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -95,7 +95,7 @@ func resourceDeviceTagsUpdate(ctx context.Context, d *schema.ResourceData, m int
}

d.SetId(deviceID)
return nil
return resourceDeviceTagsRead(ctx, d, m)
}

func resourceDeviceTagsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
4 changes: 2 additions & 2 deletions tailscale/resource_dns_nameservers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func resourceDNSNameserversCreate(ctx context.Context, d *schema.ResourceData, m
}

d.SetId(createUUID())
return nil
return resourceDNSNameserversRead(ctx, d, m)
}

func resourceDNSNameserversUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -78,7 +78,7 @@ func resourceDNSNameserversUpdate(ctx context.Context, d *schema.ResourceData, m
return diagnosticsError(err, "Failed to set dns nameservers")
}

return nil
return resourceDNSNameserversRead(ctx, d, m)
}

func resourceDNSNameserversDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
4 changes: 2 additions & 2 deletions tailscale/resource_dns_preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func resourceDNSPreferencesCreate(ctx context.Context, d *schema.ResourceData, m
}

d.SetId(createUUID())
return nil
return resourceDNSPreferencesRead(ctx, d, m)
}

func resourceDNSPreferencesUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -75,7 +75,7 @@ func resourceDNSPreferencesUpdate(ctx context.Context, d *schema.ResourceData, m
return diagnosticsError(err, "Failed to set dns preferences")
}

return nil
return resourceDNSPreferencesRead(ctx, d, m)
}

func resourceDNSPreferencesDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
4 changes: 2 additions & 2 deletions tailscale/resource_dns_search_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resourceDNSSearchPathsCreate(ctx context.Context, d *schema.ResourceData, m
}

d.SetId(createUUID())
return nil
return resourceDNSSearchPathsRead(ctx, d, m)
}

func resourceDNSSearchPathsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand All @@ -77,7 +77,7 @@ func resourceDNSSearchPathsUpdate(ctx context.Context, d *schema.ResourceData, m
return diagnosticsError(err, "Failed to fetch set search paths")
}

return nil
return resourceDNSSearchPathsRead(ctx, d, m)
}

func resourceDNSSearchPathsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
2 changes: 1 addition & 1 deletion tailscale/resource_dns_split_nameservers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func resourceSplitDNSNameserversCreate(ctx context.Context, d *schema.ResourceDa
}

d.SetId(domain)
return nil
return resourceSplitDNSNameserversRead(ctx, d, m)
}

func resourceSplitDNSNameserversUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
2 changes: 1 addition & 1 deletion tailscale/resource_tailnet_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func resourceTailnetKeyCreate(ctx context.Context, d *schema.ResourceData, m int
return diagnosticsError(err, "Failed to set 'invalid'")
}

return nil
return resourceTailnetKeyRead(ctx, d, m)
}

func resourceTailnetKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
3 changes: 3 additions & 0 deletions tailscale/resource_tailnet_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func testTailnetKeyStruct(reusable bool) tailscale.Key {

func setKeyStep(reusable bool, recreateIfInvalid string) resource.TestStep {
return resource.TestStep{
PreConfig: func() {
testServer.ResponseBody = testTailnetKeyStruct(reusable)
},
ResourceName: "tailscale_tailnet_key.example_key",
Config: fmt.Sprintf(`
resource "tailscale_tailnet_key" "example_key" {
Expand Down

0 comments on commit b22944a

Please sign in to comment.