2018-01-24 21:03:09 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2018-10-17 22:11:23 +00:00
|
|
|
func TestAccCloudIoTRegistry_basic(t *testing.T) {
|
2018-01-24 21:03:09 +00:00
|
|
|
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"),
|
|
|
|
),
|
|
|
|
},
|
2018-10-17 22:11:23 +00:00
|
|
|
{
|
|
|
|
ResourceName: "google_cloudiot_registry.foobar",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2018-01-24 21:03:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:11:23 +00:00
|
|
|
func TestAccCloudIoTRegistry_extended(t *testing.T) {
|
2018-01-24 21:03:09 +00:00
|
|
|
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"),
|
|
|
|
),
|
|
|
|
},
|
2018-10-17 22:11:23 +00:00
|
|
|
{
|
|
|
|
ResourceName: "google_cloudiot_registry.foobar",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2018-01-24 21:03:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-17 22:11:23 +00:00
|
|
|
func TestAccCloudIoTRegistry_update(t *testing.T) {
|
2018-01-24 21:03:09 +00:00
|
|
|
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),
|
|
|
|
},
|
2018-10-17 22:11:23 +00:00
|
|
|
{
|
|
|
|
ResourceName: "google_cloudiot_registry.foobar",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2018-01-24 21:03:09 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
2018-06-13 19:37:21 +00:00
|
|
|
name = "%s"
|
2018-01-24 21:03:09 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|