github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/builtin/providers/test/resource_diff_suppress.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "math/rand" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func testResourceDiffSuppress() *schema.Resource { 12 diffSuppress := func(k, old, new string, d *schema.ResourceData) bool { 13 if old == "" || strings.Contains(new, "replace") { 14 return false 15 } 16 return true 17 } 18 19 return &schema.Resource{ 20 Create: testResourceDiffSuppressCreate, 21 Read: testResourceDiffSuppressRead, 22 Delete: testResourceDiffSuppressDelete, 23 Update: testResourceDiffSuppressUpdate, 24 25 Importer: &schema.ResourceImporter{ 26 State: schema.ImportStatePassthrough, 27 }, 28 29 Schema: map[string]*schema.Schema{ 30 "optional": { 31 Type: schema.TypeString, 32 Optional: true, 33 }, 34 "val_to_upper": { 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 StateFunc: func(val interface{}) string { 39 return strings.ToUpper(val.(string)) 40 }, 41 DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { 42 return strings.ToUpper(old) == strings.ToUpper(new) 43 }, 44 }, 45 "network": { 46 Type: schema.TypeString, 47 Optional: true, 48 Default: "default", 49 ForceNew: true, 50 DiffSuppressFunc: diffSuppress, 51 }, 52 "subnetwork": { 53 Type: schema.TypeString, 54 Optional: true, 55 Computed: true, 56 ForceNew: true, 57 DiffSuppressFunc: diffSuppress, 58 }, 59 60 "node_pool": { 61 Type: schema.TypeList, 62 Optional: true, 63 Computed: true, 64 ForceNew: true, 65 Elem: &schema.Resource{ 66 Schema: map[string]*schema.Schema{ 67 "name": &schema.Schema{ 68 Type: schema.TypeString, 69 Optional: true, 70 Computed: true, 71 ForceNew: true, 72 }, 73 }, 74 }, 75 }, 76 }, 77 } 78 } 79 80 func testResourceDiffSuppressCreate(d *schema.ResourceData, meta interface{}) error { 81 d.Set("network", "modified") 82 d.Set("subnetwork", "modified") 83 84 if _, ok := d.GetOk("node_pool"); !ok { 85 d.Set("node_pool", []string{}) 86 } 87 88 id := fmt.Sprintf("%x", rand.Int63()) 89 d.SetId(id) 90 return nil 91 } 92 93 func testResourceDiffSuppressRead(d *schema.ResourceData, meta interface{}) error { 94 return nil 95 } 96 97 func testResourceDiffSuppressUpdate(d *schema.ResourceData, meta interface{}) error { 98 return nil 99 } 100 101 func testResourceDiffSuppressDelete(d *schema.ResourceData, meta interface{}) error { 102 d.SetId("") 103 return nil 104 }