/// <summary> /// Initialize the database client. /// If the client is already initialized, it will first be disposed. /// </summary> public void InitializeDatabase() { if (_Database != null) { _Logger?.Invoke(_Header + "disposing existing database client"); _Database.Dispose(); } _Logger?.Invoke(_Header + "initializing database client: " + _Settings.Type.ToString() + " using file " + _Settings.Filename); _Database = new DatabaseClient(_Settings.Filename); _Logger?.Invoke(_Header + "initialization complete"); _Initialized = true; }
/// <summary> /// Initialize the database client. /// If the client is already initialized, it will first be disposed. /// </summary> public void InitializeDatabase() { if (_Database != null) { _Logger?.Invoke(_Header + "disposing existing database client"); _Database.Dispose(); } _Logger?.Invoke(_Header + "initializing database client: " + _Settings.Type.ToString() + " on " + _Settings.Hostname + ":" + _Settings.Port); _Database = new DatabaseClient( _Settings.Hostname, _Settings.Port, _Settings.Username, _Settings.Password, _Settings.DatabaseName); _Logger?.Invoke(_Header + "initialization complete"); _Initialized = true; }
/// <summary> /// Tear down and dispose of background workers. /// </summary> public void Dispose() { if (_TokenSource != null) { if (!_TokenSource.IsCancellationRequested) { _TokenSource.Cancel(); } _TokenSource.Dispose(); _TokenSource = null; } if (_Database != null) { _Database.Dispose(); _Database = null; } _Config = null; }
static void Main(string[] args) { Console.WriteLine("Opening file"); _Database = new DatabaseClient("test", true); Console.WriteLine("Creating table 'company' with fields 'id' (int) and 'val' (string)..."); string createTableQuery = "CREATE TABLE IF NOT EXISTS company " + "( id INTEGER PRIMARY KEY AUTOINCREMENT, " + " val NVARCHAR(64))"; DataTable createTableResult = _Database.Query(createTableQuery); while (_RunForever) { string userInput = InputString("Command [? for help]:", null, false); switch (userInput) { case "?": Menu(); break; case "c": case "cls": Console.Clear(); break; case "q": _RunForever = false; break; case "dispose": _Database.Dispose(); break; case "delete": File.Delete("test"); break; case "init": _Database = new DatabaseClient("test", true); break; default: DataTable result = _Database.Query(userInput); if (result != null) { if (result.Rows != null && result.Rows.Count > 0) { if (result.Rows.Count > 1) { Console.WriteLine(SerializeJson(DataTableToListDynamic(result), true)); } else { Console.WriteLine(SerializeJson(DataTableToDynamic(result), true)); } } else { Console.WriteLine("No rows returned"); } } else { Console.WriteLine("No table returned"); } break; } } }
static async Task MainEncryptedKeyAuthentication(string[] args) { string compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID"); DatabaseClient databaseClient = null; logger.Info("Starting example"); try { // Instead of hardcoding the values here, // keyPath and passPhrase should be obtained through environment variables or // parameters to this function. // The caller is responsible for securely providing the values. string keyPath = "/path/to/some/api_key_with_passphrase.pem"; char[] chars = { 's', 'o', 'm', 'e', 'p', 'a', 's', 's', 'p', 'h', 'r', 'a', 's', 'e' }; SecureString passPhrase = new SecureString(); foreach (char c in chars) { passPhrase.AppendChar(c); } // Build FileSecurePrivateKeySupplier // It ensures that private key length is at least 2048, // private key is encrypted with a passphrase, and // only CBC chaining mode is allowed FileSecurePrivateKeySupplier keySupplier = new FileSecurePrivateKeySupplier(keyPath, passPhrase); // There are a few ways to provide config information. // 1. In addition to normal profile parameter, also provide the keySupplier. var providerFromFileAndSupplier = new ConfigFileAuthenticationDetailsProvider(OciConfigProfileName, keySupplier); // 2. Provide a custom config path plus keySupplier. var providerFromCustomFileAndSupplier = new ConfigFileAuthenticationDetailsProvider("/some/custom/path/to/config", OciConfigProfileName, keySupplier); // 3. Build a ConfigFile object plus keySupplier. ConfigFile.ConfigAccumulator accumulator = new ConfigFile.ConfigAccumulator(); Dictionary <string, string> entries = new Dictionary <string, string>(); entries.Add("user", "ocid1.user.oc1..somefakeuserocid"); entries.Add("fingerprint", "somefakefingerprint"); entries.Add("tenancy", "ocid1.tenancy.oc1..somefaketenancyocid"); entries.Add("region", "us-phoenix-1"); accumulator.configurationByProfile.Add(OciConfigProfileName, entries);; ConfigFile configFile = new ConfigFile(accumulator, OciConfigProfileName); var providerFromConfigAndSupplier = new ConfigFileAuthenticationDetailsProvider(configFile, keySupplier); // Create Database service client with any of the authentication details providers above databaseClient = new DatabaseClient(providerFromFileAndSupplier); // databaseClient = new DatabaseClient(providerFromCustomFileAndSupplier); // databaseClient = new DatabaseClient(providerFromConfigAndSupplier); // Call ListDbSystems Operation ListDbSystemsRequest listDbSystemsRequest = new ListDbSystemsRequest { CompartmentId = compartmentId }; ListDbSystemsResponse listDbSystemsResponse = await databaseClient.ListDbSystems(listDbSystemsRequest); logger.Info($"Found {listDbSystemsResponse.Items.Count} DbSystems"); foreach (var dbSystem in listDbSystemsResponse.Items) { logger.Info($"DbSystem Name: {dbSystem.DisplayName}"); } } catch (Exception e) { logger.Error($"Got exception: {e.Message}"); } finally { if (databaseClient != null) { databaseClient.Dispose(); } } logger.Info("End Example"); }
static async Task MainDatabase(string[] args) { string compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID"); string adminPassword = Environment.GetEnvironmentVariable("PASSWORD"); logger.Info("Starting example"); // Accepts profile name and creates a auth provider based on config file var provider = new ConfigFileAuthenticationDetailsProvider(OciConfigProfileName); var identityClient = new IdentityClient(provider); var virtualNetworkClient = new VirtualNetworkClient(provider); var databaseClient = new DatabaseClient(provider); var networkCidrBlock = "10.0.1.0/24"; Vcn vcn = null; Subnet subnet = null; string dbSystemId = null; AvailabilityDomain availablityDomain = await getAvailabilityDomains(identityClient, compartmentId); logger.Info($"availability domain is {availablityDomain.Name}"); try { vcn = await createVcn(virtualNetworkClient, compartmentId, networkCidrBlock); subnet = await createSubnet(virtualNetworkClient, compartmentId, availablityDomain, networkCidrBlock, vcn); logger.Info("Launching Database...."); LaunchDbSystemDetails launchDbSystemDetails = new LaunchDbSystemDetails { AvailabilityDomain = availablityDomain.Name, CompartmentId = compartmentId, CpuCoreCount = 4, Shape = "BM.DenseIO2.52", SshPublicKeys = new List <string> { "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyyA8wePstPC69PeuHFtOwyTecByonsHFAjHbVnZ+h0dpomvLZxUtbknNj3+c7MPYKqKBOx9gUKV/diR/mIDqsb405MlrI1kmNR9zbFGYAAwIH/Gxt0Lv5ffwaqsz7cECHBbMojQGEz3IH3twEvDfF6cu5p00QfP0MSmEi/eB+W+h30NGdqLJCziLDlp409jAfXbQm/4Yx7apLvEmkaYSrb5f/pfvYv1FEV1tS8/J7DgdHUAWo6gyGUUSZJgsyHcuJT7v9Tf0xwiFWOWL9WsWXa9fCKqTeYnYJhHlqfinZRnT/+jkz0OZ7YmXo6j4Hyms3RCOqenIX1W6gnIn+eQIkw== This is the key's comment" }, SubnetId = subnet.Id, DatabaseEdition = LaunchDbSystemDetails.DatabaseEditionEnum.EnterpriseEdition, DisplayName = "OCI DotNet SDK database", Hostname = "oci-dotnetsdk-host", Domain = "testdomain", DbHome = new CreateDbHomeDetails { DbVersion = "12.1.0.2", DisplayName = "Example DB Home", Database = new CreateDatabaseDetails { DbName = "dotnetdb", AdminPassword = adminPassword } } }; LaunchDbSystemRequest launchDbSystemRequest = new LaunchDbSystemRequest { LaunchDbSystemDetails = launchDbSystemDetails }; LaunchDbSystemResponse launchDbSystemResponse = await databaseClient.LaunchDbSystem(launchDbSystemRequest); logger.Info($"Database ID: {launchDbSystemResponse.DbSystem.Id} and Database display name: {launchDbSystemResponse.DbSystem.DisplayName}"); logger.Info("Waiting for Database to come to available state...."); GetDbSystemRequest getDbSystemRequest = new GetDbSystemRequest { DbSystemId = launchDbSystemResponse.DbSystem.Id }; WaiterConfiguration waiterConfiguration = new WaiterConfiguration { MaxAttempts = 20, GetNextDelayInSeconds = DelayStrategy.GetExponentialDelayInSeconds }; GetDbSystemResponse getDbSystemResponse = databaseClient.Waiters.ForDbSystem( getDbSystemRequest, waiterConfiguration, DbSystem.LifecycleStateEnum.Available).Execute(); logger.Info("Database is available"); logger.Info($"Database ID: {getDbSystemResponse.DbSystem.Id} and Database display name: {getDbSystemResponse.DbSystem.DisplayName}"); dbSystemId = getDbSystemResponse.DbSystem.Id; } catch (Exception e) { logger.Error($"failed to launch DbSystem: {e}"); } finally { if (dbSystemId != null) { await terminateDbSystem(databaseClient, dbSystemId); } if (subnet != null) { await deleteSubnet(virtualNetworkClient, subnet); } if (vcn != null) { await deleteVcn(virtualNetworkClient, vcn); } identityClient.Dispose(); virtualNetworkClient.Dispose(); databaseClient.Dispose(); logger.Info("End example"); } }