|
| 1 | +// Copyright 2025 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using Google.Cloud.Iam.V1; |
| 16 | +using Google.Cloud.PubSub.V1; |
| 17 | +using Grpc.Core; |
| 18 | +using System.Linq; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace Google.Cloud.Tools.Snippets; |
| 22 | + |
| 23 | +[Collection(nameof(SnippetFixture))] |
| 24 | +public class OccSnippets |
| 25 | +{ |
| 26 | + private readonly SnippetFixture _fixture; |
| 27 | + |
| 28 | + public OccSnippets(SnippetFixture fixture) => _fixture = fixture; |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void OccForIam() |
| 32 | + { |
| 33 | + string role = "roles/pubsub.viewer"; |
| 34 | + string member = "domain:google.com"; |
| 35 | + |
| 36 | + var topicName = _fixture.CreateTopic(); |
| 37 | + |
| 38 | + // Sample: OccForIam |
| 39 | + PublisherServiceApiClient client = PublisherServiceApiClient.Create(); |
| 40 | + |
| 41 | + try |
| 42 | + { |
| 43 | + // Get the current IAM Policy with the current Etag. |
| 44 | + Policy policy = client.IAMPolicyClient.GetIamPolicy(new GetIamPolicyRequest |
| 45 | + { |
| 46 | + Resource = topicName, |
| 47 | + }); |
| 48 | + |
| 49 | + // Modify the local IAM Policy object. |
| 50 | + Binding binding = policy.Bindings.FirstOrDefault(b => b.Role == role); |
| 51 | + |
| 52 | + if (binding != null) |
| 53 | + { |
| 54 | + if (!binding.Members.Contains(member)) |
| 55 | + { |
| 56 | + binding.Members.Add(member); |
| 57 | + } |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + policy.Bindings.Add(new Binding |
| 62 | + { |
| 63 | + Role = role, |
| 64 | + Members = { member } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + // Attempt to set the modified policy, it contains the Etag value from when it was read. |
| 69 | + client.IAMPolicyClient.SetIamPolicy(new SetIamPolicyRequest |
| 70 | + { |
| 71 | + Resource = topicName, |
| 72 | + Policy = policy |
| 73 | + }); |
| 74 | + |
| 75 | + // The policy was modified successfully. |
| 76 | + } |
| 77 | + catch (RpcException ex) when ( ex.StatusCode == StatusCode.Aborted ) |
| 78 | + { |
| 79 | + // A concurrency conflict usually manifests as Aborted but depending on the API |
| 80 | + // other error codes may be used, like FailedPrecondition. |
| 81 | + // To ensure the OCC mechanism is robust, check the documentation for the API |
| 82 | + // you are implementing OCC for. |
| 83 | + |
| 84 | + // Handle the etag mismatch. For example, retry the change or prompt the user to |
| 85 | + // submit their changes again. |
| 86 | + } |
| 87 | + // End sample |
| 88 | + } |
| 89 | +} |
0 commit comments