Skip to content

Commit 3928dca

Browse files
committed
Add support for PostgreSQL flexible server
1 parent 7fcb7df commit 3928dca

File tree

5 files changed

+279
-40
lines changed

5 files changed

+279
-40
lines changed

lib/Utils/FirewallUtils/ResourceManager.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,27 @@ class AzurePSQLResourceManager {
4646
getPSQLServer() {
4747
return this._resource;
4848
}
49-
_populatePSQLServerData(serverName) {
49+
_getPSQLServer(serverType, apiVersion, serverName) {
5050
return __awaiter(this, void 0, void 0, function* () {
51-
// trim the cloud hostname suffix from servername
52-
serverName = serverName.split('.')[0];
5351
const httpRequest = {
5452
method: 'GET',
55-
uri: this._restClient.getRequestUri('//subscriptions/{subscriptionId}/providers/Microsoft.DBforPostgreSQL/servers', {}, [], '2017-12-01')
53+
uri: this._restClient.getRequestUri(`//subscriptions/{subscriptionId}/providers/Microsoft.DBforPostgreSQL/${serverType}`, {}, [], apiVersion)
5654
};
57-
core.debug(`Get PSQL server '${serverName}' details`);
55+
core.debug(`Get '${serverName}' for PSQL ${serverType} details`);
5856
try {
5957
const httpResponse = yield this._restClient.beginRequest(httpRequest);
6058
if (httpResponse.statusCode !== 200) {
6159
throw AzureRestClient_1.ToError(httpResponse);
6260
}
63-
const sqlServers = httpResponse.body && httpResponse.body.value;
64-
if (sqlServers && sqlServers.length > 0) {
65-
this._resource = sqlServers.filter((sqlResource) => sqlResource.name.toLowerCase() === serverName.toLowerCase())[0];
66-
if (!this._resource) {
67-
throw new Error(`Unable to get details of PSQL server ${serverName}. PSQL server '${serverName}' was not found in the subscription.`);
68-
}
69-
core.debug(JSON.stringify(this._resource));
70-
}
71-
else {
72-
throw new Error(`Unable to get details of PSQL server ${serverName}. No PSQL servers were found in the subscription.`);
61+
const sqlServers = ((httpResponse.body && httpResponse.body.value) || []);
62+
const sqlServer = sqlServers.find((sqlResource) => sqlResource.name.toLowerCase() === serverName.toLowerCase());
63+
if (sqlServer) {
64+
this._serverType = serverType;
65+
this._apiVersion = apiVersion;
66+
this._resource = sqlServer;
67+
return true;
7368
}
69+
return false;
7470
}
7571
catch (error) {
7672
if (error instanceof AzureRestClient_1.AzureError) {
@@ -80,12 +76,22 @@ class AzurePSQLResourceManager {
8076
}
8177
});
8278
}
79+
_populatePSQLServerData(serverName) {
80+
return __awaiter(this, void 0, void 0, function* () {
81+
// trim the cloud hostname suffix from servername
82+
serverName = serverName.split('.')[0];
83+
(yield this._getPSQLServer('servers', '2017-12-01', serverName)) || (yield this._getPSQLServer('flexibleServers', '2021-06-01', serverName));
84+
if (!this._resource) {
85+
throw new Error(`Unable to get details of PSQL server ${serverName}. PSQL server '${serverName}' was not found in the subscription.`);
86+
}
87+
});
88+
}
8389
addFirewallRule(startIpAddress, endIpAddress) {
8490
return __awaiter(this, void 0, void 0, function* () {
8591
const firewallRuleName = `ClientIPAddress_${Date.now()}`;
8692
const httpRequest = {
8793
method: 'PUT',
88-
uri: this._restClient.getRequestUri(`/${this._resource.id}/firewallRules/${firewallRuleName}`, {}, [], '2017-12-01'),
94+
uri: this._restClient.getRequestUri(`/${this._resource.id}/firewallRules/${firewallRuleName}`, {}, [], this._apiVersion),
8995
body: JSON.stringify({
9096
'properties': {
9197
'startIpAddress': startIpAddress,
@@ -122,7 +128,7 @@ class AzurePSQLResourceManager {
122128
return __awaiter(this, void 0, void 0, function* () {
123129
const httpRequest = {
124130
method: 'GET',
125-
uri: this._restClient.getRequestUri(`/${this._resource.id}/firewallRules/${ruleName}`, {}, [], '2017-12-01')
131+
uri: this._restClient.getRequestUri(`/${this._resource.id}/firewallRules/${ruleName}`, {}, [], this._apiVersion)
126132
};
127133
try {
128134
const httpResponse = yield this._restClient.beginRequest(httpRequest);
@@ -143,7 +149,7 @@ class AzurePSQLResourceManager {
143149
return __awaiter(this, void 0, void 0, function* () {
144150
const httpRequest = {
145151
method: 'DELETE',
146-
uri: this._restClient.getRequestUri(`/${this._resource.id}/firewallRules/${firewallRule.name}`, {}, [], '2017-12-01')
152+
uri: this._restClient.getRequestUri(`/${this._resource.id}/firewallRules/${firewallRule.name}`, {}, [], this._apiVersion)
147153
};
148154
try {
149155
const httpResponse = yield this._restClient.beginRequest(httpRequest);

lib/Utils/PsqlUtils/PsqlUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PsqlUtils {
3232
// "SELECT 1" psql command is run to check if psql client is able to connect to DB using the connectionString
3333
try {
3434
yield PsqlToolRunner_1.default.init();
35-
yield PsqlToolRunner_1.default.executePsqlCommand(connectionString, Constants_1.PsqlConstants.SELECT_1, options);
35+
yield PsqlToolRunner_1.default.executePsqlCommand(`${connectionString} connect_timeout=10`, Constants_1.PsqlConstants.SELECT_1, options);
3636
}
3737
catch (_b) {
3838
if (psqlError) {

src/Utils/FirewallUtils/ResourceManager.ts

+27-19
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,29 @@ export default class AzurePSQLResourceManager {
6161
return this._resource;
6262
}
6363

64-
private async _populatePSQLServerData(serverName: string) {
65-
// trim the cloud hostname suffix from servername
66-
serverName = serverName.split('.')[0];
64+
private async _getPSQLServer(serverType: string, apiVersion: string, serverName: string) {
6765
const httpRequest: WebRequest = {
6866
method: 'GET',
69-
uri: this._restClient.getRequestUri('//subscriptions/{subscriptionId}/providers/Microsoft.DBforPostgreSQL/servers', {}, [], '2017-12-01')
67+
uri: this._restClient.getRequestUri(`//subscriptions/{subscriptionId}/providers/Microsoft.DBforPostgreSQL/${serverType}`, {}, [], apiVersion)
7068
}
7169

72-
core.debug(`Get PSQL server '${serverName}' details`);
70+
core.debug(`Get '${serverName}' for PSQL ${serverType} details`);
7371
try {
7472
const httpResponse = await this._restClient.beginRequest(httpRequest);
7573
if (httpResponse.statusCode !== 200) {
7674
throw ToError(httpResponse);
7775
}
7876

79-
const sqlServers = httpResponse.body && httpResponse.body.value as AzurePSQLServer[];
80-
if (sqlServers && sqlServers.length > 0) {
81-
this._resource = sqlServers.filter((sqlResource) => sqlResource.name.toLowerCase() === serverName.toLowerCase())[0];
82-
if (!this._resource) {
83-
throw new Error(`Unable to get details of PSQL server ${serverName}. PSQL server '${serverName}' was not found in the subscription.`);
84-
}
85-
86-
core.debug(JSON.stringify(this._resource));
87-
}
88-
else {
89-
throw new Error(`Unable to get details of PSQL server ${serverName}. No PSQL servers were found in the subscription.`);
77+
const sqlServers = ((httpResponse.body && httpResponse.body.value) || []) as AzurePSQLServer[];
78+
const sqlServer = sqlServers.find((sqlResource) => sqlResource.name.toLowerCase() === serverName.toLowerCase());
79+
if (sqlServer) {
80+
this._serverType = serverType;
81+
this._apiVersion = apiVersion;
82+
this._resource = sqlServer;
83+
return true;
9084
}
85+
86+
return false;
9187
}
9288
catch(error) {
9389
if (error instanceof AzureError) {
@@ -98,11 +94,21 @@ export default class AzurePSQLResourceManager {
9894
}
9995
}
10096

97+
private async _populatePSQLServerData(serverName: string) {
98+
// trim the cloud hostname suffix from servername
99+
serverName = serverName.split('.')[0];
100+
101+
(await this._getPSQLServer('servers', '2017-12-01', serverName)) || (await this._getPSQLServer('flexibleServers', '2021-06-01', serverName));
102+
if (!this._resource) {
103+
throw new Error(`Unable to get details of PSQL server ${serverName}. PSQL server '${serverName}' was not found in the subscription.`);
104+
}
105+
}
106+
101107
public async addFirewallRule(startIpAddress: string, endIpAddress: string): Promise<FirewallRule> {
102108
const firewallRuleName = `ClientIPAddress_${Date.now()}`;
103109
const httpRequest: WebRequest = {
104110
method: 'PUT',
105-
uri: this._restClient.getRequestUri(`/${this._resource!.id}/firewallRules/${firewallRuleName}`, {}, [], '2017-12-01'),
111+
uri: this._restClient.getRequestUri(`/${this._resource!.id}/firewallRules/${firewallRuleName}`, {}, [], this._apiVersion),
106112
body: JSON.stringify({
107113
'properties': {
108114
'startIpAddress': startIpAddress,
@@ -141,7 +147,7 @@ export default class AzurePSQLResourceManager {
141147
public async getFirewallRule(ruleName: string): Promise<FirewallRule> {
142148
const httpRequest: WebRequest = {
143149
method: 'GET',
144-
uri: this._restClient.getRequestUri(`/${this._resource!.id}/firewallRules/${ruleName}`, {}, [], '2017-12-01')
150+
uri: this._restClient.getRequestUri(`/${this._resource!.id}/firewallRules/${ruleName}`, {}, [], this._apiVersion)
145151
};
146152

147153
try {
@@ -164,7 +170,7 @@ export default class AzurePSQLResourceManager {
164170
public async removeFirewallRule(firewallRule: FirewallRule): Promise<void> {
165171
const httpRequest: WebRequest = {
166172
method: 'DELETE',
167-
uri: this._restClient.getRequestUri(`/${this._resource!.id}/firewallRules/${firewallRule.name}`, {}, [], '2017-12-01')
173+
uri: this._restClient.getRequestUri(`/${this._resource!.id}/firewallRules/${firewallRule.name}`, {}, [], this._apiVersion)
168174
};
169175

170176
try {
@@ -228,6 +234,8 @@ export default class AzurePSQLResourceManager {
228234
});
229235
}
230236

237+
private _serverType?: string;
238+
private _apiVersion?: string;
231239
private _resource?: AzurePSQLServer;
232240
private _restClient: AzureRestClient;
233241
}

src/Utils/PsqlUtils/PsqlUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ export default class PsqlUtils {
1414
},
1515
silent: true
1616
};
17+
1718
// "SELECT 1" psql command is run to check if psql client is able to connect to DB using the connectionString
1819
try {
1920
await PsqlToolRunner.init();
20-
await PsqlToolRunner.executePsqlCommand(connectionString, PsqlConstants.SELECT_1, options);
21+
await PsqlToolRunner.executePsqlCommand(`${connectionString} connect_timeout=10`, PsqlConstants.SELECT_1, options);
2122
} catch {
2223
if (psqlError) {
2324
const http = new HttpClient();
@@ -31,7 +32,6 @@ export default class PsqlUtils {
3132
}
3233
return ipAddress;
3334
}
34-
3535
}
3636

3737
export interface IPResponse {

0 commit comments

Comments
 (0)