/
core_ip_sec_connections_data_source.go
178 lines (140 loc) · 3.98 KB
/
core_ip_sec_connections_data_source.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
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"context"
"github.com/hashicorp/terraform/helper/schema"
oci_core "github.com/oracle/oci-go-sdk/core"
)
func CoreIpSecConnectionsDataSource() *schema.Resource {
return &schema.Resource{
Read: readCoreIpSecConnections,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"compartment_id": {
Type: schema.TypeString,
Required: true,
},
"cpe_id": {
Type: schema.TypeString,
Optional: true,
},
"drg_id": {
Type: schema.TypeString,
Optional: true,
},
"connections": {
Type: schema.TypeList,
Computed: true,
Elem: GetDataSourceItemSchema(CoreIpSecConnectionResource()),
},
"limit": {
Type: schema.TypeInt,
Optional: true,
Deprecated: FieldDeprecated("limit"),
},
"page": {
Type: schema.TypeString,
Optional: true,
Deprecated: FieldDeprecated("page"),
},
},
}
}
func readCoreIpSecConnections(d *schema.ResourceData, m interface{}) error {
sync := &CoreIpSecConnectionsDataSourceCrud{}
sync.D = d
sync.Client = m.(*OracleClients).virtualNetworkClient
return ReadResource(sync)
}
type CoreIpSecConnectionsDataSourceCrud struct {
D *schema.ResourceData
Client *oci_core.VirtualNetworkClient
Res *oci_core.ListIPSecConnectionsResponse
}
func (s *CoreIpSecConnectionsDataSourceCrud) VoidState() {
s.D.SetId("")
}
func (s *CoreIpSecConnectionsDataSourceCrud) Get() error {
request := oci_core.ListIPSecConnectionsRequest{}
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
tmp := compartmentId.(string)
request.CompartmentId = &tmp
}
if cpeId, ok := s.D.GetOkExists("cpe_id"); ok {
tmp := cpeId.(string)
request.CpeId = &tmp
}
if drgId, ok := s.D.GetOkExists("drg_id"); ok {
tmp := drgId.(string)
request.DrgId = &tmp
}
if limit, ok := s.D.GetOkExists("limit"); ok {
tmp := limit.(int)
request.Limit = &tmp
}
if page, ok := s.D.GetOkExists("page"); ok {
tmp := page.(string)
request.Page = &tmp
}
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core")
response, err := s.Client.ListIPSecConnections(context.Background(), request)
if err != nil {
return err
}
s.Res = &response
request.Page = s.Res.OpcNextPage
for request.Page != nil {
listResponse, err := s.Client.ListIPSecConnections(context.Background(), request)
if err != nil {
return err
}
s.Res.Items = append(s.Res.Items, listResponse.Items...)
request.Page = listResponse.OpcNextPage
}
return nil
}
func (s *CoreIpSecConnectionsDataSourceCrud) SetData() error {
if s.Res == nil {
return nil
}
s.D.SetId(GenerateDataSourceID())
resources := []map[string]interface{}{}
for _, r := range s.Res.Items {
ipSecConnection := map[string]interface{}{
"compartment_id": *r.CompartmentId,
}
if r.CpeId != nil {
ipSecConnection["cpe_id"] = *r.CpeId
}
if r.CpeLocalIdentifier != nil {
ipSecConnection["cpe_local_identifier"] = *r.CpeLocalIdentifier
}
ipSecConnection["cpe_local_identifier_type"] = r.CpeLocalIdentifierType
if r.DefinedTags != nil {
ipSecConnection["defined_tags"] = definedTagsToMap(r.DefinedTags)
}
if r.DisplayName != nil {
ipSecConnection["display_name"] = *r.DisplayName
}
if r.DrgId != nil {
ipSecConnection["drg_id"] = *r.DrgId
}
ipSecConnection["freeform_tags"] = r.FreeformTags
if r.Id != nil {
ipSecConnection["id"] = *r.Id
}
ipSecConnection["state"] = r.LifecycleState
ipSecConnection["static_routes"] = r.StaticRoutes
if r.TimeCreated != nil {
ipSecConnection["time_created"] = r.TimeCreated.String()
}
resources = append(resources, ipSecConnection)
}
if f, fOk := s.D.GetOkExists("filter"); fOk {
resources = ApplyFilters(f.(*schema.Set), resources, CoreIpSecConnectionsDataSource().Schema["connections"].Elem.(*schema.Resource).Schema)
}
if err := s.D.Set("connections", resources); err != nil {
return err
}
return nil
}