From 83f29a345671cee32b2d15a39204930183b81dbc Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 5 May 2017 17:36:30 -0400 Subject: [PATCH] Add more complete Read example --- .../writing-custom-terraform-providers.html.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/website/source/guides/writing-custom-terraform-providers.html.md b/website/source/guides/writing-custom-terraform-providers.html.md index 47ce4d078..fffc2cf0e 100644 --- a/website/source/guides/writing-custom-terraform-providers.html.md +++ b/website/source/guides/writing-custom-terraform-providers.html.md @@ -545,8 +545,22 @@ exists (maybe it was destroyed out of band). Just like the destroy callback, the `Read` function should gracefully handle this case. ```go -// Tells Terraform the resource no longer exists -d.SetId("") +func resourceServerRead(d *schema.ResourceData, m interface{}) error { + client := meta.(*MyClient) + + // Attempt to read from an upstream API + obj, ok := client.Get(d.Id()) + + // If the resource does not exist, inform Terraform. We want to immediately + // return here to prevent further processing. + if !ok { + d.SetId("") + return nil + } + + d.Set("address", obj.Address) + return nil +} ``` ## Next Steps