Skip to content

Commit

Permalink
Merge pull request #578 from inspec/CHEF-7194-MAGIC-MODULE-compute_v1…
Browse files Browse the repository at this point in the history
…-NetworkAttachment

CHEF-7194-MAGIC-MODULE-compute_v1-NetworkAttachment - Resource Implementation
  • Loading branch information
sa-progress authored Feb 1, 2024
2 parents b1d3496 + bdafed6 commit 83b2365
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ The following resources are available in the InSpec GCP Profile
| [google_compute_interconnect_location](docs/resources/google_compute_interconnect_location.md) | [google_compute_interconnect_locations](docs/resources/google_compute_interconnect_locations.md) |
| [google_compute_license_code](docs/resources/google_compute_license_code.md) | No Plural Resource |
| [google_compute_machine_image](docs/resources/google_compute_machine_image.md) | [google_compute_machine_images](docs/resources/google_compute_machine_images.md) |
| [google_compute_network_attachment](docs/resources/google_compute_network_attachment.md) | [google_compute_network_attachments](docs/resources/google_compute_network_attachments.md) |
| [google_compute_network](docs/resources/google_compute_network.md) | [google_compute_networks](docs/resources/google_compute_networks.md) |
| [google_compute_network_endpoint_group](docs/resources/google_compute_network_endpoint_group.md) | [google_compute_network_endpoint_groups](docs/resources/google_compute_network_endpoint_groups.md) |
| [google_compute_node_group](docs/resources/google_compute_node_group.md) | [google_compute_node_groups](docs/resources/google_compute_node_groups.md) |
Expand Down
94 changes: 94 additions & 0 deletions docs/resources/google_compute_network_attachment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: About the google_compute_network_attachment resource
platform: gcp
---

## Syntax
A `google_compute_network_attachment` is used to test a Google NetworkAttachment resource

## Examples
```
describe google_compute_network_attachment(name: 'value_name', project: 'chef-gcp-inspec', region: ' value_region') do
it { should exist }
its('kind') { should cmp 'value_kind' }
its('id') { should cmp 'value_id' }
its('creation_timestamp') { should cmp 'value_creationtimestamp' }
its('name') { should cmp 'value_name' }
its('description') { should cmp 'value_description' }
its('self_link') { should cmp 'value_selflink' }
its('self_link_with_id') { should cmp 'value_selflinkwithid' }
its('region') { should cmp 'value_region' }
its('connection_preference') { should cmp 'value_connectionpreference' }
its('fingerprint') { should cmp 'value_fingerprint' }
its('network') { should cmp 'value_network' }
end
describe google_compute_network_attachment(name: 'value_name', project: 'chef-gcp-inspec', region: ' value_region') do
it { should_not exist }
end
```

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


* `kind`: [Output Only] Type of the resource.

* `id`: [Output Only] The unique identifier for the resource type. The server generates this identifier.

* `creation_timestamp`: [Output Only] Creation timestamp in RFC3339 text format.

