github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 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 "version": &schema.Schema{ 40 Type: schema.TypeString, 41 Required: true, 42 }, 43 44 "administrator_login": &schema.Schema{ 45 Type: schema.TypeString, 46 Required: true, 47 }, 48 49 "administrator_login_password": &schema.Schema{ 50 Type: schema.TypeString, 51 Required: true, 52 }, 53 54 "fully_qualified_domain_name": &schema.Schema{ 55 Type: schema.TypeString, 56 Computed: true, 57 }, 58 59 "tags": tagsSchema(), 60 }, 61 } 62 } 63 64 func resourceArmSqlServerCreate(d *schema.ResourceData, meta interface{}) error { 65 client := meta.(*ArmClient) 66 rivieraClient := client.rivieraClient 67 68 tags := d.Get("tags").(map[string]interface{}) 69 expandedTags := expandTags(tags) 70 71 createRequest := rivieraClient.NewRequest() 72 createRequest.Command = &sql.CreateOrUpdateServer{ 73 Name: d.Get("name").(string), 74 Location: d.Get("location").(string), 75 ResourceGroupName: d.Get("resource_group_name").(string), 76 AdministratorLogin: azure.String(d.Get("administrator_login").(string)), 77 AdministratorLoginPassword: azure.String(d.Get("administrator_login_password").(string)), 78 Version: azure.String(d.Get("version").(string)), 79 Tags: *expandedTags, 80 } 81 82 createResponse, err := createRequest.Execute() 83 if err != nil { 84 return fmt.Errorf("Error creating SQL Server: %s", err) 85 } 86 if !createResponse.IsSuccessful() { 87 return fmt.Errorf("Error creating SQL Server: %s", createResponse.Error) 88 } 89 90 readRequest := rivieraClient.NewRequest() 91 readRequest.Command = &sql.GetServer{ 92 Name: d.Get("name").(string), 93 ResourceGroupName: d.Get("resource_group_name").(string), 94 } 95 96 readResponse, err := readRequest.Execute() 97 if err != nil { 98 return fmt.Errorf("Error reading SQL Server: %s", err) 99 } 100 if !readResponse.IsSuccessful() { 101 return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error) 102 } 103 104 resp := readResponse.Parsed.(*sql.GetServerResponse) 105 d.SetId(*resp.ID) 106 107 return resourceArmSqlServerRead(d, meta) 108 } 109 110 func resourceArmSqlServerRead(d *schema.ResourceData, meta interface{}) error { 111 client := meta.(*ArmClient) 112 rivieraClient := client.rivieraClient 113 114 readRequest := rivieraClient.NewRequestForURI(d.Id()) 115 readRequest.Command = &sql.GetServer{} 116 117 readResponse, err := readRequest.Execute() 118 if err != nil { 119 return fmt.Errorf("Error reading SQL Server: %s", err) 120 } 121 if !readResponse.IsSuccessful() { 122 log.Printf("[INFO] Error reading SQL Server %q - removing from state", d.Id()) 123 d.SetId("") 124 return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error) 125 } 126 127 resp := readResponse.Parsed.(*sql.GetServerResponse) 128 129 d.Set("fully_qualified_domain_name", resp.FullyQualifiedDomainName) 130 d.Set("administrator_login", resp.AdministratorLogin) 131 d.Set("version", resp.Version) 132 133 flattenAndSetTags(d, resp.Tags) 134 135 return nil 136 } 137 138 func resourceArmSqlServerDelete(d *schema.ResourceData, meta interface{}) error { 139 client := meta.(*ArmClient) 140 rivieraClient := client.rivieraClient 141 142 deleteRequest := rivieraClient.NewRequestForURI(d.Id()) 143 deleteRequest.Command = &sql.DeleteServer{} 144 145 deleteResponse, err := deleteRequest.Execute() 146 if err != nil { 147 return fmt.Errorf("Error deleting SQL Server: %s", err) 148 } 149 if !deleteResponse.IsSuccessful() { 150 return fmt.Errorf("Error deleting SQL Server: %s", deleteResponse.Error) 151 } 152 153 return nil 154 }