Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/magic-modules/create-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ if [ "$BRANCH_NAME" = "$ORIGINAL_PR_BRANCH" ]; then
fi

git checkout -b "$BRANCH_NAME"
if INSPEC_PR=$(hub pull-request -b "$INSPEC_REPO_USER/inspec:master" -F ./downstream_body); then
if INSPEC_PR=$(hub pull-request -b "$INSPEC_REPO_USER/inspec-gcp:master" -F ./downstream_body); then
DEPENDENCIES="${DEPENDENCIES}depends: $INSPEC_PR ${NEWLINE}"
else
echo "InSpec - did not generate a PR."
Expand Down
2 changes: 1 addition & 1 deletion build/inspec
2 changes: 1 addition & 1 deletion build/terraform
81 changes: 81 additions & 0 deletions products/compute/inspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,87 @@ manifest: !ruby/object:Provider::Inspec::Manifest
summary: 'InSpec resources for verifying GCP infrastructure'
description: |
InSpec resources for verifying GCP infrastructure
overrides: !ruby/object:Provider::ResourceOverrides
Address: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Autoscaler: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
BackendBucket: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
BackendService: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Disk: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
DiskType: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Firewall: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
ForwardingRule: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
GlobalAddress: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
GlobalForwardingRule: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
HealthCheck: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
HttpHealthCheck: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
HttpsHealthCheck: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Image: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Instance: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
InstanceGroup: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
InstanceGroupManager: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
InstanceTemplate: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
InterconnectAttachment: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
License: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
MachineType: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Network: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Region: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
RegionAutoscaler: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
RegionDisk: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
RegionDiskType: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Route: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Router: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Snapshot: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
SslCertificate: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
SslPolicy: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
Subnetwork: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
TargetHttpProxy: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
TargetHttpsProxy: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
TargetPool: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
TargetTcpProxy: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
TargetVpnGateway: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
TargetSslProxy: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
UrlMap: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
VpnTunnel: !ruby/object:Provider::Chef::ResourceOverride
exclude: true
files: !ruby/object:Provider::Config::Files
style:
functions:
Expand Down
9 changes: 9 additions & 0 deletions provider/inspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Provider
# Code generator for Example Cookbooks that manage Google Cloud Platform
# resources.
class Inspec < Provider::Core
include Google::RubyUtils
# Settings for the provider
class Config < Provider::Config
attr_reader :manifest
Expand Down Expand Up @@ -53,6 +54,14 @@ def generate_resource(data)
)
end

# Returns the url that this object can be retrieved from
# based off of the self link
def url(object)
url = object.self_link_url[1]
return url.join('') if url.is_a?(Array)
url.split("\n").join('')
end

# TODO?
def generate_resource_tests(data) end

Expand Down
23 changes: 10 additions & 13 deletions provider/terraform/resources/resource_cloudiot_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func resourceCloudIoTRegistry() *schema.Resource {
},
"mqtt_config": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Comment thread
rileykarson marked this conversation as resolved.
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -90,6 +91,7 @@ func resourceCloudIoTRegistry() *schema.Resource {
},
"http_config": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -233,6 +235,11 @@ func resourceCloudIoTRegistryCreate(d *schema.ResourceData, meta interface{}) er
d.SetId("")
return err
}

// If we infer project and region, they are never actually set so we set them here
d.Set("project", project)
d.Set("region", region)

return resourceCloudIoTRegistryRead(d, meta)
}

Expand Down Expand Up @@ -317,19 +324,9 @@ func resourceCloudIoTRegistryRead(d *schema.ResourceData, meta interface{}) erro
} else {
d.Set("state_notification_config", nil)
}
// If no config exist for mqtt or http config default values are omitted.
mqttState := res.MqttConfig.MqttEnabledState
_, hasMqttConfig := d.GetOk("mqtt_config")
if mqttState != mqttEnabled || hasMqttConfig {
d.Set("mqtt_config",
map[string]string{"mqtt_enabled_state": mqttState})
}
httpState := res.HttpConfig.HttpEnabledState
_, hasHttpConfig := d.GetOk("http_config")
if httpState != httpEnabled || hasHttpConfig {
d.Set("http_config",
map[string]string{"http_enabled_state": httpState})
}

