/// <summary>
        /// Gets a scaling policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">Id of the scaling policy</param>
        public string GetScalingPolicy(
            string projectId = "YOUR-PROJECT-ID",
            string policyId  = "YOUR-SCALING-POLICY-ID")
        {
            // Initialize the client
            var client = ScalingPoliciesServiceClient.Create();

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

            // Call the API
            try
            {
                var scalingPolicy = client.GetScalingPolicy(policyName);

                // Inspect the result
                return($"Scaling policy found: {scalingPolicy.Name}.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"GetScalingPolicy error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
Exemple #2
0
        /// <summary>
        /// Updates a scaling policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">Scaling policy to update</param>
        public string UpdateScalingPolicy(
            string projectId = "YOUR-PROJECT-ID",
            string policyId  = "YOUR-SCALING-POLICY-ID")
        {
            // Initialize the client
            var client = ScalingPoliciesServiceClient.Create();

            // Construct the request
            string policyName    = $"projects/{projectId}/locations/global/scalingPolicies/{policyId}";
            var    scalingPolicy = new ScalingPolicy
            {
                Name     = policyName,
                Priority = 10,
            };
            var fieldMask = new FieldMask
            {
                Paths = { "priority" }
            };

            // Call the API
            try
            {
                var updated = client.UpdateScalingPolicy(scalingPolicy, fieldMask);

                // Inspect the result
                return($"Scaling policy updated: {policyName} updated. Operation Id: {updated.Name}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"UpdateScalingPolicy error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
        /// <summary>
        /// Create a new scaling policy
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        /// <param name="policyId">Id of the scaling policy</param>
        /// <param name="deploymentId">Id of the deployment to scale</param>
        public string CreateScalingPolicy(
            string projectId    = "YOUR-PROJECT-ID",
            string policyId     = "YOUR-SCALING-POLICY-ID",
            string deploymentId = "YOUR-DEPLOYMENT-ID")
        {
            // Initialize the client
            var client = ScalingPoliciesServiceClient.Create();

            // Construct the request
            string parent         = $"projects/{projectId}/locations/global";
            string policyName     = $"{parent}/scalingPolicies/{policyId}";
            string deploymentName = $"{parent}/gameServerDeployments/{deploymentId}";

            var autoscalerSettings = new FleetAutoscalerSettings
            {
                BufferSizeAbsolute = 1,
                MinReplicas        = 1,
                MaxReplicas        = 2
            };
            var scalingPolicy = new ScalingPolicy
            {
                Name     = policyName,
                Priority = 1,
                FleetAutoscalerSettings = autoscalerSettings,
                GameServerDeployment    = deploymentName
            };
            var scalingPolicyRequest = new CreateScalingPolicyRequest
            {
                Parent          = parent,
                ScalingPolicyId = policyId,
                ScalingPolicy   = scalingPolicy
            };

            // Call the API
            try
            {
                var created = client.CreateScalingPolicy(scalingPolicyRequest);

                // Inspect the result
                return($"Scaling policy created for {policyName}. Operation Id {created.Name}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"CreateScalingPolicy error:");
                Console.WriteLine($"{e.Message}");
                throw;
            }
        }
        /// <summary>
        /// List scaling policies
        /// </summary>
        /// <param name="projectId">Your Google Cloud Project Id</param>
        public List <string> ListScalingPolicies(string projectId = "YOUR-PROJECT-ID")
        {
            // Initialize the client
            var client = ScalingPoliciesServiceClient.Create();

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

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

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

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

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