2015-11-02 15:15:19 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 21:47:10 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-11-02 15:15:19 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeSslCertificate_basic(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-11-02 15:15:19 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-11-28 00:32:00 +00:00
|
|
|
Config: testAccComputeSslCertificate_basic(),
|
2015-11-02 15:15:19 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeSslCertificateExists(
|
|
|
|
"google_compute_ssl_certificate.foobar"),
|
|
|
|
),
|
|
|
|
},
|
2018-01-03 14:26:55 +00:00
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_compute_ssl_certificate.foobar",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
ImportStateVerifyIgnore: []string{"private_key"},
|
|
|
|
},
|
2015-11-02 15:15:19 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-12 23:57:58 +00:00
|
|
|
func TestAccComputeSslCertificate_no_name(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-12 23:57:58 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-11-28 00:32:00 +00:00
|
|
|
Config: testAccComputeSslCertificate_no_name(),
|
2016-12-12 23:57:58 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeSslCertificateExists(
|
|
|
|
"google_compute_ssl_certificate.foobar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccComputeSslCertificate_name_prefix(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-12-12 23:57:58 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeSslCertificateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-11-28 00:32:00 +00:00
|
|
|
Config: testAccComputeSslCertificate_name_prefix(),
|
2016-12-12 23:57:58 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeSslCertificateExists(
|
|
|
|
"google_compute_ssl_certificate.foobar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-02 15:15:19 +00:00
|
|
|
func testAccCheckComputeSslCertificateDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_compute_ssl_certificate" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.SslCertificates.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("SslCertificate still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeSslCertificateExists(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)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.SslCertificates.Get(
|
|
|
|
config.Project, rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Certificate not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:32:00 +00:00
|
|
|
func testAccComputeSslCertificate_basic() string {
|
|
|
|
return fmt.Sprintf(`
|
2015-11-02 15:15:19 +00:00
|
|
|
resource "google_compute_ssl_certificate" "foobar" {
|
2016-01-05 21:47:10 +00:00
|
|
|
name = "sslcert-test-%s"
|
2015-11-02 15:15:19 +00:00
|
|
|
description = "very descriptive"
|
2015-11-20 13:49:55 +00:00
|
|
|
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
|
|
|
|
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
|
2015-11-02 15:15:19 +00:00
|
|
|
}
|
2016-01-05 21:47:10 +00:00
|
|
|
`, acctest.RandString(10))
|
2017-11-28 00:32:00 +00:00
|
|
|
}
|
2016-12-12 23:57:58 +00:00
|
|
|
|
2017-11-28 00:32:00 +00:00
|
|
|
func testAccComputeSslCertificate_no_name() string {
|
|
|
|
return fmt.Sprintf(`
|
2016-12-12 23:57:58 +00:00
|
|
|
resource "google_compute_ssl_certificate" "foobar" {
|
|
|
|
description = "really descriptive"
|
|
|
|
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
|
|
|
|
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
|
|
|
|
}
|
|
|
|
`)
|
2017-11-28 00:32:00 +00:00
|
|
|
}
|
2016-12-12 23:57:58 +00:00
|
|
|
|
2017-11-28 00:32:00 +00:00
|
|
|
func testAccComputeSslCertificate_name_prefix() string {
|
|
|
|
return fmt.Sprintf(`
|
2016-12-12 23:57:58 +00:00
|
|
|
resource "google_compute_ssl_certificate" "foobar" {
|
|
|
|
name_prefix = "sslcert-test-%s-"
|
|
|
|
description = "extremely descriptive"
|
|
|
|
private_key = "${file("test-fixtures/ssl_cert/test.key")}"
|
|
|
|
certificate = "${file("test-fixtures/ssl_cert/test.crt")}"
|
|
|
|
}
|
|
|
|
`, acctest.RandString(10))
|
2017-11-28 00:32:00 +00:00
|
|
|
}
|