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

[api, frontend] Endpoints modelization (#268) #269

Closed
wants to merge 6 commits into from
Closed
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
22 changes: 11 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ jobs:
command: |
git submodule sync
git submodule update --init --recursive --jobs 8
- run:
- run:
working_directory: ~/openex/openex-front
command: yarn install
- run:
working_directory: ~/openex/openex-front
command: NODE_OPTIONS=--max-old-space-size=8096 yarn build
no_output_timeout: 30m
- run:
- run:
working_directory: ~/
command: cp -a openex openex_docker && cp -a openex openex_musl
- slack/notify:
Expand All @@ -39,15 +39,15 @@ jobs:
- openex
- openex_docker
- openex_musl

build_platform:
working_directory: ~/openex
docker:
- image: maven:3.8.7-openjdk-18
steps:
- attach_workspace:
at: ~/
- run:
- run:
working_directory: ~/openex
command: mvn install -DskipTests -Pdev
- slack/notify:
Expand All @@ -69,7 +69,7 @@ jobs:
steps:
- attach_workspace:
at: ~/
- run:
- run:
working_directory: ~/openex_musl
command: mvn install -DskipTests -Pdev
- slack/notify:
Expand Down Expand Up @@ -214,12 +214,12 @@ jobs:
- ms-teams/report:
only_on_fail: true
webhook_url: $MS_TEAMS_WEBHOOK_URL

notify_rolling:
docker:
- image: "cimg/base:stable"
steps:
- run: sudo apt-get update -qq && sudo apt install curl
- run: sudo apt-get update -qq && sudo apt install curl
- slack/notify:
event: pass
template: basic_success_1
Expand All @@ -230,13 +230,13 @@ jobs:
docker:
- image: "cimg/base:stable"
steps:
- run: sudo apt-get update -qq && sudo apt install curl
- run: sudo apt-get update -qq && sudo apt install curl
- slack/notify:
event: pass
template: basic_success_1
- ms-teams/report:
only_on_fail: false
webhook_url: $MS_TEAMS_WEBHOOK_URL
webhook_url: $MS_TEAMS_WEBHOOK_URL

workflows:
openex:
Expand Down Expand Up @@ -290,7 +290,7 @@ workflows:
ignore: /.*/
- deploy_testing:
requires:
- docker_build_platform_rolling
- docker_build_platform_rolling
- notify_rolling:
requires:
- deploy_testing
Expand All @@ -303,4 +303,4 @@ workflows:
tags:
only: /[0-9]+(\.[0-9]+)+(\.[0-9]+)*/
branches:
ignore: /.*/
ignore: /.*/
10 changes: 10 additions & 0 deletions openex-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>openex-injector-http</artifactId>
<version>3.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.openex</groupId>
<artifactId>openex-injector-ssh</artifactId>
<version>3.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.openex</groupId>
<artifactId>openex-collector-users</artifactId>
Expand Down Expand Up @@ -208,6 +213,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
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);
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ public Object getThis() {
return this;
}
// endregion
}
}
65 changes: 65 additions & 0 deletions openex-api/src/main/java/io/openex/rest/system/SystemApi.java
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);
}
}
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;
}
85 changes: 85 additions & 0 deletions openex-api/src/main/java/io/openex/rest/zone/ZoneApi.java
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);
}
}
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;

}
Loading
Loading