11package postgres
22
33import (
4- "fmt"
5- "strings"
6-
74 "github.com/go-logr/logr"
85 "github.com/lib/pq"
96)
107
118type AzureType string
129
13- const (
14- // Azure Database for PostgreSQL Flexible Server uses default convention for login, but has not full superuser privileges
15- FLEXIBLE AzureType = "flexible"
16- // Azure Database for PostgreSQL Single Server uses <username>@<servername> convention
17- SINGLE AzureType = "single"
18- )
19-
2010type azurepg struct {
21- serverName string
22- azureType AzureType
2311 pg
2412}
2513
2614func newAzurePG (postgres * pg ) PG {
27- splitUser := strings .Split (postgres .user , "@" )
28- serverName := ""
29- azureType := FLEXIBLE
30- if len (splitUser ) > 1 {
31- // If a servername is found, we are using Azure Database for PostgreSQL Single Server
32- serverName = splitUser [1 ]
33- azureType = SINGLE
34- }
3515 return & azurepg {
36- serverName : serverName ,
37- azureType : azureType ,
38- pg : * postgres ,
16+ pg : * postgres ,
3917 }
4018}
4119
@@ -44,30 +22,12 @@ func (azpg *azurepg) CreateUserRole(role, password string) (string, error) {
4422 if err != nil {
4523 return "" , err
4624 }
47-
48- // For Flexible Server, just return the role name as-is
49- if azpg .azureType == FLEXIBLE {
50- return returnedRole , nil
51- }
52-
53- // For Single Server, format as <username>@<servername>
54- return fmt .Sprintf ("%s@%s" , returnedRole , azpg .serverName ), nil
55- }
56-
57- func (azpg * azurepg ) GetRoleForLogin (login string ) string {
58- // For Azure Flexible Server, the login name is the same as the role name
59- if azpg .azureType == FLEXIBLE {
60- return login
61- }
62-
63- // For Azure Single Server, extract the username part before the '@' symbol
64- splitUser := strings .Split (azpg .user , "@" )
65- return splitUser [0 ]
25+ return returnedRole , nil
6626}
6727
6828func (azpg * azurepg ) CreateDB (dbname , role string ) error {
6929 // This step is necessary before we can set the specified role as the database owner
70- err := azpg .GrantRole (role , azpg .GetRoleForLogin ( azpg . user ) )
30+ err := azpg .GrantRole (role , azpg .user )
7131 if err != nil {
7232 return err
7333 }
@@ -76,21 +36,15 @@ func (azpg *azurepg) CreateDB(dbname, role string) error {
7636}
7737
7838func (azpg * azurepg ) DropRole (role , newOwner , database string , logger logr.Logger ) error {
79- if azpg .azureType == FLEXIBLE {
80- // Grant the role to the user first
81- err := azpg .GrantRole (role , azpg .user )
82- if err != nil && err .(* pq.Error ).Code != "0LP01" {
83- if err .(* pq.Error ).Code == "42704" {
84- return nil
85- }
86- return err
39+ // Grant the role to the user first
40+ err := azpg .GrantRole (role , azpg .user )
41+ if err != nil && err .(* pq.Error ).Code != "0LP01" {
42+ if err .(* pq.Error ).Code == "42704" {
43+ return nil
8744 }
88-
89- // Delegate to parent implementation to perform the actual drop
90- return azpg .pg .DropRole (role , newOwner , database , logger )
45+ return err
9146 }
9247
93- // For Azure Single Server, format the new owner correctly
94- azNewOwner := azpg .GetRoleForLogin (newOwner )
95- return azpg .pg .DropRole (role , azNewOwner , database , logger )
48+ // Delegate to parent implementation to perform the actual drop
49+ return azpg .pg .DropRole (role , newOwner , database , logger )
9650}
0 commit comments