Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Add instance group manager resource to InSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
slevenick authored and modular-magician committed Jan 15, 2019
1 parent d568ab4 commit c81375b
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 0 deletions.
74 changes: 74 additions & 0 deletions docs/resources/google_compute_instance_group_manager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: About the InstanceGroupManager resource
platform: gcp
---


## Syntax
A `google_compute_instance_group_manager` is used to test a Google InstanceGroupManager resource

## Examples
```
describe google_compute_instance_group_manager(project: 'chef-gcp-inspec', zone: 'zone', name: 'inspec-gcp-igm') do
it { should exist }
its('base_instance_name') { should eq 'igm' }
its('named_ports.count') { should cmp 1 }
its('named_ports.first.name') { should eq 'port' }
its('named_ports.first.port') { should eq '80' }
its('instance_template') { should match 'gcp-managed-group-name' }
end
describe google_compute_instance_group_manager(project: 'chef-gcp-inspec', zone: 'zone', name: 'nonexistent') do
it { should_not exist }
end
```

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

* `base_instance_name`: The base instance name to use for instances in this group. The value must be 1-58 characters long. Instances are named by appending a hyphen and a random four-character string to the base instance name. The base instance name must comply with RFC1035.

* `creation_timestamp`: The creation timestamp for this managed instance group in RFC3339 text format.

* `current_actions`: The list of instance actions and the number of instances in this managed instance group that are scheduled for each of those actions.

* `abandoning`: The total number of instances in the managed instance group that are scheduled to be abandoned. Abandoning an instance removes it from the managed instance group without deleting it.

* `creating`: The number of instances in the managed instance group that are scheduled to be created or are currently being created. If the group fails to create any of these instances, it tries again until it creates the instance successfully. If you have disabled creation retries, this field will not be populated; instead, the creatingWithoutRetries field will be populated.

* `creatingWithoutRetries`: The number of instances that the managed instance group will attempt to create. The group attempts to create each instance only once. If the group fails to create any of these instances, it decreases the group's targetSize value accordingly.

* `deleting`: The number of instances in the managed instance group that are scheduled to be deleted or are currently being deleted.

* `none`: The number of instances in the managed instance group that are running and have no scheduled actions.

* `recreating`: The number of instances in the managed instance group that are scheduled to be recreated or are currently being being recreated. Recreating an instance deletes the existing root persistent disk and creates a new disk from the image that is defined in the instance template.

* `refreshing`: The number of instances in the managed instance group that are being reconfigured with properties that do not require a restart or a recreate action. For example, setting or removing target pools for the instance.

* `restarting`: The number of instances in the managed instance group that are scheduled to be restarted or are currently being restarted.

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

* `id`: A unique identifier for this resource

* `instance_group`: The instance group being managed

* `instance_template`: The instance template that is specified for this managed instance group. The group uses this template to create all new instances in the managed instance group.

* `name`: The name of the managed instance group. The name must be 1-63 characters long, and comply with RFC1035.

* `named_ports`: Named ports configured for the Instance Groups complementary to this Instance Group Manager.

* `name`: The name for this named port. The name must be 1-63 characters long, and comply with RFC1035.

* `port`: The port number, which can be a value between 1 and 65535.

* `region`: The region this managed instance group resides (for regional resources).

* `target_pools`: TargetPool resources to which instances in the instanceGroup field are added. The target pools automatically apply to all of the instances in the managed instance group.

* `target_size`: The target number of running instances for this managed instance group. Deleting or abandoning instances reduces this number. Resizing the group changes this number.

* `zone`: The zone the managed instance group resides.
38 changes: 38 additions & 0 deletions docs/resources/google_compute_instance_group_managers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: About the InstanceGroupManager resource
platform: gcp
---


## Syntax
A `google_compute_instance_group_managers` is used to test a Google InstanceGroupManager resource

## Examples
```
describe google_compute_instance_group_managers(project: 'chef-gcp-inspec', zone: 'zone') do
its('base_instance_names') { should include 'igm' }
end
```

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

See [google_compute_instance_group_manager.md](google_compute_instance_group_manager.md) for more detailed information
* `base_instance_names`: an array of `google_compute_instance_group_manager` base_instance_name
* `creation_timestamps`: an array of `google_compute_instance_group_manager` creation_timestamp
* `current_actions`: an array of `google_compute_instance_group_manager` current_actions
* `descriptions`: an array of `google_compute_instance_group_manager` description
* `ids`: an array of `google_compute_instance_group_manager` id
* `instance_groups`: an array of `google_compute_instance_group_manager` instance_group
* `instance_templates`: an array of `google_compute_instance_group_manager` instance_template
* `names`: an array of `google_compute_instance_group_manager` name
* `named_ports`: an array of `google_compute_instance_group_manager` named_ports
* `regions`: an array of `google_compute_instance_group_manager` region
* `target_pools`: an array of `google_compute_instance_group_manager` target_pools
* `target_sizes`: an array of `google_compute_instance_group_manager` target_size
* `zones`: an array of `google_compute_instance_group_manager` zone

