|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using k8s.Models; |
| 4 | +using Newtonsoft.Json; |
| 5 | + |
| 6 | +namespace k8s.Fluent |
| 7 | +{ |
| 8 | + /// <summary>Provides extension methods that implement a fluent interface for a <see cref="Kubernetes"/> client.</summary> |
| 9 | + public static class FluentExtensions |
| 10 | + { |
| 11 | + /// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/> and |
| 12 | + /// <see cref="IKubernetesObject.Kind"/>. |
| 13 | + /// </summary> |
| 14 | + /// <remarks>This method uses the <see cref="KubernetesScheme.Default"/> <see cref="KubernetesScheme"/>.</remarks> |
| 15 | + public static T New<T>(this IKubernetes client) where T : IKubernetesObject, new() => KubernetesScheme.Default.New<T>(); |
| 16 | + |
| 17 | + /// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>, |
| 18 | + /// <see cref="IKubernetesObject.Kind"/>, and <see cref="V1ObjectMeta.Name"/>. |
| 19 | + /// </summary> |
| 20 | + /// <remarks>This method uses the <see cref="KubernetesScheme.Default"/> <see cref="KubernetesScheme"/>.</remarks> |
| 21 | + public static T New<T>(this IKubernetes client, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => |
| 22 | + KubernetesScheme.Default.New<T>(name); |
| 23 | + |
| 24 | + /// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>, |
| 25 | + /// <see cref="IKubernetesObject.Kind"/>, <see cref="V1ObjectMeta.NamespaceProperty"/>, and <see cref="V1ObjectMeta.Name"/>. |
| 26 | + /// </summary> |
| 27 | + /// <remarks>This method uses the <see cref="KubernetesScheme.Default"/> <see cref="KubernetesScheme"/>.</remarks> |
| 28 | + public static T New<T>(this IKubernetes client, string ns, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => |
| 29 | + KubernetesScheme.Default.New<T>(ns, name); |
| 30 | + |
| 31 | + /// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/> |
| 32 | + /// (<see cref="HttpMethod.Get"/> by default). |
| 33 | + /// </summary> |
| 34 | + public static KubernetesRequest Request(this Kubernetes client, HttpMethod method = null) => |
| 35 | + new KubernetesRequest(client).Method(method); |
| 36 | + |
| 37 | + /// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/> |
| 38 | + /// and resource URI components. |
| 39 | + /// </summary> |
| 40 | + public static KubernetesRequest Request(this Kubernetes client, |
| 41 | + HttpMethod method, string type = null, string ns = null, string name = null, string group = null, string version = null) => |
| 42 | + new KubernetesRequest(client).Method(method).Group(group).Version(version).Type(type).Namespace(ns).Name(name); |
| 43 | + |
| 44 | + /// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object.</summary> |
| 45 | + public static KubernetesRequest Request(this Kubernetes client, Type type) => new KubernetesRequest(client).GVK(type); |
| 46 | + |
| 47 | + /// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary> |
| 48 | + public static KubernetesRequest Request(this Kubernetes client, HttpMethod method, Type type, string ns = null, string name = null) => |
| 49 | + Request(client, method).GVK(type).Namespace(ns).Name(name); |
| 50 | + |
| 51 | + /// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary> |
| 52 | + public static KubernetesRequest Request<T>(this Kubernetes client, string ns = null, string name = null) => |
| 53 | + Request(client, null, typeof(T), ns, name); |
| 54 | + |
| 55 | + /// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given object.</summary> |
| 56 | + public static KubernetesRequest Request(this Kubernetes client, IKubernetesObject obj, bool setBody = true) => |
| 57 | + new KubernetesRequest(client).Set(obj, setBody); |
| 58 | + |
| 59 | + /// <summary>Serializes an object using the <see cref="DefaultSerializer"/>.</summary> |
| 60 | + internal static object Deserialize(string json, Type type) |
| 61 | + { |
| 62 | + using(var reader = new JsonTextReader(new System.IO.StringReader(json))) return DefaultSerializer.Deserialize(reader, type); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary>Serializes an object using the <see cref="DefaultSerializer"/>.</summary> |
| 66 | + internal static string Serialize(object value) |
| 67 | + { |
| 68 | + var sw = new System.IO.StringWriter(new System.Text.StringBuilder(256), System.Globalization.CultureInfo.InvariantCulture); |
| 69 | + using(var writer = new JsonTextWriter(sw)) // do it the same way JsonConvert.SerializeObject does |
| 70 | + { |
| 71 | + writer.Formatting = DefaultSerializer.Formatting; |
| 72 | + DefaultSerializer.Serialize(writer, value, null); |
| 73 | + } |
| 74 | + return sw.ToString(); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary>Gets the <see cref="JsonSerializerSettings"/> used to serialize and deserialize Kubernetes objects.</summary> |
| 78 | + internal static readonly JsonSerializer DefaultSerializer = JsonSerializer.Create(CreateSerializerSettings()); |
| 79 | + |
| 80 | + /// <summary>Creates the JSON serializer settings used for serializing request bodies and deserializing responses.</summary> |
| 81 | + static JsonSerializerSettings CreateSerializerSettings() |
| 82 | + { |
| 83 | + var settings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }; |
| 84 | + settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); |
| 85 | + return settings; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments