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 */ 032@SuppressWarnings("PMD.DataClass") 033public class K8sDynamicModel implements KubernetesObject { 034 035 private final V1ObjectMeta metadata; 036 private final JsonObject data; 037 038 /** 039 * Instantiates a new model from the JSON representation. 040 * 041 * @param delegate the gson instance to use for extracting structured data 042 * @param json the JSON 043 */ 044 public K8sDynamicModel(Gson delegate, JsonObject json) { 045 this.data = json; 046 metadata = delegate.fromJson(data.get("metadata"), V1ObjectMeta.class); 047 } 048 049 @Override 050 public String getApiVersion() { 051 return apiVersion(); 052 } 053 054 /** 055 * Gets the API version. (Abbreviated method name for convenience.) 056 * 057 * @return the API version 058 */ 059 public String apiVersion() { 060 return data.get("apiVersion").getAsString(); 061 } 062 063 @Override 064 public String getKind() { 065 return kind(); 066 } 067 068 /** 069 * Gets the kind. (Abbreviated method name for convenience.) 070 * 071 * @return the kind 072 */ 073 public String kind() { 074 return data.get("kind").getAsString(); 075 } 076 077 @Override 078 public V1ObjectMeta getMetadata() { 079 return metadata; 080 } 081 082 /** 083 * Gets the metadata. (Abbreviated method name for convenience.) 084 * 085 * @return the metadata 086 */ 087 public V1ObjectMeta metadata() { 088 return metadata; 089 } 090 091 /** 092 * Gets the data. 093 * 094 * @return the data 095 */ 096 public JsonObject data() { 097 return data; 098 } 099 100 /** 101 * Convenience method for getting the status. 102 * 103 * @return the JSON object describing the status 104 */ 105 public JsonObject status() { 106 return data.getAsJsonObject("status"); 107 } 108 109 @Override 110 public String toString() { 111 return data.toString(); 112 } 113 114}