|
| 1 | +package postgresql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config" |
| 8 | + "github.com/Azure-Samples/azure-sdk-for-go-samples/internal/iam" |
| 9 | + flexibleservers "github.com/Azure/azure-sdk-for-go/services/preview/postgresql/mgmt/2020-02-14-preview/postgresqlflexibleservers" |
| 10 | + "github.com/Azure/go-autorest/autorest" |
| 11 | + "github.com/Azure/go-autorest/autorest/to" |
| 12 | +) |
| 13 | + |
| 14 | +// GetServersClient returns |
| 15 | +func getServersClient() flexibleservers.ServersClient { |
| 16 | + serversClient := flexibleservers.NewServersClient(config.SubscriptionID()) |
| 17 | + a, _ := iam.GetResourceManagementAuthorizer() |
| 18 | + serversClient.Authorizer = a |
| 19 | + serversClient.AddToUserAgent(config.UserAgent()) |
| 20 | + return serversClient |
| 21 | +} |
| 22 | + |
| 23 | +// CreateServer creates a new PostgreSQL Server |
| 24 | +func CreateServer(ctx context.Context, resourceGroup, serverName, dbLogin, dbPassword string) (server flexibleservers.Server, err error) { |
| 25 | + serversClient := getServersClient() |
| 26 | + |
| 27 | + // Create the server |
| 28 | + future, err := serversClient.Create( |
| 29 | + ctx, |
| 30 | + resourceGroup, |
| 31 | + serverName, |
| 32 | + flexibleservers.Server{ |
| 33 | + Location: to.StringPtr(config.Location()), |
| 34 | + Sku: &flexibleservers.Sku{ |
| 35 | + Name: to.StringPtr("Standard_D4s_v3"), |
| 36 | + Tier: "GeneralPurpose", |
| 37 | + }, |
| 38 | + ServerProperties: &flexibleservers.ServerProperties{ |
| 39 | + AdministratorLogin: to.StringPtr(dbLogin), |
| 40 | + AdministratorLoginPassword: to.StringPtr(dbPassword), |
| 41 | + Version: flexibleservers.OneTwo, |
| 42 | + StorageProfile: &flexibleservers.StorageProfile{ |
| 43 | + StorageMB: to.Int32Ptr(524288), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + return server, fmt.Errorf("cannot create pg server: %+v", err) |
| 50 | + } |
| 51 | + |
| 52 | + if err := future.WaitForCompletionRef(ctx, serversClient.Client); err != nil { |
| 53 | + return server, fmt.Errorf("cannot get the pg server create or update future response: %+v", err) |
| 54 | + } |
| 55 | + |
| 56 | + return future.Result(serversClient) |
| 57 | +} |
| 58 | + |
| 59 | +// UpdateServerStorageCapacity given the server name and the new storage capacity it updates the server's storage capacity. |
| 60 | +func UpdateServerStorageCapacity(ctx context.Context, resourceGroup, serverName string, storageCapacity int32) (server flexibleservers.Server, err error) { |
| 61 | + serversClient := getServersClient() |
| 62 | + |
| 63 | + future, err := serversClient.Update( |
| 64 | + ctx, |
| 65 | + resourceGroup, |
| 66 | + serverName, |
| 67 | + flexibleservers.ServerForUpdate{ |
| 68 | + ServerPropertiesForUpdate: &flexibleservers.ServerPropertiesForUpdate{ |
| 69 | + StorageProfile: &flexibleservers.StorageProfile{ |
| 70 | + StorageMB: &storageCapacity, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ) |
| 75 | + if err != nil { |
| 76 | + return server, fmt.Errorf("cannot update pg server: %+v", err) |
| 77 | + } |
| 78 | + |
| 79 | + if err := future.WaitForCompletionRef(ctx, serversClient.Client); err != nil { |
| 80 | + return server, fmt.Errorf("cannot get the pg server update future response: %+v", err) |
| 81 | + } |
| 82 | + |
| 83 | + return future.Result(serversClient) |
| 84 | +} |
| 85 | + |
| 86 | +// DeleteServer deletes the PostgreSQL server. |
| 87 | +func DeleteServer(ctx context.Context, resourceGroup, serverName string) (resp autorest.Response, err error) { |
| 88 | + serversClient := getServersClient() |
| 89 | + |
| 90 | + future, err := serversClient.Delete(ctx, resourceGroup, serverName) |
| 91 | + if err != nil { |
| 92 | + return resp, fmt.Errorf("cannot delete the pg server: %+v", err) |
| 93 | + } |
| 94 | + |
| 95 | + if err := future.WaitForCompletionRef(ctx, serversClient.Client); err != nil { |
| 96 | + return resp, fmt.Errorf("cannot get the pg server update future response: %+v", err) |
| 97 | + } |
| 98 | + |
| 99 | + return future.Result(serversClient) |
| 100 | +} |
| 101 | + |
| 102 | +// GetFwRulesClient returns the FirewallClient |
| 103 | +func getFwRulesClient() flexibleservers.FirewallRulesClient { |
| 104 | + fwrClient := flexibleservers.NewFirewallRulesClient(config.SubscriptionID()) |
| 105 | + a, _ := iam.GetResourceManagementAuthorizer() |
| 106 | + fwrClient.Authorizer = a |
| 107 | + fwrClient.AddToUserAgent(config.UserAgent()) |
| 108 | + return fwrClient |
| 109 | +} |
| 110 | + |
| 111 | +// CreateOrUpdateFirewallRule given the firewallname and new properties it updates the firewall rule. |
| 112 | +func CreateOrUpdateFirewallRule(ctx context.Context, resourceGroup, serverName, firewallRuleName, startIPAddr, endIPAddr string) (rule flexibleservers.FirewallRule, err error) { |
| 113 | + fwrClient := getFwRulesClient() |
| 114 | + |
| 115 | + future, err := fwrClient.CreateOrUpdate( |
| 116 | + ctx, |
| 117 | + resourceGroup, |
| 118 | + serverName, |
| 119 | + firewallRuleName, |
| 120 | + flexibleservers.FirewallRule{ |
| 121 | + FirewallRuleProperties: &flexibleservers.FirewallRuleProperties{ |
| 122 | + StartIPAddress: &startIPAddr, |
| 123 | + EndIPAddress: &endIPAddr, |
| 124 | + }, |
| 125 | + }, |
| 126 | + ) |
| 127 | + if err != nil { |
| 128 | + return rule, fmt.Errorf("cannot create the firewall rule: %+v", err) |
| 129 | + } |
| 130 | + if err := future.WaitForCompletionRef(ctx, fwrClient.Client); err != nil { |
| 131 | + return rule, fmt.Errorf("cannot get the firewall rule create or update future response: %+v", err) |
| 132 | + } |
| 133 | + |
| 134 | + return future.Result(fwrClient) |
| 135 | +} |
| 136 | + |
| 137 | +// GetConfigurationsClient creates and returns the configuration client for the server. |
| 138 | +func getConfigurationsClient() flexibleservers.ConfigurationsClient { |
| 139 | + configClient := flexibleservers.NewConfigurationsClient(config.SubscriptionID()) |
| 140 | + a, _ := iam.GetResourceManagementAuthorizer() |
| 141 | + configClient.Authorizer = a |
| 142 | + configClient.AddToUserAgent(config.UserAgent()) |
| 143 | + return configClient |
| 144 | +} |
| 145 | + |
| 146 | +// GetConfiguration given the server name and configuration name it returns the configuration. |
| 147 | +func GetConfiguration(ctx context.Context, resourceGroup, serverName, configurationName string) (flexibleservers.Configuration, error) { |
| 148 | + configClient := getConfigurationsClient() |
| 149 | + return configClient.Get(ctx, resourceGroup, serverName, configurationName) |
| 150 | +} |
| 151 | + |
| 152 | +// UpdateConfiguration given the name of the configuation and the configuration object it updates the configuration for the given server. |
| 153 | +func UpdateConfiguration(ctx context.Context, resourceGroup, serverName string, configurationName string, configuration flexibleservers.Configuration) (updatedConfig flexibleservers.Configuration, err error) { |
| 154 | + configClient := getConfigurationsClient() |
| 155 | + |
| 156 | + future, err := configClient.Update(ctx, resourceGroup, serverName, configurationName, configuration) |
| 157 | + if err != nil { |
| 158 | + return updatedConfig, fmt.Errorf("cannot update the configuration with name %s: %+v", configurationName, err) |
| 159 | + } |
| 160 | + |
| 161 | + if err := future.WaitForCompletionRef(ctx, configClient.Client); err != nil { |
| 162 | + return updatedConfig, fmt.Errorf("cannot get the pg configuration update future response: %+v", err) |
| 163 | + } |
| 164 | + |
| 165 | + return future.Result(configClient) |
| 166 | +} |
0 commit comments