From 1916fb8b08be5887a48b1f10ab945d6592ca69a3 Mon Sep 17 00:00:00 2001 From: Michael Plump Date: Mon, 15 Jul 2019 17:06:21 -0400 Subject: [PATCH] refactor(google): convert GoogleApplicationProvider to Java (#3880) * refactor(google): convert GoogleApplicationProvider to Java I did this while debugging something. It serves no particular purpose aside from the helping along the slow gradual death of Groovy. * style(google): fix copyright header --- .../view/GoogleApplicationProvider.groovy | 80 ------------ .../view/GoogleApplicationProvider.java | 115 ++++++++++++++++++ 2 files changed, 115 insertions(+), 80 deletions(-) delete mode 100644 clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.groovy create mode 100644 clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.java diff --git a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.groovy b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.groovy deleted file mode 100644 index 6aa4e66920f..00000000000 --- a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.groovy +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2016 Google, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License") - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.netflix.spinnaker.clouddriver.google.provider.view - -import com.fasterxml.jackson.databind.ObjectMapper -import com.netflix.spectator.api.Registry -import com.netflix.spinnaker.cats.cache.Cache -import com.netflix.spinnaker.cats.cache.CacheData -import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter -import com.netflix.spinnaker.clouddriver.google.GoogleCloudProvider -import com.netflix.spinnaker.clouddriver.google.cache.Keys -import com.netflix.spinnaker.clouddriver.google.model.GoogleApplication -import com.netflix.spinnaker.clouddriver.model.ApplicationProvider -import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.stereotype.Component - -import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.APPLICATIONS -import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.CLUSTERS -import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.INSTANCES - -@Component -class GoogleApplicationProvider implements ApplicationProvider { - @Autowired - Registry registry - - @Autowired - AccountCredentialsProvider accountCredentialsProvider - - @Autowired - Cache cacheView - - @Autowired - ObjectMapper objectMapper - - @Override - Set getApplications(boolean expand) { - def filter = expand ? RelationshipCacheFilter.include(CLUSTERS.ns) : RelationshipCacheFilter.none() - cacheView.getAll(APPLICATIONS.ns, - cacheView.filterIdentifiers(APPLICATIONS.ns, "$GoogleCloudProvider.ID:*"), - filter).collect { applicationFromCacheData(it) } as Set - } - - @Override - GoogleApplication.View getApplication(String name) { - CacheData cacheData = cacheView.get(APPLICATIONS.ns, - Keys.getApplicationKey(name), - RelationshipCacheFilter.include(CLUSTERS.ns, INSTANCES.ns)) - if (cacheData) { - return applicationFromCacheData(cacheData) - } - } - - GoogleApplication.View applicationFromCacheData(CacheData cacheData) { - GoogleApplication.View applicationView = objectMapper.convertValue(cacheData.attributes, GoogleApplication)?.view - - cacheData.relationships[CLUSTERS.ns].each { String clusterKey -> - def clusterKeyParsed = Keys.parse(clusterKey) - applicationView.clusterNames[clusterKeyParsed.account] << clusterKeyParsed.name - } - - applicationView.instances = cacheData?.relationships?.get(INSTANCES.ns).collect { Keys.parse(it) } ?: [] - - applicationView - } -} diff --git a/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.java b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.java new file mode 100644 index 00000000000..3140d55406d --- /dev/null +++ b/clouddriver-google/src/main/groovy/com/netflix/spinnaker/clouddriver/google/provider/view/GoogleApplicationProvider.java @@ -0,0 +1,115 @@ +/* + * Copyright 2019 Google, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.netflix.spinnaker.clouddriver.google.provider.view; + +import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.APPLICATIONS; +import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.CLUSTERS; +import static com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace.INSTANCES; +import static java.util.stream.Collectors.toList; +import static java.util.stream.Collectors.toSet; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableSet; +import com.netflix.spinnaker.cats.cache.Cache; +import com.netflix.spinnaker.cats.cache.CacheData; +import com.netflix.spinnaker.cats.cache.RelationshipCacheFilter; +import com.netflix.spinnaker.clouddriver.google.GoogleCloudProvider; +import com.netflix.spinnaker.clouddriver.google.cache.Keys; +import com.netflix.spinnaker.clouddriver.google.cache.Keys.Namespace; +import com.netflix.spinnaker.clouddriver.google.model.GoogleApplication; +import com.netflix.spinnaker.clouddriver.model.Application; +import com.netflix.spinnaker.clouddriver.model.ApplicationProvider; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +final class GoogleApplicationProvider implements ApplicationProvider { + + private final Cache cacheView; + private final ObjectMapper objectMapper; + + @Autowired + GoogleApplicationProvider(Cache cacheView, ObjectMapper objectMapper) { + this.cacheView = cacheView; + this.objectMapper = objectMapper; + } + + @Override + public Set getApplications(boolean expand) { + + RelationshipCacheFilter filter = + expand ? RelationshipCacheFilter.include(CLUSTERS.getNs()) : RelationshipCacheFilter.none(); + Collection data = + cacheView.getAll( + APPLICATIONS.getNs(), + cacheView.filterIdentifiers(APPLICATIONS.getNs(), GoogleCloudProvider.getID() + ":*"), + filter); + return data.stream().map(this::applicationFromCacheData).collect(toSet()); + } + + @Override + public Application getApplication(String name) { + + CacheData cacheData = + cacheView.get( + APPLICATIONS.getNs(), + Keys.getApplicationKey(name), + RelationshipCacheFilter.include(CLUSTERS.getNs(), INSTANCES.getNs())); + if (cacheData == null) { + return null; + } + + return applicationFromCacheData(cacheData); + } + + private GoogleApplication.View applicationFromCacheData(CacheData cacheData) { + + GoogleApplication application = + objectMapper.convertValue(cacheData.getAttributes(), GoogleApplication.class); + if (application == null) { + return null; + } + + GoogleApplication.View applicationView = application.getView(); + + Collection clusters = getRelationships(cacheData, CLUSTERS); + clusters.forEach( + key -> { + Map parsedKey = Keys.parse(key); + applicationView + .getClusterNames() + .get(parsedKey.get("account")) + .add(parsedKey.get("name")); + }); + + List> instances = + getRelationships(cacheData, INSTANCES).stream().map(Keys::parse).collect(toList()); + applicationView.setInstances(instances); + + return applicationView; + } + + private Collection getRelationships(CacheData cacheData, Namespace namespace) { + Collection result = cacheData.getRelationships().get(namespace.getNs()); + return result != null ? result : ImmutableSet.of(); + } +}