4

我正在尝试使用 Python 从 GCP Recommendations API 的 API JSON 输出中非法获取某些键值,并且对使用 Python 较新。我试图非法获取的大多数值都可以毫无问题地获取,但是,当我尝试在 JSON 中更深层嵌套的代码块中非法获取某些值时,它会失败并出现错误:TypeError: 'OperationGroup' object is not subscriptable

JSON 响应的完整输出在这里(更改了一些值以保护公司信息):

name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcd-efg-hijk-lmnop-qrstuv-123456"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
  seconds: 1623222401
}
primary_impact {
  category: COST
  cost_projection {
    cost {
      currency_code: "USD"
      units: -12
      nanos: -98539964
    }
    duration {
      seconds: 2592000
    }
  }
}
content {
  operation_groups {
    operations {
      action: "test"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/abcname123"
      path: "/machineType"
      value_matcher {
        matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
      }
    }
    operations {
      action: "replace"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/abcname123"
      path: "/machineType"
      value {
        string_value: "zones/us-central1-a/machineTypes/e2-small"
      }
    }
  }
}
state_info {
  state: ACTIVE
}
etag: "\"abc-123-def-456\""
recommender_subtype: "CHANGE_MACHINE_TYPE"

name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcdefg-hijklmnop-123-456"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
  seconds: 1623222401
}
primary_impact {
  category: COST
  cost_projection {
    cost {
      currency_code: "USD"
      units: -12
      nanos: -99648292
    }
    duration {
      seconds: 2592000
    }
  }
}
content {
  operation_groups {
    operations {
      action: "test"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/instance-example1"
      path: "/machineType"
      value_matcher {
        matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
      }
    }
    operations {
      action: "replace"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/instance-example1"
      path: "/machineType"
      value {
        string_value: "zones/us-central1-a/machineTypes/e2-small"
      }
    }
  }
}
state_info {
  state: ACTIVE
}
etag: "\"abcdefg12345\""
recommender_subtype: "CHANGE_MACHINE_TYPE"

name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcd1234"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
  seconds: 1623222401
}
primary_impact {
  category: COST
  cost_projection {
    cost {
      currency_code: "USD"
      units: -11
      nanos: -568971875
    }
    duration {
      seconds: 2592000
    }
  }
}
content {
  operation_groups {
    operations {
      action: "test"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
      path: "/machineType"
      value_matcher {
        matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
      }
    }
    operations {
      action: "replace"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
      path: "/machineType"
      value {
        string_value: "zones/us-central1-a/machineTypes/e2-small"
      }
    }
  }
}
state_info {
  state: ACTIVE
}
etag: "\"abcd1234\""
recommender_subtype: "CHANGE_MACHINE_TYPE"

这是我在 Python 中的代码:

from google.cloud import recommender
import os

client = recommender.RecommenderClient()

def main():
    name = client.list_recommendations(parent='projects/xyzproject/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender')
    for element in name:
        # print(element)
        print(element.description)
        print(element.primary_impact.category)
        print(element.primary_impact.cost_projection.cost.currency_code)
        print(element.primary_impact.cost_projection.cost.units)
        print(element.state_info.state)
        print(element.content.operation_groups)
        for item in element.content.operation_groups:
            print(item['resource_type'])
main()

上述工作的以下部分:

print(element.description)
print(element.primary_impact.category)
print(element.primary_impact.cost_projection.cost.currency_code)
print(element.primary_impact.cost_projection.cost.units)
print(element.state_info.state)
print(element.content.operation_groups)

但我遇到的错误和麻烦是:

for item in element.content.operation_groups:
    print(item['resource_type'])

每当我尝试使用 python 脚本的那部分时,它都会失败并出现错误:

TypeError: 'OperationGroup' object is not subscriptable

那么,有人可以帮助我了解如何正确利用 JSON 响应并非法获取以下块中的信息(例如“resource_type”)吗?

content {
  operation_groups {
    operations {
      action: "test"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
      path: "/machineType"
      value_matcher {
        matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
      }
    }
    operations {
      action: "replace"
      resource_type: "compute.googleapis.com/Instance"
      resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
      path: "/machineType"
      value {
        string_value: "zones/us-central1-a/machineTypes/e2-small"
      }
    }
  }
}
4

1 回答 1

1

我采用了您问题中的代码并将其重写以匹配 Python Recommender API。主要问题是您正在访问内部成员和字段名称。我已更改代码以使用公共接口。

查看 API 的类型定义:

Recommender API 客户端的类型

GitHub Gist:演示 Google Cloud Compute Recommender 列表推荐 API 的 Python 示例

# pip install google-cloud-recommender

from google.cloud import recommender
import os

# Enter values for your Project ID and Zone
PROJECT=
LOCATION=

RECOMMENDER = 'google.compute.instance.MachineTypeRecommender'

def print_recommendations():
    client = recommender.RecommenderClient()

    parent = client.recommender_path(PROJECT, LOCATION, RECOMMENDER)

    recommendations = client.list_recommendations(parent=parent)

    for recommendation in recommendations:
        print('Recommendation:')
        print('  Description:     ', recommendation.description)
        print('  Impact Category: ', recommendation.primary_impact.category)
        print('  Currency Code:   ', recommendation.primary_impact.cost_projection.cost.currency_code)
        print('  Units:           ', recommendation.primary_impact.cost_projection.cost.units)
        print('  State:           ', recommendation.state_info.state)
        print('')

        for op_group in recommendation.content._pb.operation_groups:
            for operation in op_group.operations:
                print('  Operation:')
                print('    Action:            ', operation.action)
                print('    Resource Type:     ', operation.resource_type)
                print('    Path:              ', operation.path)
                print('    Value:             ', operation.value.string_value)
                print('')

print_recommendations()

示例输出:

Recommendation:
  Description:      Save cost by changing machine type from e2-standard-4 to e2-highmem-2.
  Impact Category:  Category.COST
  Currency Code:    USD
  Units:            -31
  State:            State.ACTIVE

  Operation:
    Action:             test
    Resource Type:      compute.googleapis.com/Instance
    Path:               /machineType
    Value:

  Operation:
    Action:             replace
    Resource Type:      compute.googleapis.com/Instance
    Path:               /machineType
    Value:              zones/us-east1-b/machineTypes/e2-highmem-2
于 2021-06-14T23:30:17.023 回答