Skip to content
Open
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
5 changes: 5 additions & 0 deletions cmake/CliFboss2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ add_library(fboss2_lib
fboss/cli/fboss2/utils/PortMap.cpp
fboss/cli/fboss2/utils/Table.cpp
fboss/cli/fboss2/utils/HostInfo.h
fboss/cli/fboss2/utils/InterfaceList.h
fboss/cli/fboss2/utils/InterfaceList.cpp
fboss/cli/fboss2/utils/FilterOp.h
fboss/cli/fboss2/utils/AggregateOp.h
fboss/cli/fboss2/utils/AggregateUtils.h
Expand Down Expand Up @@ -577,6 +579,9 @@ add_library(fboss2_config_lib
fboss/cli/fboss2/commands/config/CmdConfigAppliedInfo.cpp
fboss/cli/fboss2/commands/config/CmdConfigReload.h
fboss/cli/fboss2/commands/config/CmdConfigReload.cpp
fboss/cli/fboss2/commands/config/interface/CmdConfigInterface.h
fboss/cli/fboss2/commands/config/interface/CmdConfigInterfaceDescription.h
fboss/cli/fboss2/commands/config/interface/CmdConfigInterfaceDescription.cpp
fboss/cli/fboss2/commands/config/history/CmdConfigHistory.h
fboss/cli/fboss2/commands/config/history/CmdConfigHistory.cpp
fboss/cli/fboss2/commands/config/rollback/CmdConfigRollback.h
Expand Down
1 change: 1 addition & 0 deletions cmake/CliFboss2Test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_executable(fboss2_cmd_test
fboss/cli/fboss2/test/TestMain.cpp
fboss/cli/fboss2/test/CmdConfigAppliedInfoTest.cpp
fboss/cli/fboss2/test/CmdConfigHistoryTest.cpp
fboss/cli/fboss2/test/CmdConfigInterfaceDescriptionTest.cpp
fboss/cli/fboss2/test/CmdConfigReloadTest.cpp
fboss/cli/fboss2/test/CmdConfigSessionDiffTest.cpp
fboss/cli/fboss2/test/CmdConfigSessionTest.cpp
Expand Down
5 changes: 5 additions & 0 deletions fboss/cli/fboss2/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ cpp_library(
name = "cmd-common-utils",
srcs = [
"utils/CmdUtilsCommon.cpp",
"utils/InterfaceList.cpp",
],
headers = [
"commands/clear/CmdClearUtils.h",
Expand All @@ -154,6 +155,7 @@ cpp_library(
"utils/CmdUtilsCommon.h",
"utils/FilterUtils.h",
"utils/HostInfo.h",
"utils/InterfaceList.h",
],
exported_deps = [
":cmd-global-options",
Expand Down Expand Up @@ -773,6 +775,7 @@ cpp_library(
"commands/config/CmdConfigAppliedInfo.cpp",
"commands/config/CmdConfigReload.cpp",
"commands/config/history/CmdConfigHistory.cpp",
"commands/config/interface/CmdConfigInterfaceDescription.cpp",
"commands/config/rollback/CmdConfigRollback.cpp",
"commands/config/session/CmdConfigSessionCommit.cpp",
"commands/config/session/CmdConfigSessionDiff.cpp",
Expand All @@ -782,6 +785,8 @@ cpp_library(
"commands/config/CmdConfigAppliedInfo.h",
"commands/config/CmdConfigReload.h",
"commands/config/history/CmdConfigHistory.h",
"commands/config/interface/CmdConfigInterface.h",
"commands/config/interface/CmdConfigInterfaceDescription.h",
"commands/config/rollback/CmdConfigRollback.h",
"commands/config/session/CmdConfigSessionCommit.h",
"commands/config/session/CmdConfigSessionDiff.h",
Expand Down
2 changes: 2 additions & 0 deletions fboss/cli/fboss2/CmdHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class CmdHandler {
RetType result;
try {
result = queryClientHelper(hostInfo);
} catch (std::invalid_argument const& err) {
errStr = folly::to<std::string>("Invalid argument: ", err.what());
} catch (std::exception const& err) {
errStr = folly::to<std::string>("Thrift call failed: '", err.what(), "'");
}
Expand Down
6 changes: 6 additions & 0 deletions fboss/cli/fboss2/CmdHandlerImplConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "fboss/cli/fboss2/commands/config/CmdConfigAppliedInfo.h"
#include "fboss/cli/fboss2/commands/config/CmdConfigReload.h"
#include "fboss/cli/fboss2/commands/config/history/CmdConfigHistory.h"
#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterface.h"
#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterfaceDescription.h"
#include "fboss/cli/fboss2/commands/config/rollback/CmdConfigRollback.h"
#include "fboss/cli/fboss2/commands/config/session/CmdConfigSessionCommit.h"
#include "fboss/cli/fboss2/commands/config/session/CmdConfigSessionDiff.h"
Expand All @@ -25,6 +27,10 @@ namespace facebook::fboss {
template void
CmdHandler<CmdConfigAppliedInfo, CmdConfigAppliedInfoTraits>::run();
template void CmdHandler<CmdConfigReload, CmdConfigReloadTraits>::run();
template void CmdHandler<CmdConfigInterface, CmdConfigInterfaceTraits>::run();
template void CmdHandler<
CmdConfigInterfaceDescription,
CmdConfigInterfaceDescriptionTraits>::run();
template void CmdHandler<CmdConfigHistory, CmdConfigHistoryTraits>::run();
template void CmdHandler<CmdConfigRollback, CmdConfigRollbackTraits>::run();
template void
Expand Down
16 changes: 16 additions & 0 deletions fboss/cli/fboss2/CmdListConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "fboss/cli/fboss2/commands/config/CmdConfigAppliedInfo.h"
#include "fboss/cli/fboss2/commands/config/CmdConfigReload.h"
#include "fboss/cli/fboss2/commands/config/history/CmdConfigHistory.h"
#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterface.h"
#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterfaceDescription.h"
#include "fboss/cli/fboss2/commands/config/rollback/CmdConfigRollback.h"
#include "fboss/cli/fboss2/commands/config/session/CmdConfigSessionCommit.h"
#include "fboss/cli/fboss2/commands/config/session/CmdConfigSessionDiff.h"
Expand All @@ -34,6 +36,20 @@ const CommandTree& kConfigCommandTree() {
commandHandler<CmdConfigHistory>,
argTypeHandler<CmdConfigHistoryTraits>},

{
"config",
"interface",
"Configure interface settings",
commandHandler<CmdConfigInterface>,
argTypeHandler<CmdConfigInterfaceTraits>,
{{
"description",
"Set interface description",
commandHandler<CmdConfigInterfaceDescription>,
argTypeHandler<CmdConfigInterfaceDescriptionTraits>,
}},
},

{
"config",
"session",
Expand Down
3 changes: 3 additions & 0 deletions fboss/cli/fboss2/CmdSubcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ CLI::App* CmdSubcommands::addCommand(
case utils::ObjectArgTypeId::OBJECT_ARG_TYPE_FAN_PWM:
subCmd->add_option("pwm", args, "Fan PWM (0..100) or 'disable'");
break;
case utils::ObjectArgTypeId::OBJECT_ARG_TYPE_ID_INTERFACE_LIST:
subCmd->add_option("interfaces", args, "Interface(s)");
break;
case utils::ObjectArgTypeId::OBJECT_ARG_TYPE_ID_REVISION_LIST:
subCmd->add_option(
"revisions", args, "Revision(s) in the form 'rN' or 'current'");
Expand Down
38 changes: 38 additions & 0 deletions fboss/cli/fboss2/commands/config/interface/CmdConfigInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#pragma once

#include "fboss/cli/fboss2/CmdHandler.h"
#include "fboss/cli/fboss2/utils/CmdUtils.h"

namespace facebook::fboss {

struct CmdConfigInterfaceTraits : public WriteCommandTraits {
static constexpr utils::ObjectArgTypeId ObjectArgTypeId =
utils::ObjectArgTypeId::OBJECT_ARG_TYPE_ID_PORT_LIST;
using ObjectArgType = std::vector<std::string>;
using RetType = std::string;
};

class CmdConfigInterface
: public CmdHandler<CmdConfigInterface, CmdConfigInterfaceTraits> {
public:
RetType queryClient(
const HostInfo& /* hostInfo */,
const ObjectArgType& /* interfaceNames */) {
throw std::runtime_error(
"Incomplete command, please use one of the subcommands");
}

void printOutput(const RetType& /* model */) {}
};

} // namespace facebook::fboss
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterfaceDescription.h"

#include <folly/Conv.h>
#include "fboss/cli/fboss2/session/ConfigSession.h"

namespace facebook::fboss {

CmdConfigInterfaceDescriptionTraits::RetType
CmdConfigInterfaceDescription::queryClient(
const HostInfo& hostInfo,
const utils::InterfaceList& interfaces,
const ObjectArgType& description) {
if (interfaces.empty()) {
throw std::invalid_argument("No interface name provided");
}

std::string descriptionStr = description.data()[0];

// Update description for all resolved ports
for (const utils::Intf& intf : interfaces) {
cfg::Port* port = intf.getPort();
if (port) {
port->description() = descriptionStr;
}
}

// Save the updated config
ConfigSession::getInstance().saveConfig();

std::string interfaceList = folly::join(", ", interfaces.getNames());
return "Successfully set description for interface(s) " + interfaceList;
}

void CmdConfigInterfaceDescription::printOutput(const RetType& logMsg) {
std::cout << logMsg << std::endl;
}

} // namespace facebook::fboss
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

#pragma once

#include "fboss/cli/fboss2/CmdHandler.h"
#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterface.h"
#include "fboss/cli/fboss2/utils/CmdUtils.h"
#include "fboss/cli/fboss2/utils/InterfaceList.h"

namespace facebook::fboss {

struct CmdConfigInterfaceDescriptionTraits : public WriteCommandTraits {
using ParentCmd = CmdConfigInterface;
static constexpr utils::ObjectArgTypeId ObjectArgTypeId =
utils::ObjectArgTypeId::OBJECT_ARG_TYPE_ID_MESSAGE;
using ObjectArgType = utils::Message;
using RetType = std::string;
};

class CmdConfigInterfaceDescription : public CmdHandler<
CmdConfigInterfaceDescription,
CmdConfigInterfaceDescriptionTraits> {
public:
using ObjectArgType = CmdConfigInterfaceDescriptionTraits::ObjectArgType;
using RetType = CmdConfigInterfaceDescriptionTraits::RetType;

RetType queryClient(
const HostInfo& hostInfo,
const utils::InterfaceList& interfaces,
const ObjectArgType& description);

void printOutput(const RetType& logMsg);
};

} // namespace facebook::fboss
1 change: 1 addition & 0 deletions fboss/cli/fboss2/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ cpp_unittest(
srcs = [
"CmdConfigAppliedInfoTest.cpp",
"CmdConfigHistoryTest.cpp",
"CmdConfigInterfaceDescriptionTest.cpp",
"CmdConfigReloadTest.cpp",
"CmdConfigSessionDiffTest.cpp",
"CmdConfigSessionTest.cpp",
Expand Down
90 changes: 90 additions & 0 deletions fboss/cli/fboss2/test/CmdConfigInterfaceDescriptionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// (c) Facebook, Inc. and its affiliates. Confidential and proprietary.

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <sstream>

#include "fboss/cli/fboss2/commands/config/interface/CmdConfigInterfaceDescription.h"

using namespace ::testing;

namespace facebook::fboss {

class CmdConfigInterfaceDescriptionTestFixture : public ::testing::Test {
public:
void SetUp() override {}
};

TEST_F(CmdConfigInterfaceDescriptionTestFixture, printOutputSuccess) {
auto cmd = CmdConfigInterfaceDescription();
std::string successMessage =
"Successfully set description for interface(s) eth1/1/1 to \"Test description\" and reloaded config";

// Redirect cout to capture output
std::stringstream buffer;
std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());

cmd.printOutput(successMessage);

// Restore cout
std::cout.rdbuf(old);

std::string output = buffer.str();
std::string expectedOutput =
"Successfully set description for interface(s) eth1/1/1 to \"Test description\" and reloaded config\n";

EXPECT_EQ(output, expectedOutput);
}

TEST_F(CmdConfigInterfaceDescriptionTestFixture, printOutputMultiplePorts) {
auto cmd = CmdConfigInterfaceDescription();
std::string successMessage =
"Successfully set description for interface(s) eth1/1/1, eth1/2/1 to \"Multi-port test\" and reloaded config";

// Redirect cout to capture output
std::stringstream buffer;
std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());

cmd.printOutput(successMessage);

// Restore cout
std::cout.rdbuf(old);

std::string output = buffer.str();
std::string expectedOutput =
"Successfully set description for interface(s) eth1/1/1, eth1/2/1 to \"Multi-port test\" and reloaded config\n";

EXPECT_EQ(output, expectedOutput);
}

TEST_F(CmdConfigInterfaceDescriptionTestFixture, errorOnNonExistentPort) {
auto cmd = CmdConfigInterfaceDescription();

// Test that attempting to set description on a non-existent port throws an
// error This is important because we cannot create arbitrary ports - they
// must exist in the platform mapping (hardware configuration)

// Note: This test would require mocking the queryClient method to test the
// actual error behavior. For now, we just verify the printOutput works
// correctly.
std::string errorMessage =
"Port(s) not found in configuration: eth1/99/1. Ports must exist in the "
"hardware platform mapping and be defined in the configuration before "
"setting their description.";

// Redirect cout to capture output
std::stringstream buffer;
std::streambuf* old = std::cout.rdbuf(buffer.rdbuf());

cmd.printOutput(errorMessage);

// Restore cout
std::cout.rdbuf(old);

std::string output = buffer.str();
std::string expectedOutput = errorMessage + "\n";

EXPECT_EQ(output, expectedOutput);
}

} // namespace facebook::fboss
1 change: 1 addition & 0 deletions fboss/cli/fboss2/utils/CmdUtilsCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum class ObjectArgTypeId : uint8_t {
OBJECT_ARG_TYPE_ID_MIRROR_LIST,
OBJECT_ARG_TYPE_LINK_DIRECTION,
OBJECT_ARG_TYPE_FAN_PWM,
OBJECT_ARG_TYPE_ID_INTERFACE_LIST,
OBJECT_ARG_TYPE_ID_REVISION_LIST,
};

Expand Down
Loading
Loading