2

I need to make a call using Azure REST API to get the list of databases and delete the particular database if exits.

If somebody could give me a shell script that would be helpful. I tried this to list Azure SQL Databases on a given server

wget -U --no-check-certificate    https://management.core.windows.net:8443/  subscriptionID/services/sqlservers/servers/serverName/p1y/databases? contentview=generic
--2016-01-22 22:15:57--     https://management.core.windows.net:8443/${subscriptionID}/services/sqlservers/s    ervers/${serverName}/databases?contentview=generic
Resolving management.core.windows.net (management.core.windows.net)...xx.xxx.xxx.xxx
Connecting to management.core.windows.net (management.core.windows.net)|xx.xxx.xxx.xxx|:8443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2016-01-22 22:15:57 ERROR 403: Forbidden.

1 Answer 1

1

I think you can use cURL instead of wget. But, before you use cURL, you need to create and upload a certificate.

First, you need to install openssl and curl in your machine. And then you can create a .pem file with the following command:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout azure-cert.pem -out azure-cert.pem

Note: openssl will ask you to input a lot of things.

this is for local usage. From the .pem file, you can create a .cer file which will be uploaded to Azure. Use the following command to create one.

openssl x509 -outform der -in azure-cert.pem -out azure-cert.cer

Go the the classic portal of Azure, in the Management certificates page of Settings, click upload, and choose the .cer file created above.

Wait for a few seconds, and after the certificate is created, you can use the following command to get a list of SQL Database in some SQL server.

curl -E azure-cert.pem -H "x-ms-version: 2011-10-01" "https://management.core.windows.net:8443/{subscriptionId}/services/sqlservers/servers/{serverName}/databases?contentview=generic"

If you want to delete an SQL Database, you can use the following command.

curl -X DELETE -E azure-cert.pem -H "x-ms-version: 2011-10-01" "https://management.core.windows.net:8443/{subscriptionId}/services/sqlservers/servers/{serverName}/databases/{databaseName}"

The output of these commands are XML code which is hard to read. So, I recommend you to use Azure CLI instead. But, if you insist, and need more information about cURL with HTTP Jobs, see here. If you need more information about Azure REST API, see here

Sign up to request clarification or add additional context in comments.

14 Comments

For deleting the database I can use the curl command directly right? this doesnot need the output in readable form.
Yes. For a simple deletion operation, no need to read those XML. Actually, The deletion return nothing at all. But, for debugging, reading XML is going to be a nightmare. I'm not sure your purpose of this. If just automating those operations, I strongly recommend you to use Azure CLI. Unless, you want to reuse some previous written codes which involve with XML, and those codes are hard to modify.
Thankyou so much it works for me, I am not allowed to install azure cli tool on the server, I know azure cli makes my work very easy :( I have one more question when i provide the cert.pem path it doesnot work if i curl it from the cert.pem location it work, How do i use the command if i want to provide the path like "curl -X DELETE -E /etc/ssl/azure-cert.pem .....
Make sure the current user have permission to the location of your .pem file. (For example, in Ubuntu, try the command with sudo.) I tried it at my end. It works with absolute path. If is not a problem of permission, please provide the error message, so that I can further investigate.
` sudo curl -E /etc/ssl/private/prod.pem -H "x-ms-version: 2011-10-01" "management.core.windows.net:8443/xxxxxxxxxxxxxx/s ervices/sqlservers/servers/xxxxxxx/databases?contentview=generic" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""w3.org/TR/html4/strict.dtd"> <HTML><HEAD><TITLE>Bad Request</TITLE> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> <BODY><h2>Bad Request</h2> <hr><p>HTTP Error 400. The request is badly formed.</p> </BODY></HTML>`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.