Java Code Examples for com.microsoft.azure.management.sql.SqlDatabase

The following examples show how to use com.microsoft.azure.management.sql.SqlDatabase. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: clouditor   Source File: AzureSQLDatabaseScanner.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected List<SqlDatabase> list() {
  var servers =
      new ArrayList<>(
          this.resourceGroup != null
              ? this.api.azure().sqlServers().listByResourceGroup(this.resourceGroup)
              : this.api.azure().sqlServers().list());

  List<SqlDatabase> databases = new ArrayList<>();

  for (var server : servers) {
    server
        .databases()
        .list()
        .forEach(
            database -> {
              // filter out master databases, since they are internal Azure structures
              if (!database.name().equals("master")) {
                databases.add(database);
              }
            });
  }

  return databases;
}
 
Example 2
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 6 votes vote down vote up
@Override
public Observable<SqlDatabase> listDatabasesAsync() {
    final SqlElasticPoolImpl self = this;
    return this.sqlServerManager.inner().databases()
        .listByElasticPoolAsync(self.resourceGroupName, self.sqlServerName, this.name())
        .flatMap(new Func1<List<DatabaseInner>, Observable<DatabaseInner>>() {
            @Override
            public Observable<DatabaseInner> call(List<DatabaseInner> databaseInners) {
                return Observable.from(databaseInners);
            }
        }).map(new Func1<DatabaseInner, SqlDatabase>() {
            @Override
            public SqlDatabase call(DatabaseInner databaseInner) {
                return new SqlDatabaseImpl(self.resourceGroupName, self.sqlServerName, self.sqlServerLocation, databaseInner.name(), databaseInner, self.sqlServerManager);
            }
        });
}
 
Example 3
Source Project: azure-libraries-for-java   Source File: SqlDatabaseImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void beforeGroupCreateOrUpdate() {
    if (this.importRequestInner != null && this.elasticPoolName() != null) {
        final SqlDatabaseImpl self = this;
        final String epName = this.elasticPoolName();
        this.addPostRunDependent(new FunctionalTaskItem() {
            @Override
            public Observable<Indexable> call(final Context context) {
                self.importRequestInner = null;
                self.withExistingElasticPool(epName);
                return self.createResourceAsync()
                    .flatMap(new Func1<SqlDatabase, Observable<Indexable>>() {
                        @Override
                        public Observable<Indexable> call(SqlDatabase sqlDatabase) {
                            return context.voidObservable();
                        }
                    });
            }
        });
    }
}
 
Example 4
@Override
public Observable<SqlDatabase> listDatabasesAsync() {
    final RecommendedElasticPoolImpl self = this;
    return this.sqlServer.manager().inner().databases().listByRecommendedElasticPoolAsync(
        this.sqlServer.resourceGroupName(),
        this.sqlServer.name(),
        this.name())
        .flatMap(new Func1<List<DatabaseInner>, Observable<DatabaseInner>>() {
            @Override
            public Observable<DatabaseInner> call(List<DatabaseInner> databaseInners) {
                return Observable.from(databaseInners);
            }
        }).map(new Func1<DatabaseInner, SqlDatabase>() {
            @Override
            public SqlDatabase call(DatabaseInner databaseInner) {
                return new SqlDatabaseImpl(databaseInner.name(), self.sqlServer, databaseInner, self.manager());
            }
        });
}
 
Example 5
Source Project: azure-libraries-for-java   Source File: Utils.java    License: MIT License 6 votes vote down vote up
/**
 * Prints information for the passed SQL Database.
 * @param database database to be printed
 */
public static void print(SqlDatabase database) {
    StringBuilder builder = new StringBuilder().append("Sql Database: ").append(database.id())
            .append("Name: ").append(database.name())
            .append("\n\tResource group: ").append(database.resourceGroupName())
            .append("\n\tRegion: ").append(database.region())
            .append("\n\tSqlServer Name: ").append(database.sqlServerName())
            .append("\n\tEdition of SQL database: ").append(database.edition())
            .append("\n\tCollation of SQL database: ").append(database.collation())
            .append("\n\tCreation date of SQL database: ").append(database.creationDate())
            .append("\n\tIs data warehouse: ").append(database.isDataWarehouse())
            .append("\n\tCurrent service objective of SQL database: ").append(database.serviceLevelObjective())
            .append("\n\tId of current service objective of SQL database: ").append(database.currentServiceObjectiveId())
            .append("\n\tMax size bytes of SQL database: ").append(database.maxSizeBytes())
            .append("\n\tDefault secondary location of SQL database: ").append(database.defaultSecondaryLocation());

    System.out.println(builder.toString());
}
 
