Last active
September 20, 2019 08:24
-
-
Save jeremmfr/ff8f73a8986799d1a75f582d2c769d03 to your computer and use it in GitHub Desktop.
terraform_test_partial function with different type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exemple | |
import ( | |
"fmt" | |
"github.com/hashicorp/terraform/helper/schema" | |
) | |
func resourcePartial() *schema.Resource { | |
return &schema.Resource{ | |
Create: resourcePartialCreate, | |
Read: resourcePartialRead, | |
Update: resourcePartialUpdate, | |
Delete: resourcePartialDelete, | |
Schema: map[string]*schema.Schema{ | |
"name": { | |
Type: schema.TypeString, | |
ForceNew: true, | |
Required: true, | |
}, | |
"tags": { | |
Type: schema.TypeString, | |
Optional: true, | |
}, | |
"tags_set": { | |
Type: schema.TypeSet, | |
Optional: true, | |
Elem: &schema.Resource{ | |
Schema: map[string]*schema.Schema{ | |
"name": { | |
Type: schema.TypeString, | |
Required: true, | |
}, | |
"tags": { | |
Type: schema.TypeString, | |
Optional: true, | |
}, | |
}, | |
}, | |
}, | |
"tags_list": { | |
Type: schema.TypeList, | |
Optional: true, | |
Elem: &schema.Resource{ | |
Schema: map[string]*schema.Schema{ | |
"name": { | |
Type: schema.TypeString, | |
Required: true, | |
}, | |
"tags": { | |
Type: schema.TypeSet, | |
Optional: true, | |
Elem: &schema.Schema{Type: schema.TypeString}, | |
}, | |
}, | |
}, | |
}, | |
"tags_bool": { | |
Type: schema.TypeBool, | |
Optional: true, | |
}, | |
}, | |
} | |
} | |
func resourcePartialCreate(d *schema.ResourceData, m interface{}) error { | |
d.SetId(d.Get("name").(string) + "!") | |
return nil | |
} | |
func resourcePartialRead(d *schema.ResourceData, m interface{}) error { | |
return nil | |
} | |
func resourcePartialUpdate(d *schema.ResourceData, m interface{}) error { | |
// Enable partial state mode | |
d.Partial(true) | |
if d.HasChange("tags") { | |
// If an error occurs, return with an error, | |
// we didn't finish updating | |
if err := updateTags(d, m); err != nil { | |
return err | |
} | |
d.SetPartial("tags") | |
} | |
if d.HasChange("tags_set") { | |
// If an error occurs, return with an error, | |
// we didn't finish updating | |
if err := updateTags(d, m); err != nil { | |
return err | |
/*---------------------------------- | |
| Partial NOT WORKS here | | |
| with terraform 0.12.9 | | |
-----------------------------------*/ | |
} | |
d.SetPartial("tags_set") | |
} | |
if d.HasChange("tags_list") { | |
// If an error occurs, return with an error, | |
// we didn't finish updating | |
if err := updateTags(d, m); err != nil { | |
return err | |
/*---------------------------------- | |
| Partial NOT WORKS here, | | |
| if TypeSet included in List, | | |
| with terraform 0.12.9 | | |
------------------------------------*/ | |
} | |
d.SetPartial("tags_list") | |
} | |
if d.HasChange("tags_bool") { | |
// If an error occurs, return with an error, | |
// we didn't finish updating | |
if err := updateTags(d, m); err != nil { | |
return err | |
} | |
d.SetPartial("tags_bool") | |
} | |
if d.HasChange("name") { | |
if err := updateName(d, m); err != nil { | |
return err | |
} | |
d.SetPartial("name") | |
} | |
// We succeeded, disable partial mode | |
d.Partial(false) | |
return nil | |
} | |
func resourcePartialDelete(d *schema.ResourceData, m interface{}) error { | |
return nil | |
} | |
func updateTags(d *schema.ResourceData, m interface{}) error { | |
return fmt.Errorf("generated error") | |
} | |
func updateName(d *schema.ResourceData, m interface{}) error { | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment