Skip to content

Commit

Permalink
implement reading of roles and fix reading of dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjoehnk committed Jun 12, 2019
1 parent d9ad654 commit b05cf64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion mssql/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func resourceDatabaseRead(d *schema.ResourceData, m interface{}) error {
db := m.(*sql.DB)
name := d.Id()
row, err := checkTable(db, name)
if err != nil {
if err == sql.ErrNoRows {
return nil
}else if err != nil {
return err
}

Expand Down
20 changes: 19 additions & 1 deletion mssql/resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,29 @@ func resourceRoleCreate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}
d.SetId(name)
row := db.QueryRow(fmt.Sprintf("SELECT principal_id FROM master.sys.server_principals WHERE name = '%s'", name))
var id int
if err = row.Scan(&id); err != nil {
return err
}

d.SetId(fmt.Sprint(id))
return err
}

func resourceRoleRead(d *schema.ResourceData, m interface{}) error {
db := m.(*sql.DB)
row := db.QueryRow(fmt.Sprintf("SELECT name FROM master.sys.server_principals WHERE principal_id = %s", d.Id()))
var name string
err := row.Scan(&name)
if err == sql.ErrNoRows {
return nil
}else if err != nil {
return err
}
if err := d.Set("name", name); err != nil {
return err
}
return nil
}

Expand Down

0 comments on commit b05cf64

Please sign in to comment.