-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
156 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package main | ||
|
||
// A simple example of fetching a user by ID and using the Patch API to update | ||
// their name. | ||
// | ||
// Usage: | ||
// go run main.go <host> <token> <user-id> | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/databricks/databricks-sdk-go" | ||
"github.com/databricks/databricks-sdk-go/logger" | ||
"github.com/databricks/databricks-sdk-go/service/iam" | ||
) | ||
|
||
func fetchAndPrintUserName(c *databricks.AccountClient, id string) error { | ||
user, err := c.Users.GetById(context.Background(), id) | ||
if err != nil { | ||
return fmt.Errorf("error fetching user: %s", err) | ||
} | ||
fmt.Println("User name:", user.DisplayName) | ||
fmt.Println("User roles:", user.Roles) | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Printf("Usage: %s <user name>\n", os.Args[0]) | ||
os.Exit(1) | ||
} | ||
name := os.Args[1] | ||
|
||
// Create a client | ||
cfg := databricks.Config{ | ||
Host: "https://accounts.cloud.databricks.com", | ||
AccountID: "4d9d3bc8-66c3-4e5a-8a0a-551f564257f0", | ||
} | ||
logger.DefaultLogger = &logger.SimpleLogger{ | ||
Level: logger.LevelTrace, | ||
} | ||
c, err := databricks.NewAccountClient(&cfg) | ||
if err != nil { | ||
fmt.Printf("Error creating client: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Fetch the user ID | ||
users, err := c.Users.ListAll(context.Background(), iam.ListAccountUsersRequest{ | ||
Filter: "displayName sw " + name, | ||
}) | ||
if err != nil { | ||
fmt.Printf("Error fetching user: %s\n", err) | ||
os.Exit(1) | ||
} | ||
userID := users[0].Id | ||
|
||
// Fetch the user | ||
err = fetchAndPrintUserName(c, userID) | ||
if err != nil { | ||
fmt.Printf("Error fetching user: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Make non admin | ||
pu := iam.PartialUpdate{ | ||
Id: userID, | ||
// Note that this request works without schemas | ||
Operations: []iam.Patch{ | ||
{ | ||
Op: "remove", | ||
Path: "roles", | ||
Value: []map[string]string{ | ||
{"value": "account_admin"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
err = c.Users.Patch(context.Background(), pu) | ||
if err != nil { | ||
fmt.Printf("Error updating user: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Fetch user and print its name | ||
err = fetchAndPrintUserName(c, userID) | ||
if err != nil { | ||
fmt.Printf("Error fetching user: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// restore the old name | ||
pu = iam.PartialUpdate{ | ||
Id: userID, | ||
// It also works with schemas | ||
Schema: []iam.PatchSchema{ | ||
iam.PatchSchemaUrnIetfParamsScimApiMessagesPatchop, | ||
}, | ||
Operations: []iam.Patch{ | ||
{ | ||
Op: "add", | ||
Value: map[string][]map[string]string{ | ||
"roles": { | ||
{"value": "account_admin"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
err = c.Users.Patch(context.Background(), pu) | ||
if err != nil { | ||
fmt.Printf("Error updating user: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Fetch user and print its name | ||
err = fetchAndPrintUserName(c, userID) | ||
if err != nil { | ||
fmt.Printf("Error fetching user: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.