github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_sql_firewall_rule.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 resourceArmSqlFirewallRule() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceArmSqlFirewallRuleCreate, 15 Read: resourceArmSqlFirewallRuleRead, 16 Update: resourceArmSqlFirewallRuleCreate, 17 Delete: resourceArmSqlFirewallRuleDelete, 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 "resource_group_name": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 35 "server_name": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "start_ip_address": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 }, 45 46 "end_ip_address": &schema.Schema{ 47 Type: schema.TypeString, 48 Required: true, 49 }, 50 }, 51 } 52 } 53 54 func resourceArmSqlFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error { 55 client := meta.(*ArmClient) 56 rivieraClient := client.rivieraClient 57 58 createRequest := rivieraClient.NewRequest() 59 createRequest.Command = &sql.CreateOrUpdateFirewallRule{ 60 Name: d.Get("name").(string), 61 ResourceGroupName: d.Get("resource_group_name").(string), 62 ServerName: d.Get("server_name").(string), 63 StartIPAddress: azure.String(d.Get("start_ip_address").(string)), 64 EndIPAddress: azure.String(d.Get("end_ip_address").(string)), 65 } 66 67 createResponse, err := createRequest.Execute() 68 if err != nil { 69 return fmt.Errorf("Error creating SQL Server Firewall Rule: %s", err) 70 } 71 if !createResponse.IsSuccessful() { 72 return fmt.Errorf("Error creating SQL Server Firewall Rule: %s", createResponse.Error) 73 } 74 75 readRequest := rivieraClient.NewRequest() 76 readRequest.Command = &sql.GetFirewallRule{ 77 Name: d.Get("name").(string), 78 ResourceGroupName: d.Get("resource_group_name").(string), 79 ServerName: d.Get("server_name").(string), 80 } 81 82 readResponse, err := readRequest.Execute() 83 if err != nil { 84 return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", err) 85 } 86 if !readResponse.IsSuccessful() { 87 return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", readResponse.Error) 88 } 89 90 resp := readResponse.Parsed.(*sql.GetFirewallRuleResponse) 91 d.SetId(*resp.ID) 92 93 return resourceArmSqlFirewallRuleRead(d, meta) 94 } 95 96 func resourceArmSqlFirewallRuleRead(d *schema.ResourceData, meta interface{}) error { 97 client := meta.(*ArmClient) 98 rivieraClient := client.rivieraClient 99 100 readRequest := rivieraClient.NewRequestForURI(d.Id()) 101 readRequest.Command = &sql.GetFirewallRule{} 102 103 readResponse, err := readRequest.Execute() 104 if err != nil { 105 return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", err) 106 } 107 if !readResponse.IsSuccessful() { 108 log.Printf("[INFO] Error reading SQL Server Firewall Rule %q - removing from state", d.Id()) 109 d.SetId("") 110 return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", readResponse.Error) 111 } 112 113 resp := readResponse.Parsed.(*sql.GetFirewallRuleResponse) 114 115 d.Set("name", resp.Name) 116 d.Set("start_ip_address", resp.StartIPAddress) 117 d.Set("end_ip_address", resp.EndIPAddress) 118 119 return nil 120 } 121 122 func resourceArmSqlFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error { 123 client := meta.(*ArmClient) 124 rivieraClient := client.rivieraClient 125 126 deleteRequest := rivieraClient.NewRequestForURI(d.Id()) 127 deleteRequest.Command = &sql.DeleteFirewallRule{} 128 129 deleteResponse, err := deleteRequest.Execute() 130 if err != nil { 131 return fmt.Errorf("Error deleting SQL Server Firewall Rule: %s", err) 132 } 133 if !deleteResponse.IsSuccessful() { 134 return fmt.Errorf("Error deleting SQL Server Firewall Rule: %s", deleteResponse.Error) 135 } 136 137 return nil 138 }