001/*
002 * VM-Operator
003 * Copyright (C) 2024 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.vmoperator.common;
020
021import io.kubernetes.client.Discovery.APIResource;
022import io.kubernetes.client.apimachinery.GroupVersionKind;
023import io.kubernetes.client.openapi.ApiException;
024import io.kubernetes.client.util.generic.options.ListOptions;
025import java.io.Reader;
026import java.util.Collection;
027
028/**
029 * A stub for namespaced custom objects. It uses a dynamic model
030 * (see {@link K8sDynamicModel}) for representing the object's
031 * state and can therefore be used for any kind of object, especially
032 * custom objects.
033 */
034@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
035public class VmDefinitionStub
036        extends K8sDynamicStubBase<VmDefinitionModel, VmDefinitionModels> {
037
038    private static DynamicTypeAdapterFactory<VmDefinitionModel,
039            VmDefinitionModels> taf = new VmDefintionModelTypeAdapterFactory();
040
041    /**
042     * Instantiates a new stub for VM defintions.
043     *
044     * @param client the client
045     * @param context the context
046     * @param namespace the namespace
047     * @param name the name
048     */
049    public VmDefinitionStub(K8sClient client, APIResource context,
050            String namespace, String name) {
051        super(VmDefinitionModel.class, VmDefinitionModels.class, taf, client,
052            context, namespace, name);
053    }
054
055    /**
056     * Get a dynamic object stub. If the version in parameter
057     * `gvk` is an empty string, the stub refers to the first object with
058     * matching group and kind. 
059     *
060     * @param client the client
061     * @param gvk the group, version and kind
062     * @param namespace the namespace
063     * @param name the name
064     * @return the stub if the object exists
065     * @throws ApiException the api exception
066     */
067    @SuppressWarnings({ "PMD.AvoidBranchingStatementAsLastInLoop",
068        "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseObjectForClearerAPI" })
069    public static VmDefinitionStub get(K8sClient client,
070            GroupVersionKind gvk, String namespace, String name)
071            throws ApiException {
072        return new VmDefinitionStub(client, apiResource(client, gvk), namespace,
073            name);
074    }
075
076    /**
077     * Get a dynamic object stub.
078     *
079     * @param client the client
080     * @param context the context
081     * @param namespace the namespace
082     * @param name the name
083     * @return the stub if the object exists
084     * @throws ApiException the api exception
085     */
086    @SuppressWarnings({ "PMD.AvoidBranchingStatementAsLastInLoop",
087        "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseObjectForClearerAPI" })
088    public static VmDefinitionStub get(K8sClient client,
089            APIResource context, String namespace, String name) {
090        return new VmDefinitionStub(client, context, namespace, name);
091    }
092
093    /**
094     * Creates a stub from yaml.
095     *
096     * @param client the client
097     * @param context the context
098     * @param yaml the yaml
099     * @return the k 8 s dynamic stub
100     * @throws ApiException the api exception
101     */
102    public static VmDefinitionStub createFromYaml(K8sClient client,
103            APIResource context, Reader yaml) throws ApiException {
104        var model = new VmDefinitionModel(client.getJSON().getGson(),
105            K8s.yamlToJson(client, yaml));
106        return K8sGenericStub.create(VmDefinitionModel.class,
107            VmDefinitionModels.class, client, context, model,
108            (c, ns, n) -> new VmDefinitionStub(c, context, ns, n));
109    }
110
111    /**
112     * Get the stubs for the objects in the given namespace that match
113     * the criteria from the given options.
114     *
115     * @param client the client
116     * @param namespace the namespace
117     * @param options the options
118     * @return the collection
119     * @throws ApiException the api exception
120     */
121    public static Collection<VmDefinitionStub> list(K8sClient client,
122            APIResource context, String namespace, ListOptions options)
123            throws ApiException {
124        return K8sGenericStub.list(VmDefinitionModel.class,
125            VmDefinitionModels.class, client, context, namespace, options,
126            (c, ns, n) -> new VmDefinitionStub(c, context, ns, n));
127    }
128
129    /**
130     * Get the stubs for the objects in the given namespace.
131     *
132     * @param client the client
133     * @param namespace the namespace
134     * @return the collection
135     * @throws ApiException the api exception
136     */
137    public static Collection<VmDefinitionStub> list(K8sClient client,
138            APIResource context, String namespace)
139            throws ApiException {
140        return list(client, context, namespace, new ListOptions());
141    }
142
143    /**
144     * A factory for creating VmDefinitionModel(s) objects.
145     */
146    public static class VmDefintionModelTypeAdapterFactory extends
147            DynamicTypeAdapterFactory<VmDefinitionModel, VmDefinitionModels> {
148
149        /**
150         * Instantiates a new dynamic model type adapter factory.
151         */
152        public VmDefintionModelTypeAdapterFactory() {
153            super(VmDefinitionModel.class, VmDefinitionModels.class);
154        }
155    }
156
157}