Example 6
Source Project: clouditor   Source File: AzureSQLDatabaseScanner.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Asset transform(SqlDatabase database) throws ScanException {
  var asset = super.transform(database);

  enrich(asset, "transparentDataEncryption", database, SqlDatabase::getTransparentDataEncryption);

  enrich(
      asset,
      "auditingPolicy",
      database,
      x ->
          this.api
              .azure()
              .sqlServers()
              .manager()
              .inner()
              .databaseBlobAuditingPolicies()
              .get(database.resourceGroupName(), database.sqlServerName(), database.name()),
      DatabaseBlobAuditingPolicyInner::id,
      DatabaseBlobAuditingPolicyInner::name);

  enrichList(
      asset,
      "replicationLinks",
      database,
      x -> x.listReplicationLinks().values(),
      ReplicationLink::id,
      ReplicationLink::name);

  return asset;
}
 
Example 7
Source Project: clouditor   Source File: AzureScannerTest.java    License: Apache License 2.0 5 votes vote down vote up
static SqlDatabase createSqlDatabase(String id, String name, DatabaseInner inner) {
  var db = mock(SqlDatabase.class);

  when(db.id()).thenReturn(id);
  when(db.name()).thenReturn(name);
  when(db.inner()).thenReturn(inner);

  return db;
}
 
Example 8
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlDatabase getDatabase(String databaseName) {
    DatabaseInner databaseInner = this.sqlServerManager.inner().databases()
        .get(this.resourceGroupName, this.sqlServerName, databaseName);

    return databaseInner != null ? new SqlDatabaseImpl(this.resourceGroupName, this.sqlServerName, this.sqlServerLocation, databaseName, databaseInner, this.sqlServerManager) : null;
}
 
Example 9
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlDatabase addNewDatabase(String databaseName) {
    return this.sqlServerManager.sqlServers().databases()
        .define(databaseName)
        .withExistingSqlServer(this.resourceGroupName, this.sqlServerName, this.sqlServerLocation)
        .withExistingElasticPool(this)
        .create();
}
 
Example 10
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlDatabase addExistingDatabase(String databaseName) {
    return this.getDatabase(databaseName)
        .update()
        .withExistingElasticPool(this)
        .apply();
}
 
Example 11
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlDatabase addExistingDatabase(SqlDatabase database) {
    return database
        .update()
        .withExistingElasticPool(this)
        .apply();
}
 
Example 12
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlDatabase removeDatabase(String databaseName) {
    return this.getDatabase(databaseName)
        .update()
        .withoutElasticPool()
        .withStandardEdition(SqlDatabaseStandardServiceObjective.S0)
        .apply();
}
 
Example 13
Source Project: azure-libraries-for-java   Source File: SqlElasticPoolImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlElasticPoolImpl withExistingDatabase(SqlDatabase database) {
    if (this.sqlDatabases == null) {
        this.sqlDatabases = new SqlDatabasesAsExternalChildResourcesImpl(this.taskGroup(), this.sqlServerManager, "SqlDatabase");
    }

    return new SqlDatabaseForElasticPoolImpl(this, this.sqlDatabases
        .patchUpdateDatabase(database.name()).withExistingSqlServer(this.resourceGroupName, this.sqlServerName, this.sqlServerLocation))
        .attach();
}
 
Example 14
Source Project: azure-libraries-for-java   Source File: SqlDatabaseImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlDatabase rename(String newDatabaseName) {
    ResourceId resourceId = ResourceId.fromString(this.id());
    String newId = resourceId.parent().id() + "/databases/" + newDatabaseName;
    this.sqlServerManager.inner().databases()
        .rename(this.resourceGroupName, this.sqlServerName, this.name(), newId);
    return this.sqlServerManager.sqlServers().databases()
        .getBySqlServer(this.resourceGroupName, this.sqlServerName, newDatabaseName);
}
 
Example 15
Source Project: azure-libraries-for-java   Source File: SqlDatabaseImpl.java    License: MIT License 5 votes vote down vote up
@Override
public Observable<SqlDatabase> renameAsync(final String newDatabaseName) {
    final SqlDatabaseImpl self = this;
    ResourceId resourceId = ResourceId.fromString(this.id());
    String newId = resourceId.parent().id() + "/databases/" + newDatabaseName;
    return this.sqlServerManager.inner().databases()
        .renameAsync(this.resourceGroupName, this.sqlServerName, self.name(), newId)
        .flatMap(new Func1<Void, Observable<SqlDatabase>>() {
            @Override
            public Observable<SqlDatabase> call(Void aVoid) {
                return self.sqlServerManager.sqlServers().databases()
                    .getBySqlServerAsync(self.resourceGroupName, self.sqlServerName, newDatabaseName);
            }
        });
}
 
Example 16
@Override
public SqlDatabase getBySqlServer(SqlServer sqlServer, String name) {
    if (sqlServer == null) {
        return null;
    }
    DatabaseInner inner = this.manager.inner().databases().get(sqlServer.resourceGroupName(), sqlServer.name(), name);
    return (inner != null) ? new SqlDatabaseImpl(inner.name(), (SqlServerImpl) sqlServer, inner, manager) : null;
}
 
Example 17
@Override
public SqlDatabase get(String name) {
    if (sqlServer == null) {
        return null;
    }
    return this.getBySqlServer(this.sqlServer.resourceGroupName(), this.sqlServer.name(), name);
}
 
Example 18
@Override
public Observable<SqlDatabase> getAsync(String name) {
    if (sqlServer == null) {
        return null;
    }
    return this.getBySqlServerAsync(this.sqlServer.resourceGroupName(), this.sqlServer.name(), name);
}
 
Example 19
@Override
public SqlDatabase getById(String id) {
    Objects.requireNonNull(id);
    return this.getBySqlServer(ResourceUtils.groupFromResourceId(id),
        ResourceUtils.nameFromResourceId(ResourceUtils.parentRelativePathFromResourceId(id)),
        ResourceUtils.nameFromResourceId(id));
}
 
Example 20
@Override
public Observable<SqlDatabase> getByIdAsync(String id) {
    Objects.requireNonNull(id);
    return this.getBySqlServerAsync(ResourceUtils.groupFromResourceId(id),
        ResourceUtils.nameFromResourceId(ResourceUtils.parentRelativePathFromResourceId(id)),
        ResourceUtils.nameFromResourceId(id));
}
 
Example 21
@Override
public List<SqlDatabase> listBySqlServer(String resourceGroupName, String sqlServerName) {
    List<SqlDatabase> databasesSet = new ArrayList<>();
    for (DatabaseInner inner : this.manager.inner().databases().listByServer(resourceGroupName, sqlServerName)) {
        databasesSet.add(new SqlDatabaseImpl(resourceGroupName, sqlServerName, inner.location(), inner.name(), inner, manager));
    }
    return Collections.unmodifiableList(databasesSet);
}
 
Example 22
@Override
public List<SqlDatabase> listBySqlServer(SqlServer sqlServer) {
    List<SqlDatabase> firewallRuleSet = new ArrayList<>();
    if (sqlServer != null) {
        for (DatabaseInner inner : this.manager.inner().databases().listByServer(sqlServer.resourceGroupName(), sqlServer.name())) {
            firewallRuleSet.add(new SqlDatabaseImpl(inner.name(), (SqlServerImpl) sqlServer, inner, manager));
        }
    }
    return Collections.unmodifiableList(firewallRuleSet);
}
 
Example 23
@Override
public List<SqlDatabase> list() {
    if (sqlServer == null) {
        return null;
    }
    return this.listBySqlServer(this.sqlServer.resourceGroupName(), this.sqlServer.name());
}
 
Example 24
@Override
public Observable<SqlDatabase> listAsync() {
    if (sqlServer == null) {
        return null;
    }
    return this.listBySqlServerAsync(this.sqlServer.resourceGroupName(), this.sqlServer.name());
}
 
Example 25
Source Project: azure-libraries-for-java   Source File: SqlSyncGroupImpl.java    License: MIT License 5 votes vote down vote up
@Override
public SqlSyncGroupImpl withExistingSqlDatabase(SqlDatabase sqlDatabase) {
    this.resourceGroupName = sqlDatabase.resourceGroupName();
    this.sqlServerName = sqlDatabase.sqlServerName();
    this.sqlDatabaseName = sqlDatabase.name();
    return this;
}
 
