-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add postgresql flexible servers samples (#284)
* Simple sample sceleton for mysql & postgresql * Test basic operations * Fixed issue with confusing git mod name between current and forked branch * Fix typo * Fix malformed Example_ test name * Change sample file names. Add README.md files * Properly delete old files, add necessary parameters for server creation * Update README.md files * Refactor FirewallRule call in the clients. Fix invalid name * Update postgresql sample to reflect newest sdk changes * Fix package names for mysql,postgresql flexible server samples * Add PostgreSQL samples * Change user to Azure * Revert some changes in the files * Revert groups.go * Fix module name * Change to Azure-Samples instead of Azure * Randomize the password to pass gosec * Refactor password generator * Fix Build error * Fix README.md, refactor sample * Styling fixes * Some final format refinement * Missed one error to output * Fix example test * Update output of the example Co-authored-by: Dapeng Zhang <[email protected]>
- Loading branch information
1 parent
f27a98d
commit 41e83c0
Showing
5 changed files
with
424 additions
and
36 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
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,19 @@ | ||
AZURE_BASE_GROUP_NAME=az-samples-go | ||
AZURE_LOCATION_DEFAULT=westus2 | ||
AZURE_SAMPLES_KEEP_RESOURCES=0 | ||
|
||
# create with: | ||
# `az ad sp create-for-rbac --name 'my-sp' --output json` | ||
# sp must have Contributor role on subscription | ||
AZURE_TENANT_ID= | ||
AZURE_CLIENT_ID= | ||
AZURE_CLIENT_SECRET= | ||
AZURE_SUBSCRIPTION_ID= | ||
|
||
# create with: | ||
# `az ad sp create-for-rbac --name 'my-sp' --sdk-auth > $HOME/.azure/sdk_auth.json` | ||
# sp must have Contributor role on subscription | ||
AZURE_AUTH_LOCATION=$HOME/.azure/sdk_auth.json | ||
|
||
AZURE_STORAGE_ACCOUNT_NAME= | ||
AZURE_STORAGE_ACCOUNT_GROUP_NAME= |
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,61 @@ | ||
--- | ||
services: postgresql | ||
platforms: go | ||
author: gechris | ||
--- | ||
|
||
# Azure PostgreSQL Samples | ||
|
||
This package demonstrates how to manage PostgreSQL flexible servers with the Go SDK. | ||
|
||
## Contents | ||
|
||
* [How to run all samples](#run) | ||
* Management | ||
* CreateServer - Create a PostgreSQL. | ||
* UpdateServer - Updates a PostgreSQL server. | ||
* DeleteServer - Deletes an existing PostgreSQL server. | ||
* CreateOrUpdateFirewallRules - Creates or updates a firewall rule on the server. | ||
* GetConfiguration - Get the configuration value that is set on the server. | ||
* UpdateConfiguration - Updates the configuration. | ||
|
||
|
||
<a id="run"></a> | ||
## How to run all samples | ||
|
||
1. Get this package and all dependencies. | ||
|
||
```bash | ||
export PROJECT=github.com/Azure-Samples/azure-sdk-for-go-samples/postgresql | ||
go get -u $PROJECT | ||
cd ${GOPATH}/src/${PROJECT} | ||
``` | ||
2. Create an Azure service principal with the [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli) command `az ad sp create-for-rbac --output json` and set the following environment variables per that command's output. You can also copy `.env.tpl` to `.env` and fill it in; the configuration system will utilize this. | ||
|
||
```bash | ||
AZURE_CLIENT_ID= | ||
AZURE_CLIENT_SECRET= | ||
AZURE_TENANT_ID= | ||
AZURE_SUBSCRIPTION_ID= | ||
AZURE_BASE_GROUP_NAME= | ||
AZURE_LOCATION_DEFAULT=westus2 | ||
``` | ||
|
||
3. Run the tests: `go test -v -timeout 12h` | ||
|
||
The timeout is optional, but some tests take longer than then default 10m to complete. | ||
|
||
<a id="info"></a> | ||
## More information | ||
|
||
Please refer to [Azure SDK for Go](https://github.com/Azure/azure-sdk-for-go) | ||
for more information. | ||
|
||
--- | ||
|
||
This project has adopted the [Microsoft Open Source Code of | ||
Conduct](https://opensource.microsoft.com/codeofconduct/). For more information | ||
see the [Code of Conduct | ||
FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact | ||
[[email protected]](mailto:[email protected]) with any additional | ||
questions or comments. |
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,166 @@ | ||
package postgresql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config" | ||
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/iam" | ||
flexibleservers "github.com/Azure/azure-sdk-for-go/services/preview/postgresql/mgmt/2020-02-14-preview/postgresqlflexibleservers" | ||
"github.com/Azure/go-autorest/autorest" | ||
"github.com/Azure/go-autorest/autorest/to" | ||
) | ||
|
||
// GetServersClient returns | ||
func getServersClient() flexibleservers.ServersClient { | ||
serversClient := flexibleservers.NewServersClient(config.SubscriptionID()) | ||
a, _ := iam.GetResourceManagementAuthorizer() | ||
serversClient.Authorizer = a | ||
serversClient.AddToUserAgent(config.UserAgent()) | ||
return serversClient | ||
} | ||
|
||
// CreateServer creates a new PostgreSQL Server | ||
func CreateServer(ctx context.Context, resourceGroup, serverName, dbLogin, dbPassword string) (server flexibleservers.Server, err error) { | ||
serversClient := getServersClient() | ||
|
||
// Create the server | ||
future, err := serversClient.Create( | ||
ctx, | ||
resourceGroup, | ||
serverName, | ||
flexibleservers.Server{ | ||
Location: to.StringPtr(config.Location()), | ||
Sku: &flexibleservers.Sku{ | ||
Name: to.StringPtr("Standard_D4s_v3"), | ||
Tier: "GeneralPurpose", | ||
}, | ||
ServerProperties: &flexibleservers.ServerProperties{ | ||
AdministratorLogin: to.StringPtr(dbLogin), | ||
AdministratorLoginPassword: to.StringPtr(dbPassword), | ||
Version: flexibleservers.OneTwo, | ||
StorageProfile: &flexibleservers.StorageProfile{ | ||
StorageMB: to.Int32Ptr(524288), | ||
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return server, fmt.Errorf("cannot create pg server: %+v", err) | ||
} | ||
|
||
if err := future.WaitForCompletionRef(ctx, serversClient.Client); err != nil { | ||
return server, fmt.Errorf("cannot get the pg server create or update future response: %+v", err) | ||
} | ||
|
||
return future.Result(serversClient) | ||
} | ||
|
||
// UpdateServerStorageCapacity given the server name and the new storage capacity it updates the server's storage capacity. | ||
func UpdateServerStorageCapacity(ctx context.Context, resourceGroup, serverName string, storageCapacity int32) (server flexibleservers.Server, err error) { | ||
serversClient := getServersClient() | ||
|
||
future, err := serversClient.Update( | ||
ctx, | ||
resourceGroup, | ||
serverName, | ||
flexibleservers.ServerForUpdate{ | ||
ServerPropertiesForUpdate: &flexibleservers.ServerPropertiesForUpdate{ | ||
StorageProfile: &flexibleservers.StorageProfile{ | ||
StorageMB: &storageCapacity, | ||
}, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return server, fmt.Errorf("cannot update pg server: %+v", err) | ||
} | ||
|
||
if err := future.WaitForCompletionRef(ctx, serversClient.Client); err != nil { | ||
return server, fmt.Errorf("cannot get the pg server update future response: %+v", err) | ||
} | ||
|
||
return future.Result(serversClient) | ||
} | ||
|
||
// DeleteServer deletes the PostgreSQL server. | ||
func DeleteServer(ctx context.Context, resourceGroup, serverName string) (resp autorest.Response, err error) { | ||
serversClient := getServersClient() | ||
|
||
future, err := serversClient.Delete(ctx, resourceGroup, serverName) | ||
if err != nil { | ||
return resp, fmt.Errorf("cannot delete the pg server: %+v", err) | ||
} | ||
|
||
if err := future.WaitForCompletionRef(ctx, serversClient.Client); err != nil { | ||
return resp, fmt.Errorf("cannot get the pg server update future response: %+v", err) | ||
} | ||
|
||
return future.Result(serversClient) | ||
} | ||
|
||
// GetFwRulesClient returns the FirewallClient | ||
func getFwRulesClient() flexibleservers.FirewallRulesClient { | ||
fwrClient := flexibleservers.NewFirewallRulesClient(config.SubscriptionID()) | ||
a, _ := iam.GetResourceManagementAuthorizer() | ||
fwrClient.Authorizer = a | ||
fwrClient.AddToUserAgent(config.UserAgent()) | ||
return fwrClient | ||
} | ||
|
||
// CreateOrUpdateFirewallRule given the firewallname and new properties it updates the firewall rule. | ||
func CreateOrUpdateFirewallRule(ctx context.Context, resourceGroup, serverName, firewallRuleName, startIPAddr, endIPAddr string) (rule flexibleservers.FirewallRule, err error) { | ||
fwrClient := getFwRulesClient() | ||
|
||
future, err := fwrClient.CreateOrUpdate( | ||
ctx, | ||
resourceGroup, | ||
serverName, | ||
firewallRuleName, | ||
flexibleservers.FirewallRule{ | ||
FirewallRuleProperties: &flexibleservers.FirewallRuleProperties{ | ||
StartIPAddress: &startIPAddr, | ||
EndIPAddress: &endIPAddr, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return rule, fmt.Errorf("cannot create the firewall rule: %+v", err) | ||
} | ||
if err := future.WaitForCompletionRef(ctx, fwrClient.Client); err != nil { | ||
return rule, fmt.Errorf("cannot get the firewall rule create or update future response: %+v", err) | ||
} | ||
|
||
return future.Result(fwrClient) | ||
} | ||
|
||
// GetConfigurationsClient creates and returns the configuration client for the server. | ||
func getConfigurationsClient() flexibleservers.ConfigurationsClient { | ||
configClient := flexibleservers.NewConfigurationsClient(config.SubscriptionID()) | ||
a, _ := iam.GetResourceManagementAuthorizer() | ||
configClient.Authorizer = a | ||
configClient.AddToUserAgent(config.UserAgent()) | ||
return configClient | ||
} | ||
|
||
// GetConfiguration given the server name and configuration name it returns the configuration. | ||
func GetConfiguration(ctx context.Context, resourceGroup, serverName, configurationName string) (flexibleservers.Configuration, error) { | ||
configClient := getConfigurationsClient() | ||
return configClient.Get(ctx, resourceGroup, serverName, configurationName) | ||
} | ||
|
||
// UpdateConfiguration given the name of the configuation and the configuration object it updates the configuration for the given server. | ||
func UpdateConfiguration(ctx context.Context, resourceGroup, serverName string, configurationName string, configuration flexibleservers.Configuration) (updatedConfig flexibleservers.Configuration, err error) { | ||
configClient := getConfigurationsClient() | ||
|
||
future, err := configClient.Update(ctx, resourceGroup, serverName, configurationName, configuration) | ||
if err != nil { | ||
return updatedConfig, fmt.Errorf("cannot update the configuration with name %s: %+v", configurationName, err) | ||
} | ||
|
||
if err := future.WaitForCompletionRef(ctx, configClient.Client); err != nil { | ||
return updatedConfig, fmt.Errorf("cannot get the pg configuration update future response: %+v", err) | ||
} | ||
|
||
return future.Result(configClient) | ||
} |
Oops, something went wrong.