-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api, frontend] Endpoints modelization (#268)
- Loading branch information
1 parent
d51e978
commit 95ecf37
Showing
17 changed files
with
883 additions
and
42 deletions.
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
53 changes: 53 additions & 0 deletions
53
openex-api/src/main/java/io/openex/migration/V2_60__Systems_zones.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,53 @@ | ||
package io.openex.migration; | ||
|
||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
|
||
@Component | ||
public class V2_60__Systems_zones extends BaseJavaMigration { | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Connection connection = context.getConnection(); | ||
Statement select = connection.createStatement(); | ||
// Create table system | ||
select.execute(""" | ||
CREATE TABLE systems ( | ||
system_id varchar(255) not null constraint systems_pkey primary key, | ||
system_name varchar(255) not null, | ||
system_type varchar(255) not null, | ||
system_ip varchar(255) not null, | ||
system_hostname varchar(255) not null, | ||
system_os varchar(255) not null, | ||
system_created_at timestamp not null default now(), | ||
system_updated_at timestamp not null default now() | ||
); | ||
CREATE INDEX idx_systems on systems (system_id); | ||
"""); | ||
// Create table zone | ||
select.execute(""" | ||
CREATE TABLE zones ( | ||
zone_id varchar(255) not null constraint zones_pkey primary key, | ||
zone_name varchar(255) not null, | ||
zone_description varchar(255) not null, | ||
zone_created_at timestamp not null default now(), | ||
zone_updated_at timestamp not null default now() | ||
); | ||
CREATE INDEX idx_zones on zones (zone_id); | ||
"""); | ||
// Add association table between system and zone | ||
select.execute(""" | ||
CREATE TABLE systems_zones ( | ||
system_id varchar(255) not null constraint system_id_fk references systems on delete cascade, | ||
zone_id varchar(255) not null constraint zone_id_fk references zones on delete cascade, | ||
constraint systems_zones_pkey primary key (system_id, zone_id) | ||
); | ||
CREATE INDEX idx_systems_zones_system on systems_zones (system_id); | ||
CREATE INDEX idx_systems_zones_zone on systems_zones (zone_id); | ||
"""); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -130,4 +130,4 @@ public Object getThis() { | |
return this; | ||
} | ||
// endregion | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
openex-api/src/main/java/io/openex/rest/system/SystemApi.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,65 @@ | ||
package io.openex.rest.system; | ||
|
||
import io.openex.database.model.System; | ||
import io.openex.database.model.System.OS_TYPE; | ||
import io.openex.database.model.System.SYSTEM_TYPE; | ||
import io.openex.database.repository.SystemRepository; | ||
import io.openex.rest.system.form.SystemInput; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import javax.transaction.Transactional; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotBlank; | ||
import java.time.Instant; | ||
|
||
import static io.openex.database.model.User.ROLE_ADMIN; | ||
import static io.openex.helper.StreamHelper.fromIterable; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class SystemApi { | ||
public static final String SYSTEM_URI = "/api/systems"; | ||
|
||
private final SystemRepository systemRepository; | ||
|
||
// -- CRUD -- | ||
|
||
@PostMapping(SYSTEM_URI) | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public System createSystem(@Valid @RequestBody final SystemInput input) { | ||
System system = new System(); | ||
system.setUpdateAttributes(input); | ||
// Handle enum | ||
system.setType(SYSTEM_TYPE.valueOf(input.getType())); | ||
system.setOs(OS_TYPE.valueOf(input.getOs())); | ||
return this.systemRepository.save(system); | ||
} | ||
|
||
@GetMapping(SYSTEM_URI) | ||
@PreAuthorize("isObserver()") | ||
public Iterable<System> systems() { | ||
return fromIterable(this.systemRepository.findAll()); | ||
} | ||
|
||
@PutMapping(SYSTEM_URI + "/{systemId}") | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public System updateSystem(@PathVariable @NotBlank final String systemId, | ||
@Valid @RequestBody final SystemInput input) { | ||
System system = this.systemRepository.findById(systemId).orElseThrow(); | ||
system.setUpdateAttributes(input); | ||
system.setUpdatedAt(Instant.now()); | ||
return this.systemRepository.save(system); | ||
} | ||
|
||
@DeleteMapping(SYSTEM_URI + "/{systemId}") | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public void deleteSystem(@PathVariable @NotBlank final String systemId) { | ||
this.systemRepository.deleteById(systemId); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
openex-api/src/main/java/io/openex/rest/system/form/SystemInput.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,34 @@ | ||
package io.openex.rest.system.form; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
import static io.openex.config.AppConfig.MANDATORY_MESSAGE; | ||
|
||
@Getter | ||
@Setter | ||
public class SystemInput { | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("system_name") | ||
private String name; | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("system_type") | ||
private String type; | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("system_ip") | ||
private String ip; | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("system_hostname") | ||
private String hostname; | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("system_os") | ||
private String os; | ||
} |
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,85 @@ | ||
package io.openex.rest.zone; | ||
|
||
import io.openex.database.model.System; | ||
import io.openex.database.model.Zone; | ||
import io.openex.database.repository.SystemRepository; | ||
import io.openex.database.repository.ZoneRepository; | ||
import io.openex.rest.zone.form.UpdateSystemsZoneInput; | ||
import io.openex.rest.zone.form.ZoneInput; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import javax.transaction.Transactional; | ||
import javax.validation.Valid; | ||
import javax.validation.constraints.NotBlank; | ||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
import static io.openex.database.model.User.ROLE_ADMIN; | ||
import static io.openex.helper.StreamHelper.fromIterable; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class ZoneApi { | ||
public static final String ZONE_URI = "/api/zones"; | ||
|
||
private final ZoneRepository zoneRepository; | ||
private final SystemRepository systemRepository; | ||
|
||
// -- CRUD -- | ||
|
||
@PostMapping(ZONE_URI) | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public Zone createZone(@Valid @RequestBody final ZoneInput input) { | ||
Zone zone = new Zone(); | ||
zone.setUpdateAttributes(input); | ||
return this.zoneRepository.save(zone); | ||
} | ||
|
||
@GetMapping(ZONE_URI) | ||
@PreAuthorize("isObserver()") | ||
public Iterable<Zone> zones() { | ||
return fromIterable(this.zoneRepository.findAll()); | ||
} | ||
|
||
@PutMapping(ZONE_URI + "/{zoneId}") | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public Zone updateZone(@PathVariable @NotBlank final String zoneId, | ||
@Valid @RequestBody final ZoneInput input) { | ||
Zone zone = this.zoneRepository.findById(zoneId).orElseThrow(); | ||
zone.setUpdateAttributes(input); | ||
zone.setUpdatedAt(Instant.now()); | ||
return this.zoneRepository.save(zone); | ||
} | ||
|
||
@DeleteMapping(ZONE_URI + "/{zoneId}") | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public void deleteZone(@PathVariable @NotBlank final String zoneId) { | ||
this.zoneRepository.deleteById(zoneId); | ||
} | ||
|
||
// -- SYSTEM -- | ||
|
||
@GetMapping(ZONE_URI + "/{zoneId}/systems") | ||
@Transactional(rollbackOn = Exception.class) | ||
@PreAuthorize("isObserver()") | ||
public List<System> zoneSystems(@PathVariable @NotBlank final String zoneId) { | ||
return this.zoneRepository.findById(zoneId).orElseThrow().getSystems(); | ||
} | ||
|
||
@PutMapping(ZONE_URI + "/{zoneId}/systems") | ||
@Transactional(rollbackOn = Exception.class) | ||
@RolesAllowed(ROLE_ADMIN) | ||
public Zone addSystemToZone(@PathVariable @NotBlank final String zoneId, | ||
@Valid @RequestBody final UpdateSystemsZoneInput input) { | ||
Zone zone = this.zoneRepository.findById(zoneId).orElseThrow(); | ||
Iterable<System> systems = this.systemRepository.findAllById(input.getSystemIds()); | ||
zone.setSystems(fromIterable(systems)); | ||
return this.zoneRepository.save(zone); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
openex-api/src/main/java/io/openex/rest/zone/form/UpdateSystemsZoneInput.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,18 @@ | ||
package io.openex.rest.zone.form; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class UpdateSystemsZoneInput { | ||
|
||
@NotEmpty | ||
@JsonProperty("zone_systems") | ||
private List<String> systemIds; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
openex-api/src/main/java/io/openex/rest/zone/form/ZoneInput.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,21 @@ | ||
package io.openex.rest.zone.form; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
import static io.openex.config.AppConfig.MANDATORY_MESSAGE; | ||
|
||
@Getter | ||
@Setter | ||
public class ZoneInput { | ||
|
||
@NotBlank(message = MANDATORY_MESSAGE) | ||
@JsonProperty("zone_name") | ||
private String name; | ||
|
||
@JsonProperty("zone_description") | ||
private String description; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.