Skip to content

Commit 41e83c0

Browse files
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]>
1 parent f27a98d commit 41e83c0

File tree

5 files changed

+424
-36
lines changed

5 files changed

+424
-36
lines changed

go.sum

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,28 @@ github.com/Azure/azure-sdk-for-go v46.4.0+incompatible/go.mod h1:9XXNKU+eRnpl9mo
1717
github.com/Azure/azure-storage-blob-go v0.0.0-20181023070848-cf01652132cc h1:BElWmFfsryQD72OcovStKpkIcd4e9ozSkdsTNQDSHGk=
1818
github.com/Azure/azure-storage-blob-go v0.0.0-20181023070848-cf01652132cc/go.mod h1:oGfmITT1V6x//CswqY2gtAHND+xIP64/qL7a5QJix0Y=
1919
github.com/Azure/go-autorest v11.0.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
20-
github.com/Azure/go-autorest v11.1.1+incompatible h1:kqw9PTHZBZKk6kSv/S7L/qxKKcz6hBDnmjWJU5RnHTw=
2120
github.com/Azure/go-autorest v11.1.1+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
22-
github.com/Azure/go-autorest v13.3.2+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
2321
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
2422
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
25-
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
26-
github.com/Azure/go-autorest/autorest v0.9.3/go.mod h1:GsRuLYvwzLjjjRoWEIyMUaYq8GNUx2nRB378IPt/1p0=
27-
github.com/Azure/go-autorest/autorest v0.9.4 h1:1cM+NmKw91+8h5vfjgzK4ZGLuN72k87XVZBWyGwNjUM=
28-
github.com/Azure/go-autorest/autorest v0.9.4/go.mod h1:GsRuLYvwzLjjjRoWEIyMUaYq8GNUx2nRB378IPt/1p0=
2923
github.com/Azure/go-autorest/autorest v0.11.9/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
3024
github.com/Azure/go-autorest/autorest v0.11.10 h1:j5sGbX7uj1ieYYkQ3Mpvewd4DCsEQ+ZeJpqnSM9pjnM=
3125
github.com/Azure/go-autorest/autorest v0.11.10/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
32-
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
33-
github.com/Azure/go-autorest/autorest/adal v0.8.0/go.mod h1:Z6vX6WXXuyieHAXwMj0S6HY6e6wcHn37qQMBQlvY3lc=
34-
github.com/Azure/go-autorest/autorest/adal v0.8.1 h1:pZdL8o72rK+avFWl+p9nE8RWi1JInZrWJYlnpfXJwHk=
35-
github.com/Azure/go-autorest/autorest/adal v0.8.1/go.mod h1:ZjhuQClTqx435SRJ2iMlOxPYt3d2C/T/7TiQCVZSn3Q=
3626
github.com/Azure/go-autorest/autorest/adal v0.9.5 h1:Y3bBUV4rTuxenJJs41HU3qmqsb+auo+a3Lz+PlJPpL0=
3727
github.com/Azure/go-autorest/autorest/adal v0.9.5/go.mod h1:B7KF7jKIeC9Mct5spmyCB/A8CG/sEz1vwIRGv/bbw7A=
38-
github.com/Azure/go-autorest/autorest/azure/auth v0.4.2 h1:iM6UAvjR97ZIeR93qTcwpKNMpV+/FTWjwEbuPD495Tk=
39-
github.com/Azure/go-autorest/autorest/azure/auth v0.4.2/go.mod h1:90gmfKdlmKgfjUpnCEpOJzsUEjrWDSLwHIG73tSXddM=
4028
github.com/Azure/go-autorest/autorest/azure/auth v0.5.3 h1:lZifaPRAk1bqg5vGqreL6F8uLC5V0fDpY8nFvc3boFc=
4129
github.com/Azure/go-autorest/autorest/azure/auth v0.5.3/go.mod h1:4bJZhUhcq8LB20TruwHbAQsmUs2Xh+QR7utuJpLXX3A=
42-
github.com/Azure/go-autorest/autorest/azure/cli v0.3.1 h1:LXl088ZQlP0SBppGFsRZonW6hSvwgL5gRByMbvUbx8U=
43-
github.com/Azure/go-autorest/autorest/azure/cli v0.3.1/go.mod h1:ZG5p860J94/0kI9mNJVoIoLgXcirM2gF5i2kWloofxw=
4430
github.com/Azure/go-autorest/autorest/azure/cli v0.4.2 h1:dMOmEJfkLKW/7JsokJqkyoYSgmR08hi9KrhjZb+JALY=
4531
github.com/Azure/go-autorest/autorest/azure/cli v0.4.2/go.mod h1:7qkJkT+j6b+hIpzMOwPChJhTqS8VbsqqgULzMNRugoM=
46-
github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
47-
github.com/Azure/go-autorest/autorest/date v0.2.0 h1:yW+Zlqf26583pE43KhfnhFcdmSWlm5Ew6bxipnr/tbM=
48-
github.com/Azure/go-autorest/autorest/date v0.2.0/go.mod h1:vcORJHLJEh643/Ioh9+vPmf1Ij9AEBM5FuBIXLmIy0g=
4932
github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
5033
github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
51-
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
52-
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
53-
github.com/Azure/go-autorest/autorest/mocks v0.3.0 h1:qJumjCaCudz+OcqE9/XtEPfvtOjOmKaui4EOpFI6zZc=
54-
github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN3SVSiiO77gL2j2ronKKP0syM=
34+
github.com/Azure/go-autorest/autorest/mocks v0.4.1 h1:K0laFcLE6VLTOwNgSxaGbUcLPuGXlNkbVvq4cW4nIHk=
5535
github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
56-
github.com/Azure/go-autorest/autorest/to v0.3.0 h1:zebkZaadz7+wIQYgC7GXaz3Wb28yKYfVkkBKwc38VF8=
57-
github.com/Azure/go-autorest/autorest/to v0.3.0/go.mod h1:MgwOyqaIuKdG4TL/2ywSsIWKAfJfgHDo8ObuUk3t5sA=
5836
github.com/Azure/go-autorest/autorest/to v0.4.0 h1:oXVqrxakqqV1UZdSazDOPOLvOIz+XA683u8EctwboHk=
5937
github.com/Azure/go-autorest/autorest/to v0.4.0/go.mod h1:fE8iZBn7LQR7zH/9XU2NcPR4o9jEImooCeWJcYV/zLE=
60-
github.com/Azure/go-autorest/autorest/validation v0.2.0 h1:15vMO4y76dehZSq7pAaOLQxC6dZYsSrj2GQpflyM/L4=
61-
github.com/Azure/go-autorest/autorest/validation v0.2.0/go.mod h1:3EEqHnBxQGHXRYq3HT1WyXAvT7LLY3tl70hw6tQIbjI=
6238
github.com/Azure/go-autorest/autorest/validation v0.3.0 h1:3I9AAI63HfcLtphd9g39ruUwRI+Ca+z/f36KHPFRUss=
6339
github.com/Azure/go-autorest/autorest/validation v0.3.0/go.mod h1:yhLgjC0Wda5DYXl6JAsWyUe4KVNffhoDhG0zVzUMo3E=
64-
github.com/Azure/go-autorest/logger v0.1.0 h1:ruG4BSDXONFRrZZJ2GUXDiUyVpayPmb1GnWeHDdaNKY=
65-
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
6640
github.com/Azure/go-autorest/logger v0.2.0 h1:e4RVHVZKC5p6UANLJHkM4OfR1UKZPj8Wt8Pcx+3oqrE=
6741
github.com/Azure/go-autorest/logger v0.2.0/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
68-
github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k=
69-
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
7042
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
7143
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
7244
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
@@ -82,7 +54,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
8254
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8355
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 h1:tkum0XDgfR0jcVVXuTsYv/erY2NnEDqwRojbxR1rBYA=
8456
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
85-
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
8657
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
8758
github.com/dimchansky/utfbom v1.0.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
8859
github.com/dimchansky/utfbom v1.1.0 h1:FcM3g+nofKgUteL8dm/UpdRXNC9KmADgTpLKsu0TRo4=
@@ -114,9 +85,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
11485
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
11586
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
11687
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
117-
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
11888
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
119-
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
12089
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
12190
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
12291
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -154,7 +123,6 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
154123
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
155124
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
156125
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
157-
github.com/openzipkin/zipkin-go v0.1.6 h1:yXiysv1CSK7Q5yjGy1710zZGnsbMUIjluWBxtLXHPBo=
158126
github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw=
159127
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
160128
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -191,10 +159,7 @@ go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
191159
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
192160
golang.org/x/crypto v0.0.0-20181001203147-e3636079e1a4/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
193161
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
194-
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c h1:Vj5n4GlwjmQteupaxJ9+0FNOmBrHfq7vN4btdGoDZgI=
195162
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
196-
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g=
197-
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
198163
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
199164
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
200165
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

