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