Skip to content

Commit

Permalink
Owner upload (#179)
Browse files Browse the repository at this point in the history
Signed-off-by: Nate Koenig <[email protected]>

Co-authored-by: Nate Koenig <[email protected]>
Co-authored-by: Louise Poubel <[email protected]>
  • Loading branch information
3 people authored Apr 30, 2021
1 parent 900c6cc commit 3566ea7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
14 changes: 14 additions & 0 deletions include/ignition/fuel_tools/FuelClient.hh
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ namespace ignition
const std::vector<std::string> &_headers,
bool _private = false);

/// \brief Upload a directory as a new model
/// \param[in] _pathToModelDir a path to a directory containing a model
/// \param[in] _id An identifier to assign to this new model
/// \param[in] _headers Headers to set on the HTTP request.
/// \param[in] _private True to make the model private.
/// \param[in] _owner Name of the owner. Empty string indicates that
/// the owner is specified by the private token in the headers.
/// \return Result of the upload operation
public: Result UploadModel(const std::string &_pathToModelDir,
const ModelIdentifier &_id,
const std::vector<std::string> &_headers,
bool _private,
const std::string &_owner);

/// \brief Remove a model from ignition fuel
/// \param[in] _id The model identifier.
/// \return Result of the delete operation
Expand Down
33 changes: 27 additions & 6 deletions src/FuelClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ class ignition::fuel_tools::FuelClientPrivate
/// \param[in] _pathToModelDir Path to the model directory.
/// \param[in] _id Model identifier information.
/// \param[in] _private True if this model should be private.
/// \param[in] _form Form to fill.
/// \param[in] _owner Model owner name.
/// \param[out] _form Form to fill.
/// \return True if the operation completed successfully.
public: bool FillModelForm(const std::string &_pathToModelDir,
const ModelIdentifier &_id, bool _private,
const std::string &_owner,
std::multimap<std::string, std::string> &_form);

/// \brief This function requests the available licenses from the
Expand Down Expand Up @@ -445,13 +447,24 @@ WorldIter FuelClient::Worlds(const CollectionIdentifier &_id) const
Result FuelClient::UploadModel(const std::string &_pathToModelDir,
const ModelIdentifier &_id, const std::vector<std::string> &_headers,
bool _private)
{
return this->UploadModel(_pathToModelDir, _id, _headers, _private, "");
}

