-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_atracker_routes.go
230 lines (213 loc) · 6.55 KB
/
data_source_ibm_atracker_routes.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package atracker
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/platform-services-go-sdk/atrackerv2"
)
func DataSourceIBMAtrackerRoutes() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMAtrackerRoutesRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the route.",
},
"routes": {
Type: schema.TypeList,
Computed: true,
Description: "A list of route resources.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The uuid of the route resource.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the route.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the route resource.",
},
"version": {
Type: schema.TypeInt,
Computed: true,
Description: "The version of the route.",
},
"receive_global_events": {
Type: schema.TypeBool,
Computed: true,
Deprecated: "use rules.locations instead",
Description: "Indicates whether or not all global events should be forwarded to this region.",
},
"rules": {
Type: schema.TypeList,
Computed: true,
Description: "The routing rules that will be evaluated in their order of the array. Once a rule is matched, the remaining rules in the route definition will be skipped.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_ids": {
Type: schema.TypeList,
Computed: true,
Description: "The target ID List. All the events will be send to all targets listed in the rule. You can include targets from other regions.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"locations": {
Type: schema.TypeList,
Computed: true,
Description: "Logs from these locations will be sent to the targets specified. Locations is a superset of regions including global and *.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"created": {
Type: schema.TypeString,
Computed: true,
Deprecated: "use created_at instead",
Description: "The timestamp of the route creation time.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The timestamp of the route creation time.",
},
"updated": {
Type: schema.TypeString,
Computed: true,
Deprecated: "use updated_at instead",
Description: "The timestamp of the target last updated time.",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The timestamp of the route last updated time.",
},
"api_version": {
Type: schema.TypeInt,
Computed: true,
Description: "The API version of the route.",
},
},
},
},
},
}
}
func dataSourceIBMAtrackerRoutesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
atrackerClientv2, err := getAtrackerClients(meta)
if err != nil {
return diag.FromErr(err)
}
listRoutesOptions := &atrackerv2.ListRoutesOptions{}
routeList, response, err := atrackerClientv2.ListRoutesWithContext(context, listRoutesOptions)
if err != nil {
log.Printf("[DEBUG] ListRoutesWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListRoutesWithContext failed %s\n%s", err, response))
}
// Use the provided filter argument and construct a new list with only the requested resource(s)
var matchRoutes []atrackerv2.Route
var name string
var suppliedFilter bool
if v, ok := d.GetOk("name"); ok {
name = v.(string)
suppliedFilter = true
for _, data := range routeList.Routes {
if *data.Name == name {
matchRoutes = append(matchRoutes, data)
}
}
} else {
matchRoutes = routeList.Routes
}
routeList.Routes = matchRoutes
if suppliedFilter {
if len(routeList.Routes) == 0 {
return diag.FromErr(fmt.Errorf("no Routes found with name %s", name))
}
d.SetId(name)
} else {
d.SetId(DataSourceIBMAtrackerRoutesID(d))
}
routes := []map[string]interface{}{}
if routeList.Routes != nil {
for _, modelItem := range routeList.Routes {
modelMap, err := dataSourceIBMAtrackerRoutesRouteToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
routes = append(routes, modelMap)
}
}
if err = d.Set("routes", routes); err != nil {
return diag.FromErr(fmt.Errorf("Error setting routes %s", err))
}
return nil
}
// DataSourceIBMAtrackerRoutesID returns a reasonable ID for the list.
func DataSourceIBMAtrackerRoutesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIBMAtrackerRoutesRouteToMap(model *atrackerv2.Route) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = *model.ID
}
if model.Name != nil {
modelMap["name"] = *model.Name
}
if model.CRN != nil {
modelMap["crn"] = *model.CRN
}
if model.Version != nil {
modelMap["version"] = *model.Version
}
if model.Rules != nil {
rules := []map[string]interface{}{}
for _, rulesItem := range model.Rules {
rulesItemMap, err := dataSourceIBMAtrackerRoutesRuleToMap(&rulesItem)
if err != nil {
return modelMap, err
}
rules = append(rules, rulesItemMap)
}
modelMap["rules"] = rules
}
if model.CreatedAt != nil {
modelMap["created_at"] = model.CreatedAt.String()
}
if model.UpdatedAt != nil {
modelMap["updated_at"] = model.UpdatedAt.String()
}
if model.APIVersion != nil {
modelMap["api_version"] = *model.APIVersion
} else {
modelMap["api_version"] = 1
}
return modelMap, nil
}
func dataSourceIBMAtrackerRoutesRuleToMap(model *atrackerv2.Rule) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.TargetIds != nil {
modelMap["target_ids"] = model.TargetIds
}
if model.Locations != nil {
modelMap["locations"] = model.Locations
}
return modelMap, nil
}