From 417f39d39afe50588e1acbcc60094201310af2ac Mon Sep 17 00:00:00 2001 From: Eron Wright Date: Thu, 23 May 2024 17:30:54 -0700 Subject: [PATCH] patch resources --- .../apiextensions/CustomResource.java | 74 +----- .../apiextensions/CustomResourcePatch.java | 140 +++++++++++ .../CustomResourcePatchArgs.java | 34 +++ .../CustomResourcePatchArgsBase.java | 204 ++++++++++++++++ .../pulumi/kubernetes/apiextensions/Util.java | 99 ++++++++ .../apiextensions/CustomResource.java | 229 +----------------- .../apiextensions/CustomResourceArgs.java | 35 +-- .../apiextensions/CustomResourceArgsBase.java | 205 +--------------- .../apiextensions/CustomResourcePatch.java | 1 + .../CustomResourcePatchArgs.java | 1 + .../CustomResourcePatchArgsBase.java | 1 + .../pulumi/kubernetes/apiextensions/Util.java | 1 + 12 files changed, 485 insertions(+), 539 deletions(-) create mode 100644 provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java create mode 100644 provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java create mode 100644 provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java create mode 100644 provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java mode change 100644 => 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java mode change 100644 => 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java mode change 100644 => 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java create mode 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java create mode 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java create mode 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java create mode 120000 sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java diff --git a/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java index fed69a2933..75342e3b86 100644 --- a/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java +++ b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java @@ -4,38 +4,22 @@ package com.pulumi.kubernetes.apiextensions; import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.concurrent.ExecutionException; -import java.util.stream.Collectors; -import java.util.stream.Stream; import javax.annotation.Nullable; import com.pulumi.core.Either; import com.pulumi.core.Output; import com.pulumi.core.annotations.Export; -import com.pulumi.core.annotations.Import; import com.pulumi.core.annotations.ResourceType; import com.pulumi.core.internal.Codegen; import com.pulumi.core.internal.Internal; import com.pulumi.core.internal.OutputInternal; -import com.pulumi.core.internal.annotations.ImportMetadata; import com.pulumi.kubernetes.Utilities; import com.pulumi.kubernetes.meta.v1.outputs.ObjectMeta; import com.pulumi.resources.ResourceArgs; -import net.bytebuddy.ByteBuddy; -import net.bytebuddy.description.annotation.AnnotationDescription; -import net.bytebuddy.implementation.FieldAccessor; -import net.bytebuddy.implementation.Implementation; -import net.bytebuddy.implementation.MethodCall; - /** * CustomResource represents an instance of a CustomResourceDefinition (CRD). For example, the * CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to @@ -135,16 +119,6 @@ private static String getOutputValue(OutputInternal o) { } } - private static class Field { - public Optional value; - public Either importAnnotation; - - public Field(Optional value, Either importAnnotation) { - this.value = value; - this.importAnnotation = importAnnotation; - } - } - private static ResourceArgs makeArgs(@Nullable CustomResourceArgsBase args) { if (args == null) { return null; @@ -153,53 +127,7 @@ private static ResourceArgs makeArgs(@Nullable CustomResourceArgsBase args) { // optimization: if there are no "other" fields, we can just return the args as-is. return args; } - - // collect the input properties from the annotated fields of the user-supplied args, - // plus the dynamic input properties within otherFields. - var importFields = - ImportMetadata.of(args.getClass()).values().stream() - .map(info -> new Field(info.getFieldOutput(args), Either.ofLeft(info.getAnnotation()))); - var otherFields = - args.otherFields().orElseGet(Map::of).entrySet().stream() - .map(entry -> new Field(Optional.ofNullable(entry.getValue()), Either.ofRight( - AnnotationDescription.Builder.ofType(Import.class).define("name", entry.getKey()).build()))); - List fields = - Stream.concat(importFields, otherFields) - .collect(Collectors.toList()); - - try { - // define a dynamic subclass of ResourceArgs with a field for each input property - // and a constructor that takes the property values and assigns them to the fields. - var t = new ByteBuddy().subclass(ResourceArgs.class); - Implementation.Composable c = MethodCall.invoke(ResourceArgs.class.getConstructor()); - - var paramTypes = new ArrayList>(); - var paramValues = new ArrayList(); - - for (var arg : fields) { - // define a field of type Output with @Import(name="foo") - var fieldName = String.format("f%d", paramTypes.size()); - var f = t.defineField(fieldName, Output.class, Modifier.PUBLIC); - t = arg.importAnnotation.either(a -> f.annotateField(a), ad -> f.annotateField(ad)); - - // bind the field to the corresponding constructor parameter - c = c.andThen(FieldAccessor.ofField(fieldName).setsArgumentAt(paramTypes.size())); - - paramTypes.add(Output.class); - paramValues.add(arg.value.map(v -> v instanceof Output ? v : Output.ofNullable(v)).orElse(null)); - } - - return (ResourceArgs) t - .defineConstructor(Modifier.PUBLIC).withParameters(paramTypes).intercept(c) - .make() - .load(CustomResource.class.getClassLoader()) - .getLoaded().getConstructors()[0] - .newInstance(paramValues.toArray()); - - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + return Util.generateResourceArgs(args, args.otherFields().get()); } private static com.pulumi.resources.CustomResourceOptions makeResourceOptions( diff --git a/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java new file mode 100644 index 0000000000..c8ae3ea09b --- /dev/null +++ b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java @@ -0,0 +1,140 @@ +// *** WARNING: this file was generated by pulumi-java-gen. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +package com.pulumi.kubernetes.apiextensions; + +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +import javax.annotation.Nullable; + +import com.pulumi.core.Output; +import com.pulumi.core.annotations.Export; +import com.pulumi.core.annotations.ResourceType; +import com.pulumi.core.internal.Codegen; +import com.pulumi.core.internal.Internal; +import com.pulumi.core.internal.OutputInternal; +import com.pulumi.kubernetes.Utilities; +import com.pulumi.kubernetes.meta.v1.outputs.ObjectMetaPatch; +import com.pulumi.resources.ResourceArgs; + +/** + * Patch resources are used to modify existing Kubernetes resources by using + * Server-Side Apply updates. The name of the resource must be specified, but all other properties are optional. More than + * one patch may be applied to the same resource, and a random FieldManager name will be used for each Patch resource. + * Conflicts will result in an error by default, but can be forced using the "pulumi.com/patchForce" annotation. See the + * [Server-Side Apply Docs](https://www.pulumi.com/registry/packages/kubernetes/how-to-guides/managing-resources-with-server-side-apply/) for + * additional information about using Server-Side Apply to manage Kubernetes resources with Pulumi. + * + * CustomResourcePatch represents an instance of a CustomResourceDefinition (CRD). For example, the + * CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to + * instantiate this as a Pulumi resource, one could call `new CustomResourcePatch`, passing the + * `ServiceMonitor` resource definition as an argument. + */ +@ResourceType(type="kubernetes:apiextensions:CustomResourcePatch") +public class CustomResourcePatch extends com.pulumi.resources.CustomResource { + /** + * APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + * + */ + @Export(name="apiVersion", refs={String.class}, tree="[0]") + private Output apiVersion; + + /** + * @return APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + * + */ + public Output> apiVersion() { + return Codegen.optional(this.apiVersion); + } + + /** + * Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + * + */ + @Export(name="kind", refs={String.class}, tree="[0]") + private Output kind; + + /** + * @return Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + * + */ + public Output> kind() { + return Codegen.optional(this.kind); + } + /** + * Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + * + */ + @Export(name="metadata", refs={ObjectMetaPatch.class}, tree="[0]") + private Output metadata; + + /** + * @return Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + * + */ + public Output> metadata() { + return Codegen.optional(this.metadata); + } + + /** + * + * @param name The _unique_ name of the resulting resource. + */ + public CustomResourcePatch(String name) { + this(name, CustomResourcePatchArgs.Empty); + } + + /** + * + * @param name The _unique_ name of the resulting resource. + * @param args The arguments to use to populate this resource's properties. + */ + public CustomResourcePatch(String name, @Nullable CustomResourcePatchArgsBase args) { + this(name, args, null); + } + + /** + * + * @param name The _unique_ name of the resulting resource. + * @param args The arguments to use to populate this resource's properties. + * @param options A bag of options that control this resource's behavior. + */ + public CustomResourcePatch(String name, @Nullable CustomResourcePatchArgsBase args, @Nullable com.pulumi.resources.CustomResourceOptions options) { + super(makeType(args), name, makeArgs(args), makeResourceOptions(options, Codegen.empty())); + } + + private static String makeType(@Nullable CustomResourcePatchArgsBase args) { + String apiVersion = args.apiVersion().map(Internal::of).map(CustomResourcePatch::getOutputValue).orElse(""); + String kind = args.kind().map(Internal::of).map(CustomResourcePatch::getOutputValue).orElse(""); + return String.format("kubernetes:%s:%sPatch", apiVersion, kind); + } + + private static String getOutputValue(OutputInternal o) { + try { + return o.getValueOrDefault("").get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } + + private static ResourceArgs makeArgs(@Nullable CustomResourcePatchArgsBase args) { + if (args == null) { + return null; + } + if (args.otherFields().isEmpty() || args.otherFields().get().isEmpty()) { + // optimization: if there are no "other" fields, we can just use the args as-is. + // Otherwise we generate a subclass of ResourceArgs that includes the "other" fields. + return args; + } + return Util.generateResourceArgs(args, args.otherFields().get()); + } + + private static com.pulumi.resources.CustomResourceOptions makeResourceOptions( + @Nullable com.pulumi.resources.CustomResourceOptions options, @Nullable Output id) { + var defaultOptions = com.pulumi.resources.CustomResourceOptions.builder() + .version(Utilities.getVersion()) + .build(); + return com.pulumi.resources.CustomResourceOptions.merge(defaultOptions, options, id); + } +} diff --git a/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java new file mode 100644 index 0000000000..51ed968a70 --- /dev/null +++ b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java @@ -0,0 +1,34 @@ +// *** WARNING: this file was generated by pulumi-java-gen. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +package com.pulumi.kubernetes.apiextensions; + +/** + * The set of arguments for constructing a CustomResourcePatch resource. + * + * NOTE: This type is fairly loose, since other than `apiVersion` and `kind`, + * there are no fields required across all CRDs. Use otherFields(...) to specify + * additional fields. + */ +public final class CustomResourcePatchArgs extends CustomResourcePatchArgsBase { + + public static final CustomResourcePatchArgs Empty = new CustomResourcePatchArgs(); + + public static Builder builder() { + return new Builder(); + } + + public static Builder builder(CustomResourcePatchArgs defaults) { + return new Builder(defaults); + } + + public static final class Builder extends CustomResourcePatchArgsBase.Builder { + public Builder() { + super(new CustomResourcePatchArgs()); + } + + public Builder(CustomResourcePatchArgs defaults) { + super(new CustomResourcePatchArgs(), defaults); + } + } +} diff --git a/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java new file mode 100644 index 0000000000..78f904bfd5 --- /dev/null +++ b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java @@ -0,0 +1,204 @@ +// *** WARNING: this file was generated by pulumi-java-gen. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +package com.pulumi.kubernetes.apiextensions; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; + +import javax.annotation.Nullable; + +import com.pulumi.core.Output; +import com.pulumi.core.annotations.Import; +import com.pulumi.core.internal.Codegen; +import com.pulumi.kubernetes.meta.v1.inputs.ObjectMetaPatchArgs; + +/** + * CustomResourcePatchArgsBase is the base class for a user-defined + * CustomResourcePatchArgs. + */ +public abstract class CustomResourcePatchArgsBase extends com.pulumi.resources.ResourceArgs { + + /** + * APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + * + */ + @Import(name="apiVersion") + @Nullable Output apiVersion; + + /** + * @return APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + * + */ + public Optional> apiVersion() { + return Optional.ofNullable(this.apiVersion); + } + + /** + * Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + * + */ + @Import(name="kind") + @Nullable Output kind; + + /** + * @return Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + * + */ + public Optional> kind() { + return Optional.ofNullable(this.kind); + } + + /** + * Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + * + */ + @Import(name="metadata") + @Nullable Output metadata; + + /** + * @return Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + * + */ + public Optional> metadata() { + return Optional.ofNullable(this.metadata); + } + + /** + * Untyped map that holds any user-defined fields. + * + */ + @Nullable Map> otherFields; + + /** + * @return Untyped map that holds any user-defined fields. + * + */ + public Optional>> otherFields() { + return Optional.ofNullable(this.otherFields); + } + + protected CustomResourcePatchArgsBase() { + } + + public static class Builder> { + protected T $; + + protected Builder(T $) { + this.$ = $; + } + + protected Builder(T $, T defaults) { + this($); + copy(Objects.requireNonNull(defaults)); + } + + protected void copy(T args) { + $.apiVersion = args.apiVersion; + $.kind = args.kind; + $.metadata = args.metadata; + $.otherFields = args.otherFields; + } + + /** + * @param apiVersion APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + * + * @return builder + * + */ + public U apiVersion(@Nullable Output apiVersion) { + $.apiVersion = apiVersion; + return (U) this; + } + + /** + * @param apiVersion APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + * + * @return builder + * + */ + public U apiVersion(String apiVersion) { + return apiVersion(Output.of(apiVersion)); + } + + /** + * @param kind Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + * + * @return builder + * + */ + public U kind(@Nullable Output kind) { + $.kind = kind; + return (U) this; + } + + /** + * @param kind Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + * + * @return builder + * + */ + public U kind(String kind) { + return kind(Output.of(kind)); + } + + /** + * @param metadata Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + * + * @return builder + * + */ + public U metadata(@Nullable Output metadata) { + $.metadata = metadata; + return (U) this; + } + + /** + * @param metadata Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + * + * @return builder + * + */ + public U metadata(ObjectMetaPatchArgs metadata) { + return metadata(Output.of(metadata)); + } + + /** + * @param fields Untyped map that holds any user-defined fields (which may be Output values). + * + * @return builder + * + */ + public U otherFields(Map fields) { + var map = new LinkedHashMap>(); + for (var entry : fields.entrySet()) { + var value = entry.getValue(); + map.put(entry.getKey(), (value instanceof Output) ? (Output) value : Output.of(value)); + } + $.otherFields = map; + return (U) this; + } + + /** + * @param field The user-defined field name. + * @param value The field value, which may be an Output value. + * @return builder + * + */ + public U otherField(String field, Object value) { + var map = $.otherFields != null ? new LinkedHashMap($.otherFields) : new LinkedHashMap(); + map.put(field, value instanceof Output ? value : Output.of(value)); + $.otherFields = map; + return (U) this; + } + + public T build() { + $.apiVersion = Codegen.stringProp("apiVersion").output().arg($.apiVersion).getNullable(); + $.kind = Codegen.stringProp("kind").output().arg($.kind).getNullable(); + return (T) $; + } + } + +} diff --git a/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java new file mode 100644 index 0000000000..9a9491fbfb --- /dev/null +++ b/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java @@ -0,0 +1,99 @@ +// *** WARNING: this file was generated by pulumi-java-gen. *** +// *** Do not edit by hand unless you're certain you know what you are doing! *** + +package com.pulumi.kubernetes.apiextensions; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.pulumi.core.Either; +import com.pulumi.core.Output; +import com.pulumi.core.annotations.Import; +import com.pulumi.core.internal.annotations.ImportMetadata; +import com.pulumi.resources.ResourceArgs; + +import net.bytebuddy.ByteBuddy; +import net.bytebuddy.description.annotation.AnnotationDescription; +import net.bytebuddy.implementation.FieldAccessor; +import net.bytebuddy.implementation.Implementation; +import net.bytebuddy.implementation.MethodCall; + +class Util { + private Util() { + } + + /** + * Generates a ResourceArgs containing the union of inputs in 'args' and 'others'. + * + * @param args a ResourceArgs containing fields with @Import annotations. + * @param others a map of additional inputs to include in the generated ResourceArgs. + * @return + */ + public static ResourceArgs generateResourceArgs(ResourceArgs args, Map> others) { + // define a field for each Java field that has an @Import annotation, and copy the annotation. + var importFields = + ImportMetadata.of(args.getClass()).values().stream() + .map(info -> new ImportField(info.getFieldOutput(args), Either.ofLeft(info.getAnnotation()))); + // define a field for each input in 'others' map, with a simple @Import annotation. + var otherFields = + others.entrySet().stream() + .map(entry -> new ImportField(Optional.ofNullable(entry.getValue()), Either.ofRight( + AnnotationDescription.Builder.ofType(Import.class).define("name", entry.getKey()).build()))); + List fields = + Stream.concat(importFields, otherFields) + .collect(Collectors.toList()); + + try { + // define a dynamic subclass of ResourceArgs with a field for each input property + // and a constructor that takes the property values and assigns them to the fields. + var t = new ByteBuddy().subclass(ResourceArgs.class); + Implementation.Composable c = MethodCall.invoke(ResourceArgs.class.getConstructor()); + + var paramTypes = new ArrayList>(); + var paramValues = new ArrayList(); + + for (var arg : fields) { + // define a field of type Output with @Import(name="foo") + var fieldName = String.format("f%d", paramTypes.size()); + var f = t.defineField(fieldName, Output.class, Modifier.PUBLIC); + t = arg.importAnnotation.either(a -> f.annotateField(a), ad -> f.annotateField(ad)); + + // bind the field to the corresponding constructor parameter + c = c.andThen(FieldAccessor.ofField(fieldName).setsArgumentAt(paramTypes.size())); + + paramTypes.add(Output.class); + paramValues.add(arg.value.map(v -> v instanceof Output ? v : Output.ofNullable(v)).orElse(null)); + } + + return (ResourceArgs) t + .defineConstructor(Modifier.PUBLIC).withParameters(paramTypes).intercept(c) + .make() + .load(Util.class.getClassLoader()) + .getLoaded().getConstructors()[0] + .newInstance(paramValues.toArray()); + + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException + | InvocationTargetException e) { + throw new RuntimeException("Unable to generate ResourceArgs", e); + } + } + + private static class ImportField { + public Optional value; + public Either importAnnotation; + + public ImportField(Optional value, Either importAnnotation) { + this.value = value; + this.importAnnotation = importAnnotation; + } + } +} diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java deleted file mode 100644 index fed69a2933..0000000000 --- a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java +++ /dev/null @@ -1,228 +0,0 @@ -// *** WARNING: this file was generated by pulumi-java-gen. *** -// *** Do not edit by hand unless you're certain you know what you are doing! *** - -package com.pulumi.kubernetes.apiextensions; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ExecutionException; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import javax.annotation.Nullable; - -import com.pulumi.core.Either; -import com.pulumi.core.Output; -import com.pulumi.core.annotations.Export; -import com.pulumi.core.annotations.Import; -import com.pulumi.core.annotations.ResourceType; -import com.pulumi.core.internal.Codegen; -import com.pulumi.core.internal.Internal; -import com.pulumi.core.internal.OutputInternal; -import com.pulumi.core.internal.annotations.ImportMetadata; -import com.pulumi.kubernetes.Utilities; -import com.pulumi.kubernetes.meta.v1.outputs.ObjectMeta; -import com.pulumi.resources.ResourceArgs; - -import net.bytebuddy.ByteBuddy; -import net.bytebuddy.description.annotation.AnnotationDescription; -import net.bytebuddy.implementation.FieldAccessor; -import net.bytebuddy.implementation.Implementation; -import net.bytebuddy.implementation.MethodCall; - -/** - * CustomResource represents an instance of a CustomResourceDefinition (CRD). For example, the - * CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to - * instantiate this as a Pulumi resource, one could call `new CustomResource`, passing the - * `ServiceMonitor` resource definition as an argument. - */ -@ResourceType(type="kubernetes:apiextensions:CustomResource") -public class CustomResource extends com.pulumi.resources.CustomResource { - /** - * APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources - * - */ - @Export(name="apiVersion", refs={String.class}, tree="[0]") - private Output apiVersion; - - /** - * @return APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources - * - */ - public Output apiVersion() { - return this.apiVersion; - } - - /** - * Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds - * - */ - @Export(name="kind", refs={String.class}, tree="[0]") - private Output kind; - - /** - * @return Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds - * - */ - public Output kind() { - return this.kind; - } - - /** - * Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata - * - */ - @Export(name="metadata", refs={ObjectMeta.class}, tree="[0]") - private Output metadata; - - /** - * @return Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata - * - */ - public Output metadata() { - return this.metadata; - } - - /** - * - * @param name The _unique_ name of the resulting resource. - */ - public CustomResource(String name) { - this(name, CustomResourceArgs.Empty); - } - - /** - * - * @param name The _unique_ name of the resulting resource. - * @param args The arguments to use to populate this resource's properties. - */ - public CustomResource(String name, @Nullable CustomResourceArgsBase args) { - this(name, args, null); - } - - /** - * - * @param name The _unique_ name of the resulting resource. - * @param args The arguments to use to populate this resource's properties. - * @param options A bag of options that control this resource's behavior. - */ - public CustomResource(String name, @Nullable CustomResourceArgsBase args, @Nullable com.pulumi.resources.CustomResourceOptions options) { - super(makeType(args), name, makeArgs(args), makeResourceOptions(options, Codegen.empty())); - } - - protected CustomResource(String name, String apiVersion, String kind, Output id, - @Nullable com.pulumi.resources.CustomResourceOptions options) { - super(String.format("kubernetes:%s:%s", apiVersion, kind), name, null, makeResourceOptions(options, id)); - } - - private static String makeType(@Nullable CustomResourceArgsBase args) { - String apiVersion = args.apiVersion().map(Internal::of).map(CustomResource::getOutputValue).orElse(""); - String kind = args.kind().map(Internal::of).map(CustomResource::getOutputValue).orElse(""); - return String.format("kubernetes:%s:%s", apiVersion, kind); - } - - private static String getOutputValue(OutputInternal o) { - try { - return o.getValueOrDefault("").get(); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } - } - - private static class Field { - public Optional value; - public Either importAnnotation; - - public Field(Optional value, Either importAnnotation) { - this.value = value; - this.importAnnotation = importAnnotation; - } - } - - private static ResourceArgs makeArgs(@Nullable CustomResourceArgsBase args) { - if (args == null) { - return null; - } - if (args.otherFields().isEmpty() || args.otherFields().get().isEmpty()) { - // optimization: if there are no "other" fields, we can just return the args as-is. - return args; - } - - // collect the input properties from the annotated fields of the user-supplied args, - // plus the dynamic input properties within otherFields. - var importFields = - ImportMetadata.of(args.getClass()).values().stream() - .map(info -> new Field(info.getFieldOutput(args), Either.ofLeft(info.getAnnotation()))); - var otherFields = - args.otherFields().orElseGet(Map::of).entrySet().stream() - .map(entry -> new Field(Optional.ofNullable(entry.getValue()), Either.ofRight( - AnnotationDescription.Builder.ofType(Import.class).define("name", entry.getKey()).build()))); - List fields = - Stream.concat(importFields, otherFields) - .collect(Collectors.toList()); - - try { - // define a dynamic subclass of ResourceArgs with a field for each input property - // and a constructor that takes the property values and assigns them to the fields. - var t = new ByteBuddy().subclass(ResourceArgs.class); - Implementation.Composable c = MethodCall.invoke(ResourceArgs.class.getConstructor()); - - var paramTypes = new ArrayList>(); - var paramValues = new ArrayList(); - - for (var arg : fields) { - // define a field of type Output with @Import(name="foo") - var fieldName = String.format("f%d", paramTypes.size()); - var f = t.defineField(fieldName, Output.class, Modifier.PUBLIC); - t = arg.importAnnotation.either(a -> f.annotateField(a), ad -> f.annotateField(ad)); - - // bind the field to the corresponding constructor parameter - c = c.andThen(FieldAccessor.ofField(fieldName).setsArgumentAt(paramTypes.size())); - - paramTypes.add(Output.class); - paramValues.add(arg.value.map(v -> v instanceof Output ? v : Output.ofNullable(v)).orElse(null)); - } - - return (ResourceArgs) t - .defineConstructor(Modifier.PUBLIC).withParameters(paramTypes).intercept(c) - .make() - .load(CustomResource.class.getClassLoader()) - .getLoaded().getConstructors()[0] - .newInstance(paramValues.toArray()); - - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException - | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - - private static com.pulumi.resources.CustomResourceOptions makeResourceOptions( - @Nullable com.pulumi.resources.CustomResourceOptions options, @Nullable Output id) { - var defaultOptions = com.pulumi.resources.CustomResourceOptions.builder() - .version(Utilities.getVersion()) - .build(); - return com.pulumi.resources.CustomResourceOptions.merge(defaultOptions, options, id); - } - - /** - * Get the state of an existing `CustomResource` resource, as identified by `id`. - * Typically this ID is of the form [namespace]/[name]; if [namespace] is omitted, - * then (per Kubernetes convention) the ID becomes default/[name]. - * - * @param name The _unique_ name of the resulting resource. - * @param apiVersion The API version of the CustomResource we wish to select, as defined by the CRD. - * @param kind The kind of the CustomResource we wish to select, as defined by the CRD. - * @param id An ID for the Kubernetes resource to retrieve. - * @param options Optional settings to control the behavior of the CustomResource. - */ - public static CustomResource get(String name, String apiVersion, String kind, Output id, - @Nullable com.pulumi.resources.CustomResourceOptions options) { - return new CustomResource(name, apiVersion, kind, id, options); - } -} diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java new file mode 120000 index 0000000000..665b6d88e0 --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java \ No newline at end of file diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java deleted file mode 100644 index b342836b16..0000000000 --- a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java +++ /dev/null @@ -1,34 +0,0 @@ -// *** WARNING: this file was generated by pulumi-java-gen. *** -// *** Do not edit by hand unless you're certain you know what you are doing! *** - -package com.pulumi.kubernetes.apiextensions; - -/** - * The set of arguments for constructing a CustomResource resource. - * - * NOTE: This type is fairly loose, since other than `apiVersion` and `kind`, - * there are no fields required across all CRDs. Use otherFields(...) to specify - * additional fields. - */ -public final class CustomResourceArgs extends CustomResourceArgsBase { - - public static final CustomResourceArgs Empty = new CustomResourceArgs(); - - public static Builder builder() { - return new Builder(); - } - - public static Builder builder(CustomResourceArgs defaults) { - return new Builder(defaults); - } - - public static final class Builder extends CustomResourceArgsBase.Builder { - public Builder() { - super(new CustomResourceArgs()); - } - - public Builder(CustomResourceArgs defaults) { - super(new CustomResourceArgs(), defaults); - } - } -} diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java new file mode 120000 index 0000000000..bf70065022 --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgs.java \ No newline at end of file diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java deleted file mode 100644 index 0cb26e19b9..0000000000 --- a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java +++ /dev/null @@ -1,204 +0,0 @@ -// *** WARNING: this file was generated by pulumi-java-gen. *** -// *** Do not edit by hand unless you're certain you know what you are doing! *** - -package com.pulumi.kubernetes.apiextensions; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; - -import javax.annotation.Nullable; - -import com.pulumi.core.Output; -import com.pulumi.core.annotations.Import; -import com.pulumi.core.internal.Codegen; -import com.pulumi.kubernetes.meta.v1.inputs.ObjectMetaArgs; - -/** - * CustomResourceArgsBase is the base class for a user-defined - * CustomResourceArgs. - */ -public abstract class CustomResourceArgsBase extends com.pulumi.resources.ResourceArgs { - - /** - * APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources - * - */ - @Import(name="apiVersion") - @Nullable Output apiVersion; - - /** - * @return APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources - * - */ - public Optional> apiVersion() { - return Optional.ofNullable(this.apiVersion); - } - - /** - * Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds - * - */ - @Import(name="kind") - @Nullable Output kind; - - /** - * @return Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds - * - */ - public Optional> kind() { - return Optional.ofNullable(this.kind); - } - - /** - * Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata - * - */ - @Import(name="metadata") - @Nullable Output metadata; - - /** - * @return Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata - * - */ - public Optional> metadata() { - return Optional.ofNullable(this.metadata); - } - - /** - * Untyped map that holds any user-defined fields. - * - */ - @Nullable Map> otherFields; - - /** - * @return Untyped map that holds any user-defined fields. - * - */ - public Optional>> otherFields() { - return Optional.ofNullable(this.otherFields); - } - - protected CustomResourceArgsBase() { - } - - public static class Builder> { - protected T $; - - protected Builder(T $) { - this.$ = $; - } - - protected Builder(T $, T defaults) { - this($); - copy(Objects.requireNonNull(defaults)); - } - - protected void copy(T args) { - $.apiVersion = args.apiVersion; - $.kind = args.kind; - $.metadata = args.metadata; - $.otherFields = args.otherFields; - } - - /** - * @param apiVersion APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources - * - * @return builder - * - */ - public U apiVersion(@Nullable Output apiVersion) { - $.apiVersion = apiVersion; - return (U) this; - } - - /** - * @param apiVersion APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources - * - * @return builder - * - */ - public U apiVersion(String apiVersion) { - return apiVersion(Output.of(apiVersion)); - } - - /** - * @param kind Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds - * - * @return builder - * - */ - public U kind(@Nullable Output kind) { - $.kind = kind; - return (U) this; - } - - /** - * @param kind Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds - * - * @return builder - * - */ - public U kind(String kind) { - return kind(Output.of(kind)); - } - - /** - * @param metadata Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata - * - * @return builder - * - */ - public U metadata(@Nullable Output metadata) { - $.metadata = metadata; - return (U) this; - } - - /** - * @param metadata Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata - * - * @return builder - * - */ - public U metadata(ObjectMetaArgs metadata) { - return metadata(Output.of(metadata)); - } - - /** - * @param fields Untyped map that holds any user-defined fields (which may be Output values). - * - * @return builder - * - */ - public U otherFields(Map fields) { - var map = new LinkedHashMap>(); - for (var entry : fields.entrySet()) { - var value = entry.getValue(); - map.put(entry.getKey(), (value instanceof Output) ? (Output) value : Output.of(value)); - } - $.otherFields = map; - return (U) this; - } - - /** - * @param field The user-defined field name. - * @param value The field value, which may be an Output value. - * @return builder - * - */ - public U otherField(String field, Object value) { - var map = $.otherFields != null ? new LinkedHashMap($.otherFields) : new LinkedHashMap(); - map.put(field, value instanceof Output ? value : Output.of(value)); - $.otherFields = map; - return (U) this; - } - - public T build() { - $.apiVersion = Codegen.stringProp("apiVersion").output().arg($.apiVersion).getNullable(); - $.kind = Codegen.stringProp("kind").output().arg($.kind).getNullable(); - return (T) $; - } - } - -} diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java new file mode 120000 index 0000000000..0148908caf --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourceArgsBase.java \ No newline at end of file diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java new file mode 120000 index 0000000000..b5c01b813b --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatch.java \ No newline at end of file diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java new file mode 120000 index 0000000000..64efab1d8e --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgs.java \ No newline at end of file diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java new file mode 120000 index 0000000000..cdf5e620d1 --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResourcePatchArgsBase.java \ No newline at end of file diff --git a/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java new file mode 120000 index 0000000000..688a4808fc --- /dev/null +++ b/sdk/java/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java @@ -0,0 +1 @@ +/Users/eronwright/Pulumi/pulumi-kubernetes/provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/Util.java \ No newline at end of file