-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Adding pipeline template storage (#222)
- Loading branch information
1 parent
d5f6463
commit e03b778
Showing
12 changed files
with
404 additions
and
1 deletion.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
.../main/groovy/com/netflix/spinnaker/front50/model/pipeline/DefaultPipelineTemplateDAO.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,54 @@ | ||
/* | ||
* Copyright 2017 Netflix, 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.front50.model.pipeline; | ||
|
||
import com.netflix.spectator.api.Registry; | ||
import com.netflix.spinnaker.front50.model.ObjectType; | ||
import com.netflix.spinnaker.front50.model.StorageService; | ||
import com.netflix.spinnaker.front50.model.StorageServiceSupport; | ||
import org.springframework.util.Assert; | ||
import rx.Scheduler; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class DefaultPipelineTemplateDAO extends StorageServiceSupport<PipelineTemplate> implements PipelineTemplateDAO { | ||
|
||
public DefaultPipelineTemplateDAO(StorageService service, | ||
Scheduler scheduler, | ||
long refreshIntervalMs, | ||
Registry registry) { | ||
super(ObjectType.PIPELINE_TEMPLATE, service, scheduler, refreshIntervalMs, registry); | ||
} | ||
|
||
@Override | ||
public Collection<PipelineTemplate> getPipelineTemplatesByScope(List<String> scope) { | ||
return all() | ||
.stream() | ||
.filter(pt -> pt.containsAnyScope(scope)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public PipelineTemplate create(String id, PipelineTemplate item) { | ||
Assert.notNull(item.getId(), "id field must NOT to be null!"); | ||
Assert.notEmpty(item.getScopes(), "scopes field must have at least ONE scope!"); | ||
|
||
update(id, item); | ||
return findById(id); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...0-core/src/main/groovy/com/netflix/spinnaker/front50/model/pipeline/PipelineTemplate.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,74 @@ | ||
/* | ||
* Copyright 2017 Netflix, 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.front50.model.pipeline; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.netflix.spinnaker.front50.model.Timestamped; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PipelineTemplate extends HashMap<String, Object> implements Timestamped { | ||
|
||
@JsonIgnore | ||
@SuppressWarnings("unchecked") | ||
public List<String> getScopes() { | ||
Map<String, Object> metadata = (Map<String, Object>) super.get("metadata"); | ||
if (metadata == null || metadata.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
return (List<String>) metadata.get("scopes"); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return (String) super.get("id"); | ||
} | ||
|
||
@Override | ||
public Long getLastModified() { | ||
String updateTs = (String) super.get("updateTs"); | ||
return (updateTs != null) ? Long.valueOf(updateTs) : null; | ||
} | ||
|
||
@Override | ||
public void setLastModified(Long lastModified) { | ||
super.put("updateTs", lastModified.toString()); | ||
} | ||
|
||
@Override | ||
public String getLastModifiedBy() { | ||
return (String) super.get("lastModifiedBy"); | ||
} | ||
|
||
@Override | ||
public void setLastModifiedBy(String lastModifiedBy) { | ||
super.put("lastModifiedBy", lastModifiedBy); | ||
} | ||
|
||
public boolean containsAnyScope(List<String> scope) { | ||
for (String s : scope) { | ||
for (String s2 : getScopes()) { | ||
if (s.equalsIgnoreCase(s2)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ore/src/main/groovy/com/netflix/spinnaker/front50/model/pipeline/PipelineTemplateDAO.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,26 @@ | ||
/* | ||
* Copyright 2017 Netflix, 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.front50.model.pipeline; | ||
|
||
import com.netflix.spinnaker.front50.model.ItemDAO; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public interface PipelineTemplateDAO extends ItemDAO<PipelineTemplate> { | ||
|
||
Collection<PipelineTemplate> getPipelineTemplatesByScope(List<String> scope); | ||
} |
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
106 changes: 106 additions & 0 deletions
106
...0-redis/src/main/groovy/com/netflix/spinnaker/front50/redis/RedisPipelineTemplateDAO.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,106 @@ | ||
/* | ||
* Copyright 2017 Netflix, 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.front50.redis; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.netflix.spinnaker.front50.exception.NotFoundException; | ||
import com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate; | ||
import com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ScanOptions; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class RedisPipelineTemplateDAO implements PipelineTemplateDAO { | ||
|
||
static final String BOOK_KEEPING_KEY = "com.netflix.spinnaker:front50:pipelineTemplates"; | ||
|
||
RedisTemplate<String, PipelineTemplate> redisTemplate; | ||
|
||
@Override | ||
public Collection<PipelineTemplate> getPipelineTemplatesByScope(List<String> scope) { | ||
return all() | ||
.stream() | ||
.filter(it -> it.containsAnyScope(scope)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public PipelineTemplate findById(String id) throws NotFoundException { | ||
PipelineTemplate pipelineTemplate = (PipelineTemplate) redisTemplate.opsForHash().get(BOOK_KEEPING_KEY, id); | ||
if (pipelineTemplate == null) { | ||
throw new NotFoundException("No pipeline template found with id '" + id + "'"); | ||
} | ||
return pipelineTemplate; | ||
} | ||
|
||
@Override | ||
public Collection<PipelineTemplate> all() { | ||
return all(true); | ||
} | ||
|
||
@Override | ||
public Collection<PipelineTemplate> all(boolean refresh) { | ||
return Lists.newArrayList(redisTemplate.opsForHash().scan(BOOK_KEEPING_KEY, ScanOptions.scanOptions().match("*").build())) | ||
.stream() | ||
.map(e -> (PipelineTemplate) e.getValue()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Collection<PipelineTemplate> history(String id, int maxResults) { | ||
return Lists.newArrayList(findById(id)); | ||
} | ||
|
||
@Override | ||
public PipelineTemplate create(String id, PipelineTemplate item) { | ||
Assert.notNull(item.getId(), "id field must NOT to be null!"); | ||
Assert.notEmpty(item.getScopes(), "scope field must have at least ONE scope!"); | ||
|
||
redisTemplate.opsForHash().put(BOOK_KEEPING_KEY, item.getId(), item); | ||
|
||
return item; | ||
} | ||
|
||
@Override | ||
public void update(String id, PipelineTemplate item) { | ||
item.setLastModified(System.currentTimeMillis()); | ||
create(id, item); | ||
} | ||
|
||
@Override | ||
public void delete(String id) { | ||
redisTemplate.opsForHash().delete(BOOK_KEEPING_KEY, id); | ||
} | ||
|
||
@Override | ||
public void bulkImport(Collection<PipelineTemplate> items) { | ||
items.forEach(it -> create(it.getId(), it)); | ||
} | ||
|
||
@Override | ||
public boolean isHealthy() { | ||
try { | ||
redisTemplate.opsForHash().get("", ""); | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.