Merge pull request #278 from gregmankes/user_auth_plugin_dbaas
add mysql user auth settings for database users
This commit is contained in:
commit
c333a45c9e
21
databases.go
21
databases.go
|
@ -62,6 +62,12 @@ const (
|
|||
SQLModeTraditional = "TRADITIONAL"
|
||||
)
|
||||
|
||||
// SQL Auth constants allow for MySQL-specific user auth plugins
|
||||
const (
|
||||
SQLAuthPluginNative = "mysql_native_password"
|
||||
SQLAuthPluginCachingSHA2 = "caching_sha2_password"
|
||||
)
|
||||
|
||||
// DatabasesService is an interface for interfacing with the databases endpoints
|
||||
// of the DigitalOcean API.
|
||||
// See: https://developers.digitalocean.com/documentation/v2#databases
|
||||
|
@ -143,9 +149,15 @@ type DatabaseConnection struct {
|
|||
|
||||
// DatabaseUser represents a user in the database
|
||||
type DatabaseUser struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Role string `json:"role,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Role string `json:"role,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
|
||||
}
|
||||
|
||||
// DatabaseMySQLUserSettings contains MySQL-specific user settings
|
||||
type DatabaseMySQLUserSettings struct {
|
||||
AuthPlugin string `json:"auth_plugin"`
|
||||
}
|
||||
|
||||
// DatabaseMaintenanceWindow represents the maintenance_window of a database
|
||||
|
@ -235,7 +247,8 @@ type DatabaseCreatePoolRequest struct {
|
|||
|
||||
// DatabaseCreateUserRequest is used to create a new database user
|
||||
type DatabaseCreateUserRequest struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name"`
|
||||
MySQLSettings *DatabaseMySQLUserSettings `json:"mysql_settings,omitempty"`
|
||||
}
|
||||
|
||||
// DatabaseCreateDBRequest is used to create a new engine-specific database within the cluster
|
||||
|
|
|
@ -1297,3 +1297,113 @@ func TestDatabases_UpdateFirewallRules(t *testing.T) {
|
|||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestDatabases_CreateDatabaseUserWithMySQLSettings(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"
|
||||
|
||||
path := fmt.Sprintf("/v2/databases/%s/users", dbID)
|
||||
|
||||
responseJSON := []byte(fmt.Sprintf(`{
|
||||
"user": {
|
||||
"name": "foo",
|
||||
"mysql_settings": {
|
||||
"auth_plugin": "%s"
|
||||
}
|
||||
}
|
||||
}`, SQLAuthPluginNative))
|
||||
expectedUser := &DatabaseUser{
|
||||
Name: "foo",
|
||||
MySQLSettings: &DatabaseMySQLUserSettings{
|
||||
AuthPlugin: SQLAuthPluginNative,
|
||||
},
|
||||
}
|
||||
|
||||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodPost)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(responseJSON)
|
||||
})
|
||||
|
||||
user, _, err := client.Databases.CreateUser(ctx, dbID, &DatabaseCreateUserRequest{
|
||||
Name: expectedUser.Name,
|
||||
MySQLSettings: expectedUser.MySQLSettings,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedUser, user)
|
||||
}
|
||||
|
||||
func TestDatabases_ListDatabaseUsersWithMySQLSettings(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"
|
||||
|
||||
path := fmt.Sprintf("/v2/databases/%s/users", dbID)
|
||||
|
||||
responseJSON := []byte(fmt.Sprintf(`{
|
||||
"users": [
|
||||
{
|
||||
"name": "foo",
|
||||
"mysql_settings": {
|
||||
"auth_plugin": "%s"
|
||||
}
|
||||
}
|
||||
]
|
||||
}`, SQLAuthPluginNative))
|
||||
expectedUsers := []DatabaseUser{
|
||||
{
|
||||
Name: "foo",
|
||||
MySQLSettings: &DatabaseMySQLUserSettings{
|
||||
AuthPlugin: SQLAuthPluginNative,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(responseJSON)
|
||||
})
|
||||
|
||||
users, _, err := client.Databases.ListUsers(ctx, dbID, &ListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedUsers, users)
|
||||
}
|
||||
|
||||
func TestDatabases_GetDatabaseUserWithMySQLSettings(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
dbID := "deadbeef-dead-4aa5-beef-deadbeef347d"
|
||||
userID := "d290a0a0-27da-42bd-a4b2-bcecf43b8832"
|
||||
|
||||
path := fmt.Sprintf("/v2/databases/%s/users/%s", dbID, userID)
|
||||
|
||||
responseJSON := []byte(fmt.Sprintf(`{
|
||||
"user": {
|
||||
"name": "foo",
|
||||
"mysql_settings": {
|
||||
"auth_plugin": "%s"
|
||||
}
|
||||
}
|
||||
}`, SQLAuthPluginNative))
|
||||
expectedUser := &DatabaseUser{
|
||||
Name: "foo",
|
||||
MySQLSettings: &DatabaseMySQLUserSettings{
|
||||
AuthPlugin: SQLAuthPluginNative,
|
||||
},
|
||||
}
|
||||
|
||||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(responseJSON)
|
||||
})
|
||||
|
||||
user, _, err := client.Databases.GetUser(ctx, dbID, userID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedUser, user)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue