/// <summary> /// Creates a new operation context based on the Cmdlet's parameter set and the manageUrl. /// </summary> /// <param name="serverName">The server name.</param> /// <param name="managementServiceUri">The server's ManagementService Uri.</param> /// <returns>A new operation context for the server.</returns> internal IServerDataServiceContext CreateServerDataServiceContext( string serverName, Uri managementServiceUri, Uri manageUrl) { switch (this.ParameterSetName) { case ServerNameWithSqlAuthParamSet: case FullyQualifiedServerNameWithSqlAuthParamSet: case ManageUrlWithSqlAuthParamSet: // Obtain the Server DataService Context by Sql Authentication SqlAuthenticationCredentials credentials = this.GetSqlAuthCredentials(); return(this.GetServerDataServiceBySqlAuth( serverName, managementServiceUri, credentials, manageUrl)); case FullyQualifiedServerNameWithCertAuthParamSet: case ServerNameWithCertAuthParamSet: // Get the current subscription data. IAzureSubscription subscription = CurrentSubscription; // Create a context using the subscription datat return(this.GetServerDataServiceByCertAuth( serverName, subscription)); default: throw new InvalidOperationException(Resources.UnknownParameterSet); } }
/// <summary> /// Creates and returns a new instance of the <see cref="ServerDataServiceSqlAuth"/> class which /// connects to the specified server using the specified credentials. If the server name /// is null, the default server name from the serviceRoot Uri will be used. /// </summary> /// <param name="managementServiceUri">The server's management service <see cref="Uri"/>.</param> /// <param name="sessionActivityId">An activity ID provided by the user that should be associated with this session.</param> /// <param name="credentials">The credentials to be used to authenticate the user.</param> /// <param name="serverName">The name of the server to connect to. (Optional)</param> /// <returns>An instance of <see cref="ServerDataServiceSqlAuth"/> class.</returns> public static ServerDataServiceSqlAuth Create( Uri managementServiceUri, Guid sessionActivityId, SqlAuthenticationCredentials credentials, string serverName) { if (managementServiceUri == null) { throw new ArgumentNullException("managementServiceUri"); } if (credentials == null) { throw new ArgumentNullException("credentials"); } // Retrieve GetAccessToken operation Uri Uri accessUri = DataConnectionUtility.GetAccessTokenUri(managementServiceUri); // Synchronously call GetAccessToken AccessTokenResult result = DataServiceAccess.GetAccessToken(accessUri, credentials); // Validate the retrieved access token AccessTokenResult.ValidateAccessToken(managementServiceUri, result); // Create and return a ServerDataService object return(Create(managementServiceUri, sessionActivityId, result, serverName, credentials)); }
/// <summary> /// Connect to a Azure SQL Server with the given ManagementService Uri using /// SQL authentication credentials. /// </summary> /// <param name="serverName">The server name.</param> /// <param name="managementServiceUri">The server's ManagementService Uri.</param> /// <param name="credentials">The SQL Authentication credentials for the server.</param> /// <returns>A new <see cref="ServerDataServiceSqlAuth"/> context, /// or <c>null</c> if an error occurred.</returns> internal IServerDataServiceContext GetServerDataServiceBySqlAuth( string serverName, Uri managementServiceUri, SqlAuthenticationCredentials credentials, Uri manageUrl) { IServerDataServiceContext context = null; Guid sessionActivityId = Guid.NewGuid(); try { context = SqlAuthContextFactory.GetContext(this, serverName, manageUrl, credentials, sessionActivityId, managementServiceUri); } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, sessionActivityId.ToString(), ex); // The context is not in an valid state because of the error, set the context // back to null. context = null; } return(context); }
/// <summary> /// Queries the server to get the server version /// </summary> /// <param name="manageUrl">The manage url of the server. Eg: https://{serverName}.database.windows.net</param> /// <param name="credentials">The login credentials</param> /// <returns>The server version</returns> private static Version GetVersion(Uri manageUrl, SqlAuthenticationCredentials credentials) { string serverName = manageUrl.Host.Split('.').First(); SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder["Server"] = manageUrl.Host; builder.UserID = credentials.UserName + "@" + serverName; builder.Password = credentials.Password; builder["Database"] = null; builder["Encrypt"] = false; builder.ConnectTimeout = 60; string commandText = "select serverproperty('ProductVersion')"; using (SqlConnection conn = new SqlConnection(builder.ConnectionString)) { using (SqlCommand command = new SqlCommand(commandText, conn)) { conn.Open(); string val = (string)command.ExecuteScalar(); return(new Version(val)); } } }
/// <summary> /// Creates an instance of a SQLAuth to TSql class /// </summary> /// <param name="fullyQualifiedServerName">The full server name</param> /// <param name="credentials">The login credentials for the server</param> public TSqlConnectionContext(Guid sessionActivityId, string fullyQualifiedServerName, SqlAuthenticationCredentials credentials, string serverName) { this.serverName = serverName; this.sessionActivityId = sessionActivityId; this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); this.credentials = credentials; builder = GenerateSqlConnectionBuilder(fullyQualifiedServerName, credentials.UserName, credentials.Password); }
/// <summary> /// Gets a sql auth connection context. /// </summary> /// <param name="cmdlet">The cmdlet requesting the context</param> /// <param name="serverName">The name of the server to connect to</param> /// <param name="manageUrl">The manage url of the server</param> /// <param name="credentials">The credentials to connect to the server</param> /// <param name="sessionActivityId">The session activity ID</param> /// <param name="managementServiceUri">The URI for management service</param> /// <returns>The connection context</returns> public static IServerDataServiceContext GetContext( PSCmdlet cmdlet, string serverName, Uri manageUrl, SqlAuthenticationCredentials credentials, Guid sessionActivityId, Uri managementServiceUri) { Version version; // If a version was specified (by tests) us it. if (sqlVersion == SqlVersion.v2) { version = new Version(11, 0); } else if (sqlVersion == SqlVersion.v12) { version = new Version(12, 0); } else // If no version specified, determine the version by querying the server. { version = GetVersion(manageUrl, credentials); } sqlVersion = SqlVersion.None; IServerDataServiceContext context = null; if (version.Major >= 12) { context = new TSqlConnectionContext( sessionActivityId, manageUrl.Host, credentials, serverName); } else { context = ServerDataServiceSqlAuth.Create( managementServiceUri, sessionActivityId, credentials, serverName); // Retrieve $metadata to verify model version compatibility XDocument metadata = ((ServerDataServiceSqlAuth)context).RetrieveMetadata(); XDocument filteredMetadata = DataConnectionUtility.FilterMetadataDocument(metadata); string metadataHash = DataConnectionUtility.GetDocumentHash(filteredMetadata); if (!((ServerDataServiceSqlAuth)context).metadataHashes.Any(knownHash => metadataHash == knownHash)) { cmdlet.WriteWarning(Resources.WarningModelOutOfDate); } ((ServerDataServiceSqlAuth)context).MergeOption = MergeOption.PreserveChanges; } return(context); }
/// <summary> /// Performs the call to import database using the server data service context channel. /// </summary> /// <param name="serverName">The name of the server to connect to.</param> /// <param name="blobUri">The storage blob Uri to import from.</param> /// <param name="storageAccessKey">The access key for the given storage blob.</param> /// <param name="fullyQualifiedServerName">The fully qualified server name.</param> /// <param name="databaseName">The name of the database for import.</param> /// <param name="edition">The edition of the database for import.</param> /// <param name="maxDatabaseSizeInGB">The database size for import.</param> /// <param name="sqlCredentials">The credentials used to connect to the database.</param> /// <returns>The result of the import request. Upon success the <see cref="ImportExportRequest"/> /// for the request</returns> internal ImportExportRequest ImportSqlAzureDatabaseProcess( string serverName, Uri blobUri, string storageAccessKey, string fullyQualifiedServerName, string databaseName, string edition, int maxDatabaseSizeInGB, SqlAuthenticationCredentials sqlCredentials) { this.WriteVerbose("BlobUri: " + blobUri); this.WriteVerbose("ServerName: " + fullyQualifiedServerName); this.WriteVerbose("DatabaseName: " + databaseName); this.WriteVerbose("Edition: " + edition); this.WriteVerbose("MaxDatabaseSizeInGB: " + maxDatabaseSizeInGB); this.WriteVerbose("UserName: " + sqlCredentials.UserName); // Get the SQL management client for the current subscription SqlManagementClient sqlManagementClient = GetCurrentSqlClient(); // Start the database export operation DacImportExportResponse response = sqlManagementClient.Dac.Import( serverName, new DacImportParameters() { BlobCredentials = new DacImportParameters.BlobCredentialsParameter() { Uri = blobUri, StorageAccessKey = storageAccessKey, }, ConnectionInfo = new DacImportParameters.ConnectionInfoParameter() { ServerName = fullyQualifiedServerName, DatabaseName = databaseName, UserName = sqlCredentials.UserName, Password = sqlCredentials.Password, }, AzureEdition = edition, DatabaseSizeInGB = maxDatabaseSizeInGB }); ImportExportRequest result = new ImportExportRequest() { OperationStatus = Services.Constants.OperationSuccess, OperationDescription = CommandRuntime.ToString(), OperationId = response.RequestId, RequestGuid = response.Guid, ServerName = serverName, SqlCredentials = sqlCredentials, }; return(result); }
/// <summary> /// Initializes a new instance of the <see cref="ServerDataServiceSqlAuth"/> class. /// </summary> /// <param name="managementServiceUri">The server's management service Uri.</param> /// <param name="connectionType">The server connection type with the server name</param> /// <param name="sessionActivityId">An activity ID provided by the user that should be associated with this session.</param> /// <param name="accessToken">The authentication access token to be used for executing web requests.</param> /// <param name="credentials">The SQL authentication credentials used for this context</param> private ServerDataServiceSqlAuth( Uri managementServiceUri, DataServiceConnectionType connectionType, Guid sessionActivityId, AccessTokenResult accessToken, SqlAuthenticationCredentials credentials) : base(new Uri(managementServiceUri, connectionType.RelativeEntityUri)) { this.sessionActivityId = sessionActivityId; this.connectionType = connectionType; this.accessToken = accessToken; this.credentials = credentials; // Generate a requestId and retrieve the server name this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); this.serverName = this.Servers.First().Name; }
/// <summary> /// Connect to a Azure Sql Server with the given ManagementService Uri using /// Sql Authentication credentials. /// </summary> /// <param name="serverName">The server name.</param> /// <param name="managementServiceUri">The server's ManagementService Uri.</param> /// <param name="credentials">The Sql Authentication credentials for the server.</param> /// <returns>A new <see cref="ServerDataServiceSqlAuth"/> context, /// or <c>null</c> if an error occurred.</returns> internal ServerDataServiceSqlAuth GetServerDataServiceBySqlAuth( string serverName, Uri managementServiceUri, SqlAuthenticationCredentials credentials) { ServerDataServiceSqlAuth context = null; Guid sessionActivityId = Guid.NewGuid(); try { context = ServerDataServiceSqlAuth.Create( managementServiceUri, sessionActivityId, credentials, serverName); // Retrieve $metadata to verify model version compativility XDocument metadata = context.RetrieveMetadata(); XDocument filteredMetadata = DataConnectionUtility.FilterMetadataDocument(metadata); string metadataHash = DataConnectionUtility.GetDocumentHash(filteredMetadata); if (!context.metadataHashes.Any(knownHash => metadataHash == knownHash)) { this.WriteWarning(Resources.WarningModelOutOfDate); } context.MergeOption = MergeOption.PreserveChanges; } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, sessionActivityId.ToString(), ex); // The context is not in an valid state because of the error, set the context // back to null. context = null; } return(context); }
/// <summary> /// Creates a new operation context based on the Cmdlet's parameter set and the manageUrl. /// </summary> /// <param name="serverName">The server name.</param> /// <param name="managementServiceUri">The server's ManagementService Uri.</param> /// <returns>A new operation context for the server.</returns> internal IServerDataServiceContext CreateServerDataServiceContext( string serverName, Uri managementServiceUri) { switch (this.ParameterSetName) { case ServerNameWithSqlAuthParamSet: case FullyQualifiedServerNameWithSqlAuthParamSet: case ManageUrlWithSqlAuthParamSet: // Obtain the Server DataService Context by Sql Authentication SqlAuthenticationCredentials credentials = this.GetSqlAuthCredentials(); return(this.GetServerDataServiceBySqlAuth( serverName, managementServiceUri, credentials)); default: throw new InvalidOperationException(Resources.UnknownParameterSet); } }
/// <summary> /// Creates and returns a new instance of the <see cref="ServerDataServiceSqlAuth"/> class which /// connects to the specified server using the specified credentials. /// </summary> /// <param name="managementServiceUri">The server's management service <see cref="Uri"/>.</param> /// <param name="sessionActivityId">An activity ID provided by the user that should be associated with this session.</param> /// <param name="accessTokenResult">The accessToken to be used to authenticate the user.</param> /// <param name="serverName">The name of the server to connect to. (Optional)</param> /// <param name="credentials">The SQL authentication credentials used for this context</param> /// <returns>An instance of <see cref="ServerDataServiceSqlAuth"/> class.</returns> public static ServerDataServiceSqlAuth Create( Uri managementServiceUri, Guid sessionActivityId, AccessTokenResult accessTokenResult, string serverName, SqlAuthenticationCredentials credentials) { if (managementServiceUri == null) { throw new ArgumentNullException("managementServiceUri"); } if (accessTokenResult == null) { throw new ArgumentNullException("accessTokenResult"); } // Create a ServerDataServiceSqlAuth object if (serverName == null) { return(new ServerDataServiceSqlAuth( managementServiceUri, new DataServiceConnectionType(ServerModelConnectionType), sessionActivityId, accessTokenResult, credentials)); } else { return(new ServerDataServiceSqlAuth( managementServiceUri, new DataServiceConnectionType(ServerModelConnectionType, serverName), sessionActivityId, accessTokenResult, credentials)); } }
/// <summary> /// Connect to a Azure SQL Server with the given ManagementService Uri using /// SQL authentication credentials. /// </summary> /// <param name="serverName">The server name.</param> /// <param name="managementServiceUri">The server's ManagementService Uri.</param> /// <param name="credentials">The SQL Authentication credentials for the server.</param> /// <returns>A new <see cref="ServerDataServiceSqlAuth"/> context, /// or <c>null</c> if an error occurred.</returns> internal ServerDataServiceSqlAuth GetServerDataServiceBySqlAuth( string serverName, Uri managementServiceUri, SqlAuthenticationCredentials credentials) { ServerDataServiceSqlAuth context = null; Guid sessionActivityId = Guid.NewGuid(); try { context = ServerDataServiceSqlAuth.Create( managementServiceUri, sessionActivityId, credentials, serverName); // Retrieve $metadata to verify model version compatibility XDocument metadata = context.RetrieveMetadata(); XDocument filteredMetadata = DataConnectionUtility.FilterMetadataDocument(metadata); string metadataHash = DataConnectionUtility.GetDocumentHash(filteredMetadata); if (!context.metadataHashes.Any(knownHash => metadataHash == knownHash)) { this.WriteWarning(Resources.WarningModelOutOfDate); } context.MergeOption = MergeOption.PreserveChanges; } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, sessionActivityId.ToString(), ex); // The context is not in an valid state because of the error, set the context // back to null. context = null; } return context; }