## 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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.
#
# ----------------------------------------------------------------------------
module GoogleInSpec
module Compute
module Property
class InstanceGroupManagerCurrentactions
attr_reader :abandoning

attr_reader :creating

attr_reader :creating_without_retries

attr_reader :deleting

attr_reader :none

attr_reader :recreating

attr_reader :refreshing

attr_reader :restarting

def initialize(args = nil)
return if args.nil?
@abandoning = args['abandoning']
@creating = args['creating']
@creating_without_retries = args['creatingWithoutRetries']
@deleting = args['deleting']
@none = args['none']
@recreating = args['recreating']
@refreshing = args['refreshing']
@restarting = args['restarting']
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.
#
# ----------------------------------------------------------------------------
module GoogleInSpec
module Compute
module Property
class InstanceGroupManagerNamedports
attr_reader :name

attr_reader :port

def initialize(args = nil)
return if args.nil?
@name = args['name']
@port = args['port']
end
end

class InstanceGroupManagerNamedportsArray
def self.parse(value)
return if value.nil?
return InstanceGroupManagerNamedports.new(value) unless value.is_a?(::Array)
value.map { |v| InstanceGroupManagerNamedports.new(v) }
end
end
end
end
end
77 changes: 77 additions & 0 deletions libraries/google_compute_instance_group_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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'
require 'google/compute/property/instancegroupmanager_current_actions'
require 'google/compute/property/instancegroupmanager_named_ports'

# A provider to manage Google Compute Engine resources.
class InstanceGroupManager < GcpResourceBase
name 'google_compute_instance_group_manager'
desc 'InstanceGroupManager'
supports platform: 'gcp'

attr_reader :base_instance_name
attr_reader :creation_timestamp
attr_reader :current_actions
attr_reader :description
attr_reader :id
attr_reader :instance_group
attr_reader :instance_template
attr_reader :name
attr_reader :named_ports
attr_reader :region
attr_reader :target_pools
attr_reader :target_size
attr_reader :zone
def base
'https://www.googleapis.com/compute/v1/'
end

def url
'projects/{{project}}/zones/{{zone}}/instanceGroupManagers/{{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
@base_instance_name = @fetched['baseInstanceName']
@creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
@current_actions = GoogleInSpec::Compute::Property::InstanceGroupManagerCurrentactions.new(@fetched['currentActions'])
@description = @fetched['description']
@id = @fetched['id']
@instance_group = @fetched['instanceGroup']
@instance_template = @fetched['instanceTemplate']
@name = @fetched['name']
@named_ports = GoogleInSpec::Compute::Property::InstanceGroupManagerNamedportsArray.parse(@fetched['namedPorts'])
@region = @fetched['region']
@target_pools = @fetched['targetPools']
@target_size = @fetched['targetSize']
@zone = @fetched['zone']
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
74 changes: 74 additions & 0 deletions libraries/google_compute_instance_group_managers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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 InstanceGroupManagers < GcpResourceBase
name 'google_compute_instance_group_managers'
desc 'InstanceGroupManager plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:base_instance_names, field: :baseInstanceName)
filter_table_config.add(:creation_timestamps, field: :creationTimestamp)
filter_table_config.add(:current_actions, field: :currentActions)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:instance_groups, field: :instanceGroup)
filter_table_config.add(:instance_templates, field: :instanceTemplate)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:named_ports, field: :namedPorts)
filter_table_config.add(:regions, field: :region)
filter_table_config.add(:target_pools, field: :targetPools)
filter_table_config.add(:target_sizes, field: :targetSize)
filter_table_config.add(:zones, field: :zone)

filter_table_config.connect(self, :table)

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

def url
'projects/{{project}}/zones/{{zone}}/instanceGroupManagers'
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_pair { |k, v| hash_with_symbols[k.to_sym] = v }
converted.push(hash_with_symbols)
end
end

converted
end
end
18 changes: 18 additions & 0 deletions test/integration/build/gcp-mm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ variable "record_set" {
type = "map"
}

variable "instance_group_manager" {
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 @@ -58,3 +62,17 @@ resource "google_dns_record_set" "a" {
rrdatas = ["${var.record_set["rrdatas1"]}", "${var.record_set["rrdatas2"]}"]
project = "${var.gcp_project_id}"
}

resource "google_compute_instance_group_manager" "gcp-inspec-igm" {
project = "${var.gcp_project_id}"
zone = "${var.gcp_zone}"
name = "${var.instance_group_manager["name"]}"
instance_template = "${google_compute_instance_template.default.self_link}"
base_instance_name = "${var.instance_group_manager["base_instance_name"]}"
target_pools = []
target_size = 0
named_port {
name = "${var.instance_group_manager["named_port_name"]}"
port = "${var.instance_group_manager["named_port_port"]}"
}
}
6 changes: 6 additions & 0 deletions test/integration/configuration/mm-attributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ record_set:
ttl: 300
rrdatas1: '8.8.8.8'
rrdatas2: '8.8.4.4'

instance_group_manager:
name: 'inspec-gcp-igm'
base_instance_name: 'igm'
named_port_name: 'port'
named_port_port: 80
Loading

0 comments on commit c81375b

Please sign in to comment.