Skip to content

Commit

Permalink
Merge pull request #3617 from lwander/f-gcp-sql-instance
Browse files Browse the repository at this point in the history
provider/google: SQL instance & database tests & documentation
  • Loading branch information
sparkprime committed Oct 30, 2015
2 parents 07a9c34 + 07de5e7 commit f562d17
Show file tree
Hide file tree
Showing 35 changed files with 1,949 additions and 53 deletions.
9 changes: 9 additions & 0 deletions builtin/providers/google/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"google.golang.org/api/container/v1"
"google.golang.org/api/dns/v1"
"google.golang.org/api/storage/v1"
"google.golang.org/api/sqladmin/v1beta4"
)

// Config is the configuration structure used to instantiate the Google
Expand All @@ -31,6 +32,7 @@ type Config struct {
clientContainer *container.Service
clientDns *dns.Service
clientStorage *storage.Service
clientSqlAdmin *sqladmin.Service
}

func (c *Config) loadAndValidate() error {
Expand Down Expand Up @@ -149,6 +151,13 @@ func (c *Config) loadAndValidate() error {
}
c.clientStorage.UserAgent = userAgent

log.Printf("[INFO] Instantiating Google SqlAdmin Client...")
c.clientSqlAdmin, err = sqladmin.New(client)
if err != nil {
return err
}
c.clientSqlAdmin.UserAgent = userAgent

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions builtin/providers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Provider() terraform.ResourceProvider {
"google_dns_managed_zone": resourceDnsManagedZone(),
"google_dns_record_set": resourceDnsRecordSet(),
"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
"google_sql_database": resourceSqlDatabase(),
"google_sql_database_instance": resourceSqlDatabaseInstance(),
"google_storage_bucket": resourceStorageBucket(),
"google_storage_bucket_acl": resourceStorageBucketAcl(),
"google_storage_bucket_object": resourceStorageBucketObject(),
Expand Down
113 changes: 113 additions & 0 deletions builtin/providers/google/resource_sql_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"

"google.golang.org/api/sqladmin/v1beta4"
)

func resourceSqlDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceSqlDatabaseCreate,
Read: resourceSqlDatabaseRead,
Delete: resourceSqlDatabaseDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
project := config.Project

db := &sqladmin.Database{
Name: database_name,
Instance: instance_name,
}

op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name,
db).Do()

if err != nil {
return fmt.Errorf("Error, failed to insert "+
"database %s into instance %s: %s", database_name,
instance_name, err)
}

err = sqladminOperationWait(config, op, "Insert Database")

if err != nil {
return fmt.Errorf("Error, failure waiting for insertion of %s "+
"into %s: %s", database_name, instance_name, err)
}

return resourceSqlDatabaseRead(d, meta)
}

func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
project := config.Project

db, err := config.clientSqlAdmin.Databases.Get(project, instance_name,
database_name).Do()

if err != nil {
return fmt.Errorf("Error, failed to get"+
"database %s in instance %s: %s", database_name,
instance_name, err)
}

d.Set("self_link", db.SelfLink)
d.SetId(instance_name + ":" + database_name)

return nil
}

func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
project := config.Project

op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name,
database_name).Do()

if err != nil {
return fmt.Errorf("Error, failed to delete"+
"database %s in instance %s: %s", database_name,
instance_name, err)
}

err = sqladminOperationWait(config, op, "Delete Database")

if err != nil {
return fmt.Errorf("Error, failure waiting for deletion of %s "+
"in %s: %s", database_name, instance_name, err)
}

return nil
}
Loading

0 comments on commit f562d17

Please sign in to comment.