Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to manage tags #284

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion service/src/main/java/org/kiwiproject/champagne/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.kiwiproject.champagne.dao.HostDao;
import org.kiwiproject.champagne.dao.ReleaseDao;
import org.kiwiproject.champagne.dao.ReleaseStatusDao;
import org.kiwiproject.champagne.dao.TagDao;
import org.kiwiproject.champagne.dao.TaskDao;
import org.kiwiproject.champagne.dao.TaskStatusDao;
import org.kiwiproject.champagne.dao.UserDao;
Expand All @@ -39,6 +40,7 @@
import org.kiwiproject.champagne.resource.DeployableSystemResource;
import org.kiwiproject.champagne.resource.DeploymentEnvironmentResource;
import org.kiwiproject.champagne.resource.HostConfigurationResource;
import org.kiwiproject.champagne.resource.TagResource;
import org.kiwiproject.champagne.resource.TaskResource;
import org.kiwiproject.champagne.resource.UserResource;
import org.kiwiproject.champagne.resource.filter.DeployableSystemRequestFilter;
Expand Down Expand Up @@ -103,6 +105,7 @@ public void run(AppConfig configuration, Environment environment) {
var componentDao = jdbi.onDemand(ComponentDao.class);
var errorDao = setupApplicationErrors(jdbi, configuration, environment);
var deployableSystemDao = jdbi.onDemand(DeployableSystemDao.class);
var tagDao = jdbi.onDemand(TagDao.class);

var jsonHelper = JsonHelper.newDropwizardJsonHelper();
jdbi.registerRowMapper(Build.class, new BuildMapper(jsonHelper));
Expand All @@ -113,11 +116,12 @@ public void run(AppConfig configuration, Environment environment) {
environment.jersey().register(new AuditRecordResource(auditRecordDao));
environment.jersey().register(new BuildResource(buildDao, jsonHelper));
environment.jersey().register(new DeploymentEnvironmentResource(deploymentEnvironmentDao, auditRecordDao, errorDao, manualTaskService));
environment.jersey().register(new HostConfigurationResource(hostDao, componentDao, auditRecordDao, errorDao));
environment.jersey().register(new HostConfigurationResource(hostDao, componentDao, tagDao, auditRecordDao, errorDao));
environment.jersey().register(new TaskResource(releaseDao, releaseStatusDao, taskDao, taskStatusDao, deploymentEnvironmentDao, auditRecordDao, errorDao));
environment.jersey().register(new UserResource(userDao, auditRecordDao, errorDao));
environment.jersey().register(new ApplicationErrorResource(errorDao));
environment.jersey().register(new DeployableSystemResource(deployableSystemDao, userDao, auditRecordDao, errorDao));
environment.jersey().register(new TagResource(tagDao, auditRecordDao, errorDao));

configureCors(environment);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.kiwiproject.champagne.dao;

import java.util.List;

import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
Expand All @@ -10,23 +12,24 @@
import org.kiwiproject.champagne.dao.mappers.ComponentMapper;
import org.kiwiproject.champagne.model.Component;

import java.util.List;

@RegisterRowMapper(ComponentMapper.class)
public interface ComponentDao {

@SqlQuery("select * from components where tag in (<tags>)")
List<Component> findComponentsByHostTags(@BindList("tags") List<String> tags);
@SqlQuery("select * from components where tag_id in (<tagIds>) order by component_name")
List<Component> findComponentsByHostTags(@BindList("tagIds") List<Long> tagIds);

@SqlQuery("select * from components where deployable_system_id = :deployableSystemId")
@SqlQuery("select * from components where deployable_system_id = :deployableSystemId order by component_name")
List<Component> findComponentsForSystem(@Bind("deployableSystemId") long deployableSystemId);

@SqlUpdate("insert into components (component_name, tag, deployable_system_id) values (:componentName, :tag, :deployableSystemId)")
@SqlQuery("select * from components where deployable_system_id = :deployableSystemId and component_name like :name order by component_name")
List<Component> findComponentsForSystemMatchingName(@Bind("deployableSystemId") long deployableSystemId, @Bind("name") String name);

@SqlUpdate("insert into components (component_name, tag_id, deployable_system_id) values (:componentName, :tagId, :deployableSystemId)")
@GetGeneratedKeys
long insertComponent(@BindBean Component component);

@SqlUpdate("update components set component_name = :componentName, tag = :tag where id = :id")
int updateComponent(@Bind("componentName") String componentName, @Bind("tag") String tag, @Bind("id") long id);
@SqlUpdate("update components set component_name = :componentName, tag_id = :tagId where id = :id")
int updateComponent(@Bind("componentName") String componentName, @Bind("tagId") Long tagId, @Bind("id") long id);

@SqlUpdate("delete from components where id = :id")
int deleteComponent(@Bind("id") long id);
Expand Down
35 changes: 27 additions & 8 deletions service/src/main/java/org/kiwiproject/champagne/dao/HostDao.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
package org.kiwiproject.champagne.dao;

import java.util.List;
import java.util.Optional;

import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.customizer.BindList;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlBatch;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.kiwiproject.champagne.dao.mappers.HostMapper;
import org.kiwiproject.champagne.model.Host;

import java.util.List;
import java.util.Optional;

@RegisterRowMapper(HostMapper.class)
public interface HostDao {

@SqlQuery("select * from hosts where environment_id = :envId and deployable_system_id = :systemId")
@SqlQuery("select * from hosts where environment_id = :envId and deployable_system_id = :systemId order by hostname")
List<Host> findHostsByEnvId(@Bind("envId") Long envId, @Bind("systemId") long systemId);

@SqlUpdate("insert into hosts (environment_id, hostname, tags, source, deployable_system_id) values (:environmentId, :hostname, :tagCsv, :source, :deployableSystemId)")
@SqlUpdate("insert into hosts (environment_id, hostname, source, deployable_system_id) values (:environmentId, :hostname, :source, :deployableSystemId)")
@GetGeneratedKeys
long insertHost(@BindBean Host host, @Bind("tagCsv") String tagCsv);
long insertHost(@BindBean Host host);

default void updateTagList(long hostId, List<Long> tagIds) {
removeTagsForHost(hostId);
addTagsForHost(hostId, tagIds);
}

@SqlUpdate("delete from host_tags where host_id = :hostId")
void removeTagsForHost(@Bind("hostId") long hostId);

@SqlQuery("select tag_id from host_tags where host_id = :hostId")
List<Long> findTagIdsForHost(@Bind("hostId") long hostId);

@SqlQuery("select h.* from hosts h inner join host_tags ht on ht.host_id = h.id where h.deployable_system_id = :systemId and h.environment_id = :envId and ht.tag_id in (<tagIds>) order by h.hostname")
List<Host> findHostsForTagsInEnv(@Bind("systemId") long systemId, @Bind("envId") long envId, @BindList("tagIds") List<Long> tagIds);

@SqlBatch("insert into host_tags (host_id, tag_id) values (:hostId, :tagId)")
void addTagsForHost(@Bind("hostId") long hostId, @Bind("tagId") List<Long> tagIds);

@SqlUpdate("update hosts set hostname = :hostname, tags = :tags where id = :id")
int updateHost(@Bind("hostname") String hostname, @Bind("tags") String tagCsv, @Bind("id") long id);
@SqlUpdate("update hosts set hostname = :hostname, updated_at = now() where id = :id")
int updateHost(@Bind("hostname") String hostname, @Bind("id") long id);

@SqlUpdate("delete from hosts where id = :id")
int deleteHost(@Bind("id") long id);
Expand Down
36 changes: 36 additions & 0 deletions service/src/main/java/org/kiwiproject/champagne/dao/TagDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.kiwiproject.champagne.dao;

import java.util.List;

import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.kiwiproject.champagne.dao.mappers.TagMapper;
import org.kiwiproject.champagne.model.Tag;

@RegisterRowMapper(TagMapper.class)
public interface TagDao {

@SqlQuery("select * from tags where deployable_system_id = :deployableSystemId order by name")
List<Tag> findTagsForSystem(@Bind("deployableSystemId") long deployableSystemId);

@SqlUpdate("insert into tags (name, deployable_system_id) values (:name, :deployableSystemId)")
@GetGeneratedKeys
long insertTag(@BindBean Tag tag);

@SqlUpdate("update tags set name = :name, updated_at = now() where id = :id")
int updateTag(@Bind("id") long id, @Bind("name") String name);

@SqlUpdate("delete from tags where id = :id")
int deleteTag(@Bind("id") long id);

@SqlQuery("select t.* from tags t left join host_tags ht on ht.tag_id = t.id where ht.host_id = :hostId order by t.name")
List<Tag> findTagsForHost(@Bind("hostId") long hostId);

@SqlQuery("select * from tags where id = :id")
Tag findTagById(@Bind("id") long id);

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.kiwiproject.champagne.dao.mappers;

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.kiwiproject.champagne.model.Component;
import static org.kiwiproject.jdbc.KiwiJdbc.instantFromTimestamp;

import java.sql.ResultSet;
import java.sql.SQLException;

import static org.kiwiproject.jdbc.KiwiJdbc.instantFromTimestamp;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.kiwiproject.champagne.model.Component;

public class ComponentMapper implements RowMapper<Component> {

Expand All @@ -18,7 +18,7 @@ public Component map(ResultSet rs, StatementContext ctx) throws SQLException {
.createdAt(instantFromTimestamp(rs, "created_at"))
.updatedAt(instantFromTimestamp(rs, "updated_at"))
.componentName(rs.getString("component_name"))
.tag(rs.getString("tag"))
.tagId(rs.getLong("tag_id"))
.deployableSystemId(rs.getLong("deployable_system_id"))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.kiwiproject.base.KiwiStrings;
import org.kiwiproject.champagne.model.Host;
import org.kiwiproject.champagne.model.Host.Source;

public class HostMapper implements RowMapper<Host> {

@Override
public Host map(ResultSet rs, StatementContext ctx) throws SQLException {
var tagList = KiwiStrings.nullSafeSplitOnCommas(rs.getString("tags"));

return Host.builder()
.id(rs.getLong("id"))
.createdAt(instantFromTimestamp(rs, "created_at"))
.updatedAt(instantFromTimestamp(rs, "updated_at"))
.environmentId(rs.getLong("id"))
.hostname(rs.getString("hostname"))
.tags(tagList)
.source(enumValueOrNull(rs, "source", Source.class))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.kiwiproject.champagne.dao.mappers;

import static org.kiwiproject.jdbc.KiwiJdbc.instantFromTimestamp;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.kiwiproject.champagne.model.Tag;

public class TagMapper implements RowMapper<Tag> {

@Override
public Tag map(ResultSet rs, StatementContext ctx) throws SQLException {
return Tag.builder()
.id(rs.getLong("id"))
.createdAt(instantFromTimestamp(rs, "created_at"))
.updatedAt(instantFromTimestamp(rs, "updated_at"))
.name(rs.getString("name"))
.deployableSystemId(rs.getLong("deployable_system_id"))
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public class Component {
/**
* A tag for the component used to link up with {@link Host} instances for deployments
*/
String tag;
Long tagId;

@With
Tag tag;

/**
* The Deployable System that this component is tied to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kiwiproject.champagne.model;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import lombok.Builder;
Expand Down Expand Up @@ -36,7 +37,9 @@ public enum Source {
/**
* List of tags used to link up components to hosts dynamically.
*/
List<String> tags;
@With
@Builder.Default
List<Tag> tags = new ArrayList<>();

/**
* The source of truth for the definition of this host, whether it is stored in Champagne or pulled dynamically from a cloud.
Expand Down
26 changes: 26 additions & 0 deletions service/src/main/java/org/kiwiproject/champagne/model/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.kiwiproject.champagne.model;

import javax.validation.constraints.NotBlank;
import java.time.Instant;

import lombok.Builder;
import lombok.Value;
import lombok.With;

/**
* Representation of a tag to link Hosts and Components.
*/
@Builder
@Value
public class Tag {

Long id;
chrisrohr marked this conversation as resolved.
Show resolved Hide resolved
Instant createdAt;
Instant updatedAt;

@NotBlank
String name;

@With
Long deployableSystemId;
}
Loading