-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_portobjectgroup.go
165 lines (140 loc) · 4.12 KB
/
resource_portobjectgroup.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/bluecough/go-ftd"
"log"
"strings"
)
func resourcePortObjectGroup() *schema.Resource {
return &schema.Resource{
Create: resourcePortObjectGroupCreate,
Read: resourcePortObjectGroupRead,
Update: resourcePortObjectGroupUpdate,
Delete: resourcePortObjectGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"objects": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource {
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func resourcePortObjectGroupCreate(d *schema.ResourceData, m interface{}) error {
cf := m.(*goftd.FTD)
total := d.Get("objects.#")
log.Println("GS DEBUG =====PortkObjectGroupCreate==",total)
po := new(goftd.PortObjectGroup)
po.Name = d.Get("name").(string)
entries := d.Get("objects").(*schema.Set)
var batchEntries = []*goftd.ReferenceObject{}
for _, vRaw := range entries.List() {
val := vRaw.(map[string]interface{})
batchEntries = append(batchEntries, &goftd.ReferenceObject{
ID: val["id"].(string),
Version: val["version"].(string),
Name: val["name"].(string),
Type: val["type"].(string),
})
}
po.Objects = batchEntries
err := cf.CreatePortObjectGroup(po, goftd.DuplicateActionReplace)
//log.Println("GS DEBUG ====== NetworkObjectGroupCreate-E==== ",n.ID)
d.SetId(po.ID + " " + po.Name)
if err != nil{
log.Println("==== > Error %s",err)
return err
}
return resourceServerRead(d, m)
}
func resourcePortObjectGroupRead(d *schema.ResourceData, m interface{}) error {
return nil
}
func resourcePortObjectGroupUpdate(d *schema.ResourceData, m interface{}) error {
cf := m.(*goftd.FTD)
pog := []*goftd.PortObjectGroup{}
idsplit := strings.Split(d.Id(), " ")
n := "name: " + idsplit[1]
pog,err := cf.GetPortObjectGroupBy(n)
if err != nil{
log.Println("GS DEBUG ====> call for GetPortObjectGroupBy failed :", err)
return err
}
entries := d.Get("objects").(*schema.Set)
var batchEntries = []*goftd.ReferenceObject{}
for _, vRaw := range entries.List() {
val := vRaw.(map[string]interface{})
batchEntries = append(batchEntries, &goftd.ReferenceObject{
ID: val["id"].(string),
Version: val["version"].(string),
Name: val["name"].(string),
Type: val["type"].(string),
})
}
// Create localro as slice pointer and add more slices based upon the number of Objects we had
// Then assign values that we had from the read
localnog := new(goftd.PortObjectGroup)
localro := []*goftd.ReferenceObject{}
for i := 0; i < len(batchEntries); i++ {
localro = append(localro,new(goftd.ReferenceObject))
localro[i].Name = batchEntries[i].Name
localro[i].ID = batchEntries[i].ID
localro[i].Version = batchEntries[i].Version
localro[i].Type = batchEntries[i].Type
}
localnog.Objects = localro
localnog.Name = d.Get("name").(string)
localnog.ID = idsplit[0]
localnog.Type = pog[0].Type
localnog.Version = pog[0].Version
cf.UpdatePortObjectGroup(localnog)
if err != nil{
log.Println("GS DEBUG =====NetworkObjectGRPtUpdate-E====== \n", err)
}
idsplit[1] = d.Get("name").(string)
d.SetId(idsplit[0] + " " + idsplit[1])
return resourceServerRead(d, m)
}
func resourcePortObjectGroupDelete(d *schema.ResourceData, m interface{}) error {
cf := m.(*goftd.FTD)
n := new(goftd.PortObjectGroup)
v := strings.Split(d.Id(), " ")
n.ID = v[0]
err := cf.DeletePortObjectGroup(n)
if err != nil {
log.Println("Error: %s\n", err)
return err
}
return nil
}