d.Set("mqtt_config", map[string]string{"mqtt_enabled_state": res.MqttConfig.MqttEnabledState})
d.Set("http_config", map[string]string{"http_enabled_state": res.HttpConfig.HttpEnabledState})

credentials := make([]map[string]interface{}, len(res.Credentials))
for i, item := range res.Credentials {
Expand Down
181 changes: 181 additions & 0 deletions provider/terraform/tests/resource_cloudiot_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccCloudIoTRegistry_basic(t *testing.T) {
t.Parallel()

registryName := fmt.Sprintf("psregistry-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudIoTRegistryDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudIoTRegistry_basic(registryName),
Check: resource.ComposeTestCheckFunc(
testAccCloudIoTRegistryExists(
"google_cloudiot_registry.foobar"),
),
},
{
ResourceName: "google_cloudiot_registry.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccCloudIoTRegistry_extended(t *testing.T) {
t.Parallel()

registryName := fmt.Sprintf("psregistry-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudIoTRegistryDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudIoTRegistry_extended(registryName),
Check: resource.ComposeTestCheckFunc(
testAccCloudIoTRegistryExists(
"google_cloudiot_registry.foobar"),
),
},
{
ResourceName: "google_cloudiot_registry.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccCloudIoTRegistry_update(t *testing.T) {
t.Parallel()

registryName := fmt.Sprintf("psregistry-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudIoTRegistryDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCloudIoTRegistry_basic(registryName),
Check: resource.ComposeTestCheckFunc(
testAccCloudIoTRegistryExists(
"google_cloudiot_registry.foobar"),
),
},
resource.TestStep{
Config: testAccCloudIoTRegistry_extended(registryName),
},
resource.TestStep{
Config: testAccCloudIoTRegistry_basic(registryName),
},
{
ResourceName: "google_cloudiot_registry.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckCloudIoTRegistryDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "google_cloudiot_registry" {
continue
}
config := testAccProvider.Meta().(*Config)
registry, _ := config.clientCloudIoT.Projects.Locations.Registries.Get(rs.Primary.ID).Do()
if registry != nil {
return fmt.Errorf("Registry still present")
}
}
return nil
}

func testAccCloudIoTRegistryExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
_, err := config.clientCloudIoT.Projects.Locations.Registries.Get(rs.Primary.ID).Do()
if err != nil {
return fmt.Errorf("Registry does not exist")
}
return nil
}
}

func testAccCloudIoTRegistry_basic(registryName string) string {
return fmt.Sprintf(`
resource "google_cloudiot_registry" "foobar" {
name = "%s"
}`, registryName)
}

func testAccCloudIoTRegistry_extended(registryName string) string {
return fmt.Sprintf(`
resource "google_project_iam_binding" "cloud-iot-iam-binding" {
members = ["serviceAccount:cloud-iot@system.gserviceaccount.com"]
role = "roles/pubsub.publisher"
}

resource "google_pubsub_topic" "default-devicestatus" {
name = "psregistry-test-devicestatus-%s"
}

resource "google_pubsub_topic" "default-telemetry" {
name = "psregistry-test-telemetry-%s"
}

resource "google_cloudiot_registry" "foobar" {
depends_on = ["google_project_iam_binding.cloud-iot-iam-binding"]

name = "%s"

event_notification_config = {
pubsub_topic_name = "${google_pubsub_topic.default-devicestatus.id}"
}

state_notification_config = {
pubsub_topic_name = "${google_pubsub_topic.default-telemetry.id}"
}

http_config = {
http_enabled_state = "HTTP_DISABLED"
}

mqtt_config = {
mqtt_enabled_state = "MQTT_DISABLED"
}

credentials = [
{
"public_key_certificate" = {
format = "X509_CERTIFICATE_PEM"
certificate = "${file("test-fixtures/rsa_cert.pem")}"
}
},
]
}
`, acctest.RandString(10), acctest.RandString(10), registryName)
}
Loading