github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_sql_user_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccGoogleSqlUser_basic(t *testing.T) {
    13  	user := acctest.RandString(10)
    14  	instance := acctest.RandString(10)
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccGoogleSqlUserDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testGoogleSqlUser_basic(instance, user),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckGoogleSqlUserExists("google_sql_user.user"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccGoogleSqlUser_update(t *testing.T) {
    32  	user := acctest.RandString(10)
    33  	instance := acctest.RandString(10)
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccGoogleSqlUserDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testGoogleSqlUser_basic(instance, user),
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckGoogleSqlUserExists("google_sql_user.user"),
    44  				),
    45  			},
    46  
    47  			resource.TestStep{
    48  				Config: testGoogleSqlUser_basic2(instance, user),
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckGoogleSqlUserExists("google_sql_user.user"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func testAccCheckGoogleSqlUserExists(n string) resource.TestCheckFunc {
    58  	return func(s *terraform.State) error {
    59  		config := testAccProvider.Meta().(*Config)
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("Resource not found: %s", n)
    63  		}
    64  
    65  		name := rs.Primary.Attributes["name"]
    66  		instance := rs.Primary.Attributes["instance"]
    67  		host := rs.Primary.Attributes["host"]
    68  		users, err := config.clientSqlAdmin.Users.List(config.Project,
    69  			instance).Do()
    70  
    71  		for _, user := range users.Items {
    72  			if user.Name == name && user.Host == host {
    73  				return nil
    74  			}
    75  		}
    76  
    77  		return fmt.Errorf("Not found: %s: %s", n, err)
    78  	}
    79  }
    80  
    81  func testAccGoogleSqlUserDestroy(s *terraform.State) error {
    82  	for _, rs := range s.RootModule().Resources {
    83  		config := testAccProvider.Meta().(*Config)
    84  		if rs.Type != "google_sql_database" {
    85  			continue
    86  		}
    87  
    88  		name := rs.Primary.Attributes["name"]
    89  		instance := rs.Primary.Attributes["instance"]
    90  		host := rs.Primary.Attributes["host"]
    91  		users, err := config.clientSqlAdmin.Users.List(config.Project,
    92  			instance).Do()
    93  
    94  		for _, user := range users.Items {
    95  			if user.Name == name && user.Host == host {
    96  				return fmt.Errorf("User still %s exists %s", name, err)
    97  			}
    98  		}
    99  
   100  		return nil
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func testGoogleSqlUser_basic(instance, user string) string {
   107  	return fmt.Sprintf(`
   108  	resource "google_sql_database_instance" "instance" {
   109  		name = "i%s"
   110  		region = "us-central"
   111  		settings {
   112  			tier = "D0"
   113  		}
   114  	}
   115  
   116  	resource "google_sql_user" "user" {
   117  		name = "user%s"
   118  		instance = "${google_sql_database_instance.instance.name}"
   119  		host = "google.com"
   120  		password = "hunter2"
   121  	}
   122  	`, instance, user)
   123  }
   124  
   125  func testGoogleSqlUser_basic2(instance, user string) string {
   126  	return fmt.Sprintf(`
   127  	resource "google_sql_database_instance" "instance" {
   128  		name = "i%s"
   129  		region = "us-central"
   130  		settings {
   131  			tier = "D0"
   132  		}
   133  	}
   134  
   135  	resource "google_sql_user" "user" {
   136  		name = "user%s"
   137  		instance = "${google_sql_database_instance.instance.name}"
   138  		host = "google.com"
   139  		password = "oops"
   140  	}
   141  	`, instance, user)
   142  }