0

使用 DescribeInstancesRequest (c++ sdk) 获取有关特定 instance_id 的响应。我在构建过滤器时遇到问题。

我正在调整 aws-doc-sdk-examples c++ 示例代码 describe_instances.cpp 提供的示例代码。我添加了代码来过滤响应以使用已知的有效(现在是硬编码)实例 ID。

我尝试了多种变体来设置过滤器,但是文档对我来说并不清楚过滤器的“值对”格式。

这是完整的代码。它只编译 find,但总是以“找不到:...”作为响应

请让我知道过滤器语法有什么问题!(见评论部分过滤实例ID)

谢谢

void Server::set_instance_info()
{
  // Get server instance information via aws sdk
  Aws::SDKOptions options;
  Aws::InitAPI(options);
  {
    /* @TODO Make this a startup config value */
    Aws::Client::ClientConfiguration clientConfig;
    clientConfig.region = "us-west-2";
    Aws::EC2::EC2Client ec2(clientConfig);

    Aws::EC2::Model::DescribeInstancesRequest request;

    // Filter an instance_id
    Aws::EC2::Model::Filter filter;
    filter.SetName("instance_id");
    Aws::String filter_val{"Name=instance_id,Values=i-0e120b44acc929946"};
    Aws::Vector<Aws::String> filter_values;
    filter_values.push_back(filter_val);
    filter.SetValues(filter_values);
    Aws::Vector<Aws::EC2::Model::Filter> DIRFilter;
    DIRFilter.push_back(filter);
    request.SetFilters(DIRFilter);

    auto outcome = ec2.DescribeInstances(request);
    if (outcome.IsSuccess())
    {
      const auto &reservations =
        outcome.GetResult().GetReservations();

      for (const auto &reservation : reservations)
      {
        const auto &instances = reservation.GetInstances();
        for (const auto &instance : instances)
        {
          Aws::String instanceStateString =
            Aws::EC2::Model::InstanceStateNameMapper::GetNameForInstanceStateName(
                instance.GetState().GetName());

          Aws::String type_string =
            Aws::EC2::Model::InstanceTypeMapper::GetNameForInstanceType(
                instance.GetInstanceType());

          Aws::String name = "Unknown";
          const auto &tags = instance.GetTags();
          auto nameIter = std::find_if(tags.cbegin(), tags.cend(),
              [](const Aws::EC2::Model::Tag &tag)
              {
              return tag.GetKey() == "Name";
              });
          if (nameIter != tags.cend())
          {
            name = nameIter->GetValue();
          }
          Server::id_ = instance.GetInstanceId();
          Server::name_ = name;
          Server::type_ = type_string;
          Server::dn_ = "Not implemented";
          Server::ip_ = "Not implmented";
        }
      }
    } else {
      Server::id_ = "Could not find: " + filter_val;;
      Server::name_ = "";
      Server::type_ = "";
      Server::dn_ = "";
      Server::ip_ = "";
    }
  }
  return;
}
4

1 回答 1

0

我只是无法让过滤器工作。任何输入将不胜感激。但是,还有一种使用 WithInstanceIds 成员函数的替代方法。阅读 API 文档总是一个好主意!!

这是有效的子程序:

void Server::set_instance_info()
{
  // Get server instance information via aws sdk
  Aws::SDKOptions options;
  Aws::InitAPI(options);
  {
    /* @TODO Make this a startup config value */
    Aws::Client::ClientConfiguration clientConfig;
    clientConfig.region = "us-west-2";
    Aws::EC2::EC2Client ec2(clientConfig);

    Aws::EC2::Model::DescribeInstancesRequest request;

    /* @TODO Make this a startup config value */
    const Aws::String instanceId{"i-0e120b44acc929946"};
    Aws::Vector<Aws::String> instances;
    instances.push_back(instanceId);
    request.WithInstanceIds(instances);

    auto outcome = ec2.DescribeInstances(request);
    if (outcome.IsSuccess())
    {
      const auto &reservations =
        outcome.GetResult().GetReservations();

      for (const auto &reservation : reservations)
      {
        const auto &instances = reservation.GetInstances();
        for (const auto &instance : instances)
        {
          Aws::String instanceStateString =
            Aws::EC2::Model::InstanceStateNameMapper::GetNameForInstanceStateName(
                instance.GetState().GetName());

          Aws::String type_string =
            Aws::EC2::Model::InstanceTypeMapper::GetNameForInstanceType(
                instance.GetInstanceType());

          Aws::String name = "Unknown";
          const auto &tags = instance.GetTags();
          auto nameIter = std::find_if(tags.cbegin(), tags.cend(),
              [](const Aws::EC2::Model::Tag &tag)
              {
              return tag.GetKey() == "Name";
              });
          if (nameIter != tags.cend())
          {
            name = nameIter->GetValue();
          }
          Server::id_ = instance.GetInstanceId();
          Server::name_ = name;
          Server::type_ = type_string;
          Server::dn_ = "Not implemented";
          Server::ip_ = "Not implmented";
        }
      }
    } else {
      Server::id_ = "Could not find: "+ instanceId;
      Server::name_ = "";
      Server::type_ = "";
      Server::dn_ = "";
      Server::ip_ = "";
    }
  }
  return;
}
于 2019-03-29T17:58:35.327 回答