|
| 1 | +// Package mssqlserver manages database servers and failover groups configuration |
| 2 | +package mssqlserver |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + |
| 7 | + "csbbrokerpakazure/acceptance-tests/helpers/environment" |
| 8 | + "csbbrokerpakazure/acceptance-tests/helpers/random" |
| 9 | + |
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 11 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" |
| 12 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 13 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources" |
| 14 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/sql/armsql" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + mainLocation = "westus" |
| 19 | +) |
| 20 | + |
| 21 | +// DatabaseServerPairCnf represents a pair of database servers |
| 22 | +type DatabaseServerPairCnf struct { |
| 23 | + ServerPairTag string |
| 24 | + Username string `json:"admin_username"` |
| 25 | + Password string `json:"admin_password"` |
| 26 | + PrimaryServer DatabaseServer `json:"primary"` |
| 27 | + SecondaryServer DatabaseServer `json:"secondary"` |
| 28 | + SecondaryResourceGroup string `json:"-"` |
| 29 | + ResourceGroup string `json:"-"` |
| 30 | +} |
| 31 | + |
| 32 | +// DatabaseServer represents a database server |
| 33 | +type DatabaseServer struct { |
| 34 | + Name string `json:"server_name"` |
| 35 | + ResourceGroup string `json:"resource_group"` |
| 36 | +} |
| 37 | + |
| 38 | +// NewDatabaseServerPairCnf creates a new database server pair configuration |
| 39 | +func NewDatabaseServerPairCnf(metadata environment.Metadata) DatabaseServerPairCnf { |
| 40 | + secondaryResourceGroup := random.Name(random.WithPrefix(metadata.ResourceGroup)) |
| 41 | + return DatabaseServerPairCnf{ |
| 42 | + ServerPairTag: random.Name(random.WithMaxLength(10)), |
| 43 | + Username: random.Name(random.WithMaxLength(10)), |
| 44 | + Password: random.Password(), |
| 45 | + PrimaryServer: DatabaseServer{ |
| 46 | + Name: random.Name(random.WithPrefix("server")), |
| 47 | + ResourceGroup: metadata.ResourceGroup, |
| 48 | + }, |
| 49 | + SecondaryServer: DatabaseServer{ |
| 50 | + Name: random.Name(random.WithPrefix("server")), |
| 51 | + ResourceGroup: secondaryResourceGroup, |
| 52 | + }, |
| 53 | + ResourceGroup: metadata.ResourceGroup, |
| 54 | + SecondaryResourceGroup: secondaryResourceGroup, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// PrimaryConfig returns the configuration for the primary database server |
| 59 | +func (d DatabaseServerPairCnf) PrimaryConfig() any { |
| 60 | + return d.memberConfig(d.PrimaryServer.Name, mainLocation, d.PrimaryServer.ResourceGroup) |
| 61 | +} |
| 62 | + |
| 63 | +// SecondaryConfig returns the configuration for the secondary database server |
| 64 | +func (d DatabaseServerPairCnf) SecondaryConfig() any { |
| 65 | + return d.memberConfig(d.SecondaryServer.Name, mainLocation, d.SecondaryServer.ResourceGroup) |
| 66 | +} |
| 67 | + |
| 68 | +// memberConfig returns the configuration for a database server |
| 69 | +func (d DatabaseServerPairCnf) memberConfig(name, location, rg string) any { |
| 70 | + return struct { |
| 71 | + Name string `json:"instance_name"` |
| 72 | + Username string `json:"admin_username"` |
| 73 | + Password string `json:"admin_password"` |
| 74 | + Location string `json:"location"` |
| 75 | + ResourceGroup string `json:"resource_group"` |
| 76 | + }{ |
| 77 | + Name: name, |
| 78 | + Username: d.Username, |
| 79 | + Password: d.Password, |
| 80 | + Location: location, |
| 81 | + ResourceGroup: rg, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// SecondaryResourceGroupConfig returns the configuration for the secondary resource group |
| 86 | +func (d DatabaseServerPairCnf) SecondaryResourceGroupConfig() any { |
| 87 | + return struct { |
| 88 | + InstanceName string `json:"instance_name"` |
| 89 | + Location string `json:"location"` |
| 90 | + }{ |
| 91 | + InstanceName: d.SecondaryResourceGroup, |
| 92 | + Location: mainLocation, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (d DatabaseServerPairCnf) ServerPairsConfig() any { |
| 97 | + return map[string]any{d.ServerPairTag: d} |
| 98 | +} |
| 99 | + |
| 100 | +// CreateServerPair creates a new database server pair |
| 101 | +func CreateServerPair(ctx context.Context, metadata environment.Metadata, subscriptionID string) (DatabaseServerPairCnf, error) { |
| 102 | + |
| 103 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 104 | + if err != nil { |
| 105 | + return DatabaseServerPairCnf{}, err |
| 106 | + } |
| 107 | + |
| 108 | + cnf := NewDatabaseServerPairCnf(metadata) |
| 109 | + |
| 110 | + if err := createServer(ctx, cred, cnf.PrimaryServer, cnf.Username, cnf.Password, subscriptionID); err != nil { |
| 111 | + return DatabaseServerPairCnf{}, err |
| 112 | + } |
| 113 | + |
| 114 | + if err := createFirewallRule(ctx, cred, cnf.PrimaryServer, subscriptionID); err != nil { |
| 115 | + return DatabaseServerPairCnf{}, err |
| 116 | + } |
| 117 | + |
| 118 | + if err := createResourceGroup(ctx, cred, cnf.SecondaryServer.ResourceGroup, subscriptionID); err != nil { |
| 119 | + return DatabaseServerPairCnf{}, err |
| 120 | + } |
| 121 | + |
| 122 | + if err := createServer(ctx, cred, cnf.SecondaryServer, cnf.Username, cnf.Password, subscriptionID); err != nil { |
| 123 | + return DatabaseServerPairCnf{}, err |
| 124 | + } |
| 125 | + |
| 126 | + if err := createFirewallRule(ctx, cred, cnf.SecondaryServer, subscriptionID); err != nil { |
| 127 | + return DatabaseServerPairCnf{}, err |
| 128 | + } |
| 129 | + |
| 130 | + return cnf, nil |
| 131 | +} |
| 132 | + |
| 133 | +func CreateFirewallRule(ctx context.Context, member DatabaseServer, subscriptionID string) error { |
| 134 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + return createFirewallRule(ctx, cred, member, subscriptionID) |
| 140 | +} |
| 141 | + |
| 142 | +func createFirewallRule(ctx context.Context, cred azcore.TokenCredential, member DatabaseServer, subscriptionID string) error { |
| 143 | + firewallClient, err := armsql.NewFirewallRulesClient(subscriptionID, cred, nil) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + _, err = firewallClient.CreateOrUpdate( |
| 149 | + ctx, |
| 150 | + member.ResourceGroup, |
| 151 | + member.Name, |
| 152 | + "firewallrule-"+member.Name, |
| 153 | + armsql.FirewallRule{ |
| 154 | + Properties: &armsql.ServerFirewallRuleProperties{ |
| 155 | + StartIPAddress: to.Ptr("0.0.0.0"), |
| 156 | + EndIPAddress: to.Ptr("0.0.0.0"), |
| 157 | + }, |
| 158 | + }, |
| 159 | + nil, |
| 160 | + ) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | + |
| 167 | +} |
| 168 | + |
| 169 | +func CleanFirewallRule(ctx context.Context, member DatabaseServer, subscriptionID string) error { |
| 170 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + return cleanupFirewallRule(ctx, cred, member, subscriptionID) |
| 176 | +} |
| 177 | + |
| 178 | +func cleanupFirewallRule(ctx context.Context, cred azcore.TokenCredential, member DatabaseServer, subscriptionID string) error { |
| 179 | + firewallClient, err := armsql.NewFirewallRulesClient(subscriptionID, cred, nil) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + |
| 184 | + _, err = firewallClient.Delete(ctx, member.ResourceGroup, member.Name, "firewallrule-"+member.Name, nil) |
| 185 | + if err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + |
| 189 | + return nil |
| 190 | +} |
| 191 | + |
| 192 | +func CleanupServer(ctx context.Context, member DatabaseServer, subscriptionID string) error { |
| 193 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 194 | + if err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + |
| 198 | + return cleanupServer(ctx, cred, member, subscriptionID) |
| 199 | +} |
| 200 | + |
| 201 | +func cleanupServer(ctx context.Context, cred azcore.TokenCredential, member DatabaseServer, subscriptionID string) error { |
| 202 | + |
| 203 | + serversClient, err := armsql.NewServersClient(subscriptionID, cred, nil) |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + |
| 208 | + pollerResp, err := serversClient.BeginDelete(ctx, member.ResourceGroup, member.Name, nil) |
| 209 | + if err != nil { |
| 210 | + return err |
| 211 | + } |
| 212 | + |
| 213 | + _, err = pollerResp.PollUntilDone(ctx, nil) |
| 214 | + if err != nil { |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
| 220 | + |
| 221 | +func Cleanup(ctx context.Context, cnf DatabaseServerPairCnf, subscriptionID string) error { |
| 222 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + |
| 227 | + if err := cleanupFirewallRule(ctx, cred, cnf.PrimaryServer, subscriptionID); err != nil { |
| 228 | + return err |
| 229 | + } |
| 230 | + |
| 231 | + if err := cleanupServer(ctx, cred, cnf.PrimaryServer, subscriptionID); err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + |
| 235 | + if err := cleanupFirewallRule(ctx, cred, cnf.SecondaryServer, subscriptionID); err != nil { |
| 236 | + return err |
| 237 | + } |
| 238 | + |
| 239 | + if err := cleanupServer(ctx, cred, cnf.SecondaryServer, subscriptionID); err != nil { |
| 240 | + return err |
| 241 | + } |
| 242 | + |
| 243 | + if err := cleanupResourceGroup(ctx, cred, cnf.SecondaryResourceGroup, subscriptionID); err != nil { |
| 244 | + return err |
| 245 | + } |
| 246 | + |
| 247 | + return nil |
| 248 | +} |
| 249 | + |
| 250 | +func cleanupResourceGroup(ctx context.Context, cred azcore.TokenCredential, resourceGroupName, subscriptionID string) error { |
| 251 | + resourceGroupClient, err := armresources.NewResourceGroupsClient(subscriptionID, cred, nil) |
| 252 | + if err != nil { |
| 253 | + return err |
| 254 | + } |
| 255 | + |
| 256 | + pollerResp, err := resourceGroupClient.BeginDelete(ctx, resourceGroupName, nil) |
| 257 | + if err != nil { |
| 258 | + return err |
| 259 | + } |
| 260 | + |
| 261 | + _, err = pollerResp.PollUntilDone(ctx, nil) |
| 262 | + if err != nil { |
| 263 | + return err |
| 264 | + } |
| 265 | + return nil |
| 266 | +} |
| 267 | + |
| 268 | +func CreateServer(ctx context.Context, member DatabaseServer, username, password, subscriptionID string) error { |
| 269 | + cred, err := azidentity.NewDefaultAzureCredential(nil) |
| 270 | + if err != nil { |
| 271 | + return err |
| 272 | + } |
| 273 | + |
| 274 | + return createServer(ctx, cred, member, username, password, subscriptionID) |
| 275 | +} |
| 276 | + |
| 277 | +func createServer(ctx context.Context, cred azcore.TokenCredential, member DatabaseServer, username, password, subscriptionID string) error { |
| 278 | + serversClient, err := armsql.NewServersClient(subscriptionID, cred, nil) |
| 279 | + if err != nil { |
| 280 | + return err |
| 281 | + } |
| 282 | + |
| 283 | + pollerResp, err := serversClient.BeginCreateOrUpdate( |
| 284 | + ctx, |
| 285 | + member.ResourceGroup, |
| 286 | + member.Name, |
| 287 | + armsql.Server{ |
| 288 | + Location: to.Ptr(mainLocation), |
| 289 | + Properties: &armsql.ServerProperties{ |
| 290 | + AdministratorLogin: to.Ptr(username), |
| 291 | + AdministratorLoginPassword: to.Ptr(password), |
| 292 | + }, |
| 293 | + }, |
| 294 | + nil, |
| 295 | + ) |
| 296 | + if err != nil { |
| 297 | + return err |
| 298 | + } |
| 299 | + |
| 300 | + _, err = pollerResp.PollUntilDone(ctx, nil) |
| 301 | + if err != nil { |
| 302 | + return err |
| 303 | + } |
| 304 | + |
| 305 | + return nil |
| 306 | +} |
| 307 | + |
| 308 | +func createResourceGroup(ctx context.Context, cred azcore.TokenCredential, resourceGroupName, subscriptionID string) error { |
| 309 | + resourceGroupClient, err := armresources.NewResourceGroupsClient(subscriptionID, cred, nil) |
| 310 | + if err != nil { |
| 311 | + return err |
| 312 | + } |
| 313 | + |
| 314 | + _, err = resourceGroupClient.CreateOrUpdate(ctx, resourceGroupName, armresources.ResourceGroup{Location: to.Ptr(mainLocation)}, nil) |
| 315 | + if err != nil { |
| 316 | + return err |
| 317 | + } |
| 318 | + |
| 319 | + return nil |
| 320 | +} |
0 commit comments