-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_identity_providers.go
176 lines (162 loc) · 5.19 KB
/
data_source_huaweicloud_identity_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
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
// Generated by PMS #114
package iam
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/filters"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)
func DataSourceIamIdentityProviders() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIamIdentityProvidersRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the name of the identity provider.`,
},
"sso_type": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the single sign-on type of the identity provider.`,
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the status of the identity provider. The value can be **true** or **false**`,
},
"identity_providers": {
Type: schema.TypeList,
Computed: true,
Description: `The list of identity providers.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The identity provider ID which equals the identity provider name.`,
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: `The description of the identity provider.`,
},
"sso_type": {
Type: schema.TypeString,
Computed: true,
Description: `The single sign-on type of the identity provider.`,
},
"status": {
Type: schema.TypeBool,
Computed: true,
Description: `The enabled status for the identity provider.`,
},
"remote_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The list of federated user IDs configured for the identity provider.`,
},
"links": {
Type: schema.TypeList,
Computed: true,
Description: `The links of identity provider.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"self": {
Type: schema.TypeString,
Computed: true,
Description: `The identity provider resource link.`,
},
"protocols": {
Type: schema.TypeString,
Computed: true,
Description: `The protocol resource link.`,
},
},
},
},
},
},
},
},
}
}
type IdentityProvidersDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}
func newIdentityProvidersDSWrapper(d *schema.ResourceData, meta interface{}) *IdentityProvidersDSWrapper {
return &IdentityProvidersDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}
func dataSourceIamIdentityProvidersRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newIdentityProvidersDSWrapper(d, meta)
keyLisIdeProRst, err := wrapper.KeystoneListIdentityProviders()
if err != nil {
return diag.FromErr(err)
}
id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)
err = wrapper.keystoneListIdentityProvidersToSchema(keyLisIdeProRst)
if err != nil {
return diag.FromErr(err)
}
return nil
}
// @API IAM GET /v3/OS-FEDERATION/identity_providers
func (w *IdentityProvidersDSWrapper) KeystoneListIdentityProviders() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "iam")
if err != nil {
return nil, err
}
uri := "/v3/OS-FEDERATION/identity_providers"
return httphelper.New(client).
Method("GET").
URI(uri).
LinkPager("identity_providers", "links.next").
Filter(
filters.New().From("identity_providers").
Where("id", "=", w.Get("name")).
Where("sso_type", "=", w.Get("sso_type")).
Where("enabled", "=", w.GetToBool("status")),
).
Request().
Result()
}
func (w *IdentityProvidersDSWrapper) keystoneListIdentityProvidersToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("identity_providers", schemas.SliceToList(body.Get("identity_providers"),
func(idePro gjson.Result) any {
return map[string]any{
"id": idePro.Get("id").Value(),
"description": idePro.Get("description").Value(),
"sso_type": idePro.Get("sso_type").Value(),
"status": idePro.Get("enabled").Value(),
"remote_ids": schemas.SliceToStrList(idePro.Get("remote_ids")),
"links": schemas.SliceToList(idePro.Get("links"),
func(link gjson.Result) any {
return map[string]any{
"self": link.Get("self").Value(),
"protocols": link.Get("protocols").Value(),
}
},
),
}
},
)),
)
return mErr.ErrorOrNil()
}