forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_arm_route.go
144 lines (116 loc) · 3.17 KB
/
resource_arm_route.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
package azurerm
import (
"fmt"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmRoute() *schema.Resource {
return &schema.Resource{
Create: resourceArmRouteCreate,
Read: resourceArmRouteRead,
Update: resourceArmRouteCreate,
Delete: resourceArmRouteDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"route_table_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"address_prefix": {
Type: schema.TypeString,
Required: true,
},
"next_hop_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateRouteTableNextHopType,
},
"next_hop_in_ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceArmRouteCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
routesClient := client.routesClient
name := d.Get("name").(string)
rtName := d.Get("route_table_name").(string)
resGroup := d.Get("resource_group_name").(string)
addressPrefix := d.Get("address_prefix").(string)
nextHopType := d.Get("next_hop_type").(string)
armMutexKV.Lock(rtName)
defer armMutexKV.Unlock(rtName)
properties := network.RoutePropertiesFormat{
AddressPrefix: &addressPrefix,
NextHopType: network.RouteNextHopType(nextHopType),
}
if v, ok := d.GetOk("next_hop_in_ip_address"); ok {
nextHopInIpAddress := v.(string)
properties.NextHopIPAddress = &nextHopInIpAddress
}
route := network.Route{
Name: &name,
Properties: &properties,
}
_, err := routesClient.CreateOrUpdate(resGroup, rtName, name, route, make(chan struct{}))
if err != nil {
return err
}
read, err := routesClient.Get(resGroup, rtName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Route %s/%s (resource group %s) ID", rtName, name, resGroup)
}
d.SetId(*read.ID)
return resourceArmRouteRead(d, meta)
}
func resourceArmRouteRead(d *schema.ResourceData, meta interface{}) error {
routesClient := meta.(*ArmClient).routesClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
rtName := id.Path["routeTables"]
routeName := id.Path["routes"]
resp, err := routesClient.Get(resGroup, rtName, routeName)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Route %s: %s", routeName, err)
}
return nil
}
func resourceArmRouteDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
routesClient := client.routesClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
rtName := id.Path["routeTables"]
routeName := id.Path["routes"]
armMutexKV.Lock(rtName)
defer armMutexKV.Unlock(rtName)
_, err = routesClient.Delete(resGroup, rtName, routeName, make(chan struct{}))
return err
}