-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_odl_virtual_tenant_network.go
149 lines (139 loc) · 4.2 KB
/
resource_odl_virtual_tenant_network.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
146
147
148
149
package odl
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOdlVirtualTenantNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceVtnAdd,
Read: resourceVtnRead,
Delete: resourceVtnDelete,
Schema: map[string]*schema.Schema{
"tenant_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"operation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateOperation,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"idle_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"hard_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceVtnAdd(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
tenantName := d.Get("tenant_name").(string)
var body map[string]interface{}
var input map[string]string
input = make(map[string]string)
log.Println("[DEBUG] Creating Vtn with name " + tenantName)
input["tenant-name"] = tenantName
input["update-mode"] = "UPDATE"
if operation, found := d.GetOk("operation"); found {
input["operation"] = operation.(string)
}
if description, found := d.GetOk("description"); found {
input["description"] = description.(string)
}
if idleTimeout, found := d.GetOk("idle_timeout"); found {
input["idle-timeout"] = strconv.Itoa(idleTimeout.(int))
}
if hardTimeout, found := d.GetOk("hard_timeout"); found {
input["hard-timeout"] = strconv.Itoa(hardTimeout.(int))
}
log.Println("[DEBUG] All options collected for Vtn with name " + tenantName)
body = make(map[string]interface{})
body["input"] = input
response, err := config.PostRequest("restconf/operations/vtn:update-vtn", body)
if err != nil {
log.Printf("[ERROR] POST Request failed")
return err
}
isCreated, output, errorOutput, err := Status(response)
if isCreated {
d.SetId(tenantName + output.Output.Status)
} else {
if errorOutput != nil {
log.Printf("[ERROR] While creating vtn %s", errorOutput.Errors.Error[0].Message)
return fmt.Errorf("[ERROR] While creating vtn %s", errorOutput.Errors.Error[0].Message)
}
if err != nil {
return fmt.Errorf("[ERROR] Whlie creating vtn %s", err.Error())
}
}
return nil
}
func resourceVtnRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
tenantName := d.Get("tenant_name").(string)
log.Println("[DEBUG] Read Vtn with name " + tenantName)
response, err := config.GetRequest("restconf/operational/vtn:vtns")
if err != nil {
log.Printf("[ERROR] POST Request failed")
return err
}
present, err := CheckResponseVirtualTenantNetworkExists(response, tenantName)
if err != nil {
log.Println("[ERROR] Vtn Read failed")
return fmt.Errorf("[ERROR] Vtn could not be read %v", err)
}
if !present {
log.Println("[DEBUG] Vtn with name " + tenantName + "was not found")
d.SetId("")
}
return nil
}
func resourceVtnDelete(d *schema.ResourceData, meta interface{}) error {
err := resourceVtnRead(d, meta)
if d.Id() == "" {
return fmt.Errorf("[ERROR] vtn does not exists")
}
config := meta.(*Config)
tenantName := d.Get("tenant_name").(string)
var body map[string]interface{}
var input map[string]string
input = make(map[string]string)
input["tenant-name"] = tenantName
body = make(map[string]interface{})
body["input"] = input
log.Println("[DEBUG] All options collected for Vtn with name " + tenantName)
log.Println("[DEBUG] Preparing to destroy Vtn with name " + tenantName)
response, err := config.PostRequest("restconf/operations/vtn:remove-vtn", body)
if err != nil {
log.Printf("[ERROR] POST Request failed")
return err
}
isDestroyed, _, errorOutput, err := Status(response)
if isDestroyed {
d.SetId("")
} else {
if errorOutput != nil {
log.Printf("[ERROR] While destroying vtn %s", errorOutput.Errors.Error[0].Message)
return fmt.Errorf("[ERROR] While creating vtn %s", errorOutput.Errors.Error[0].Message)
}
if err != nil {
return fmt.Errorf("[ERROR] Whlie destroying vtn %s", err.Error())
}
}
return nil
}