* `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.

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

* `self_link`: [Output Only] Server-defined URL for the resource.

* `self_link_with_id`: [Output Only] Server-defined URL for this resource's resource id.

* `region`: [Output Only] URL of the region where the network attachment resides. This field applies only to the region resource. You must specify this field as part of the HTTP request URL. It is not settable as a field in the request body.

* `connection_preference`:
Possible values:
* ACCEPT_AUTOMATIC
* ACCEPT_MANUAL
* INVALID

* `connection_endpoints`: [Output Only] An array of connections for all the producers connected to this network attachment.

* `status`: The status of a connected endpoint to this network attachment.
Possible values:
* ACCEPTED
* CLOSED
* NEEDS_ATTENTION
* PENDING
* REJECTED
* STATUS_UNSPECIFIED

* `project_id_or_num`: The project id or number of the interface to which the IP was assigned.

* `subnetwork`: The subnetwork used to assign the IP to the producer instance network interface.

* `ip_address`: The IPv4 address assigned to the producer instance network interface. This value will be a range in case of Serverless.

* `ipv6_address`: The IPv6 address assigned to the producer instance network interface. This is only assigned when the stack types of both the instance network interface and the consumer subnet are IPv4_IPv6.

* `secondary_ip_cidr_ranges`: Alias IP ranges from the same subnetwork.

* `subnetwork_cidr_range`: [Output Only] The CIDR range of the subnet from which the IPv4 internal IP was allocated from.

* `subnetworks`: An array of URLs where each entry is the URL of a subnet provided by the service consumer to use for endpoints in the producers that connect to this network attachment.

* `producer_reject_lists`: Projects that are not allowed to connect to this network attachment. The project can be specified using its id or number.

* `producer_accept_lists`: Projects that are allowed to connect to this network attachment. The project can be specified using its id or number.

* `fingerprint`: Fingerprint of this resource. A hash of the contents stored in this object. This field is used in optimistic locking. An up-to-date fingerprint must be provided in order to patch.

* `network`: [Output Only] The URL of the network which the Network Attachment belongs to. Practically it is inferred by fetching the network of the first subnetwork associated. Because it is required that all the subnetworks must be from the same network, it is assured that the Network Attachment belongs to the same network as all the subnetworks.


## GCP Permissions

Ensure the [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the current project.
42 changes: 42 additions & 0 deletions docs/resources/google_compute_network_attachments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: About the google_compute_network_attachments resource
platform: gcp
---

## Syntax
A `google_compute_network_attachments` is used to test a Google NetworkAttachment resource

## Examples
```
describe google_compute_network_attachments(project: 'chef-gcp-inspec', region: ' value_region') do
it { should exist }
end
```

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

See [google_compute_network_attachment.md](google_compute_network_attachment.md) for more detailed information
* `kinds`: an array of `google_compute_network_attachment` kind
* `ids`: an array of `google_compute_network_attachment` id
* `creation_timestamps`: an array of `google_compute_network_attachment` creation_timestamp
* `names`: an array of `google_compute_network_attachment` name
* `descriptions`: an array of `google_compute_network_attachment` description
* `self_links`: an array of `google_compute_network_attachment` self_link
* `self_link_with_ids`: an array of `google_compute_network_attachment` self_link_with_id
* `regions`: an array of `google_compute_network_attachment` region
* `connection_preferences`: an array of `google_compute_network_attachment` connection_preference
* `connection_endpoints`: an array of `google_compute_network_attachment` connection_endpoints
* `subnetworks`: an array of `google_compute_network_attachment` subnetworks
* `producer_reject_lists`: an array of `google_compute_network_attachment` producer_reject_lists
* `producer_accept_lists`: an array of `google_compute_network_attachment` producer_accept_lists
* `fingerprints`: an array of `google_compute_network_attachment` fingerprint
* `networks`: an array of `google_compute_network_attachment` network

## 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.

## GCP Permissions

Ensure the [Compute Engine API](https://console.cloud.google.com/apis/library/compute.googleapis.com/) is enabled for the current project.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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 NetworkAttachmentConnectionEndpoints
attr_reader :status

attr_reader :project_id_or_num

attr_reader :subnetwork

attr_reader :ip_address

attr_reader :ipv6_address

attr_reader :secondary_ip_cidr_ranges

attr_reader :subnetwork_cidr_range

def initialize(args = nil, parent_identifier = nil)
return if args.nil?
@parent_identifier = parent_identifier
@status = args['status']
@project_id_or_num = args['projectIdOrNum']
@subnetwork = args['subnetwork']
@ip_address = args['ipAddress']
@ipv6_address = args['ipv6Address']
@secondary_ip_cidr_ranges = args['secondaryIpCidrRanges']
@subnetwork_cidr_range = args['subnetworkCidrRange']
end

def to_s
"#{@parent_identifier} NetworkAttachmentConnectionEndpoints"
end
end

class NetworkAttachmentConnectionEndpointsArray
def self.parse(value, parent_identifier)
return if value.nil?
return NetworkAttachmentConnectionEndpoints.new(value, parent_identifier) unless value.is_a?(::Array)
value.map { |v| NetworkAttachmentConnectionEndpoints.new(v, parent_identifier) }
end
end
end
end
end
84 changes: 84 additions & 0 deletions libraries/google_compute_network_attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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/networkattachment_connection_endpoints'

# A provider to manage Compute Engine resources.
class ComputeNetworkAttachment < GcpResourceBase
name 'google_compute_network_attachment'
desc 'NetworkAttachment'
supports platform: 'gcp'

attr_reader :params
attr_reader :kind
attr_reader :id
attr_reader :creation_timestamp
attr_reader :name
attr_reader :description
attr_reader :self_link
attr_reader :self_link_with_id
attr_reader :region
attr_reader :connection_preference
attr_reader :connection_endpoints
attr_reader :subnetworks
attr_reader :producer_reject_lists
attr_reader :producer_accept_lists
attr_reader :fingerprint
attr_reader :network

def initialize(params)
super(params.merge({ use_http_transport: true }))
@params = params
@fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get')
parse unless @fetched.nil?
end

def parse
@kind = @fetched['kind']
@id = @fetched['id']
@creation_timestamp = @fetched['creationTimestamp']
@name = @fetched['name']
@description = @fetched['description']
@self_link = @fetched['selfLink']
@self_link_with_id = @fetched['selfLinkWithId']
@region = @fetched['region']
@connection_preference = @fetched['connectionPreference']
@connection_endpoints = GoogleInSpec::Compute::Property::NetworkAttachmentConnectionEndpointsArray.parse(@fetched['connectionEndpoints'], to_s)
@subnetworks = @fetched['subnetworks']
@producer_reject_lists = @fetched['producerRejectLists']
@producer_accept_lists = @fetched['producerAcceptLists']
@fingerprint = @fetched['fingerprint']
@network = @fetched['network']
end

def exists?
!@fetched.nil?
end

def to_s
"NetworkAttachment #{@params[:networkAttachment]}"
end

private

def product_url(_ = nil)
'https://compute.googleapis.com/compute/v1/'
end

def resource_base_url
'projects/{{project}}/regions/{{region}}/networkAttachments/{{name}}'
end
end
107 changes: 107 additions & 0 deletions libraries/google_compute_network_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** Type: MMv1 ***
#
# ----------------------------------------------------------------------------
#
# 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 ComputeNetworkAttachments < GcpResourceBase
name 'google_compute_network_attachments'
desc 'NetworkAttachment plural resource'
supports platform: 'gcp'

attr_reader :table

filter_table_config = FilterTable.create

filter_table_config.add(:kinds, field: :kind)
filter_table_config.add(:ids, field: :id)
filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
filter_table_config.add(:names, field: :name)
filter_table_config.add(:descriptions, field: :description)
filter_table_config.add(:self_links, field: :self_link)
filter_table_config.add(:self_link_with_ids, field: :self_link_with_id)
filter_table_config.add(:regions, field: :region)
filter_table_config.add(:connection_preferences, field: :connection_preference)
filter_table_config.add(:connection_endpoints, field: :connection_endpoints)
filter_table_config.add(:subnetworks, field: :subnetworks)
filter_table_config.add(:producer_reject_lists, field: :producer_reject_lists)
filter_table_config.add(:producer_accept_lists, field: :producer_accept_lists)
filter_table_config.add(:fingerprints, field: :fingerprint)
filter_table_config.add(:networks, field: :network)

filter_table_config.connect(self, :table)

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(product_url, resource_base_url, @params, 'Get')
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
{
'kind' => ->(obj) { [:kind, obj['kind']] },
'id' => ->(obj) { [:id, obj['id']] },
'creationTimestamp' => ->(obj) { [:creation_timestamp, obj['creationTimestamp']] },
'name' => ->(obj) { [:name, obj['name']] },
'description' => ->(obj) { [:description, obj['description']] },
'selfLink' => ->(obj) { [:self_link, obj['selfLink']] },
'selfLinkWithId' => ->(obj) { [:self_link_with_id, obj['selfLinkWithId']] },
'region' => ->(obj) { [:region, obj['region']] },
'connectionPreference' => ->(obj) { [:connection_preference, obj['connectionPreference']] },
'connectionEndpoints' => ->(obj) { [:connection_endpoints, GoogleInSpec::Compute::Property::NetworkAttachmentConnectionEndpointsArray.parse(obj['connectionEndpoints'], to_s)] },
'subnetworks' => ->(obj) { [:subnetworks, obj['subnetworks']] },
'producerRejectLists' => ->(obj) { [:producer_reject_lists, obj['producerRejectLists']] },
'producerAcceptLists' => ->(obj) { [:producer_accept_lists, obj['producerAcceptLists']] },
'fingerprint' => ->(obj) { [:fingerprint, obj['fingerprint']] },
'network' => ->(obj) { [:network, obj['network']] },
}
end

private

def product_url(_ = nil)
'https://compute.googleapis.com/compute/v1/'
end

def resource_base_url
'projects/{{project}}/regions/{{region}}/networkAttachments'
end
end
Loading

0 comments on commit 83b2365

Please sign in to comment.