diff --git a/internal/provider/resource_account.go b/internal/provider/resource_account.go index 4cb13525..37efd67a 100644 --- a/internal/provider/resource_account.go +++ b/internal/provider/resource_account.go @@ -62,7 +62,6 @@ func setFromAccountResponseMap(d *schema.ResourceData, raw map[string]interface{ if attrsVal, ok := raw["attributes"]; ok { attrs := attrsVal.(map[string]interface{}) d.Set(accountLoginNameKey, attrs["login_name"]) - d.Set(accountPasswordKey, attrs["password"]) } } @@ -106,6 +105,7 @@ func resourceAccountCreate(ctx context.Context, d *schema.ResourceData, meta int } if password != nil { opts = append(opts, accounts.WithPasswordAccountPassword(*password)) + d.Set(accountPasswordKey, *password) } default: return diag.Errorf("invalid type provided") diff --git a/internal/provider/resource_account_test.go b/internal/provider/resource_account_test.go new file mode 100644 index 00000000..498f4ee6 --- /dev/null +++ b/internal/provider/resource_account_test.go @@ -0,0 +1,145 @@ +package provider + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/boundary/api/accounts" + "github.com/hashicorp/boundary/testing/controller" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +const ( + fooAccountDesc = "test account" + fooAccountDescUpdate = "test account update" +) + +var ( + fooAccount = fmt.Sprintf(` +resource "boundary_auth_method" "foo" { + name = "test" + description = "test account" + type = "password" + scope_id = boundary_scope.org1.id +} + +resource "boundary_account" "foo" { + name = "test" + description = "%s" + type = "password" + login_name = "foo" + password = "foofoofoo" + auth_method_id = boundary_auth_method.foo.id +}`, fooAccountDesc) + + fooAccountUpdate = fmt.Sprintf(` +resource "boundary_auth_method" "foo" { + name = "test" + description = "test account" + type = "password" + scope_id = boundary_scope.org1.id +} + +resource "boundary_account" "foo" { + name = "test" + description = "%s" + type = "password" + login_name = "foo" + password = "foofoofoo" + auth_method_id = boundary_auth_method.foo.id +}`, fooAccountDescUpdate) +) + +func TestAccAccount(t *testing.T) { + tc := controller.NewTestController(t, tcConfig...) + defer tc.Shutdown() + url := tc.ApiAddrs()[0] + + resource.Test(t, resource.TestCase{ + Providers: testProviders, + CheckDestroy: testAccCheckAccountResourceDestroy(t), + Steps: []resource.TestStep{ + { + //create + Config: testConfig(url, fooOrg, fooAccount), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("boundary_account.foo", "description", fooAccountDesc), + resource.TestCheckResourceAttr("boundary_account.foo", "name", "test"), + resource.TestCheckResourceAttr("boundary_account.foo", "type", "password"), + resource.TestCheckResourceAttr("boundary_account.foo", "login_name", "foo"), + resource.TestCheckResourceAttr("boundary_account.foo", "password", "foofoofoo"), + testAccCheckAccountResourceExists("boundary_account.foo"), + ), + }, + { + // update + Config: testConfig(url, fooOrg, fooAccountUpdate), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("boundary_account.foo", "description", fooAccountDescUpdate), + resource.TestCheckResourceAttr("boundary_account.foo", "name", "test"), + resource.TestCheckResourceAttr("boundary_account.foo", "type", "password"), + resource.TestCheckResourceAttr("boundary_account.foo", "login_name", "foo"), + resource.TestCheckResourceAttr("boundary_account.foo", "password", "foofoofoo"), + testAccCheckAccountResourceExists("boundary_account.foo"), + ), + }, + }, + }) +} + +func testAccCheckAccountResourceExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + id := rs.Primary.ID + if id == "" { + return fmt.Errorf("No ID is set") + } + + md := testProvider.Meta().(*metaData) + + amClient := accounts.NewClient(md.client) + + if _, _, err := amClient.Read(context.Background(), id); err != nil { + return fmt.Errorf("Got an error when reading account %q: %v", id, err) + } + + return nil + } +} + +func testAccCheckAccountResourceDestroy(t *testing.T) resource.TestCheckFunc { + return func(s *terraform.State) error { + if testProvider.Meta() == nil { + t.Fatal("got nil provider metadata") + } + md := testProvider.Meta().(*metaData) + + for _, rs := range s.RootModule().Resources { + switch rs.Type { + case "boundary_account": + id := rs.Primary.ID + + amClient := accounts.NewClient(md.client) + + _, apiErr, err := amClient.Read(context.Background(), id) + if err != nil { + return fmt.Errorf("Error when reading destroyed account %q: %v", id, err) + } + if apiErr == nil || apiErr.Status != http.StatusNotFound { + return fmt.Errorf("Didn't get a 404 when reading destroyed account %q: %v", id, apiErr) + } + + default: + continue + } + } + return nil + } +} diff --git a/internal/provider/resource_auth_method.go b/internal/provider/resource_auth_method.go index c00f543e..d8868f79 100644 --- a/internal/provider/resource_auth_method.go +++ b/internal/provider/resource_auth_method.go @@ -43,10 +43,12 @@ func resourceAuthMethod() *schema.Resource { authmethodMinLoginNameLengthKey: { Type: schema.TypeInt, Optional: true, + Computed: true, }, authmethodMinPasswordLengthKey: { Type: schema.TypeInt, Optional: true, + Computed: true, }, }, } diff --git a/internal/provider/resource_auth_method_test.go b/internal/provider/resource_auth_method_test.go new file mode 100644 index 00000000..0acef1b8 --- /dev/null +++ b/internal/provider/resource_auth_method_test.go @@ -0,0 +1,125 @@ +package provider + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/boundary/api/authmethods" + "github.com/hashicorp/boundary/testing/controller" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +const ( + fooAuthMethodDesc = "test auth method" + fooAuthMethodDescUpdate = "test auth method update" +) + +var ( + fooAuthMethod = fmt.Sprintf(` +resource "boundary_auth_method" "foo" { + name = "test" + description = "%s" + type = "password" + scope_id = boundary_scope.org1.id +}`, fooAuthMethodDesc) + + fooAuthMethodUpdate = fmt.Sprintf(` +resource "boundary_auth_method" "foo" { + name = "test" + description = "%s" + type = "password" + scope_id = boundary_scope.org1.id +}`, fooAuthMethodDescUpdate) +) + +func TestAccAuthMethod(t *testing.T) { + tc := controller.NewTestController(t, tcConfig...) + defer tc.Shutdown() + url := tc.ApiAddrs()[0] + + resource.Test(t, resource.TestCase{ + Providers: testProviders, + CheckDestroy: testAccCheckAuthMethodResourceDestroy(t), + Steps: []resource.TestStep{ + { + //create + Config: testConfig(url, fooOrg, fooAuthMethod), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("boundary_auth_method.foo", "description", fooAuthMethodDesc), + resource.TestCheckResourceAttr("boundary_auth_method.foo", "name", "test"), + resource.TestCheckResourceAttr("boundary_auth_method.foo", "type", "password"), + testAccCheckAuthMethodResourceExists("boundary_auth_method.foo"), + ), + }, + { + // update + Config: testConfig(url, fooOrg, fooAuthMethodUpdate), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("boundary_auth_method.foo", "description", fooAuthMethodDescUpdate), + resource.TestCheckResourceAttr("boundary_auth_method.foo", "name", "test"), + resource.TestCheckResourceAttr("boundary_auth_method.foo", "type", "password"), + testAccCheckAuthMethodResourceExists("boundary_auth_method.foo"), + ), + }, + }, + }) +} + +func testAccCheckAuthMethodResourceExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + id := rs.Primary.ID + if id == "" { + return fmt.Errorf("No ID is set") + } + + md := testProvider.Meta().(*metaData) + + amClient := authmethods.NewClient(md.client) + + if _, _, err := amClient.Read(context.Background(), id); err != nil { + return fmt.Errorf("Got an error when reading auth method %q: %v", id, err) + } + + return nil + } +} + +func testAccCheckAuthMethodResourceDestroy(t *testing.T) resource.TestCheckFunc { + return func(s *terraform.State) error { + if testProvider.Meta() == nil { + t.Fatal("got nil provider metadata") + } + md := testProvider.Meta().(*metaData) + + for _, rs := range s.RootModule().Resources { + switch rs.Type { + case "boundary_scope": + continue + case "boundary_auth_method": + id := rs.Primary.ID + + amClient := authmethods.NewClient(md.client) + + _, apiErr, err := amClient.Read(context.Background(), id) + if err != nil { + return fmt.Errorf("Error when reading destroyed auth method %q: %v", id, err) + } + if apiErr == nil || apiErr.Status != http.StatusNotFound { + return fmt.Errorf("Didn't get a 404 when reading destroyed auth method %q: %v", id, apiErr) + } + + default: + continue + } + } + return nil + } +}