From bca96412ea2705b3b5924c0fb68bea2d0a267fc7 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Wed, 13 Nov 2019 21:15:13 +0000 Subject: [PATCH] Inspec cloudbilling support Signed-off-by: Modular Magician --- .../google_billing_project_billing_info.md | 30 +++++++++ .../google_billing_project_billing_info.rb | 64 +++++++++++++++++++ .../google_billing_project_billing_info.rb | 29 +++++++++ 3 files changed, 123 insertions(+) create mode 100644 docs/resources/google_billing_project_billing_info.md create mode 100644 libraries/google_billing_project_billing_info.rb create mode 100644 test/integration/verify/controls/google_billing_project_billing_info.rb diff --git a/docs/resources/google_billing_project_billing_info.md b/docs/resources/google_billing_project_billing_info.md new file mode 100644 index 000000000..899963e3e --- /dev/null +++ b/docs/resources/google_billing_project_billing_info.md @@ -0,0 +1,30 @@ +--- +title: About the google_billing_project_billing_info resource +platform: gcp +--- + +## Syntax +A `google_billing_project_billing_info` is used to test a Google ProjectBillingInfo resource + +## Examples +``` +describe google_billing_project_billing_info(project_id: 'chef-gcp-inspec') do + it { should exist } + + its('billing_account_name') { should eq 'billingAccounts/012345-567890-ABCDEF'} + its('billing_enabled') { should eq true } +end +``` + +## Properties +Properties that can be accessed from the `google_billing_project_billing_info` resource: + + + * `project_id`: The project id to retrieve billing info for. + + * `billing_account_name`: The resource name of the billing account associated with the project, if any. For example, `billingAccounts/012345-567890-ABCDEF`. + + * `billing_enabled`: True if the project is associated with an open billing account, to which usage on the project is charged. False if the project is associated with a closed billing account, or no billing account at all, and therefore cannot use paid services. + + +## GCP Permissions diff --git a/libraries/google_billing_project_billing_info.rb b/libraries/google_billing_project_billing_info.rb new file mode 100644 index 000000000..d755b4608 --- /dev/null +++ b/libraries/google_billing_project_billing_info.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: false + +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in README.md and +# CONTRIBUTING.md located at the root of this package. +# +# ---------------------------------------------------------------------------- +require 'gcp_backend' + +# A provider to manage Cloud Billing resources. +class BillingProjectBillingInfo < GcpResourceBase + name 'google_billing_project_billing_info' + desc 'ProjectBillingInfo' + supports platform: 'gcp' + + attr_reader :params + attr_reader :project_id + attr_reader :billing_account_name + attr_reader :billing_enabled + + def initialize(params) + super(params.merge({ use_http_transport: true })) + @params = params + @fetched = @connection.fetch(product_url, resource_base_url, params, 'Get') + parse unless @fetched.nil? + end + + def parse + @project_id = @fetched['projectId'] + @billing_account_name = @fetched['billingAccountName'] + @billing_enabled = @fetched['billingEnabled'] + end + + # Handles parsing RFC3339 time string + def parse_time_string(time_string) + time_string ? Time.parse(time_string) : nil + end + + def exists? + !@fetched.nil? + end + + def to_s + "ProjectBillingInfo #{@params[:projectId]}" + end + + private + + def product_url + 'https://cloudbilling.googleapis.com/v1/' + end + + def resource_base_url + 'projects/{{project_id}}/billingInfo' + end +end diff --git a/test/integration/verify/controls/google_billing_project_billing_info.rb b/test/integration/verify/controls/google_billing_project_billing_info.rb new file mode 100644 index 000000000..fda670118 --- /dev/null +++ b/test/integration/verify/controls/google_billing_project_billing_info.rb @@ -0,0 +1,29 @@ +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in README.md and +# CONTRIBUTING.md located at the root of this package. +# +# ---------------------------------------------------------------------------- + +title 'Test GCP google_billing_project_billing_info resource.' + +gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.') +control 'google_billing_project_billing_info-1.0' do + impact 1.0 + title 'google_billing_project_billing_info resource test' + + only_if { gcp_enable_privileged_resources.to_i == 1 && gcp_organization_id != ''} + describe google_billing_project_billing_info(project_id: gcp_project_id) do + it { should exist } + + its('billing_account_name') { should eq 'billingAccounts/012345-567890-ABCDEF'} + its('billing_enabled') { should eq true } + end +end