Skip to content

Commit

Permalink
fix(api): remove jackson annotations from model (#4798)
Browse files Browse the repository at this point in the history
Not having the jackson annotations directly on the model allows them to more easily be moved to an external library without needing a Jackson dependency.
  • Loading branch information
claymccoy committed Aug 13, 2020
1 parent 7e7a978 commit a92a4d8
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
package com.netflix.spinnaker.clouddriver.aws.model

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.clouddriver.jackson.ClouddriverApiModule
import spock.lang.Specification

class AmazonClusterSpec extends Specification {
void "should serialize null loadBalancers and serverGroups as empty arrays"() {
def objectMapper = new ObjectMapper()
def objectMapper = new ObjectMapper().registerModule(new ClouddriverApiModule())

when:
def nullCluster = objectMapper.convertValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.netflix.spinnaker.clouddriver.core.limits.ServiceLimitConfigurationBu
import com.netflix.spinnaker.clouddriver.core.provider.CoreProvider
import com.netflix.spinnaker.clouddriver.core.services.Front50Service
import com.netflix.spinnaker.clouddriver.deploy.DescriptionAuthorizer
import com.netflix.spinnaker.clouddriver.jackson.ClouddriverApiModule
import com.netflix.spinnaker.clouddriver.model.ApplicationProvider
import com.netflix.spinnaker.clouddriver.model.CloudMetricProvider
import com.netflix.spinnaker.clouddriver.model.ClusterProvider
Expand Down Expand Up @@ -136,7 +137,8 @@ class CloudDriverConfig {
jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL)
jacksonObjectMapperBuilder.failOnEmptyBeans(false)
jacksonObjectMapperBuilder.failOnUnknownProperties(false)
jacksonObjectMapperBuilder.modules(new Jdk8Module(), new JavaTimeModule(), new KotlinModule())
jacksonObjectMapperBuilder.modules(
new Jdk8Module(), new JavaTimeModule(), new KotlinModule(), new ClouddriverApiModule())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.netflix.spinnaker.clouddriver.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.netflix.spinnaker.clouddriver.documentation.Empty;
import com.netflix.spinnaker.clouddriver.names.NamerRegistry;
import com.netflix.spinnaker.moniker.Moniker;
Expand Down Expand Up @@ -69,7 +67,6 @@ default Moniker getMoniker() {
* @return a set of {@link ServerGroup} objects or an empty set if none exist
*/
@Empty
@JsonSerialize(nullsUsing = NullCollectionSerializer.class)
Set<? extends ServerGroup> getServerGroups();

/**
Expand All @@ -78,7 +75,6 @@ default Moniker getMoniker() {
* @return a set of {@link LoadBalancer} objects or an empty set if none exist
*/
@Empty
@JsonSerialize(nullsUsing = NullCollectionSerializer.class)
// TODO(ttomsu): Why are load balancers associated with Clusters instead of ServerGroups?
Set<? extends LoadBalancer> getLoadBalancers();

Expand All @@ -93,7 +89,6 @@ class SimpleCluster implements Cluster {
Set<LoadBalancer> loadBalancers;
}

@JsonIgnore
default Map<String, Object> getExtraAttributes() {
return Collections.EMPTY_MAP;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.netflix.spinnaker.clouddriver.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.netflix.spinnaker.clouddriver.documentation.Empty;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -61,22 +60,18 @@ public interface LoadBalancerProvider<T extends LoadBalancer> {
interface Item {
String getName();

@JsonProperty("accounts")
List<? extends ByAccount> getByAccounts();
}

interface ByAccount {
String getName();

@JsonProperty("regions")
List<? extends ByRegion> getByRegions();
}

interface ByRegion {
@JsonProperty("name")
String getName();

@JsonProperty("loadBalancers")
List<? extends Details> getLoadBalancers();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.netflix.spinnaker.clouddriver.model;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.netflix.spinnaker.clouddriver.model.securitygroups.Rule;
import com.netflix.spinnaker.clouddriver.names.NamerRegistry;
import com.netflix.spinnaker.moniker.Moniker;
Expand All @@ -25,7 +24,6 @@
import java.util.Set;

/** A representation of a security group */
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
public interface SecurityGroup {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

package com.netflix.spinnaker.clouddriver.model;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.netflix.spinnaker.clouddriver.documentation.Empty;
import com.netflix.spinnaker.clouddriver.names.NamerRegistry;
import com.netflix.spinnaker.moniker.Moniker;
Expand Down Expand Up @@ -75,7 +70,6 @@ default Moniker getMoniker() {
*
* @return true if the server group is disabled; false otherwise
*/
@JsonGetter
Boolean isDisabled();

/**
Expand Down Expand Up @@ -156,7 +150,6 @@ default Map<String, Object> getTags() {
* This represents all images deployed to the server group. For most providers, this will be a
* singleton.
*/
@JsonIgnore
ImagesSummary getImagesSummary();

/**
Expand All @@ -165,7 +158,6 @@ default Map<String, Object> getTags() {
*
* <p>Deprecated in favor of getImagesSummary, which is a more generic getImageSummary.
*/
@JsonIgnore
@Deprecated
ImageSummary getImageSummary();

Expand All @@ -177,7 +169,6 @@ default Map<String, String> getLabels() {
return new HashMap<>();
}

@JsonIgnore
default Map<String, Object> getExtraAttributes() {
return Collections.EMPTY_MAP;
}
Expand Down Expand Up @@ -241,7 +232,6 @@ public boolean isPinned() {
* Cloud provider-specific data related to the build and VM image of the server group. Deprecated
* in favor of Images summary
*/
@JsonInclude(NON_NULL)
public static interface ImageSummary extends Summary {
String getServerGroupName();

Expand All @@ -256,7 +246,6 @@ public static interface ImageSummary extends Summary {
}

/** Cloud provider-specific data related to the build and VM image of the server group. */
@JsonInclude(NON_NULL)
public static interface ImagesSummary extends Summary {
List<? extends ImageSummary> getSummaries();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.netflix.spinnaker.clouddriver.model.securitygroups;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.util.SortedSet;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
Expand All @@ -27,7 +26,6 @@
* @see IpRangeRule
* @see SecurityGroupRule
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class")
public interface Rule {
/**
* The port ranges associated with this rule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020 Armory
*
* 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.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.netflix.spinnaker.clouddriver.jackson.mixins.*;
import com.netflix.spinnaker.clouddriver.model.Cluster;
import com.netflix.spinnaker.clouddriver.model.LoadBalancerProvider;
import com.netflix.spinnaker.clouddriver.model.SecurityGroup;
import com.netflix.spinnaker.clouddriver.model.ServerGroup;
import com.netflix.spinnaker.clouddriver.model.securitygroups.Rule;

public class ClouddriverApiModule extends SimpleModule {

public ClouddriverApiModule() {
super("Clouddriver API");
}

@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.setMixInAnnotations(SecurityGroup.class, SecurityGroupMixin.class);
context.setMixInAnnotations(Rule.class, RuleMixin.class);
context.setMixInAnnotations(Cluster.class, ClusterMixin.class);
context.setMixInAnnotations(ServerGroup.class, ServerGroupMixin.class);
context.setMixInAnnotations(ServerGroup.ImageSummary.class, ImageSummaryMixin.class);
context.setMixInAnnotations(ServerGroup.ImagesSummary.class, ImagesSummaryMixin.class);
context.setMixInAnnotations(
LoadBalancerProvider.Item.class, LoadBalancerProviderItemMixin.class);
context.setMixInAnnotations(
LoadBalancerProvider.ByAccount.class, LoadBalancerProviderByAccountMixin.class);
context.setMixInAnnotations(
LoadBalancerProvider.ByRegion.class, LoadBalancerProviderByRegionMixin.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020 Armory
*
* 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.jackson.mixins;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.netflix.spinnaker.clouddriver.model.LoadBalancer;
import com.netflix.spinnaker.clouddriver.model.NullCollectionSerializer;
import com.netflix.spinnaker.clouddriver.model.ServerGroup;
import java.util.Map;
import java.util.Set;

public interface ClusterMixin {

@JsonSerialize(nullsUsing = NullCollectionSerializer.class)
Set<? extends ServerGroup> getServerGroups();

@JsonSerialize(nullsUsing = NullCollectionSerializer.class)
Set<? extends LoadBalancer> getLoadBalancers();

@JsonIgnore
Map<String, Object> getExtraAttributes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 Armory
*
* 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.jackson.mixins;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public interface ImageSummaryMixin {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 Armory
*
* 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.jackson.mixins;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public interface ImagesSummaryMixin {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2020 Armory
*
* 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.jackson.mixins;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.netflix.spinnaker.clouddriver.model.LoadBalancerProvider;
import java.util.List;

public interface LoadBalancerProviderByAccountMixin {

@JsonProperty("regions")
List<? extends LoadBalancerProvider.ByRegion> getByRegions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020 Armory
*
* 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.jackson.mixins;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.netflix.spinnaker.clouddriver.model.LoadBalancerProvider;
import java.util.List;

public interface LoadBalancerProviderByRegionMixin {

@JsonProperty("name")
String getName();

@JsonProperty("loadBalancers")
List<? extends LoadBalancerProvider.Details> getLoadBalancers();
}
Loading

0 comments on commit a92a4d8

Please sign in to comment.