|
| 1 | +// Copyright 2021, Nitric Technologies Pty Ltd. |
| 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 | +using System; |
| 15 | +using System.Text; |
| 16 | +using Nitric.Proto.Secret.v1; |
| 17 | +using GrpcClient = Nitric.Proto.Secret.v1.SecretService.SecretServiceClient; |
| 18 | +namespace Nitric.Api.Secret |
| 19 | +{ |
| 20 | + public class Secret |
| 21 | + { |
| 22 | + public readonly static string LATEST = "latest"; |
| 23 | + |
| 24 | + internal GrpcClient client; |
| 25 | + public readonly string Name; |
| 26 | + |
| 27 | + internal Secret(GrpcClient client, string name) |
| 28 | + { |
| 29 | + this.client = client; |
| 30 | + this.Name = name; |
| 31 | + } |
| 32 | + public SecretVersion Version(string version) |
| 33 | + { |
| 34 | + if (string.IsNullOrEmpty(version)) |
| 35 | + { |
| 36 | + throw new ArgumentNullException(version); |
| 37 | + } |
| 38 | + return new SecretVersion(this, version); |
| 39 | + } |
| 40 | + public SecretVersion Latest() |
| 41 | + { |
| 42 | + return new SecretVersion(this, LATEST); |
| 43 | + } |
| 44 | + public SecretVersion Put(byte[] value) |
| 45 | + { |
| 46 | + if (value == null || value.Length == 0) |
| 47 | + { |
| 48 | + throw new ArgumentNullException("provide non-empty value"); |
| 49 | + } |
| 50 | + var request = new SecretPutRequest |
| 51 | + { |
| 52 | + Secret = new Proto.Secret.v1.Secret { Name = this.Name }, |
| 53 | + Value = Google.Protobuf.ByteString.CopyFrom(value), |
| 54 | + }; |
| 55 | + var secretResponse = client.Put(request); |
| 56 | + return new SecretVersion( |
| 57 | + new Secret( |
| 58 | + this.client, |
| 59 | + secretResponse.SecretVersion.Secret.Name |
| 60 | + ), |
| 61 | + secretResponse.SecretVersion.Version |
| 62 | + ); |
| 63 | + } |
| 64 | + public SecretVersion Put(string value) |
| 65 | + { |
| 66 | + if (string.IsNullOrEmpty(value)) |
| 67 | + { |
| 68 | + throw new ArgumentNullException(value); |
| 69 | + } |
| 70 | + return Put(Encoding.UTF8.GetBytes(value)); |
| 71 | + } |
| 72 | + public override string ToString() |
| 73 | + { |
| 74 | + return "[name=" + this.Name + "]"; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments