-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider/ecs): Ecs instance cache client (#2289)
* Added EC2 Instance cache client. * Removed unneeded import. * Removed @qualifier from cache client constructors, and updated existing ones.
- Loading branch information
1 parent
38d47ad
commit b7b83d4
Showing
5 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
.../main/java/com/netflix/spinnaker/clouddriver/ecs/cache/client/EcsInstanceCacheClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...test/groovy/com/netflix/spinnaker/clouddriver/ecs/cache/EcsInstanceCacheClientSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |