github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/aws/resource_aws_db_snapshot.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/rds"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsDbSnapshot() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsDbSnapshotCreate,
    18  		Read:   resourceAwsDbSnapshotRead,
    19  		Delete: resourceAwsDbSnapshotDelete,
    20  
    21  		Timeouts: &schema.ResourceTimeout{
    22  			Read: schema.DefaultTimeout(10 * time.Minute),
    23  		},
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"db_snapshot_identifier": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  			"db_instance_identifier": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"allocated_storage": {
    38  				Type:     schema.TypeInt,
    39  				Computed: true,
    40  			},
    41  			"availability_zone": {
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  			"db_snapshot_arn": {
    46  				Type:     schema.TypeString,
    47  				Computed: true,
    48  			},
    49  			"encrypted": {
    50  				Type:     schema.TypeBool,
    51  				Computed: true,
    52  			},
    53  			"engine": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  			"engine_version": {
    58  				Type:     schema.TypeString,
    59  				Computed: true,
    60  			},
    61  			"iops": {
    62  				Type:     schema.TypeInt,
    63  				Computed: true,
    64  			},
    65  			"kms_key_id": {
    66  				Type:     schema.TypeString,
    67  				Computed: true,
    68  			},
    69  			"license_model": {
    70  				Type:     schema.TypeString,
    71  				Computed: true,
    72  			},
    73  			"option_group_name": {
    74  				Type:     schema.TypeString,
    75  				Computed: true,
    76  			},
    77  			"port": {
    78  				Type:     schema.TypeInt,
    79  				Computed: true,
    80  			},
    81  			"source_db_snapshot_identifier": {
    82  				Type:     schema.TypeString,
    83  				Computed: true,
    84  			},
    85  			"source_region": {
    86  				Type:     schema.TypeString,
    87  				Computed: true,
    88  			},
    89  			"snapshot_type": {
    90  				Type:     schema.TypeString,
    91  				Computed: true,
    92  			},
    93  			"status": {
    94  				Type:     schema.TypeString,
    95  				Computed: true,
    96  			},
    97  			"storage_type": {
    98  				Type:     schema.TypeString,
    99  				Computed: true,
   100  			},
   101  			"vpc_id": {
   102  				Type:     schema.TypeString,
   103  				Computed: true,
   104  			},
   105  		},
   106  	}
   107  }
   108  
   109  func resourceAwsDbSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
   110  	conn := meta.(*AWSClient).rdsconn
   111  
   112  	params := &rds.CreateDBSnapshotInput{
   113  		DBInstanceIdentifier: aws.String(d.Get("db_instance_identifier").(string)),
   114  		DBSnapshotIdentifier: aws.String(d.Get("db_snapshot_identifier").(string)),
   115  	}
   116  
   117  	_, err := conn.CreateDBSnapshot(params)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	d.SetId(d.Get("db_snapshot_identifier").(string))
   122  
   123  	stateConf := &resource.StateChangeConf{
   124  		Pending:    []string{"creating"},
   125  		Target:     []string{"available"},
   126  		Refresh:    resourceAwsDbSnapshotStateRefreshFunc(d, meta),
   127  		Timeout:    d.Timeout(schema.TimeoutRead),
   128  		MinTimeout: 10 * time.Second,
   129  		Delay:      30 * time.Second, // Wait 30 secs before starting
   130  	}
   131  
   132  	// Wait, catching any errors
   133  	_, err = stateConf.WaitForState()
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	return resourceAwsDbSnapshotRead(d, meta)
   139  }
   140  
   141  func resourceAwsDbSnapshotRead(d *schema.ResourceData, meta interface{}) error {
   142  	conn := meta.(*AWSClient).rdsconn
   143  
   144  	params := &rds.DescribeDBSnapshotsInput{
   145  		DBSnapshotIdentifier: aws.String(d.Id()),
   146  	}
   147  	resp, err := conn.DescribeDBSnapshots(params)
   148  	if err != nil {
   149  		return err
   150  	}
   151  
   152  	snapshot := resp.DBSnapshots[0]
   153  
   154  	d.Set("allocated_storage", snapshot.AllocatedStorage)
   155  	d.Set("availability_zone", snapshot.AvailabilityZone)
   156  	d.Set("db_snapshot_arn", snapshot.DBSnapshotArn)
   157  	d.Set("encrypted", snapshot.Encrypted)
   158  	d.Set("engine", snapshot.Engine)
   159  	d.Set("engine_version", snapshot.EngineVersion)
   160  	d.Set("iops", snapshot.Iops)
   161  	d.Set("kms_key_id", snapshot.KmsKeyId)
   162  	d.Set("license_model", snapshot.LicenseModel)
   163  	d.Set("option_group_name", snapshot.OptionGroupName)
   164  	d.Set("port", snapshot.Port)
   165  	d.Set("source_db_snapshot_identifier", snapshot.SourceDBSnapshotIdentifier)
   166  	d.Set("source_region", snapshot.SourceRegion)
   167  	d.Set("snapshot_type", snapshot.SnapshotType)
   168  	d.Set("status", snapshot.Status)
   169  	d.Set("vpc_id", snapshot.VpcId)
   170  
   171  	return nil
   172  }
   173  
   174  func resourceAwsDbSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
   175  	conn := meta.(*AWSClient).rdsconn
   176  
   177  	params := &rds.DeleteDBSnapshotInput{
   178  		DBSnapshotIdentifier: aws.String(d.Id()),
   179  	}
   180  	_, err := conn.DeleteDBSnapshot(params)
   181  	if err != nil {
   182  		return err
   183  	}
   184  
   185  	return nil
   186  }
   187  
   188  func resourceAwsDbSnapshotStateRefreshFunc(
   189  	d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
   190  	return func() (interface{}, string, error) {
   191  		conn := meta.(*AWSClient).rdsconn
   192  
   193  		opts := &rds.DescribeDBSnapshotsInput{
   194  			DBSnapshotIdentifier: aws.String(d.Id()),
   195  		}
   196  
   197  		log.Printf("[DEBUG] DB Snapshot describe configuration: %#v", opts)
   198  
   199  		resp, err := conn.DescribeDBSnapshots(opts)
   200  		if err != nil {
   201  			snapshoterr, ok := err.(awserr.Error)
   202  			if ok && snapshoterr.Code() == "DBSnapshotNotFound" {
   203  				return nil, "", nil
   204  			}
   205  			return nil, "", fmt.Errorf("Error retrieving DB Snapshots: %s", err)
   206  		}
   207  
   208  		if len(resp.DBSnapshots) != 1 {
   209  			return nil, "", fmt.Errorf("No snapshots returned for %s", d.Id())
   210  		}
   211  
   212  		snapshot := resp.DBSnapshots[0]
   213  
   214  		return resp, *snapshot.Status, nil
   215  	}
   216  }