/
data_source_ibm_cis_custom_pages.go
130 lines (122 loc) · 3.6 KB
/
data_source_ibm_cis_custom_pages.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
package ibm
import (
"log"
"time"
"github.com/IBM/go-sdk-core/v3/core"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const (
cisCustomPages = "cis_custom_pages"
)
func dataSourceIBMCISCustomPages() *schema.Resource {
return &schema.Resource{
Read: dataSourceIBMCISCustomPagesRead,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
cisID: {
Type: schema.TypeString,
Required: true,
},
cisDomainID: {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppressDomainIDDiff,
},
cisCustomPages: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
cisCustomPageIdentifier: {
Type: schema.TypeString,
Description: "Custom page identifier",
Computed: true,
},
cisCustomPageURL: {
Type: schema.TypeString,
Description: "Custom page url",
Computed: true,
},
cisCustomPageState: {
Type: schema.TypeString,
Description: "Custom page state",
Computed: true,
},
cisCustomPageDesc: {
Type: schema.TypeString,
Description: "Free text",
Computed: true,
},
cisCustomPageRequiredTokens: {
Type: schema.TypeList,
Description: "Custom page state",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
cisCustomPagePreviewTarget: {
Type: schema.TypeString,
Description: "Custom page preview target",
Computed: true,
},
cisCustomPageCreatedOn: {
Type: schema.TypeString,
Description: "Custom page created date",
Computed: true,
},
cisCustomPageModifiedOn: {
Type: schema.TypeString,
Description: "Custom page modified date",
Computed: true,
},
},
},
},
},
}
}
func dataSourceIBMCISCustomPagesRead(d *schema.ResourceData, meta interface{}) error {
cisClient, err := meta.(ClientSession).CisCustomPageClientSession()
if err != nil {
return err
}
crn := d.Get(cisID).(string)
zoneID := d.Get(cisDomainID).(string)
cisClient.Crn = core.StringPtr(crn)
cisClient.ZoneIdentifier = core.StringPtr(zoneID)
opt := cisClient.NewListZoneCustomPagesOptions()
result, response, err := cisClient.ListZoneCustomPages(opt)
if err != nil {
log.Printf("List custom pages failed: %v", response)
return err
}
customPagesOutput := make([]map[string]interface{}, 0)
for _, instance := range result.Result {
customPage := make(map[string]interface{}, 0)
customPage[cisCustomPageIdentifier] = *instance.ID
customPage[cisCustomPageState] = *instance.State
customPage[cisCustomPageDesc] = *instance.Description
customPage[cisCustomPagePreviewTarget] = *instance.PreviewTarget
customPage[cisCustomPageRequiredTokens] = flattenStringList(instance.RequiredTokens)
if instance.CreatedOn != nil {
customPage[cisCustomPageCreatedOn] = (*instance.CreatedOn).String()
}
if instance.ModifiedOn != nil {
customPage[cisCustomPageModifiedOn] = (*instance.ModifiedOn).String()
}
if instance.URL != nil {
customPage[cisCustomPageURL] = *instance.URL
}
customPagesOutput = append(customPagesOutput, customPage)
}
d.SetId(dataSourceIBMCISCustomPageID(d))
d.Set(cisCustomPages, customPagesOutput)
return nil
}
func dataSourceIBMCISCustomPageID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}