zuckermanori
zuckermanori

Reputation: 1755

Listing SQL Server Instances on Azure

I'm trying to implement code that uses the Azure Java SDK to list the SQL Server instances running on my Azure subscription.

I followed the examples posted and wrote the following code:

Azure azure  = Azure.authenticate(credentials)).withDefaultSubscription();
SqlServers sqlServers = azure.sqlServers();
PagedList<SqlServer> list = sqlServers.list();

But the last line throws a java.lang.NoSuchMethodError with the following stack trace:

Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeBindings.create(Ljava/lang/Class;[Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/type/TypeBindings;
at com.microsoft.rest.serializer.JacksonAdapter.constructJavaType(JacksonAdapter.java:119)
at com.microsoft.rest.serializer.JacksonAdapter.deserialize(JacksonAdapter.java:131)
at com.microsoft.rest.ServiceResponseBuilder.buildBody(ServiceResponseBuilder.java:216)
at com.microsoft.rest.ServiceResponseBuilder.build(ServiceResponseBuilder.java:110)
at com.microsoft.azure.AzureResponseBuilder.build(AzureResponseBuilder.java:56)
at com.microsoft.azure.management.sql.implementation.ServersInner.listDelegate(ServersInner.java:553)
at com.microsoft.azure.management.sql.implementation.ServersInner.access$400(ServersInner.java:42)
at com.microsoft.azure.management.sql.implementation.ServersInner$17.call(ServersInner.java:539)
at com.microsoft.azure.management.sql.implementation.ServersInner$17.call(ServersInner.java:535)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
at rx.Observable.unsafeSubscribe(Observable.java:10142)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.subscribe(Observable.java:10238)
at rx.Observable.subscribe(Observable.java:10205)
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:444)
at rx.observables.BlockingObservable.single(BlockingObservable.java:341)
at com.microsoft.azure.management.sql.implementation.ServersInner.list(ServersInner.java:488)
at com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.TopLevelModifiableResourcesImpl.list(TopLevelModifiableResourcesImpl.java:116)
at AzureDiscoveryClient.main(AzureDiscoveryClient.java:19)

This obviously looks like a defect as the SDK allows to do that and should return a reasonable error code if something goes wrong.

Still, is there any other way to list the SQL Server instances running in my Azure subscription? Or perhaps I'm doing something in my code?

Upvotes: 1

Views: 211

Answers (3)

zuckermanori
zuckermanori

Reputation: 1755

Problem solved!

The error came from an incompatibility between the Azure SDK and the jackson-databind library. One of the methods used from this library is relatively new, and the version I had in my project (clean project with just one import - Azure SDK) did not have this method.

Imported the latest jackson-databind as follows:

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.5</version>
</dependency>

Has solved the problem.

Thanks Everyone for the help.

Upvotes: 0

Jay Gong
Jay Gong

Reputation: 23782

Please refer to my working code as below:

import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.sql.SqlServer;
import com.microsoft.azure.management.sql.SqlServers;

import java.io.IOException;

public class ListSqlInstance {

    public static void main(String[] args) throws IOException {


        ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
                "{client Id}",
                "{talent Id}",
                "{secret}",
                AzureEnvironment.AZURE);
        Azure.Authenticated azureAuth = Azure.authenticate(credentials);
       Azure azure = azureAuth.withDefaultSubscription();

        SqlServers sqlServers = azure.sqlServers();
        PagedList<SqlServer> list = sqlServers.list();
        System.out.println(list.size());
    }
}

My sdk version:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    version>1.12.0</version>
</dependency>

Don't forget grant access sql server permissons to your client.

enter image description here

Hope it helps you.

Upvotes: 1

Avanish
Avanish

Reputation: 345

I am not sure if you are looking for a powershell cmdlet solution. But this Powershell cmdlet should list all the sql server instances in your subscription.

Assuming prior to running this powershell cmdlet you already logged in to your azure account in powershell and set the current subscription in the current powershell session if you have multiple subscriptions mapped to your account.

PS C:\azure> get-azurermresourcegroup | get-azurermsqlserver
ResourceGroupName : DbRG
ServerName : testSqlserver
Location : East US
SqlAdministratorLogin : XXXX
SqlAdministratorPassword : XXX
ServerVersion : 12.0
Tags :

Upvotes: 0

Related Questions