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 */
034public class VmDefinitionStub
035        extends K8sDynamicStubBase<VmDefinition, VmDefinitions> {
036
037    private static DynamicTypeAdapterFactory<VmDefinition,
038            VmDefinitions> taf = new VmDefintionModelTypeAdapterFactory();
039
040    /**
041     * Instantiates a new stub for VM defintions.
042     *
043     * @param client the client
044     * @param context the context
045     * @param namespace the namespace
046     * @param name the name
047     */
048    public VmDefinitionStub(K8sClient client, APIResource context,
049            String namespace, String name) {
050        super(VmDefinition.class, VmDefinitions.class, taf, client,
051            context, namespace, name);
052    }
053
054    /**
055     * Get a dynamic object stub. If the version in parameter
056     * `gvk` is an empty string, the stub refers to the first object with
057     * matching group and kind. 
058     *
059     * @param client the client
060     * @param gvk the group, version and kind
061     * @param namespace the namespace
062     * @param name the name
063     * @return the stub if the object exists
064     * @throws ApiException the api exception
065     */
066    public static VmDefinitionStub get(K8sClient client,
067            GroupVersionKind gvk, String namespace, String name)
068            throws ApiException {
069        return new VmDefinitionStub(client, apiResource(client, gvk), namespace,
070            name);
071    }
072
073    /**
074     * Get a dynamic object stub.
075     *
076     * @param client the client
077     * @param context the context
078     * @param namespace the namespace
079     * @param name the name
080     * @return the stub if the object exists
081     * @throws ApiException the api exception
082     */
083    public static VmDefinitionStub get(K8sClient client,
084            APIResource context, String namespace, String name) {
085        return new VmDefinitionStub(client, context, namespace, name);
086    }
087
088    /**
089     * Creates a stub from yaml.
090     *
091     * @param client the client
092     * @param context the context
093     * @param yaml the yaml
094     * @return the k 8 s dynamic stub
095     * @throws ApiException the api exception
096     */
097    public static VmDefinitionStub createFromYaml(K8sClient client,
098            APIResource context, Reader yaml) throws ApiException {
099        var model = new VmDefinition(client.getJSON().getGson(),
100            K8s.yamlToJson(client, yaml));
101        return K8sGenericStub.create(VmDefinition.class,
102            VmDefinitions.class, client, context, model,
103            (c, ns, n) -> new VmDefinitionStub(c, context, ns, n));
104    }
105
106    /**
107     * Get the stubs for the objects in the given namespace that match
108     * the criteria from the given options.
109     *
110     * @param client the client
111     * @param namespace the namespace
112     * @param options the options
113     * @return the collection
114     * @throws ApiException the api exception
115     */
116    public static Collection<VmDefinitionStub> list(K8sClient client,
117            APIResource context, String namespace, ListOptions options)
118            throws ApiException {
119        return K8sGenericStub.list(VmDefinition.class,
120            VmDefinitions.class, client, context, namespace, options,
121            (c, ns, n) -> new VmDefinitionStub(c, context, ns, n));
122    }
123
124    /**
125     * Get the stubs for the objects in the given namespace.
126     *
127     * @param client the client
128     * @param namespace the namespace
129     * @return the collection
130     * @throws ApiException the api exception
131     */
132    public static Collection<VmDefinitionStub> list(K8sClient client,
133            APIResource context, String namespace)
134            throws ApiException {
135        return list(client, context, namespace, new ListOptions());
136    }
137
138    /**
139     * A factory for creating VmDefinitionModel(s) objects.
140     */
141    public static class VmDefintionModelTypeAdapterFactory extends
142            DynamicTypeAdapterFactory<VmDefinition, VmDefinitions> {
143
144        /**
145         * Instantiates a new dynamic model type adapter factory.
146         */
147        public VmDefintionModelTypeAdapterFactory() {
148            super(VmDefinition.class, VmDefinitions.class);
149        }
150    }
151
152}