forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_sql_firewall_rule.go
145 lines (119 loc) · 3.97 KB
/
resource_arm_sql_firewall_rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package azurerm
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/azure"
"github.com/jen20/riviera/sql"
)
func resourceArmSqlFirewallRule() *schema.Resource {
return &schema.Resource{
Create: resourceArmSqlFirewallRuleCreate,
Read: resourceArmSqlFirewallRuleRead,
Update: resourceArmSqlFirewallRuleCreate,
Delete: resourceArmSqlFirewallRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"server_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"start_ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"end_ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceArmSqlFirewallRuleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
createRequest := rivieraClient.NewRequest()
createRequest.Command = &sql.CreateOrUpdateFirewallRule{
Name: d.Get("name").(string),
ResourceGroupName: d.Get("resource_group_name").(string),
ServerName: d.Get("server_name").(string),
StartIPAddress: azure.String(d.Get("start_ip_address").(string)),
EndIPAddress: azure.String(d.Get("end_ip_address").(string)),
}
createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating SQL Server Firewall Rule: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating SQL Server Firewall Rule: %s", createResponse.Error)
}
readRequest := rivieraClient.NewRequest()
readRequest.Command = &sql.GetFirewallRule{
Name: d.Get("name").(string),
ResourceGroupName: d.Get("resource_group_name").(string),
ServerName: d.Get("server_name").(string),
}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*sql.GetFirewallRuleResponse)
d.SetId(*resp.ID)
return resourceArmSqlFirewallRuleRead(d, meta)
}
func resourceArmSqlFirewallRuleRead(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &sql.GetFirewallRule{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading SQL Server Firewall Rule %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading SQL Server Firewall Rule: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*sql.GetFirewallRuleResponse)
d.Set("resource_group_name", resGroup)
d.Set("name", resp.Name)
d.Set("start_ip_address", resp.StartIPAddress)
d.Set("end_ip_address", resp.EndIPAddress)
return nil
}
func resourceArmSqlFirewallRuleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &sql.DeleteFirewallRule{}
deleteResponse, err := deleteRequest.Execute()
if err != nil {
return fmt.Errorf("Error deleting SQL Server Firewall Rule: %s", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting SQL Server Firewall Rule: %s", deleteResponse.Error)
}
return nil
}