forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_obmcs_core_dhcp_options.go
115 lines (95 loc) · 2.73 KB
/
data_source_obmcs_core_dhcp_options.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
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package main
import (
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/oracle/terraform-provider-baremetal/options"
"github.com/MustWin/baremetal-sdk-go"
"github.com/oracle/terraform-provider-baremetal/client"
"github.com/oracle/terraform-provider-baremetal/crud"
)
func DHCPOptionsDatasource() *schema.Resource {
return &schema.Resource{
Read: readDHCPOptionsList,
Schema: map[string]*schema.Schema{
"compartment_id": {
Type: schema.TypeString,
Required: true,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
},
"page": {
Type: schema.TypeString,
Optional: true,
},
"options": {
Type: schema.TypeList,
Computed: true,
Elem: DHCPOptionsResource(),
},
"vcn_id": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func readDHCPOptionsList(d *schema.ResourceData, m interface{}) (e error) {
client := m.(client.BareMetalClient)
reader := &DHCPOptionsDatasourceCrud{}
reader.D = d
reader.Client = client
return crud.ReadResource(reader)
}
type DHCPOptionsDatasourceCrud struct {
crud.BaseCrud
Res *baremetal.ListDHCPOptions
}
func (s *DHCPOptionsDatasourceCrud) Get() (e error) {
compartmentID := s.D.Get("compartment_id").(string)
vcnID := s.D.Get("vcn_id").(string)
opts := &baremetal.ListOptions{}
options.SetListOptions(s.D, opts)
s.Res = &baremetal.ListDHCPOptions{DHCPOptions: []baremetal.DHCPOptions{}}
for {
var list *baremetal.ListDHCPOptions
if list, e = s.Client.ListDHCPOptions(compartmentID, vcnID, opts); e != nil {
break
}
s.Res.DHCPOptions = append(s.Res.DHCPOptions, list.DHCPOptions...)
if hasNextPage := options.SetNextPageOption(list.NextPage, &opts.PageListOptions); !hasNextPage {
break
}
}
return
}
func (s *DHCPOptionsDatasourceCrud) SetData() {
if s.Res != nil {
s.D.SetId(time.Now().UTC().String())
stateObjs := []map[string]interface{}{}
for _, res := range s.Res.DHCPOptions {
nestedStateObjs := []map[string]interface{}{}
for _, nestedRes := range res.Options {
nestedStateObj := map[string]interface{}{
"type": nestedRes.Type,
"custom_dns_servers": nestedRes.CustomDNSServers,
"server_type": nestedRes.ServerType,
}
nestedStateObjs = append(nestedStateObjs, nestedStateObj)
}
stateObj := map[string]interface{}{
"compartment_id": res.CompartmentID,
"display_name": res.DisplayName,
"id": res.ID,
"options": nestedStateObjs,
"state": res.State,
"time_created": res.TimeCreated.String(),
}
stateObjs = append(stateObjs, stateObj)
}
s.D.Set("options", stateObjs)
}
return
}