github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_sql_server.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 resourceArmSqlServer() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmSqlServerCreate,
    15  		Read:   resourceArmSqlServerRead,
    16  		Update: resourceArmSqlServerCreate,
    17  		Delete: resourceArmSqlServerDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"location": locationSchema(),
    30  
    31  			"resource_group_name": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"version": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  			},
    41  
    42  			"administrator_login": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  			},
    46  
    47  			"administrator_login_password": &schema.Schema{
    48  				Type:      schema.TypeString,
    49  				Required:  true,
    50  				Sensitive: true,
    51  			},
    52  
    53  			"fully_qualified_domain_name": &schema.Schema{
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"tags": tagsSchema(),
    59  		},
    60  	}
    61  }
    62  
    63  func resourceArmSqlServerCreate(d *schema.ResourceData, meta interface{}) error {
    64  	client := meta.(*ArmClient)
    65  	rivieraClient := client.rivieraClient
    66  
    67  	tags := d.Get("tags").(map[string]interface{})
    68  	expandedTags := expandTags(tags)
    69  
    70  	createRequest := rivieraClient.NewRequest()
    71  	createRequest.Command = &sql.CreateOrUpdateServer{
    72  		Name:                       d.Get("name").(string),
    73  		Location:                   d.Get("location").(string),
    74  		ResourceGroupName:          d.Get("resource_group_name").(string),
    75  		AdministratorLogin:         azure.String(d.Get("administrator_login").(string)),
    76  		AdministratorLoginPassword: azure.String(d.Get("administrator_login_password").(string)),
    77  		Version:                    azure.String(d.Get("version").(string)),
    78  		Tags:                       *expandedTags,
    79  	}
    80  
    81  	createResponse, err := createRequest.Execute()
    82  	if err != nil {
    83  		return fmt.Errorf("Error creating SQL Server: %s", err)
    84  	}
    85  	if !createResponse.IsSuccessful() {
    86  		return fmt.Errorf("Error creating SQL Server: %s", createResponse.Error)
    87  	}
    88  
    89  	readRequest := rivieraClient.NewRequest()
    90  	readRequest.Command = &sql.GetServer{
    91  		Name:              d.Get("name").(string),
    92  		ResourceGroupName: d.Get("resource_group_name").(string),
    93  	}
    94  
    95  	readResponse, err := readRequest.Execute()
    96  	if err != nil {
    97  		return fmt.Errorf("Error reading SQL Server: %s", err)
    98  	}
    99  	if !readResponse.IsSuccessful() {
   100  		return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error)
   101  	}
   102  
   103  	resp := readResponse.Parsed.(*sql.GetServerResponse)
   104  	d.SetId(*resp.ID)
   105  
   106  	return resourceArmSqlServerRead(d, meta)
   107  }
   108  
   109  func resourceArmSqlServerRead(d *schema.ResourceData, meta interface{}) error {
   110  	client := meta.(*ArmClient)
   111  	rivieraClient := client.rivieraClient
   112  
   113  	id, err := parseAzureResourceID(d.Id())
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   119  	readRequest.Command = &sql.GetServer{}
   120  
   121  	readResponse, err := readRequest.Execute()
   122  	if err != nil {
   123  		return fmt.Errorf("Error reading SQL Server: %s", err)
   124  	}
   125  	if !readResponse.IsSuccessful() {
   126  		log.Printf("[INFO] Error reading SQL Server %q - removing from state", d.Id())
   127  		d.SetId("")
   128  		return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error)
   129  	}
   130  
   131  	resp := readResponse.Parsed.(*sql.GetServerResponse)
   132  
   133  	d.Set("name", id.Path["servers"])
   134  	d.Set("resource_group_name", id.ResourceGroup)
   135  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   136  	d.Set("fully_qualified_domain_name", resp.FullyQualifiedDomainName)
   137  	d.Set("administrator_login", resp.AdministratorLogin)
   138  	d.Set("version", resp.Version)
   139  
   140  	flattenAndSetTags(d, resp.Tags)
   141  
   142  	return nil
   143  }
   144  
   145  func resourceArmSqlServerDelete(d *schema.ResourceData, meta interface{}) error {
   146  	client := meta.(*ArmClient)
   147  	rivieraClient := client.rivieraClient
   148  
   149  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   150  	deleteRequest.Command = &sql.DeleteServer{}
   151  
   152  	deleteResponse, err := deleteRequest.Execute()
   153  	if err != nil {
   154  		return fmt.Errorf("Error deleting SQL Server: %s", err)
   155  	}
   156  	if !deleteResponse.IsSuccessful() {
   157  		return fmt.Errorf("Error deleting SQL Server: %s", deleteResponse.Error)
   158  	}
   159  
   160  	return nil
   161  }