/
data_source_router_isp.go
111 lines (93 loc) · 2.53 KB
/
data_source_router_isp.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
// Copyright 2022 Fortinet, Inc. All rights reserved.
// Author: Shih-Hsin Huang
// Description: Configure router isp.
package fortiadc
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
)
func dataSourceRouterIsp() *schema.Resource {
return &schema.Resource{
Read: dataSourceRouterIspRead,
Schema: map[string]*schema.Schema{
"destination": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"mkey": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"gateway": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"vdom": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceRouterIspRead(d *schema.ResourceData, m interface{}) error {
c := m.(*FortiClient).Client
c.Retries = 1
vdom := ""
if v, ok := d.GetOk("vdom"); ok {
if s, ok := v.(string); ok {
vdom = s
}
}
mkey := ""
t := d.Get("mkey")
if v, ok := t.(string); ok {
mkey = v
} else if v, ok := t.(int); ok {
mkey = strconv.Itoa(v)
} else {
return fmt.Errorf("Error describing RouterIsp: type error")
}
o, err := c.ReadRouterIsp(mkey, vdom)
if err != nil {
return fmt.Errorf("Error describing RouterIsp: %v", err)
}
if o == nil {
d.SetId("")
return nil
}
err = dataSourceRefreshObjectRouterIsp(d, o)
if err != nil {
return fmt.Errorf("Error describing RouterIsp from API: %v", err)
}
d.SetId(mkey)
return nil
}
func dataSourceFlattenRouterIspDestination(v interface{}, d *schema.ResourceData, pre string) interface{} {
return v
}
func dataSourceFlattenRouterIspMkey(v interface{}, d *schema.ResourceData, pre string) interface{} {
return v
}
func dataSourceFlattenRouterIspGateway(v interface{}, d *schema.ResourceData, pre string) interface{} {
return v
}
func dataSourceRefreshObjectRouterIsp(d *schema.ResourceData, o map[string]interface{}) error {
var err error
if err = d.Set("destination", dataSourceFlattenRouterIspDestination(o["destination"], d, "destination")); err != nil {
if !fortiAPIPatch(o["destination"]) {
return fmt.Errorf("Error reading destination: %v", err)
}
}
if err = d.Set("mkey", dataSourceFlattenRouterIspMkey(o["mkey"], d, "mkey")); err != nil {
if !fortiAPIPatch(o["mkey"]) {
return fmt.Errorf("Error reading mkey: %v", err)
}
}
if err = d.Set("gateway", dataSourceFlattenRouterIspGateway(o["gateway"], d, "gateway")); err != nil {
if !fortiAPIPatch(o["gateway"]) {
return fmt.Errorf("Error reading gateway: %v", err)
}
}
return nil
}