Skip to content

Commit

Permalink
Read password when reading role
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jul 18, 2023
1 parent 1df751f commit 35fe8cf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.4

### Bug fixes
* Fixes issue with password not appearing when reading role

## 0.1.3

### Enhancements
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/client_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ type RoleOutput struct {
Role Role `json:"role"`
}

type RolePasswordOutput struct {
Password string `json:"password"`
}

type RoleCreateInputRole struct {
Name string `json:"name"`
}
Expand Down
17 changes: 12 additions & 5 deletions internal/provider/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ func (r *RoleResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: "Password of the role.",
Computed: true,
Sensitive: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"branch_id": schema.StringAttribute{
MarkdownDescription: "Branch the role belongs to.",
Expand Down Expand Up @@ -170,19 +167,29 @@ func (r *RoleResource) Read(ctx context.Context, req resource.ReadRequest, resp
}

var role RoleOutput
var rolePassword RolePasswordOutput

err = get(r.client, fmt.Sprintf("/projects/%s/branches/%s/roles/%s", data.ProjectId.ValueString(), data.BranchId.ValueString(), data.Name.ValueString()), &role)
roleUrl := fmt.Sprintf("/projects/%s/branches/%s/roles/%s", data.ProjectId.ValueString(), data.BranchId.ValueString(), data.Name.ValueString())

err = get(r.client, roleUrl, &role)

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read role, got error: %s", err))
return
}

err = get(r.client, fmt.Sprintf("%s/reveal_password", roleUrl), &rolePassword)

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read role password, got error: %s", err))
return
}

tflog.Trace(ctx, "read a role")

data.Id = types.StringValue(role.Role.Name)
data.Name = types.StringValue(role.Role.Name)
data.Password = types.StringValue(role.Role.Password)
data.Password = types.StringValue(rolePassword.Password)
data.BranchId = types.StringValue(role.Role.BranchId)
data.ProjectId = types.StringValue(branch.Branch.ProjectId)

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAccRoleResourceDefault(t *testing.T) {
Config: testAccRoleResourceConfigDefault("sally"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("neon_role.test", "name", "sally"),
resource.TestCheckResourceAttr("neon_role.test", "password", ""),
resource.TestMatchResourceAttr("neon_role.test", "password", existRegex()),
resource.TestCheckResourceAttr("neon_role.test", "branch_id", "br-patient-mode-718259"),
resource.TestCheckResourceAttr("neon_role.test", "project_id", "polished-snowflake-328957"),
),
Expand Down

0 comments on commit 35fe8cf

Please sign in to comment.