Skip to content

Commit

Permalink
feat(provider/ecs): Ecs instance cache client (#2289)
Browse files Browse the repository at this point in the history
* Added EC2 Instance cache client.

* Removed unneeded import.

* Removed @qualifier from cache client constructors, and updated existing ones.
  • Loading branch information
dkirillov authored and robzienert committed Jan 19, 2018
1 parent 38d47ad commit b7b83d4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 13 deletions.
@@ -0,0 +1,59 @@
/*
* Copyright 2017 Lookout, 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.ecs.cache.client;

import com.amazonaws.services.ec2.model.Instance;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.cats.cache.Cache;
import com.netflix.spinnaker.clouddriver.aws.data.Keys;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES;

@Component
public class EcsInstanceCacheClient {

private final Cache cacheView;
private final ObjectMapper objectMapper;

public EcsInstanceCacheClient(Cache cacheView, ObjectMapper objectMapper) {
this.cacheView = cacheView;
this.objectMapper = objectMapper;
}

public Set<Instance> findAll() {
return find("*", "*", "*");
}

public Set<Instance> find(String instanceId, String account, String region) {
instanceId = instanceId != null ? instanceId : "*";
account = account != null ? account : "*";
region = region != null ? region : "*";

String searchKey = Keys.getInstanceKey(instanceId, account, region);
Collection<String> instanceKeys = cacheView.filterIdentifiers(INSTANCES.getNs(), searchKey);

return cacheView.getAll(INSTANCES.getNs(), instanceKeys).stream()
.map(cacheData -> objectMapper.convertValue(cacheData.getAttributes(), Instance.class))
.collect(Collectors.toSet());
}

}
Expand Up @@ -31,12 +31,12 @@
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.SERVICES;

public class ServiceCacheClient extends AbstractCacheClient<Service> {
private ObjectMapper mapper;
private ObjectMapper objectMapper;

@Autowired
public ServiceCacheClient(Cache cacheView, ObjectMapper mapper) {
public ServiceCacheClient(Cache cacheView, ObjectMapper objectMapper) {
super(cacheView, SERVICES.toString());
this.mapper = mapper;
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -62,8 +62,8 @@ protected Service convert(CacheData cacheData) {
List<LoadBalancer> deserializedLoadbalancers = new ArrayList<>(loadBalancers.size());

for (Map<String, Object> serializedLoadbalancer : loadBalancers) {
if(serializedLoadbalancer!=null) {
deserializedLoadbalancers.add(mapper.convertValue(serializedLoadbalancer, LoadBalancer.class));
if (serializedLoadbalancer != null) {
deserializedLoadbalancers.add(objectMapper.convertValue(serializedLoadbalancer, LoadBalancer.class));
}
}

Expand Down
Expand Up @@ -31,12 +31,12 @@
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.TASKS;

public class TaskCacheClient extends AbstractCacheClient<Task> {
private ObjectMapper mapper;
private ObjectMapper objectMapper;

@Autowired
public TaskCacheClient(Cache cacheView, ObjectMapper mapper) {
public TaskCacheClient(Cache cacheView, ObjectMapper objectMapper) {
super(cacheView, TASKS.toString());
this.mapper = mapper;
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -58,7 +58,7 @@ protected Task convert(CacheData cacheData) {

for (Map<String, Object> serializedContainer : containers) {
if (serializedContainer != null) {
deserializedLoadbalancers.add(mapper.convertValue(serializedContainer, Container.class));
deserializedLoadbalancers.add(objectMapper.convertValue(serializedContainer, Container.class));
}
}

Expand Down
Expand Up @@ -31,12 +31,12 @@
import static com.netflix.spinnaker.clouddriver.ecs.cache.Keys.Namespace.TASK_DEFINITIONS;

public class TaskDefinitionCacheClient extends AbstractCacheClient<TaskDefinition> {
private ObjectMapper mapper;
private ObjectMapper objectMapper;

@Autowired
public TaskDefinitionCacheClient(Cache cacheView, ObjectMapper mapper) {
public TaskDefinitionCacheClient(Cache cacheView, ObjectMapper objectMapper) {
super(cacheView, TASK_DEFINITIONS.toString());
this.mapper = mapper;
this.objectMapper = objectMapper;
}

@Override
Expand All @@ -52,7 +52,7 @@ protected TaskDefinition convert(CacheData cacheData) {

for (Map<String, Object> serializedContainerDefinitions : containerDefinitions) {
if (serializedContainerDefinitions != null) {
deserializedContainerDefinitions.add(mapper.convertValue(serializedContainerDefinitions, ContainerDefinition.class));
deserializedContainerDefinitions.add(objectMapper.convertValue(serializedContainerDefinitions, ContainerDefinition.class));
}
}

Expand Down
@@ -0,0 +1,63 @@
/*
* Copyright 2017 Lookout, 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.ecs.cache

import com.amazonaws.services.ec2.model.Instance
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.cats.cache.DefaultCacheData
import com.netflix.spinnaker.clouddriver.aws.data.Keys
import com.netflix.spinnaker.clouddriver.ecs.cache.client.EcsInstanceCacheClient
import spock.lang.Specification
import spock.lang.Subject

import static com.netflix.spinnaker.clouddriver.core.provider.agent.Namespace.INSTANCES

class EcsInstanceCacheClientSpec extends Specification {
def cacheView = Mock(Cache)
def objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

@Subject
def client = new EcsInstanceCacheClient(cacheView, objectMapper)

def 'should convert into an object'() {
given:
def instanceId = 'instance-id'
def key = Keys.getInstanceKey(instanceId, 'test-account', 'us-west-1')
def givenInstance = new Instance(
instanceId: instanceId,
privateIpAddress: '127.0.0.1',
publicDnsName: 'localhost',
launchTime: new Date()
)

def attributes = objectMapper.convertValue(givenInstance, Map)
def instanceCache = new DefaultCacheData(key, attributes, [:])

cacheView.filterIdentifiers(INSTANCES.getNs(), _) >> [key]
cacheView.getAll(INSTANCES.getNs(), [key]) >> [instanceCache]

when:
def foundInstances = client.findAll()

then:
foundInstances.size() == 1
foundInstances[0] == givenInstance
}
}

0 comments on commit b7b83d4

Please sign in to comment.