-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting tests for hashicorp/terraform-plugin-sdk#802. Testing the new cty stuff hanging off the ResourceData and ResourceDiff types.
- Loading branch information
1 parent
5c728f7
commit e1f1862
Showing
8 changed files
with
335 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package sdkv2 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-corner/internal/backend" | ||
) | ||
|
||
func dataSourceRegionsCty() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceRegionsCtyRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"filter": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"names": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceRegionsCtyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*backend.Client) | ||
|
||
config := d.GetRawConfig().AsValueMap() | ||
filter := config["filter"].AsString() | ||
if filter == "" { | ||
return diag.FromErr(errors.New("filter wasn't set")) | ||
} | ||
|
||
regions, err := client.ReadRegions() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
names := []string{} | ||
for _, r := range regions { | ||
names = append(names, r.Name) | ||
} | ||
|
||
d.SetId("regions") | ||
|
||
err = d.Set("names", names) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sdkv2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func testAccDataSourceRegionsCty(t *testing.T) resource.TestCase { | ||
return resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configDataSourceRegionsCtyBasic, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.corner_regions_cty.foo", "names.#")), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
const configDataSourceRegionsCtyBasic = ` | ||
data "corner_regions_cty" "foo" { | ||
filter = "foo" | ||
} | ||
` |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package sdkv2 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-corner/internal/backend" | ||
) | ||
|
||
func resourceUserCty() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceUserCtyCreate, | ||
ReadContext: resourceUserCtyRead, | ||
UpdateContext: resourceUserCtyUpdate, | ||
DeleteContext: resourceUserCtyDelete, | ||
CustomizeDiff: resourceUserCtyCustomizeDiff, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"email": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"age": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceUserCtyCustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error { | ||
if d.Get("email").(string) != "" && d.GetRawConfig().IsNull() { | ||
return errors.New("raw config not set in plan") | ||
} | ||
if d.Id() != "" && d.GetRawState().IsNull() { | ||
return errors.New("raw state not set in plan") | ||
} | ||
if d.GetRawPlan().IsNull() { | ||
return errors.New("raw plan not set in plan") | ||
} | ||
return nil | ||
} | ||
|
||
func resourceUserCtyCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*backend.Client) | ||
config := d.GetRawConfig().AsValueMap() | ||
age, _ := config["age"].AsBigFloat().Int64() | ||
newUser := &backend.User{ | ||
Email: config["email"].AsString(), | ||
Name: config["name"].AsString(), | ||
Age: int(age), | ||
} | ||
|
||
// plan should be set | ||
if d.GetRawPlan().IsNull() { | ||
return diag.FromErr(errors.New("plan wasn't set")) | ||
} | ||
|
||
// state should not be set | ||
if !d.GetRawState().IsNull() { | ||
return diag.FromErr(fmt.Errorf("state was %s, not null", d.GetRawState().GoString())) | ||
} | ||
|
||
err := client.CreateUser(newUser) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(newUser.Email) | ||
|
||
err = d.Set("name", newUser.Name) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
err = d.Set("age", newUser.Age) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceUserCtyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*backend.Client) | ||
|
||
// state should be set | ||
state := d.GetRawState().AsValueMap() | ||
email := state["email"].AsString() | ||
|
||
// plan should not be set | ||
if !d.GetRawPlan().IsNull() { | ||
return diag.FromErr(fmt.Errorf("plan was %s, not null", d.GetRawPlan().GoString())) | ||
} | ||
|
||
// config should not be set | ||
if !d.GetRawConfig().IsNull() { | ||
return diag.FromErr(fmt.Errorf("config was %s, not null", d.GetRawConfig().GoString())) | ||
} | ||
|
||
p, err := client.ReadUser(email) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if p == nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
err = d.Set("name", p.Name) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
err = d.Set("age", p.Age) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceUserCtyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*backend.Client) | ||
|
||
config := d.GetRawConfig().AsValueMap() | ||
age, _ := config["age"].AsBigFloat().Int64() | ||
user := &backend.User{ | ||
Email: config["email"].AsString(), | ||
Name: config["name"].AsString(), | ||
Age: int(age), | ||
} | ||
|
||
// plan should be set | ||
if d.GetRawPlan().IsNull() { | ||
return diag.FromErr(errors.New("plan wasn't set")) | ||
} | ||
|
||
// state should be set | ||
if d.GetRawState().IsNull() { | ||
return diag.FromErr(errors.New("state wasn't set")) | ||
} | ||
|
||
err := client.UpdateUser(user) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceUserCtyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*backend.Client) | ||
|
||
state := d.GetRawState().AsValueMap() | ||
age, _ := state["age"].AsBigFloat().Int64() | ||
user := &backend.User{ | ||
Email: state["email"].AsString(), | ||
Name: state["name"].AsString(), | ||
Age: int(age), | ||
} | ||
|
||
// plan should be null | ||
if !d.GetRawPlan().IsNull() { | ||
return diag.FromErr(fmt.Errorf("plan was set to %s", d.GetRawPlan().GoString())) | ||
} | ||
|
||
// config should be null | ||
if !d.GetRawConfig().IsNull() { | ||
return diag.FromErr(fmt.Errorf("config was set to %s", d.GetRawConfig().GoString())) | ||
} | ||
|
||
err := client.DeleteUser(user) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package sdkv2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func testAccResourceUserCty(t *testing.T) resource.TestCase { | ||
return resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configResourceUserCtyBasic, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"corner_user.foo", "email", "ford@prefect.co"), | ||
resource.TestCheckResourceAttr( | ||
"corner_user.foo", "name", "Ford Prefect"), | ||
resource.TestCheckResourceAttr( | ||
"corner_user.foo", "age", "200"), | ||
), | ||
}, | ||
{ | ||
Config: configResourceUserCtyUpdate, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"corner_user.foo", "email", "ford@prefect.co"), | ||
resource.TestCheckResourceAttr( | ||
"corner_user.foo", "name", "Ford Prefect II"), | ||
resource.TestCheckResourceAttr( | ||
"corner_user.foo", "age", "300"), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
const configResourceUserCtyBasic = ` | ||
resource "corner_user" "foo" { | ||
email = "ford@prefect.co" | ||
name = "Ford Prefect" | ||
age = 200 | ||
} | ||
` | ||
|
||
const configResourceUserCtyUpdate = ` | ||
resource "corner_user" "foo" { | ||
email = "ford@prefect.co" | ||
name = "Ford Prefect II" | ||
age = 300 | ||
} | ||
` |