Skip to content

Commit

Permalink
feat(app,back) add nautobot (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
helderbetiol authored Feb 23, 2024
1 parent d3ae0fc commit b679439
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 85 deletions.
26 changes: 23 additions & 3 deletions APP/lib/common/api_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ Future<Result<(List<Tenant>, List<DockerContainer>), Exception>>
for (var tool in data["tools"]) {
var container = DockerContainer.fromMap(tool);
if (container.ports.isNotEmpty) {
container.ports = "http://${container.ports.split("-").first}";
container.ports =
"http://${container.ports.split(",").last.split("-").first.trim()}";
container.ports =
container.ports.replaceFirst("0.0.0.0", "localhost");
}
Expand Down Expand Up @@ -801,7 +802,7 @@ Future<Result<String, Exception>> fetchContainerLogs(String name,
}
}

Future<Result<void, Exception>> createNetbox(Netbox netbox) async {
Future<Result<void, Exception>> createNetbox(Nbox netbox) async {
print("API create Netbox");
try {
Uri url = Uri.parse('$apiUrl/api/tools/netbox');
Expand All @@ -820,6 +821,25 @@ Future<Result<void, Exception>> createNetbox(Netbox netbox) async {
}
}

Future<Result<void, Exception>> createNautobot(Nbox nautobot) async {
print("API create nautobot");
try {
Uri url = Uri.parse('$apiUrl/api/tools/nautobot');
final response = await http.post(url,
body: nautobot.toJson(), headers: getHeader(token));
print(response);
if (response.statusCode == 200) {
return const Success(null);
} else {
String data = json.decode(response.body);
return Failure(Exception(
wrapResponseMsg(response, message: "Error creating nautobot $data")));
}
} on Exception catch (e) {
return Failure(e);
}
}

Future<Result<void, Exception>> createOpenDcim(
String dcimPort, adminerPort) async {
print("API create OpenDCIM");
Expand All @@ -845,7 +865,7 @@ Future<Result<void, Exception>> createOpenDcim(
}

Future<Result<void, Exception>> deleteTool(String tool) async {
print("API delete Netbox");
print("API delete Tool");
try {
Uri url = Uri.parse('$apiUrl/api/tools/$tool');
final response = await http.delete(url, headers: getHeader(token));
Expand Down
15 changes: 8 additions & 7 deletions APP/lib/models/netbox.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:convert';

enum Tools { netbox, opendcim, cli }
enum Tools { netbox, nautobot, opendcim, cli }

class Netbox {
// Nbox applies to both netbox and nautobot
class Nbox {
String userName;
String userPassword;
String port;

Netbox(this.userName, this.userPassword, this.port);
Nbox(this.userName, this.userPassword, this.port);

Map<String, dynamic> toMap() {
return <String, dynamic>{
Expand All @@ -17,8 +18,8 @@ class Netbox {
};
}

factory Netbox.fromMap(Map<String, dynamic> map) {
return Netbox(
factory Nbox.fromMap(Map<String, dynamic> map) {
return Nbox(
map['username'].toString(),
map['password'].toString(),
map['port'].toString(),
Expand All @@ -27,6 +28,6 @@ class Netbox {

String toJson() => json.encode(toMap());

factory Netbox.fromJson(String source) =>
Netbox.fromMap(json.decode(source) as Map<String, dynamic>);
factory Nbox.fromJson(String source) =>
Nbox.fromMap(json.decode(source) as Map<String, dynamic>);
}
28 changes: 26 additions & 2 deletions APP/lib/pages/projects_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class _ProjectsPageState extends State<ProjectsPage> {
List<DockerContainer>? _tools;
bool _isSmallDisplay = false;
bool _hasNetbox = false;
bool _hasNautobot = false;
bool _hasOpenDcim = false;
bool _gotData = false;

Expand Down Expand Up @@ -226,6 +227,10 @@ class _ProjectsPageState extends State<ProjectsPage> {
value: Tools.netbox,
child: Text("${localeMsg.create} Netbox"),
),
PopupMenuItem(
value: Tools.nautobot,
child: Text("${localeMsg.create} Nautobot"),
),
PopupMenuItem(
value: Tools.opendcim,
child: Text("${localeMsg.create} OpenDCIM"),
Expand All @@ -251,8 +256,23 @@ class _ProjectsPageState extends State<ProjectsPage> {
showSnackBar(ScaffoldMessenger.of(context),
localeMsg.onlyOneTool("Netbox"));
} else {
showCustomPopup(context,
CreateNetboxPopup(parentCallback: refreshFromChildren));
showCustomPopup(
context,
CreateNboxPopup(
parentCallback: refreshFromChildren,
tool: Tools.netbox));
}
break;
case Tools.nautobot:
if (_hasNautobot) {
showSnackBar(ScaffoldMessenger.of(context),
localeMsg.onlyOneTool("Nautobot"));
} else {
showCustomPopup(
context,
CreateNboxPopup(
parentCallback: refreshFromChildren,
tool: Tools.nautobot));
}
break;
case Tools.opendcim:
Expand Down Expand Up @@ -299,11 +319,15 @@ class _ProjectsPageState extends State<ProjectsPage> {
if (_tools != null && _tools!.isNotEmpty) {
_hasOpenDcim = false;
_hasNetbox = false;
_hasNautobot = false;
for (var tool in _tools!) {
var type = Tools.netbox;
if (tool.name.contains(Tools.opendcim.name)) {
type = Tools.opendcim;
_hasOpenDcim = true;
} else if (tool.name.contains(Tools.nautobot.name)) {
type = Tools.nautobot;
_hasNautobot = true;
} else {
_hasNetbox = true;
}
Expand Down
5 changes: 3 additions & 2 deletions APP/lib/widgets/common/delete_dialog_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class _DeleteDialogState extends State<DeleteDialog> {
Result result;
if (widget.objType == "tenants") {
result = await deleteTenant(obj);
} else if (widget.objType == Tools.netbox.name ||
widget.objType == Tools.opendcim.name) {
} else if (Tools.values
.where((tool) => tool.name == widget.objType)
.isNotEmpty) {
result = await deleteTool(widget.objType);
} else {
result = await removeObject(obj, widget.objType);
Expand Down
45 changes: 29 additions & 16 deletions APP/lib/widgets/tools/create_netbox_popup.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flex_color_picker/flex_color_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ogree_app/common/api_backend.dart';
Expand All @@ -8,26 +9,36 @@ import 'package:ogree_app/common/theme.dart';
import 'package:ogree_app/models/netbox.dart';
import 'package:ogree_app/widgets/common/form_field.dart';

class CreateNetboxPopup extends StatefulWidget {
//Create Nbox: Netbox or Nautobot
class CreateNboxPopup extends StatefulWidget {
Function() parentCallback;
CreateNetboxPopup({super.key, required this.parentCallback});
Tools tool;
CreateNboxPopup(
{super.key, required this.parentCallback, required this.tool});

@override
State<CreateNetboxPopup> createState() => _CreateNetboxPopupState();
State<CreateNboxPopup> createState() => _CreateNboxPopupState();
}

class _CreateNetboxPopupState extends State<CreateNetboxPopup> {
class _CreateNboxPopupState extends State<CreateNboxPopup> {
final _formKey = GlobalKey<FormState>();
String? _userName;
String? _userPassword;
String? _port = "8000";
String? _port;
bool _isLoading = false;
bool _isSmallDisplay = false;

@override
void initState() {
super.initState();
_port = widget.tool == Tools.netbox ? "8000" : "8001";
}

@override
Widget build(BuildContext context) {
final localeMsg = AppLocalizations.of(context)!;
_isSmallDisplay = IsSmallDisplay(MediaQuery.of(context).size.width);
final toolName = widget.tool.name.capitalize;
return Center(
child: Container(
width: 500,
Expand All @@ -45,27 +56,25 @@ class _CreateNetboxPopupState extends State<CreateNetboxPopup> {
backgroundColor: Colors.white,
body: ListView(
padding: EdgeInsets.zero,
//shrinkWrap: true,
children: [
Center(
child: Text(
"${localeMsg.create} netbox",
"${localeMsg.create} $toolName",
style:
Theme.of(context).textTheme.headlineMedium,
)),
// const Divider(height: 35),
const SizedBox(height: 20),
CustomFormField(
save: (newValue) => _userName = newValue,
label: "Netbox user name",
label: "$toolName user name",
icon: Icons.person),
CustomFormField(
save: (newValue) => _userPassword = newValue,
label: "Netbox user password",
label: "$toolName user password",
icon: Icons.lock),
CustomFormField(
save: (newValue) => _port = newValue,
label: "Netbox port",
label: "$toolName port",
initialValue: _port,
icon: Icons.numbers,
formatters: <TextInputFormatter>[
Expand All @@ -90,7 +99,7 @@ class _CreateNetboxPopupState extends State<CreateNetboxPopup> {
const SizedBox(width: 15),
ElevatedButton.icon(
onPressed: () =>
submitCreateNetbox(localeMsg),
submitCreateNbox(localeMsg),
label: Text(localeMsg.create),
icon: _isLoading
? Container(
Expand All @@ -117,16 +126,20 @@ class _CreateNetboxPopupState extends State<CreateNetboxPopup> {
);
}

submitCreateNetbox(AppLocalizations localeMsg) async {
submitCreateNbox(AppLocalizations localeMsg) async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
setState(() {
_isLoading = true;
});
final messenger = ScaffoldMessenger.of(context);
// Create tenant
var result =
await createNetbox(Netbox(_userName!, _userPassword!, _port!));
Result<void, Exception> result;
if (widget.tool == Tools.netbox) {
result = await createNetbox(Nbox(_userName!, _userPassword!, _port!));
} else {
//nautobot
result = await createNautobot(Nbox(_userName!, _userPassword!, _port!));
}
switch (result) {
case Success():
widget.parentCallback();
Expand Down
10 changes: 7 additions & 3 deletions APP/lib/widgets/tools/tool_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ class ToolCard extends StatelessWidget {
backgroundColor: Colors.green.shade600,
label: type == Tools.opendcim
? const Text(" OpenDCIM ")
: const Text(" NETBOX "),
: type == Tools.nautobot
? const Text(" NAUTOBOT ")
: const Text(" NETBOX "),
),
),
type == Tools.opendcim
type != Tools.netbox
? Container()
: CircleAvatar(
radius: 13,
Expand Down Expand Up @@ -82,7 +84,9 @@ class ToolCard extends StatelessWidget {
child: Text("URL:"),
),
Text(
container.ports,
container.ports.isEmpty
? localeMsg.unavailable
: container.ports,
style: TextStyle(backgroundColor: Colors.grey.shade200),
),
],
Expand Down
5 changes: 5 additions & 0 deletions BACK/app/handlers/docker/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ func GetAllApps(c *gin.Context) {
} else {
response["tools"] = append(response["tools"].([]models.ContainerInfo), opendcim...)
}
if nautobot, err := getDockerInfo("nautobot"); err != nil {
println(err.Error())
} else {
response["tools"] = append(response["tools"].([]models.ContainerInfo), nautobot...)
}
c.IndentedJSON(http.StatusOK, response)
}
Loading

0 comments on commit b679439

Please sign in to comment.