github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_sql_database.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/jen20/riviera/azure"
     9  	"github.com/jen20/riviera/sql"
    10  )
    11  
    12  func resourceArmSqlDatabase() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmSqlDatabaseCreate,
    15  		Read:   resourceArmSqlDatabaseRead,
    16  		Update: resourceArmSqlDatabaseCreate,
    17  		Delete: resourceArmSqlDatabaseDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"location": locationSchema(),
    27  
    28  			"resource_group_name": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"server_name": {
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"create_mode": {
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  				Default:  "Default",
    44  			},
    45  
    46  			"source_database_id": {
    47  				Type:     schema.TypeString,
    48  				Optional: true,
    49  				Computed: true,
    50  			},
    51  
    52  			"restore_point_in_time": {
    53  				Type:     schema.TypeString,
    54  				Optional: true,
    55  				Computed: true,
    56  			},
    57  
    58  			"edition": {
    59  				Type:         schema.TypeString,
    60  				Optional:     true,
    61  				Computed:     true,
    62  				ValidateFunc: validateArmSqlDatabaseEdition,
    63  			},
    64  
    65  			"collation": {
    66  				Type:     schema.TypeString,
    67  				Optional: true,
    68  				Computed: true,
    69  			},
    70  
    71  			"max_size_bytes": {
    72  				Type:     schema.TypeString,
    73  				Optional: true,
    74  				Computed: true,
    75  			},
    76  
    77  			"requested_service_objective_id": {
    78  				Type:     schema.TypeString,
    79  				Optional: true,
    80  				Computed: true,
    81  			},
    82  
    83  			"requested_service_objective_name": {
    84  				Type:     schema.TypeString,
    85  				Optional: true,
    86  				Computed: true,
    87  			},
    88  
    89  			"source_database_deletion_date": {
    90  				Type:     schema.TypeString,
    91  				Optional: true,
    92  				Computed: true,
    93  			},
    94  
    95  			"elastic_pool_name": {
    96  				Type:     schema.TypeString,
    97  				Optional: true,
    98  				Computed: true,
    99  			},
   100  
   101  			"encryption": {
   102  				Type:     schema.TypeString,
   103  				Computed: true,
   104  			},
   105  
   106  			"creation_date": {
   107  				Type:     schema.TypeString,
   108  				Computed: true,
   109  			},
   110  
   111  			"default_secondary_location": {
   112  				Type:     schema.TypeString,
   113  				Computed: true,
   114  			},
   115  
   116  			"tags": tagsSchema(),
   117  		},
   118  	}
   119  }
   120  
   121  func resourceArmSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
   122  	client := meta.(*ArmClient)
   123  	rivieraClient := client.rivieraClient
   124  
   125  	tags := d.Get("tags").(map[string]interface{})
   126  	expandedTags := expandTags(tags)
   127  
   128  	command := &sql.CreateOrUpdateDatabase{
   129  		Name:              d.Get("name").(string),
   130  		Location:          d.Get("location").(string),
   131  		ResourceGroupName: d.Get("resource_group_name").(string),
   132  		ServerName:        d.Get("server_name").(string),
   133  		Tags:              *expandedTags,
   134  		CreateMode:        azure.String(d.Get("create_mode").(string)),
   135  	}
   136  
   137  	if v, ok := d.GetOk("source_database_id"); ok {
   138  		command.SourceDatabaseID = azure.String(v.(string))
   139  	}
   140  
   141  	if v, ok := d.GetOk("edition"); ok {
   142  		command.Edition = azure.String(v.(string))
   143  	}
   144  
   145  	if v, ok := d.GetOk("collation"); ok {
   146  		command.Collation = azure.String(v.(string))
   147  	}
   148  
   149  	if v, ok := d.GetOk("max_size_bytes"); ok {
   150  		command.MaxSizeBytes = azure.String(v.(string))
   151  	}
   152  
   153  	if v, ok := d.GetOk("source_database_deletion_date"); ok {
   154  		command.SourceDatabaseDeletionDate = azure.String(v.(string))
   155  	}
   156  
   157  	if v, ok := d.GetOk("requested_service_objective_id"); ok {
   158  		command.RequestedServiceObjectiveID = azure.String(v.(string))
   159  	}
   160  
   161  	if v, ok := d.GetOk("requested_service_objective_name"); ok {
   162  		command.RequestedServiceObjectiveName = azure.String(v.(string))
   163  	}
   164  
   165  	createRequest := rivieraClient.NewRequest()
   166  	createRequest.Command = command
   167  
   168  	createResponse, err := createRequest.Execute()
   169  	if err != nil {
   170  		return fmt.Errorf("Error creating SQL Database: %s", err)
   171  	}
   172  	if !createResponse.IsSuccessful() {
   173  		return fmt.Errorf("Error creating SQL Database: %s", createResponse.Error)
   174  	}
   175  
   176  	readRequest := rivieraClient.NewRequest()
   177  	readRequest.Command = &sql.GetDatabase{
   178  		Name:              d.Get("name").(string),
   179  		ResourceGroupName: d.Get("resource_group_name").(string),
   180  		ServerName:        d.Get("server_name").(string),
   181  	}
   182  
   183  	readResponse, err := readRequest.Execute()
   184  	if err != nil {
   185  		return fmt.Errorf("Error reading SQL Database: %s", err)
   186  	}
   187  	if !readResponse.IsSuccessful() {
   188  		return fmt.Errorf("Error reading SQL Database: %s", readResponse.Error)
   189  	}
   190  
   191  	resp := readResponse.Parsed.(*sql.GetDatabaseResponse)
   192  	d.SetId(*resp.ID)
   193  
   194  	return resourceArmSqlDatabaseRead(d, meta)
   195  }
   196  
   197  func resourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
   198  	client := meta.(*ArmClient)
   199  	rivieraClient := client.rivieraClient
   200  
   201  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   202  	readRequest.Command = &sql.GetDatabase{}
   203  
   204  	readResponse, err := readRequest.Execute()
   205  	if err != nil {
   206  		return fmt.Errorf("Error reading SQL Database: %s", err)
   207  	}
   208  	if !readResponse.IsSuccessful() {
   209  		log.Printf("[INFO] Error reading SQL Database %q - removing from state", d.Id())
   210  		d.SetId("")
   211  		return fmt.Errorf("Error reading SQL Database: %s", readResponse.Error)
   212  	}
   213  
   214  	resp := readResponse.Parsed.(*sql.GetDatabaseResponse)
   215  
   216  	d.Set("name", resp.Name)
   217  	d.Set("creation_date", resp.CreationDate)
   218  	d.Set("default_secondary_location", resp.DefaultSecondaryLocation)
   219  
   220  	flattenAndSetTags(d, resp.Tags)
   221  
   222  	return nil
   223  }
   224  
   225  func resourceArmSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
   226  	client := meta.(*ArmClient)
   227  	rivieraClient := client.rivieraClient
   228  
   229  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   230  	deleteRequest.Command = &sql.DeleteDatabase{}
   231  
   232  	deleteResponse, err := deleteRequest.Execute()
   233  	if err != nil {
   234  		return fmt.Errorf("Error deleting SQL Database: %s", err)
   235  	}
   236  	if !deleteResponse.IsSuccessful() {
   237  		return fmt.Errorf("Error deleting SQL Database: %s", deleteResponse.Error)
   238  	}
   239  
   240  	return nil
   241  }
   242  
   243  func validateArmSqlDatabaseEdition(v interface{}, k string) (ws []string, errors []error) {
   244  	editions := map[string]bool{
   245  		"Basic":         true,
   246  		"Standard":      true,
   247  		"Premium":       true,
   248  		"DataWarehouse": true,
   249  	}
   250  	if !editions[v.(string)] {
   251  		errors = append(errors, fmt.Errorf("SQL Database Edition can only be Basic, Standard, Premium or DataWarehouse"))
   252  	}
   253  	return
   254  }