Skip to content

Commit

Permalink
Merge pull request #95 from modular-magician/codegen-pr-1319
Browse files Browse the repository at this point in the history
Add global forwarding rule generation to InSpec
  • Loading branch information
slevenick authored Jan 29, 2019
2 parents 226fac4 + 67a8582 commit 1753ef9
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/resources/google_compute_global_forwarding_rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: About the GlobalForwardingRule resource
platform: gcp
---


## Syntax
A `google_compute_global_forwarding_rule` is used to test a Google GlobalForwardingRule resource

## Examples
```
describe google_compute_global_forwarding_rule(project: 'chef-gcp-inspec', name: 'inspec-gcp-global-forwarding-rule') do
it { should exist }
its('port_range') { should eq '80-80' }
its('target') { should match /\/inspec-gcp-http-proxy$/ }
end
describe google_compute_global_forwarding_rule(project: 'chef-gcp-inspec', name: 'nonexistent') do
it { should_not exist }
end
```

## Properties
Properties that can be accessed from the `google_compute_global_forwarding_rule` resource:

* `creation_timestamp`: Creation timestamp in RFC3339 text format.

* `description`: An optional description of this resource. Provide this property when you create the resource.

* `id`: The unique identifier for the resource.

* `ip_address`: The IP address that this forwarding rule is serving on behalf of. Addresses are restricted based on the forwarding rule's load balancing scheme (EXTERNAL or INTERNAL) and scope (global or regional). When the load balancing scheme is EXTERNAL, for global forwarding rules, the address must be a global IP, and for regional forwarding rules, the address must live in the same region as the forwarding rule. If this field is empty, an ephemeral IPv4 address from the same scope (global or regional) will be assigned. A regional forwarding rule supports IPv4 only. A global forwarding rule supports either IPv4 or IPv6. When the load balancing scheme is INTERNAL, this can only be an RFC 1918 IP address belonging to the network/subnet configured for the forwarding rule. By default, if this field is empty, an ephemeral internal IP address will be automatically allocated from the IP range of the subnet or network configured for this forwarding rule. An address can be specified either by a literal IP address or a URL reference to an existing Address resource. The following examples are all valid: * 100.1.2.3 * https://www.googleapis.com/compute/v1/projects/project/regions/ region/addresses/address * projects/project/regions/region/addresses/address * regions/region/addresses/address * global/addresses/address * address

* `ip_protocol`: The IP protocol to which this rule applies. Valid options are TCP, UDP, ESP, AH, SCTP or ICMP. When the load balancing scheme is INTERNAL, only TCP and UDP are valid.

* `backend_service`: A reference to a BackendService to receive the matched traffic. This is used for internal load balancing. (not used for external load balancing)

* `ip_version`: The IP Version that will be used by this forwarding rule. Valid options are IPV4 or IPV6. This can only be specified for a global forwarding rule.

* `load_balancing_scheme`: This signifies what the ForwardingRule will be used for and can only take the following values: INTERNAL, EXTERNAL The value of INTERNAL means that this will be used for Internal Network Load Balancing (TCP, UDP). The value of EXTERNAL means that this will be used for External Load Balancing (HTTP(S) LB, External TCP/UDP LB, SSL Proxy)

* `name`: Name of the resource; provided by the client when the resource is created. The name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.

* `network`: For internal load balancing, this field identifies the network that the load balanced IP should belong to for this Forwarding Rule. If this field is not specified, the default network will be used. This field is not used for external load balancing.

* `port_range`: This field is used along with the target field for TargetHttpProxy, TargetHttpsProxy, TargetSslProxy, TargetTcpProxy, TargetVpnGateway, TargetPool, TargetInstance. Applicable only when IPProtocol is TCP, UDP, or SCTP, only packets addressed to ports in the specified range will be forwarded to target. Forwarding rules with the same [IPAddress, IPProtocol] pair must have disjoint port ranges. Some types of forwarding target have constraints on the acceptable ports: * TargetHttpProxy: 80, 8080 * TargetHttpsProxy: 443 * TargetTcpProxy: 25, 43, 110, 143, 195, 443, 465, 587, 700, 993, 995, 1883, 5222 * TargetSslProxy: 25, 43, 110, 143, 195, 443, 465, 587, 700, 993, 995, 1883, 5222 * TargetVpnGateway: 500, 4500

* `ports`: This field is used along with the backend_service field for internal load balancing. When the load balancing scheme is INTERNAL, a single port or a comma separated list of ports can be configured. Only packets addressed to these ports will be forwarded to the backends configured with this forwarding rule. You may specify a maximum of up to 5 ports.

* `subnetwork`: A reference to a subnetwork. For internal load balancing, this field identifies the subnetwork that the load balanced IP should belong to for this Forwarding Rule. If the network specified is in auto subnet mode, this field is optional. However, if the network is in custom subnet mode, a subnetwork must be specified. This field is not used for external load balancing.

