|
| 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 | + |
| 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: expandTags(tags), |
| 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 | + readRequest := rivieraClient.NewRequestForURI(d.Id()) |
| 114 | + readRequest.Command = &sql.GetServer{} |
| 115 | + |
| 116 | + readResponse, err := readRequest.Execute() |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("Error reading SQL Server: %s", err) |
| 119 | + } |
| 120 | + if !readResponse.IsSuccessful() { |
| 121 | + log.Printf("[INFO] Error reading SQL Server %q - removing from state", d.Id()) |
| 122 | + d.SetId("") |
| 123 | + return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error) |
| 124 | + } |
| 125 | + |
| 126 | + resp := readResponse.Parsed.(*sql.GetServerResponse) |
| 127 | + |
| 128 | + d.Set("fully_qualified_domain_name", resp.FullyQualifiedDomainName) |
| 129 | + d.Set("administrator_login", resp.AdministratorLogin) |
| 130 | + d.Set("version", resp.Version) |
| 131 | + |
| 132 | + flattenAndSetTags(d, resp.Tags) |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func resourceArmSqlServerDelete(d *schema.ResourceData, meta interface{}) error { |
| 138 | + client := meta.(*ArmClient) |
| 139 | + rivieraClient := client.rivieraClient |
| 140 | + |
| 141 | + deleteRequest := rivieraClient.NewRequestForURI(d.Id()) |
| 142 | + deleteRequest.Command = &sql.DeleteServer{} |
| 143 | + |
| 144 | + deleteResponse, err := deleteRequest.Execute() |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("Error deleting SQL Server: %s", err) |
| 147 | + } |
| 148 | + if !deleteResponse.IsSuccessful() { |
| 149 | + return fmt.Errorf("Error deleting SQL Server: %s", deleteResponse.Error) |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
0 commit comments