//////////////////////////////////////////////////
Result FuelClient::UploadModel(const std::string &_pathToModelDir,
const ModelIdentifier &_id, const std::vector<std::string> &_headers,
bool _private, const std::string &_owner)
{
ignition::fuel_tools::Rest rest;
RestResponse resp;

std::multimap<std::string, std::string> form;
if (!this->dataPtr->FillModelForm(_pathToModelDir, _id, _private, form))
if (!this->dataPtr->FillModelForm(_pathToModelDir, _id, _private, _owner,
form))
{
return Result(ResultType::UPLOAD_ERROR);
}

std::vector<std::string> headersIncludingServerConfig = _headers;
AddServerConfigParametersToHeaders(
Expand Down Expand Up @@ -479,8 +492,10 @@ Result FuelClient::UploadModel(const std::string &_pathToModelDir,
<< "Suggestions" << std::endl
<< " 1. Is the Server URL correct? Try entering it on a browser.\n"
<< " 2. Do the categories exist? If you are using the Fuel server,"
<< " then you can get the complete list at"
<< " https://fuel.ignitionrobotics.org/1.0/categories." << std::endl;
<< " then you can get the complete list at"
<< " https://fuel.ignitionrobotics.org/1.0/categories.\n"
<< " 3. If the owner is specified, make sure you have correct\n"
<< " permissions." << std::endl;
return Result(ResultType::FETCH_ERROR);
}

Expand Down Expand Up @@ -1323,7 +1338,7 @@ Result FuelClient::PatchModel(

if (!_pathToModelDir.empty() &&
!this->dataPtr->FillModelForm(_pathToModelDir, _model,
_model.Private(), form))
_model.Private(), _model.Owner(), form))
{
return Result(ResultType::UPLOAD_ERROR);
}
Expand Down Expand Up @@ -1369,7 +1384,7 @@ void FuelClient::PopulateLicenses(const ServerConfig &_server)

//////////////////////////////////////////////////
bool FuelClientPrivate::FillModelForm(const std::string &_pathToModelDir,
const ModelIdentifier &_id, bool _private,
const ModelIdentifier &_id, bool _private, const std::string &_owner,
std::multimap<std::string, std::string> &_form)
{
if (!common::exists(_pathToModelDir))
Expand Down Expand Up @@ -1426,6 +1441,12 @@ bool FuelClientPrivate::FillModelForm(const std::string &_pathToModelDir,
{"private", _private ? "1" : "0"},
};

// Add owner if specified.
if (!_owner.empty())
{
_form.emplace("owner", _owner);
}

// \todo(nkoenig) The ign-fuelserver expects an integer number for the
// license information. The fuelserver should be modified to accept
// a string. Otherwise, we have to bake into each client a mapping of
Expand Down
9 changes: 7 additions & 2 deletions src/cmd/cmdfuel.rb.in
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ SUBCOMMANDS = {
" -u [--url] arg URL of the server that should receive \n"\
" the model. If unspecified, the server will be\n"\
" https://fuel.ignitionrobotics.org. \n"\
" -o [--owner] arg Upload to the given owner, which can be an. \n"\
" organization. Default behavior is to upload \n"\
" to the user account associated with the \n"\
" private token specified in the header. \n"\
" -p [--private] Use this argument to make the model private. \n"\
" Otherwise, the model will be public. \n"\
" --header arg Set an HTTP header, such as \n"\
Expand Down Expand Up @@ -410,11 +414,12 @@ class Cmd
end
end
when 'upload'
Importer.extern 'int upload(const char *, const char *, const char *, const char *)'
Importer.extern 'int upload(const char *, const char *, const char *, const char *, const char *)'
if not Importer.upload(options['model'],
options['url'],
options['header'],
options['private'])
options['private'],
options['owner'])
exit(-1)
end
end
Expand Down
7 changes: 4 additions & 3 deletions src/ign.cc
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ extern "C" IGNITION_FUEL_TOOLS_VISIBLE void cmdVerbosity(const char *_verbosity)

//////////////////////////////////////////////////
extern "C" IGNITION_FUEL_TOOLS_VISIBLE int upload(const char *_path,
const char *_url, const char *_header, const char *_private)
const char *_url, const char *_header, const char *_private,
const char *_owner)
{
ignition::common::SignalHandler handler;
bool sigKilled{false};
Expand Down Expand Up @@ -789,7 +790,7 @@ extern "C" IGNITION_FUEL_TOOLS_VISIBLE int upload(const char *_path,
{
std::cout << "Uploading a model[" << _path << "]\n";
// Upload the model
return client.UploadModel(_path, model, headers, privateBool);
return client.UploadModel(_path, model, headers, privateBool, _owner);
}

// If a model.config or metadata.pbtxt file does not exist, then assume
Expand All @@ -804,7 +805,7 @@ extern "C" IGNITION_FUEL_TOOLS_VISIBLE int upload(const char *_path,
ignition::common::exists(
ignition::common::joinPaths(*dirIter, "model.config"))))
{
if (!client.UploadModel(*dirIter, model, headers, privateBool))
if (!client.UploadModel(*dirIter, model, headers, privateBool, _owner))
{
ignerr << "Failed to upload model[" << *dirIter << "]\n";
}
Expand Down
4 changes: 3 additions & 1 deletion src/ign.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ extern "C" IGNITION_FUEL_TOOLS_VISIBLE int downloadUrl(
/// \param[in] _header An HTTP header.
/// \param[in] _private "1" to make the resource private, "0" to make it
/// public.
/// \param[in] _owner Upload the resource to the provided owner, or nullptr
/// to upload to the account specified by the token in the header.
/// \return 1 if successful, 0 if not.
extern "C" IGNITION_FUEL_TOOLS_VISIBLE int upload(const char *_path,
const char *_url, const char *_header = nullptr,
const char *_private = nullptr);
const char *_private = nullptr, const char *_owner = nullptr);

/// \brief External hook to execute 'ign fuel delete [options]' from the command
/// line.
Expand Down

0 comments on commit 3566ea7

Please sign in to comment.