From cf905f52dbb9cfb20d08ec943419c8224166f4b6 Mon Sep 17 00:00:00 2001 From: "maxim.manuylov" Date: Wed, 23 Aug 2017 20:40:49 +0300 Subject: [PATCH] cached variables --- build.sh | 2 +- ext/provider_ext.go | 44 ++++++++++++++++++++++++++++++++++ ext/resource_ext_cached_var.go | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 ext/resource_ext_cached_var.go diff --git a/build.sh b/build.sh index cb7e401..f6498ef 100644 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -VERSION="v1.4" +VERSION="v1.5" govendor sync diff --git a/ext/provider_ext.go b/ext/provider_ext.go index d699f14..2646166 100644 --- a/ext/provider_ext.go +++ b/ext/provider_ext.go @@ -80,6 +80,50 @@ func Provider() terraform.ResourceProvider { Delete: deleteRemote, }, + "ext_cached_var": { + Schema: map[string]*schema.Schema{ + "value": { + Type: schema.TypeString, + Required: true, + }, + "cached": { + Type: schema.TypeString, + Computed: true, + }, + "trigger": { + Type: schema.TypeString, + Optional: true, + }, + }, + Create: createCachedVar, + Read: readCachedVar, + Update: updateCachedVar, + Delete: deleteCachedVar, + }, + + "ext_cached_svar": { + Schema: map[string]*schema.Schema{ + "value": { + Type: schema.TypeString, + Required: true, + Sensitive: true, + }, + "cached": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + "trigger": { + Type: schema.TypeString, + Optional: true, + }, + }, + Create: createCachedVar, + Read: readCachedVar, + Update: updateCachedVar, + Delete: deleteCachedVar, + }, + }, DataSourcesMap: map[string]*schema.Resource{ diff --git a/ext/resource_ext_cached_var.go b/ext/resource_ext_cached_var.go new file mode 100644 index 0000000..07e8872 --- /dev/null +++ b/ext/resource_ext_cached_var.go @@ -0,0 +1,42 @@ +package ext + +import ( + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform/helper/schema" +) + +func createCachedVar(resourceData *schema.ResourceData, _ interface{}) error { + id, err := uuid.GenerateUUID() + if err != nil { + return err + } + + cacheValue(resourceData) + + resourceData.SetId(id) + + return nil +} + +func readCachedVar(_ *schema.ResourceData, _ interface{}) error { + return nil +} + +func updateCachedVar(resourceData *schema.ResourceData, _ interface{}) error { + if resourceData.HasChange("trigger") { + cacheValue(resourceData) + } + + return nil +} + +func deleteCachedVar(resourceData *schema.ResourceData, _ interface{}) error { + resourceData.SetId("") + resourceData.Set("cached", "") + + return nil +} + +func cacheValue(resourceData *schema.ResourceData) { + resourceData.Set("cached", resourceData.Get("value")) +}