/// <summary>
        /// Get details for Allocation Policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">The id of the game policy</param>
        /// <returns>Allocated Policy Name</returns>
        public string GetAllocationPolicy(
            string projectId = "YOUR-PROJECT-ID",
            string policyId  = "YOUR-POLICY-ID")
        {
            // Initialize the client
            AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.Create();

            // Construct the request
            string parent           = $"projects/{projectId}/locations/global";
            string policyName       = $"{parent}/allocationPolicies/{policyId}";
            var    allocationPolicy = new AllocationPolicy {
                Name = policyName, Priority = 1
            };
            var request = new GetAllocationPolicyRequest
            {
                Name = policyName
            };

            // Call the API
            try
            {
                var result = client.GetAllocationPolicy(request);

                // Inspect the result
                return($"Allocation Policy found: {result.Name}.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"GetAllocationPolicy error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
        /// <summary>
        /// Delete Allocation Policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">The id of the game policy</param>
        public string DeleteAllocationPolicy(
            string projectId = "YOUR-PROJECT-ID",
            string policyId  = "YOUR-POLICY-ID")
        {
            // Initialize the client
            AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.Create();

            // Construct the request
            string parent     = $"projects/{projectId}/locations/global";
            string policyName = $"{parent}/allocationPolicies/{policyId}";

            var request = new DeleteAllocationPolicyRequest
            {
                Name = policyName
            };

            // Call the API
            try
            {
                var result = client.DeleteAllocationPolicy(request);

                // Inspect the result
                return($"Allocation Policy deleted: {policyName}. Operation Id: {result.Name}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"CreateGameServerCluster error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// List Allocation Policies
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">The id of the game policy</param>
        public List <string> ListAllocationPolicy(
            string projectId = "YOUR-PROJECT-ID",
            string policyId  = "YOUR-ALLOCATION-POLICY")
        {
            // Initialize the client
            AllocationPoliciesServiceClient client = AllocationPoliciesServiceClient.Create();

            // Construct the request
            string parent  = $"projects/{projectId}/locations/global";
            var    request = new ListAllocationPoliciesRequest
            {
                Parent = parent,
            };

            // Call the API
            try
            {
                var response = client.ListAllocationPolicies(parent);

                // Inspect the result
                List <string>           result  = new List <string>();
                bool                    hasMore = true;
                Page <AllocationPolicy> currentPage;
                while (hasMore)
                {
                    currentPage = response.ReadPage(pageSize: 10);

                    // Read the result in a given page
                    foreach (var policy in currentPage)
                    {
                        Console.WriteLine($"Allocation policy found: {policy.Name}");
                        result.Add(policy.Name);
                    }
                    hasMore = currentPage != null;
                }
                ;

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine($"ListAllocationPolicies error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }