postgresql

package
v0.0.0-...-57c00fc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2022 License: MIT Imports: 7 Imported by: 0

README


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
  • 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.

How to run all samples

  1. Get this package and all dependencies.
export PROJECT=github.com/Azure-Samples/azure-sdk-for-go-samples/postgresql
go get -u $PROJECT
cd ${GOPATH}/src/${PROJECT}
  1. Create an Azure service principal with the 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.
AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=
AZURE_TENANT_ID=
AZURE_SUBSCRIPTION_ID=
AZURE_BASE_GROUP_NAME=
AZURE_LOCATION_DEFAULT=westus2
  1. Run the tests: go test -v -timeout 12h

The timeout is optional, but some tests take longer than then default 10m to complete.

More information

Please refer to Azure SDK for Go for more information.


This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Documentation

Overview

Example (PerformServerOperations)

Example_performServerOperations creates a postgresql server, updates it, add firewall rules and configurations and at the end it deletes it.

serverName = strings.ToLower(serverName)

ctx := context.Background()
defer resources.Cleanup(ctx)

if _, err := resources.CreateGroup(ctx, groupName); err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("resource group created")

// Create the server.
if _, err := CreateServer(ctx, groupName, serverName, dbLogin, dbPassword); err != nil {
	util.LogAndPanic(fmt.Errorf("cannot create postgresql server: %+v", err))
}
util.PrintAndLog("postgresql server created")

// Update the server's storage capacity field.
if _, err := UpdateServerStorageCapacity(ctx, groupName, serverName, 1048576); err != nil {
	util.LogAndPanic(fmt.Errorf("cannot update postgresql server: %+v", err))
}
util.PrintAndLog("postgresql server's storage capacity updated")

if _, err := CreateOrUpdateFirewallRule(ctx, groupName, serverName, "FirewallRuleName", "0.0.0.0", "0.0.0.0"); err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("firewall rule set created")

if _, err := CreateOrUpdateFirewallRule(ctx, groupName, serverName, "FirewallRuleName", "0.0.0.0", "1.1.1.1"); err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("firewall rule updated")

var configuration flexibleservers.Configuration

configuration, err := GetConfiguration(ctx, groupName, serverName, "max_replication_slots")
if err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("got the max_replication_slots configuration")

// Update the configuration Value.
configuration.ConfigurationProperties.Value = to.StringPtr("20")
configuration.ConfigurationProperties.Source = to.StringPtr("user-override")

if _, err := UpdateConfiguration(ctx, groupName, serverName, "max_replication_slots", configuration); err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("max_replication_slots configuration updated")

// Finally delete the server.
if _, err := DeleteServer(ctx, groupName, serverName); err != nil {
	util.LogAndPanic(err)
}
util.PrintAndLog("postgresql server deleted")
Output:

resource group created
postgresql server created
postgresql server's storage capacity updated
firewall rule set created
firewall rule updated
got the max_replication_slots configuration
max_replication_slots configuration updated
postgresql server deleted

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateOrUpdateFirewallRule

func CreateOrUpdateFirewallRule(ctx context.Context, resourceGroup, serverName, firewallRuleName, startIPAddr, endIPAddr string) (rule flexibleservers.FirewallRule, err error)

CreateOrUpdateFirewallRule given the firewallname and new properties it updates the firewall rule.

func CreateServer

func CreateServer(ctx context.Context, resourceGroup, serverName, dbLogin, dbPassword string) (server flexibleservers.Server, err error)

CreateServer creates a new PostgreSQL Server

func DeleteServer

func DeleteServer(ctx context.Context, resourceGroup, serverName string) (resp autorest.Response, err error)

DeleteServer deletes the PostgreSQL server.

func GetConfiguration

func GetConfiguration(ctx context.Context, resourceGroup, serverName, configurationName string) (flexibleservers.Configuration, error)

GetConfiguration given the server name and configuration name it returns the configuration.

func UpdateConfiguration

func UpdateConfiguration(ctx context.Context, resourceGroup, serverName string, configurationName string, configuration flexibleservers.Configuration) (updatedConfig flexibleservers.Configuration, err error)

UpdateConfiguration given the name of the configuation and the configuration object it updates the configuration for the given server.

func UpdateServerStorageCapacity

func UpdateServerStorageCapacity(ctx context.Context, resourceGroup, serverName string, storageCapacity int32) (server flexibleservers.Server, err error)

UpdateServerStorageCapacity given the server name and the new storage capacity it updates the server's storage capacity.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL