001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2017-2018 microBean. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 014 * implied. See the License for the specific language governing 015 * permissions and limitations under the License. 016 */ 017package org.microbean.kubernetes.controller; 018 019import java.util.logging.Level; 020import java.util.logging.Logger; 021 022import io.fabric8.kubernetes.api.model.HasMetadata; 023import io.fabric8.kubernetes.api.model.ObjectMeta; 024 025/** 026 * A utility class for working with {@link HasMetadata} resources. 027 * 028 * @author <a href="https://about.me/lairdnelson" 029 * target="_parent">Laird Nelson</a> 030 * 031 * @see #getKey(HasMetadata) 032 * 033 * @see HasMetadata 034 */ 035public final class HasMetadatas { 036 037 038 /* 039 * Constructors. 040 */ 041 042 043 /** 044 * Creates a new {@link HasMetadatas}. 045 */ 046 private HasMetadatas() { 047 super(); 048 } 049 050 051 /* 052 * Static methods. 053 */ 054 055 056 /** 057 * Returns a <em>key</em> for the supplied {@link HasMetadata} 058 * derived from its {@linkplain ObjectMeta#getName() name} and 059 * {@linkplain ObjectMeta#getNamespace() namespace}. 060 * 061 * <p>This method may return {@code null}.</p> 062 * 063 * @param resource the {@link HasMetadata} for which a key should be 064 * returned; may be {@code null} in which case {@code null} will be 065 * returned 066 * 067 * @return a key for the supplied {@link HasMetadata} 068 * 069 * @exception IllegalStateException if the supplied {@link 070 * HasMetadata}'s {@linkplain ObjectMeta metadata}'s {@link 071 * ObjectMeta#getName()} method returns {@code null} or an 072 * {@linkplain String#isEmpty() empty} {@link String} 073 * 074 * @see HasMetadata#getMetadata() 075 * 076 * @see ObjectMeta#getName() 077 * 078 * @see ObjectMeta#getNamespace() 079 */ 080 public static final Object getKey(final HasMetadata resource) { 081 final String cn = HasMetadatas.class.getName(); 082 final String mn = "getKey"; 083 final Logger logger = Logger.getLogger(cn); 084 assert logger != null; 085 if (logger.isLoggable(Level.FINER)) { 086 logger.entering(cn, mn, resource); 087 } 088 089 final Object returnValue; 090 if (resource == null) { 091 returnValue = null; 092 } else { 093 final ObjectMeta metadata = resource.getMetadata(); 094 if (metadata == null) { 095 returnValue = null; 096 } else { 097 String name = metadata.getName(); 098 if (name == null) { 099 throw new IllegalStateException("metadata.getName() == null"); 100 } else if (name.isEmpty()) { 101 throw new IllegalStateException("metadata.getName().isEmpty()"); 102 } 103 final String namespace = metadata.getNamespace(); 104 if (namespace == null || namespace.isEmpty()) { 105 returnValue = name; 106 } else { 107 returnValue = new StringBuilder(namespace).append("/").append(name).toString(); 108 } 109 } 110 } 111 112 if (logger.isLoggable(Level.FINER)) { 113 logger.exiting(cn, mn, returnValue); 114 } 115 return returnValue; 116 } 117 118}