Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("+----------------------------------------+");
            Console.WriteLine("|                                        |");
            Console.WriteLine("|         OCISDK SessionExample          |");
            Console.WriteLine("|                                        |");
            Console.WriteLine("+----------------------------------------+");

            string configPath = ".oci/config";

            System.OperatingSystem os = System.Environment.OSVersion;
            // windows
            if (os.Platform == PlatformID.Win32NT)
            {
                string rootPath = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
                configPath = $"{rootPath}/{configPath}";
                if (!Directory.Exists($"{rootPath}/.oci"))
                {
                    Directory.CreateDirectory($"{rootPath}/.oci");
                }
            }
            else
            {
                configPath = $"/home/user/{configPath}";
            }

            string     profile;
            ConfigFile configReader;

            if (File.Exists(configPath))
            {
                Console.Write("Connection ProfileName(Empty Input is DEFAULT): ");
                profile = Console.ReadLine();
                if (string.IsNullOrEmpty(profile))
                {
                    profile = "DEFAULT";
                }

                // load connection file
                configReader = ConfigFileReader.Parse(configPath, profile);
            }
            else
            {
                profile = "DEFAULT";

                // create connection file
                Console.WriteLine("Create connection settings to Oracle Cloud Infrastructure");

                Console.Write("TenancyId (Required): ");
                string tenancyId = OCIDInput();

                Console.Write("UserId (Required): ");
                string userId = OCIDInput();

                Console.Write("Fingerprint (Required): ");
                string fingerprint = KeyInput();

                Console.Write("PrivateKeyPath (Required): ");
                string privateKeyPath = KeyInput();

                Console.Write("PrivateKeyPassphrase: ");
                string privateKeyPassphrase = InputPassword();

                string testFileContentsMyProfile = $"[{profile}]\n" +
                                                   $"tenancy={tenancyId}\n" +
                                                   $"user={userId}\n" +
                                                   $"fingerprint={fingerprint}\n" +
                                                   $"key_file={privateKeyPath}\n" +
                                                   $"pass_phrase={privateKeyPassphrase}\n";

                File.WriteAllText(configPath, testFileContentsMyProfile);

                configReader = ConfigFileReader.Parse(configPath, profile);
            }

            // ClientConfig settings
            var         configSt = new ClientConfigStream();
            IOciSession session;

            using (var st = File.OpenText(configReader.Get("key_file")))
            {
                configSt = new ClientConfigStream
                {
                    TenancyId            = configReader.Get("tenancy"),
                    UserId               = configReader.Get("user"),
                    Fingerprint          = configReader.Get("fingerprint"),
                    PrivateKey           = st,
                    PrivateKeyPassphrase = configReader.Get("pass_phrase"),
                    AccountId            = configReader.Get("accountId"),
                    IdentityDomain       = configReader.Get("domain"),
                    UserName             = configReader.Get("user"),
                    Password             = configReader.Get("password"),
                    HomeRegion           = configReader.Get("home_region")
                };

                session = new OciSession(configSt);
            }

            var comClient = session.GetCommercialClient();
            var listPurchaseEntitlementsRequest = new ListPurchaseEntitlementsRequest
            {
                CompartmentId = configSt.TenancyId
            };
            var purchase = comClient.ListPurchaseEntitlements(listPurchaseEntitlementsRequest);

            var getServiceEntitlementRegistrationsRequest = new ListServiceEntitlementRegistrationsRequest()
            {
                CompartmentId = configSt.TenancyId
            };
            var services = comClient.ListServiceEntitlementRegistrations(getServiceEntitlementRegistrationsRequest);

            var costClient = session.GetUsageCostsClient();
            var getSubscriptionInfoRequest = new GetSubscriptionInfoRequest
            {
                TenancyId = configSt.TenancyId
            };
            var subsc = costClient.GetSubscriptionInfo(getSubscriptionInfoRequest);

            // get Client
            var identityClient = session.GetIdentityClient();

            var getTenancyRequest = new GetTenancyRequest()
            {
                TenancyId = configSt.TenancyId
            };
            var getTenacy = identityClient.GetTenancy(getTenancyRequest);

            Console.WriteLine($"tenantName: {getTenacy.Tenancy.Name}");

            // get compute
            var computeClient = session.GetComputeClient();

            IDictionary <string, IDictionary <string, string> > tags = new Dictionary <string, IDictionary <string, string> >();

            tags.Add("CostTracking", new Dictionary <string, string> {
                { "cost-trakcerA", "aaaa" }
            });

            var dbClient = session.GetDatabaseClient();
            GetDbSystemRequest getDbSystemRequest = new GetDbSystemRequest()
            {
                DbSystemId = "ocid1.dbsystem.oc1.iad.abuwcljrbukbjzlameegvsn3u7qb3qcqvtcdvl74jxfth7xjsya7cxkdpibq"
            };
            var dbSystem = dbClient.GetDbSystem(getDbSystemRequest);

            UpdateDbSystemRequest updateDbSystemRequest = new UpdateDbSystemRequest()
            {
                DbSystemId            = "ocid1.dbsystem.oc1.iad.abuwcljrbukbjzlameegvsn3u7qb3qcqvtcdvl74jxfth7xjsya7cxkdpibq",
                UpdateDbSystemDetails = new OCISDK.Core.Database.Model.UpdateDbSystemDetails()
                {
                    DefinedTags = tags
                }
            };

            dbClient.UpdateDbSystem(updateDbSystemRequest);

            var listCompartmentRequest = new ListCompartmentRequest()
            {
                CompartmentId          = getTenacy.Tenancy.Id,
                CompartmentIdInSubtree = true
            };
            var cmparts = identityClient.ListCompartment(listCompartmentRequest);

            foreach (var com in cmparts.Items)
            {
                Console.WriteLine($"compartmentName: {com.Name}");

                var listInstanceRequest = new ListInstancesRequest()
                {
                    CompartmentId = com.Id,
                    Limit         = 10,
                    SortOrder     = SortOrder.ASC
                };
                var instances = computeClient.ListInstances(listInstanceRequest);
                foreach (var ins in instances.Items)
                {
                    Console.WriteLine($"rootCompartmentInstanceName: {ins.DisplayName}");


                    var workReqestClient        = session.GetWorkRequestClient();
                    var listWorkRequestsRequest = new OCISDK.Core.Core.Request.WorkRequest.ListWorkRequestsRequest()
                    {
                        CompartmentId = ins.CompartmentId,
                        ResourceId    = ins.Id
                    };
                    var workreqs = workReqestClient.ListWorkRequests(listWorkRequestsRequest);
                    foreach (var wq in workreqs.Items)
                    {
                        Console.WriteLine($"\tWorkRequest: {wq.OperationType}, state:{wq.Status}");

                        var getWorkRequestRequest = new OCISDK.Core.Core.Request.WorkRequest.GetWorkRequestRequest()
                        {
                            WorkRequestId = wq.Id
                        };
                        var gw = workReqestClient.GetWorkRequest(getWorkRequestRequest);
                        Console.WriteLine($"\taccepted:{gw.WorkRequest.TimeAccepted}, finished:{gw.WorkRequest.TimeFinished}");

                        var listWorkRequestErrorsRequest = new OCISDK.Core.Core.Request.WorkRequest.ListWorkRequestErrorsRequest()
                        {
                            WorkRequestId = wq.Id,
                            Limit         = 100,
                            SortOrder     = SortOrder.ASC
                        };
                        var wqErrors = workReqestClient.ListWorkRequestErrors(listWorkRequestErrorsRequest);
                        foreach (var error in wqErrors.Items)
                        {
                            Console.WriteLine($"\tErrorCode: {error.Code}, ErrorMessage:{error.Message}, ErrorTimeStamp:{error.Timestamp}");
                        }

                        var listWorkRequestLogsRequest = new OCISDK.Core.Core.Request.WorkRequest.ListWorkRequestLogsRequest()
                        {
                            WorkRequestId = wq.Id,
                            Limit         = 100,
                            SortOrder     = SortOrder.ASC
                        };
                        var wqLogs = workReqestClient.ListWorkRequestLogs(listWorkRequestLogsRequest);
                        foreach (var log in wqLogs.Items)
                        {
                            Console.WriteLine($"\tLogMessage:{log.Message}, LogTimeStamp:{log.Timestamp}");
                        }
                    }
                }
            }

            Console.WriteLine("Exit with key press...");
            Console.ReadLine();
        }
        static async Task Main(string[] args)
        {
            Console.WriteLine("+----------------------------------------+");
            Console.WriteLine("|                                        |");
            Console.WriteLine("|       OCISDK SessionAsyncExample       |");
            Console.WriteLine("|                                        |");
            Console.WriteLine("+----------------------------------------+");

            string configPath = ".oci/config";

            System.OperatingSystem os = System.Environment.OSVersion;
            // windows
            if (os.Platform == PlatformID.Win32NT)
            {
                string rootPath = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
                configPath = $"{rootPath}/{configPath}";
                if (!Directory.Exists($"{rootPath}/.oci"))
                {
                    Directory.CreateDirectory($"{rootPath}/.oci");
                }
            }
            else
            {
                configPath = $"/home/user/{configPath}";
            }

            string     profile;
            ConfigFile configReader;

            if (File.Exists(configPath))
            {
                Console.Write("Connection ProfileName(Empty Input is DEFAULT): ");
                profile = Console.ReadLine();
                if (string.IsNullOrEmpty(profile))
                {
                    profile = "DEFAULT";
                }

                // load connection file
                configReader = ConfigFileReader.Parse(configPath, profile);
            }
            else
            {
                profile = "DEFAULT";

                // create connection file
                Console.WriteLine("Create connection settings to Oracle Cloud Infrastructure");

                Console.Write("TenancyId (Required): ");
                string tenancyId = OCIDInput();

                Console.Write("UserId (Required): ");
                string userId = OCIDInput();

                Console.Write("Fingerprint (Required): ");
                string fingerprint = KeyInput();

                Console.Write("PrivateKeyPath (Required): ");
                string privateKeyPath = KeyInput();

                Console.Write("PrivateKeyPassphrase: ");
                string privateKeyPassphrase = InputPassword();

                string testFileContentsMyProfile = $"[{profile}]\n" +
                                                   $"tenancy={tenancyId}\n" +
                                                   $"user={userId}\n" +
                                                   $"fingerprint={fingerprint}\n" +
                                                   $"key_file={privateKeyPath}\n" +
                                                   $"pass_phrase={privateKeyPassphrase}\n";

                File.WriteAllText(configPath, testFileContentsMyProfile);

                configReader = ConfigFileReader.Parse(configPath, profile);
            }

            // ClientConfig settings
            var         configSt = new ClientConfigStream();
            IOciSession session;
            var         stt = File.ReadAllText(configReader.Get("key_file"));


            using (var st = File.OpenText(configReader.Get("key_file")))
            {
                configSt = new ClientConfigStream
                {
                    TenancyId            = configReader.Get("tenancy"),
                    UserId               = configReader.Get("user"),
                    Fingerprint          = configReader.Get("fingerprint"),
                    PrivateKey           = st,
                    PrivateKeyPassphrase = configReader.Get("pass_phrase"),
                    AccountId            = configReader.Get("accountId"),
                    DomainName           = configReader.Get("domain_name"),
                    IdentityDomain       = configReader.Get("identity_domain"),
                    UserName             = configReader.Get("user_name"),
                    Password             = configReader.Get("password"),
                    HomeRegion           = configReader.Get("home_region")
                };

                session = new OciSession(configSt);
            }

            // get Client
            var identityClientAsync       = session.GetIdentityClientAsync();
            var computeClientAsync        = session.GetComputeClientAsync();
            var virtualNetworkClientAsync = session.GetVirtualNetworkClientAsync();

            // get tenant
            var getTenancyRequest = new GetTenancyRequest()
            {
                TenancyId = configSt.TenancyId
            };
            var getTenacy = await identityClientAsync.GetTenancy(getTenancyRequest);

            Console.WriteLine($"tenantName: {getTenacy.Tenancy.Name}");

            // get compartments
            var listCompartmentRequest = new ListCompartmentRequest()
            {
                CompartmentId          = configSt.TenancyId,
                AccessLevel            = ListCompartmentRequest.AccessLevels.ACCESSIBLE,
                CompartmentIdInSubtree = true
            };
            var compartments = await identityClientAsync.ListCompartment(listCompartmentRequest);

            // loop get compartments async
            var infoTasks = new List <Task>();

            foreach (var com in compartments.Items)
            {
                if (com.LifecycleState != "ACTIVE")
                {
                    continue;
                }

                infoTasks.Add(LoadCompartmentDetail(com, identityClientAsync));
                infoTasks.Add(LoadInstances(com, computeClientAsync));
                infoTasks.Add(LoadVcn(com, virtualNetworkClientAsync));
            }

            await Task.WhenAll(infoTasks);

            Console.WriteLine();
            Console.WriteLine("Exit with key press...");
            Console.ReadLine();
        }