public void ChangeSeverity(
        string projectId  = "YOUR-PROJECT-ID",
        string incidentId = "an.opaque.random.id")
    {
        // Create client
        IncidentServiceClient incidentServiceClient =
            IncidentServiceClient.Create();

        // Update the severity.
        Incident incidentChange = new Incident()
        {
            Name     = new IncidentName(projectId, incidentId).ToString(),
            Severity = Severity.Minor,
        };

        // Tell the API which fields to update.
        FieldMask mask = new FieldMask()
        {
            Paths = { "severity" }
        };

        // Call the API to update the incident.
        var incident =
            incidentServiceClient.UpdateIncident(incidentChange, mask);

        Console.WriteLine("Changed severity of {0}.", incident.Name);
    }
Exemple #2
0
    public void ChangeStageAtomically(
        string projectId  = "YOUR-PROJECT-ID",
        string incidentId = "an.opaque.random.id")
    {
        // Create client
        IncidentServiceClient incidentServiceClient =
            IncidentServiceClient.Create();

        for (int retry = 0; true; ++retry)
        {
            try
            {
                // Observe the current state of the incident.
                Incident incident = incidentServiceClient.GetIncident(
                    new IncidentName(projectId, incidentId).ToString());
                Incident incidentChange = new Incident()
                {
                    // TODO: Remove the name hack.
                    Name = incident.Name.Replace("/-/", $"/{projectId}/"),
                    // Use the ETag to prevent race conditions.  Only update the
                    // incident if it hasn't changed since we observed it.
                    Etag  = incident.Etag,
                    Stage = Stage.Detected
                };
                // Tell the API which fields to update.
                FieldMask mask = new FieldMask()
                {
                    Paths = { "stage" }
                };
                // Call the API to update the incident.
                incident = incidentServiceClient.UpdateIncident(
                    incidentChange, mask);
                Console.WriteLine("Changed stage of {0}.", incident.Name);
                break;
            }
            catch (Grpc.Core.RpcException e)
                when(retry < 3 && e.StatusCode == Grpc.Core.StatusCode.Aborted)
                {
                    // Somebody else must have updated the incident at the same
                    // time!  Try again.
                }
        }
    }