-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_iam_trusted_profile_links.go
199 lines (175 loc) · 6.01 KB
/
data_source_ibm_iam_trusted_profile_links.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright IBM Corp. 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package iamidentity
import (
"context"
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/platform-services-go-sdk/iamidentityv1"
)
func DataSourceIBMIamTrustedProfileLinks() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIamTrustedProfileLinkListRead,
Schema: map[string]*schema.Schema{
"profile_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the trusted profile.",
ValidateFunc: validate.InvokeDataSourceValidator("ibm_iam_trusted_profile_links",
"profile_id"),
},
"links": {
Type: schema.TypeList,
Computed: true,
Description: "List of links to a trusted profile.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "the unique identifier of the claim rule.",
},
"entity_tag": {
Type: schema.TypeString,
Computed: true,
Description: "version of the claim rule.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "If set contains a date time string of the creation date in ISO format.",
},
"modified_at": {
Type: schema.TypeString,
Computed: true,
Description: "If set contains a date time string of the last modification date in ISO format.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Optional name of the Link.",
},
"cr_type": {
Type: schema.TypeString,
Computed: true,
Description: "The compute resource type. Valid values are VSI, IKS_SA, ROKS_SA.",
},
"link": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN of the compute resource.",
},
"namespace": {
Type: schema.TypeString,
Computed: true,
Description: "The compute resource namespace, only required if cr_type is IKS_SA or ROKS_SA.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the compute resource, only required if cr_type is IKS_SA or ROKS_SA.",
},
},
},
},
},
},
},
},
}
}
func DataSourceIBMIamTrustedProfileLinksValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "profile_id",
ValidateFunctionIdentifier: validate.ValidateCloudData,
Type: validate.TypeString,
CloudDataType: "iam",
CloudDataRange: []string{"service:trusted_profile", "resolved_to:id"},
Required: true})
iBMIamTrustedProfileLinksValidator := validate.ResourceValidator{ResourceName: "ibm_iam_trusted_profile_links", Schema: validateSchema}
return &iBMIamTrustedProfileLinksValidator
}
func dataSourceIBMIamTrustedProfileLinkListRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamIdentityClient, err := meta.(conns.ClientSession).IAMIdentityV1API()
if err != nil {
return diag.FromErr(err)
}
listLinkOptions := &iamidentityv1.ListLinksOptions{}
listLinkOptions.SetProfileID(d.Get("profile_id").(string))
profileLinkList, response, err := iamIdentityClient.ListLinks(listLinkOptions)
if err != nil {
log.Printf("[DEBUG] ListLink failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListLink failed %s\n%s", err, response))
}
d.SetId(dataSourceIBMIamTrustedProfileLinkListID(d))
if profileLinkList.Links != nil {
err = d.Set("links", dataSourceProfileLinkListFlattenLinks(profileLinkList.Links))
if err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting links %s", err))
}
}
return nil
}
// dataSourceIBMIamTrustedProfileLinkListID returns a reasonable ID for the list.
func dataSourceIBMIamTrustedProfileLinkListID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceProfileLinkListFlattenLinks(result []iamidentityv1.ProfileLink) (links []map[string]interface{}) {
for _, linksItem := range result {
links = append(links, dataSourceProfileLinkListLinksToMap(linksItem))
}
return links
}
func dataSourceProfileLinkListLinksToMap(linksItem iamidentityv1.ProfileLink) (linksMap map[string]interface{}) {
linksMap = map[string]interface{}{}
if linksItem.ID != nil {
linksMap["id"] = linksItem.ID
}
if linksItem.EntityTag != nil {
linksMap["entity_tag"] = linksItem.EntityTag
}
if linksItem.CreatedAt != nil {
linksMap["created_at"] = linksItem.CreatedAt.String()
}
if linksItem.ModifiedAt != nil {
linksMap["modified_at"] = linksItem.ModifiedAt.String()
}
if linksItem.Name != nil {
linksMap["name"] = linksItem.Name
}
if linksItem.CrType != nil {
linksMap["cr_type"] = linksItem.CrType
}
if linksItem.Link != nil {
linkList := []map[string]interface{}{}
linkMap := dataSourceProfileLinkListLinksLinkToMap(*linksItem.Link)
linkList = append(linkList, linkMap)
linksMap["link"] = linkList
}
return linksMap
}
func dataSourceProfileLinkListLinksLinkToMap(linkItem iamidentityv1.ProfileLinkLink) (linkMap map[string]interface{}) {
linkMap = map[string]interface{}{}
if linkItem.CRN != nil {
linkMap["crn"] = linkItem.CRN
}
if linkItem.Namespace != nil {
linkMap["namespace"] = linkItem.Namespace
}
if linkItem.Name != nil {
linkMap["name"] = linkItem.Name
}
return linkMap
}