postgresql/.env.tpl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
AZURE_BASE_GROUP_NAME=az-samples-go
2+
AZURE_LOCATION_DEFAULT=westus2
3+
AZURE_SAMPLES_KEEP_RESOURCES=0
4+
5+
# create with:
6+
# `az ad sp create-for-rbac --name 'my-sp' --output json`
7+
# sp must have Contributor role on subscription
8+
AZURE_TENANT_ID=
9+
AZURE_CLIENT_ID=
10+
AZURE_CLIENT_SECRET=
11+
AZURE_SUBSCRIPTION_ID=
12+
13+
# create with:
14+
# `az ad sp create-for-rbac --name 'my-sp' --sdk-auth > $HOME/.azure/sdk_auth.json`
15+
# sp must have Contributor role on subscription
16+
AZURE_AUTH_LOCATION=$HOME/.azure/sdk_auth.json
17+
18+
AZURE_STORAGE_ACCOUNT_NAME=
19+
AZURE_STORAGE_ACCOUNT_GROUP_NAME=

postgresql/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
services: postgresql
3+
platforms: go
4+
author: gechris
5+
---
6+
7+
# Azure PostgreSQL Samples
8+
9+
This package demonstrates how to manage PostgreSQL flexible servers with the Go SDK.
10+
11+
## Contents
12+
13+
* [How to run all samples](#run)
14+
* Management
15+
* CreateServer - Create a PostgreSQL.
16+
* UpdateServer - Updates a PostgreSQL server.
17+
* DeleteServer - Deletes an existing PostgreSQL server.
18+
* CreateOrUpdateFirewallRules - Creates or updates a firewall rule on the server.
19+
* GetConfiguration - Get the configuration value that is set on the server.
20+
* UpdateConfiguration - Updates the configuration.
21+
22+
23+
<a id="run"></a>
24+
## How to run all samples
25+
26+
1. Get this package and all dependencies.
27+
28+
```bash
29+
export PROJECT=github.com/Azure-Samples/azure-sdk-for-go-samples/postgresql
30+
go get -u $PROJECT
31+
cd ${GOPATH}/src/${PROJECT}
32+
```
33+
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.
34+
35+
```bash
36+
AZURE_CLIENT_ID=
37+
AZURE_CLIENT_SECRET=
38+
AZURE_TENANT_ID=
39+
AZURE_SUBSCRIPTION_ID=
40+
AZURE_BASE_GROUP_NAME=
41+
AZURE_LOCATION_DEFAULT=westus2
42+
```
43+
44+
3. Run the tests: `go test -v -timeout 12h`
45+
46+
The timeout is optional, but some tests take longer than then default 10m to complete.
47+
48+
<a id="info"></a>
49+
## More information
50+
51+
Please refer to [Azure SDK for Go](https://github.com/Azure/azure-sdk-for-go)
52+
for more information.
53+
54+
---
55+
56+
This project has adopted the [Microsoft Open Source Code of
57+
Conduct](https://opensource.microsoft.com/codeofconduct/). For more information
58+
see the [Code of Conduct
59+
FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
60+
[[email protected]](mailto:[email protected]) with any additional
61+
questions or comments.

postgresql/postgresql_client.go

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
 (0)