// This is an automatically generated code sample. 
// To make this code sample work in your Oracle Cloud tenancy, 
// please replace the values for any parameters whose current values do not fit
// your use case (such as resource IDs, strings containing ‘EXAMPLE’ or ‘unique_id’, and 
// boolean, number, and enum parameters with values not fitting your use case).

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Oci.UsageapiService;
using Oci.Common;
using Oci.Common.Auth;

namespace Oci.Sdk.DotNet.Example.Usageapi
{
    public class CreateQueryExample
    {
        public static async Task Main()
        {
            // Create a request and dependent object(s).
			var createQueryDetails = new Oci.UsageapiService.Models.CreateQueryDetails
			{
				CompartmentId = "ocid1.test.oc1..<unique_ID>EXAMPLE-compartmentId-Value",
				QueryDefinition = new Oci.UsageapiService.Models.QueryDefinition
				{
					DisplayName = "EXAMPLE-displayName-Value",
					ReportQuery = new Oci.UsageapiService.Models.ReportQuery
					{
						TenantId = "ocid1.test.oc1..<unique_ID>EXAMPLE-tenantId-Value",
						TimeUsageStarted = DateTime.Parse("12/12/2018 18:28:56"),
						TimeUsageEnded = DateTime.Parse("07/24/2022 02:54:53"),
						Granularity = Oci.UsageapiService.Models.ReportQuery.GranularityEnum.Daily,
						IsAggregateByTime = false,
						Forecast = new Oci.UsageapiService.Models.Forecast
						{
							ForecastType = Oci.UsageapiService.Models.Forecast.ForecastTypeEnum.Basic,
							TimeForecastStarted = DateTime.Parse("06/28/2025 19:15:15"),
							TimeForecastEnded = DateTime.Parse("10/02/2034 22:16:54")
						},
						QueryType = Oci.UsageapiService.Models.ReportQuery.QueryTypeEnum.Credit,
						GroupBy = new List<string>
						{
							"EXAMPLE--Value"
						},
						GroupByTag = new List<Oci.UsageapiService.Models.Tag>
						{
							new Oci.UsageapiService.Models.Tag
							{
								Namespace = "EXAMPLE-namespace-Value",
								Key = "EXAMPLE-key-Value",
								Value = "EXAMPLE-value-Value"
							}
						},
						CompartmentDepth = new System.Decimal
						{
						},
						Filter = new Oci.UsageapiService.Models.Filter
						{
							Operator = Oci.UsageapiService.Models.Filter.OperatorEnum.And,
							Dimensions = new List<Oci.UsageapiService.Models.Dimension>
							{
								new Oci.UsageapiService.Models.Dimension
								{
									Key = "EXAMPLE-key-Value",
									Value = "EXAMPLE-value-Value"
								}
							},
							Tags = new List<Oci.UsageapiService.Models.Tag>
							{
								new Oci.UsageapiService.Models.Tag
								{
									Namespace = "EXAMPLE-namespace-Value",
									Key = "EXAMPLE-key-Value",
									Value = "EXAMPLE-value-Value"
								}
							},
						},
						DateRangeName = Oci.UsageapiService.Models.ReportQuery.DateRangeNameEnum.LastTenDays
					},
					CostAnalysisUI = new Oci.UsageapiService.Models.CostAnalysisUI
					{
						Graph = Oci.UsageapiService.Models.CostAnalysisUI.GraphEnum.Lines,
						IsCumulativeGraph = false
					},
					Version = new System.Decimal
					{
					}
				}
			};
			var createQueryRequest = new Oci.UsageapiService.Requests.CreateQueryRequest
			{
				CreateQueryDetails = createQueryDetails,
				OpcRequestId = "BDTEKBBS3O2VUMUEA7GM<unique_ID>",
				OpcRetryToken = "EXAMPLE-opcRetryToken-Value"
			};

            // Create a default authentication provider that uses the DEFAULT
            // profile in the configuration file.
            // Refer to <see href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File>the public documentation</see> on how to prepare a configuration file. 
            var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
            try
            {
                // Create a service client and send the request.
				using (var client = new UsageapiClient(provider, new ClientConfiguration()))
				{
					var response = await client.CreateQuery(createQueryRequest);
					// Retrieve value from the response.
					var id = response.Query.Id;
				}
            }
            catch (Exception e)
            {
                Console.WriteLine($"CreateQuery Failed with {e.Message}");
                throw e;
            }
        }

    }
}

Was this article helpful?