Example 26
@Override
public SqlDatabase getDatabase(String databaseName) {
    DatabaseInner databaseInner = this.sqlServer.manager().inner().databases().getByRecommendedElasticPool(
        this.sqlServer.resourceGroupName(),
        this.sqlServer.name(),
        this.name(),
        databaseName);

    return new SqlDatabaseImpl(databaseInner.name(), this.sqlServer, databaseInner, this.manager());
}
 
Example 27
@Override
public Observable<SqlDatabase> getDatabaseAsync(String databaseName) {
    final RecommendedElasticPoolImpl self = this;
    return this.sqlServer.manager().inner().databases().getByRecommendedElasticPoolAsync(
            this.sqlServer.resourceGroupName(),
            this.sqlServer.name(),
            this.name(),
            databaseName)
        .map(new Func1<DatabaseInner, SqlDatabase>() {
            @Override
            public SqlDatabase call(DatabaseInner databaseInner) {
                return new SqlDatabaseImpl(databaseInner.name(), self.sqlServer, databaseInner, self.manager());
            }
        });
}
 
Example 28
Source Project: clouditor   Source File: AzureSQLDatabaseScanner.java    License: Apache License 2.0 4 votes vote down vote up
public AzureSQLDatabaseScanner() {
  super(SqlDatabase::id, SqlDatabase::name);
}
 
Example 29
Source Project: pacbot   Source File: SQLDatabaseInventoryCollector.java    License: Apache License 2.0 4 votes vote down vote up
public List<SQLDatabaseVH> fetchSQLDatabaseDetails(SubscriptionVH subscription,
		Map<String, Map<String, String>> tagMap) {

	List<SQLDatabaseVH> sqlDatabaseList = new ArrayList<SQLDatabaseVH>();

	Azure azure = azureCredentialProvider.getClient(subscription.getTenant(),subscription.getSubscriptionId());
	PagedList<SqlServer> sqlServers = azure.sqlServers().list();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
	for (SqlServer sqlServer : sqlServers) {
		List<SqlDatabase> sqlDatabases = azure.sqlServers().databases().listBySqlServer(sqlServer);
		for (SqlDatabase sqlDatabase : sqlDatabases) {
			if (!sqlDatabase.name().contentEquals("master")) {
				SQLDatabaseVH sqlDatabaseVH = new SQLDatabaseVH();
				if (sqlDatabase.creationDate() != null) {

					sqlDatabaseVH.setCreationDate(sdf.format(sqlDatabase.creationDate().toDate()));
				}
				sqlDatabaseVH.setCurrentServiceObjectiveId(sqlDatabase.currentServiceObjectiveId());
				sqlDatabaseVH.setCollation(sqlDatabase.collation());
				sqlDatabaseVH.setDatabaseId(sqlDatabase.databaseId());
				sqlDatabaseVH.setDefaultSecondaryLocation(sqlDatabase.defaultSecondaryLocation());
				if (sqlDatabase.earliestRestoreDate() != null) {

					sqlDatabaseVH.setEarliestRestoreDate(sdf.format(sqlDatabase.earliestRestoreDate().toDate()));
				}
				sqlDatabaseVH.setEdition(sqlDatabase.edition().toString());
				sqlDatabaseVH.setElasticPoolName(sqlDatabase.elasticPoolName());
				sqlDatabaseVH.setId(sqlDatabase.id());
				sqlDatabaseVH.setDataWarehouse(sqlDatabase.isDataWarehouse());
				sqlDatabaseVH.setName(sqlDatabase.name());
				sqlDatabaseVH.setStatus(sqlDatabase.status());
				sqlDatabaseVH.setSubscription(subscription.getSubscriptionId());
				sqlDatabaseVH.setSubscriptionName(subscription.getSubscriptionName());
				sqlDatabaseVH.setServerName(sqlDatabase.sqlServerName());
				sqlDatabaseVH.setResourceGroupName(sqlDatabase.resourceGroupName());

				for (Map.Entry<String, Map<String, String>> resourceGroupTag : tagMap.entrySet()) {

					if (resourceGroupTag.getKey().equalsIgnoreCase(sqlDatabase.resourceGroupName())) {
						sqlDatabaseVH.setTags(resourceGroupTag.getValue());
						break;
					}

				}

				firewallRule(sqlServer, sqlDatabaseVH);
				sqlDatabaseList.add(sqlDatabaseVH);
			}

		}

	}
	log.info("Target Type : {}  Total: {} ","Sql Databse",sqlDatabaseList.size());
	return sqlDatabaseList;

}
 
Example 30
@Override
public SqlDatabase parent() {
    return null;
}