-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_iam_api_key.go
132 lines (119 loc) · 4.5 KB
/
data_source_ibm_iam_api_key.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package iamidentity
import (
"context"
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"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 DataSourceIBMIamApiKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmIamApiKeyRead,
Schema: map[string]*schema.Schema{
"apikey_id": {
Type: schema.TypeString,
Required: true,
Description: "Unique ID of the API key.",
},
"entity_tag": {
Type: schema.TypeString,
Computed: true,
Description: "Version of the API Key details object. You need to specify this value when updating the API key to avoid stale updates.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "Cloud Resource Name of the item. Example Cloud Resource Name: 'crn:v1:bluemix:public:iam-identity:us-south:a/myaccount::apikey:1234-9012-5678'.",
},
"locked": {
Type: schema.TypeBool,
Computed: true,
Description: "The API key cannot be changed if set to true.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "If set contains a date time string of the creation date in ISO format.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "IAM ID of the user or service which created the API key.",
},
"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: "Name of the API key. The name is not checked for uniqueness. Therefore multiple names with the same value can exist. Access is done via the UUID of the API key.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The optional description of the API key. The 'description' property is only available if a description was provided during a create of an API key.",
},
"iam_id": {
Type: schema.TypeString,
Computed: true,
Description: "The iam_id that this API key authenticates.",
},
"account_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the account that this API key authenticates for.",
},
},
}
}
func dataSourceIbmIamApiKeyRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamIdentityClient, err := meta.(conns.ClientSession).IAMIdentityV1API()
if err != nil {
return diag.FromErr(err)
}
getApiKeyOptions := &iamidentityv1.GetAPIKeyOptions{}
getApiKeyOptions.SetID(d.Get("apikey_id").(string))
apiKey, response, err := iamIdentityClient.GetAPIKey(getApiKeyOptions)
if err != nil {
log.Printf("[DEBUG] GetApiKey failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId(*apiKey.ID)
if err = d.Set("entity_tag", apiKey.EntityTag); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting entity_tag: %s", err))
}
if err = d.Set("crn", apiKey.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
if err = d.Set("locked", apiKey.Locked); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting locked: %s", err))
}
if err = d.Set("created_at", apiKey.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("created_by", apiKey.CreatedBy); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_by: %s", err))
}
if err = d.Set("modified_at", apiKey.ModifiedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting modified_at: %s", err))
}
if err = d.Set("name", apiKey.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if err = d.Set("description", apiKey.Description); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting description: %s", err))
}
if err = d.Set("iam_id", apiKey.IamID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting iam_id: %s", err))
}
if err = d.Set("account_id", apiKey.AccountID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting account_id: %s", err))
}
return nil
}