-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdata_source_tc_cam_saml_providers.go
126 lines (113 loc) · 3.64 KB
/
data_source_tc_cam_saml_providers.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
package cam
import (
"context"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudCamSAMLProviders() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCamSAMLProvidersRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the CAM SAML provider to be queried.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The description of the CAM SAML provider.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
"provider_list": {
Type: schema.TypeList,
Computed: true,
Description: "A list of CAM SAML providers. Each element contains the following attributes:",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of CAM SAML provider.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of CAM SAML provider.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Create time of the CAM SAML provider.",
},
"modify_time": {
Type: schema.TypeString,
Computed: true,
Description: "The last modify time of the CAM SAML provider.",
},
},
},
},
},
}
}
func dataSourceTencentCloudCamSAMLProvidersRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_cam_saml_providers.read")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
params := make(map[string]interface{})
if v, ok := d.GetOk("name"); ok {
params["name"] = v.(string)
}
if v, ok := d.GetOk("description"); ok {
params["description"] = v.(string)
}
camService := CamService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
var providers []*cam.SAMLProviderInfo
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
results, e := camService.DescribeSAMLProvidersByFilter(ctx, params)
if e != nil {
return tccommon.RetryError(e)
}
providers = results
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read CAM groups failed, reason:%s\n", logId, err.Error())
return err
}
providerList := make([]map[string]interface{}, 0, len(providers))
ids := make([]string, 0, len(providers))
for _, provider := range providers {
mapping := map[string]interface{}{
"name": *provider.Name,
"description": *provider.Description,
"create_time": *provider.CreateTime,
"modify_time": *provider.ModifyTime,
}
providerList = append(providerList, mapping)
ids = append(ids, *provider.Name)
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("provider_list", providerList); e != nil {
log.Printf("[CRITAL]%s provider set provider list fail, reason:%s\n", logId, e.Error())
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), providerList); e != nil {
return e
}
}
return nil
}