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 com.google.gson.Gson;
022import com.google.gson.JsonObject;
023import io.kubernetes.client.common.KubernetesObject;
024import io.kubernetes.client.openapi.models.V1ObjectMeta;
025
026/**
027 * Represents a Kubernetes object using a JSON data structure.
028 * Some information that is common to all Kubernetes objects,
029 * notably the metadata, is made available through the methods
030 * defined by {@link KubernetesObject}.
031 */
032public class K8sDynamicModel implements KubernetesObject {
033
034    private final V1ObjectMeta metadata;
035    private final JsonObject data;
036
037    /**
038     * Instantiates a new model from the JSON representation.
039     *
040     * @param delegate the gson instance to use for extracting structured data
041     * @param json the JSON
042     */
043    public K8sDynamicModel(Gson delegate, JsonObject json) {
044        this.data = json;
045        metadata = delegate.fromJson(data.get("metadata"), V1ObjectMeta.class);
046    }
047
048    @Override
049    public String getApiVersion() {
050        return apiVersion();
051    }
052
053    /**
054     * Gets the API version. (Abbreviated method name for convenience.)
055     *
056     * @return the API version
057     */
058    public String apiVersion() {
059        return data.get("apiVersion").getAsString();
060    }
061
062    @Override
063    public String getKind() {
064        return kind();
065    }
066
067    /**
068     * Gets the kind. (Abbreviated method name for convenience.)
069     *
070     * @return the kind
071     */
072    public String kind() {
073        return data.get("kind").getAsString();
074    }
075
076    @Override
077    public V1ObjectMeta getMetadata() {
078        return metadata;
079    }
080
081    /**
082     * Gets the metadata. (Abbreviated method name for convenience.)
083     *
084     * @return the metadata
085     */
086    public V1ObjectMeta metadata() {
087        return metadata;
088    }
089
090    /**
091     * Gets the data.
092     *
093     * @return the data
094     */
095    public JsonObject data() {
096        return data;
097    }
098
099    /**
100     * Convenience method for getting the status. 
101     *
102     * @return the JSON object describing the status
103     */
104    public JsonObject statusJson() {
105        return data.getAsJsonObject("status");
106    }
107
108    @Override
109    public String toString() {
110        return data.toString();
111    }
112
113}