* `region`: A reference to the region where the regional forwarding rule resides. This field is not applicable to global forwarding rules.

* `target`: This target must be a global load balancing resource. The forwarded traffic must be of a type appropriate to the target object. Valid types: HTTP_PROXY, HTTPS_PROXY, SSL_PROXY, TCP_PROXY
40 changes: 40 additions & 0 deletions docs/resources/google_compute_global_forwarding_rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: About the GlobalForwardingRule resource
platform: gcp
---


## Syntax
A `google_compute_global_forwarding_rules` is used to test a Google GlobalForwardingRule resource

## Examples
```
describe google_compute_global_forwarding_rules(project: 'chef-gcp-inspec') do
its('count') { should be >= 1 }
its('port_ranges') { should include '80-80' }
end
```

## Properties
Properties that can be accessed from the `google_compute_global_forwarding_rules` resource:

See [google_compute_global_forwarding_rule.md](google_compute_global_forwarding_rule.md) for more detailed information
* `creation_timestamps`: an array of `google_compute_global_forwarding_rule` creation_timestamp
* `descriptions`: an array of `google_compute_global_forwarding_rule` description
* `ids`: an array of `google_compute_global_forwarding_rule` id
* `ip_addresses`: an array of `google_compute_global_forwarding_rule` ip_address
* `ip_protocols`: an array of `google_compute_global_forwarding_rule` ip_protocol
* `backend_services`: an array of `google_compute_global_forwarding_rule` backend_service
* `ip_versions`: an array of `google_compute_global_forwarding_rule` ip_version
* `load_balancing_schemes`: an array of `google_compute_global_forwarding_rule` load_balancing_scheme
* `names`: an array of `google_compute_global_forwarding_rule` name
* `networks`: an array of `google_compute_global_forwarding_rule` network
* `port_ranges`: an array of `google_compute_global_forwarding_rule` port_range
* `ports`: an array of `google_compute_global_forwarding_rule` ports
* `subnetworks`: an array of `google_compute_global_forwarding_rule` subnetwork
* `regions`: an array of `google_compute_global_forwarding_rule` region
* `targets`: an array of `google_compute_global_forwarding_rule` target

## Filter Criteria
This resource supports all of the above properties as filter criteria, which can be used
with `where` as a block or a method.
79 changes: 79 additions & 0 deletions libraries/google_compute_global_forwarding_rule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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 Google Compute Engine resources.
class GlobalForwardingRule < GcpResourceBase
name 'google_compute_global_forwarding_rule'
desc 'GlobalForwardingRule'
supports platform: 'gcp'

attr_reader :creation_timestamp
attr_reader :description
attr_reader :id
attr_reader :ip_address
attr_reader :ip_protocol
attr_reader :backend_service
attr_reader :ip_version
attr_reader :load_balancing_scheme
attr_reader :name
attr_reader :network
attr_reader :port_range
attr_reader :ports
attr_reader :subnetwork
attr_reader :region
attr_reader :target
def base
'https://www.googleapis.com/compute/v1/'
end

def url
'projects/{{project}}/global/forwardingRules/{{name}}'
end

def initialize(params)
super(params.merge({ use_http_transport: true }))
@fetched = @connection.fetch(base, url, params)
parse unless @fetched.nil?
end

def parse
@creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
@description = @fetched['description']
@id = @fetched['id']
@ip_address = @fetched['IPAddress']
@ip_protocol = @fetched['IPProtocol']
@backend_service = @fetched['backendService']
@ip_version = @fetched['ipVersion']
@load_balancing_scheme = @fetched['loadBalancingScheme']
@name = @fetched['name']
@network = @fetched['network']
@port_range = @fetched['portRange']
@ports = @fetched['ports']
@subnetwork = @fetched['subnetwork']
@region = @fetched['region']
@target = @fetched['target']
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
end
110 changes: 110 additions & 0 deletions libraries/google_compute_global_forwarding_rules.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# 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'
class GlobalForwardingRules < GcpResourceBase
name 'google_compute_global_forwarding_rules'
desc 'GlobalForwardingRule plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:ip_addresses, field: :ip_address)
filter_table_config.add(:ip_protocols, field: :ip_protocol)
filter_table_config.add(:backend_services, field: :backend_service)
filter_table_config.add(:ip_versions, field: :ip_version)
filter_table_config.add(:load_balancing_schemes, field: :load_balancing_scheme)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:networks, field: :network)
filter_table_config.add(:port_ranges, field: :port_range)
filter_table_config.add(:ports, field: :ports)
filter_table_config.add(:subnetworks, field: :subnetwork)
filter_table_config.add(:regions, field: :region)
filter_table_config.add(:targets, field: :target)

filter_table_config.connect(self, :table)

def base
'https://www.googleapis.com/compute/v1/'
end

def url
'projects/{{project}}/global/forwardingRules'
end

def initialize(params = {})
super(params.merge({ use_http_transport: true }))
@params = params
@table = fetch_wrapped_resource('items')
end

def fetch_wrapped_resource(wrap_path)
# fetch_resource returns an array of responses (to handle pagination)
result = @connection.fetch_all(base, url, @params)
return if result.nil?

# Conversion of string -> object hash to symbol -> object hash that InSpec needs
converted = []
result.each do |response|
next if response.nil? || !response.key?(wrap_path)
response[wrap_path].each do |hash|
hash_with_symbols = {}
hash.each_key do |key|
name, value = transform(key, hash)
hash_with_symbols[name] = value
end
converted.push(hash_with_symbols)
end
end

converted
end

def transform(key, value)
return transformers[key].call(value) if transformers.key?(key)

[key.to_sym, value]
end

def transformers
{
'creationTimestamp' => ->(obj) { return :creation_timestamp, parse_time_string(obj['creationTimestamp']) },
'description' => ->(obj) { return :description, obj['description'] },
'id' => ->(obj) { return :id, obj['id'] },
'IPAddress' => ->(obj) { return :ip_address, obj['IPAddress'] },
'IPProtocol' => ->(obj) { return :ip_protocol, obj['IPProtocol'] },
'backendService' => ->(obj) { return :backend_service, obj['backendService'] },
'ipVersion' => ->(obj) { return :ip_version, obj['ipVersion'] },
'loadBalancingScheme' => ->(obj) { return :load_balancing_scheme, obj['loadBalancingScheme'] },
'name' => ->(obj) { return :name, obj['name'] },
'network' => ->(obj) { return :network, obj['network'] },
'portRange' => ->(obj) { return :port_range, obj['portRange'] },
'ports' => ->(obj) { return :ports, obj['ports'] },
'subnetwork' => ->(obj) { return :subnetwork, obj['subnetwork'] },
'region' => ->(obj) { return :region, obj['region'] },
'target' => ->(obj) { return :target, obj['target'] },
}
end

# Handles parsing RFC3339 time string
def parse_time_string(time_string)
time_string ? Time.parse(time_string) : nil
end
end
11 changes: 11 additions & 0 deletions test/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ variable "http_proxy" {
type = "map"
}

variable "global_forwarding_rule" {
type = "map"
}

resource "google_compute_ssl_policy" "custom-ssl-policy" {
name = "${var.ssl_policy["name"]}"
min_tls_version = "${var.ssl_policy["min_tls_version"]}"
Expand Down Expand Up @@ -277,4 +281,11 @@ resource "google_compute_target_http_proxy" "gcp-inspec-http-proxy" {
name = "${var.http_proxy["name"]}"
url_map = "${google_compute_url_map.gcp-inspec-url-map.self_link}"
description = "${var.http_proxy["description"]}"
}

resource "google_compute_global_forwarding_rule" "gcp-inspec-global-forwarding-rule" {
project = "${var.gcp_project_id}"
name = "${var.global_forwarding_rule["name"]}"
target = "${google_compute_target_http_proxy.gcp-inspec-http-proxy.self_link}"
port_range = "${var.global_forwarding_rule["port_range"]}"
}
4 changes: 4 additions & 0 deletions test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ url_map:
http_proxy:
name: inspec-gcp-http-proxy
description: A HTTP proxy

global_forwarding_rule:
name: inspec-gcp-global-forwarding-rule
port_range: 80-80
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ----------------------------------------------------------------------------
#
# *** 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_compute_global_forwarding_rule resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
global_forwarding_rule = attribute('global_forwarding_rule', default: {
"name": "inspec-gcp-global-forwarding-rule",
"port_range": "80-80"
}, description: 'Compute global forwarding rule definition')
control 'google_compute_global_forwarding_rule-1.0' do
impact 1.0
title 'google_compute_global_forwarding_rule resource test'

describe google_compute_global_forwarding_rule(project: gcp_project_id, name: global_forwarding_rule['name']) do
it { should exist }
its('port_range') { should eq global_forwarding_rule['port_range'] }
its('target') { should match /\/inspec-gcp-http-proxy$/ }
end

describe google_compute_global_forwarding_rule(project: gcp_project_id, name: 'nonexistent') do
it { should_not exist }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ----------------------------------------------------------------------------
#
# *** 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_compute_global_forwarding_rules resource.'

gcp_project_id = attribute(:gcp_project_id, default: 'gcp_project_id', description: 'The GCP project identifier.')
global_forwarding_rule = attribute('global_forwarding_rule', default: {
"name": "inspec-gcp-global-forwarding-rule",
"port_range": "80-80"
}, description: 'Compute global forwarding rule definition')
control 'google_compute_global_forwarding_rules-1.0' do
impact 1.0
title 'google_compute_global_forwarding_rules resource test'

describe google_compute_global_forwarding_rules(project: gcp_project_id) do
its('count') { should be >= 1 }
its('port_ranges') { should include global_forwarding_rule['port_range'] }
end
end

0 comments on commit 1753ef9

Please sign in to comment.