/
identity_customer_secret_keys_data_source.go
115 lines (89 loc) · 2.72 KB
/
identity_customer_secret_keys_data_source.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
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"context"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
oci_identity "github.com/oracle/oci-go-sdk/identity"
)
func IdentityCustomerSecretKeysDataSource() *schema.Resource {
return &schema.Resource{
Read: readIdentityCustomerSecretKeys,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"user_id": {
Type: schema.TypeString,
Required: true,
},
"customer_secret_keys": {
Type: schema.TypeList,
Computed: true,
Elem: GetDataSourceItemSchema(IdentityCustomerSecretKeyResource()),
},
},
}
}
func readIdentityCustomerSecretKeys(d *schema.ResourceData, m interface{}) error {
sync := &IdentityCustomerSecretKeysDataSourceCrud{}
sync.D = d
sync.Client = m.(*OracleClients).identityClient
return ReadResource(sync)
}
type IdentityCustomerSecretKeysDataSourceCrud struct {
D *schema.ResourceData
Client *oci_identity.IdentityClient
Res *oci_identity.ListCustomerSecretKeysResponse
}
func (s *IdentityCustomerSecretKeysDataSourceCrud) VoidState() {
s.D.SetId("")
}
func (s *IdentityCustomerSecretKeysDataSourceCrud) Get() error {
request := oci_identity.ListCustomerSecretKeysRequest{}
if userId, ok := s.D.GetOkExists("user_id"); ok {
tmp := userId.(string)
request.UserId = &tmp
}
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity")
response, err := s.Client.ListCustomerSecretKeys(context.Background(), request)
if err != nil {
return err
}
s.Res = &response
return nil
}
func (s *IdentityCustomerSecretKeysDataSourceCrud) SetData() error {
if s.Res == nil {
return nil
}
s.D.SetId(GenerateDataSourceID())
resources := []map[string]interface{}{}
for _, r := range s.Res.Items {
customerSecretKey := map[string]interface{}{
"user_id": *r.UserId,
}
if r.DisplayName != nil {
customerSecretKey["display_name"] = *r.DisplayName
}
if r.Id != nil {
customerSecretKey["id"] = *r.Id
}
if r.InactiveStatus != nil {
customerSecretKey["inactive_state"] = strconv.FormatInt(*r.InactiveStatus, 10)
}
customerSecretKey["state"] = r.LifecycleState
if r.TimeCreated != nil {
customerSecretKey["time_created"] = r.TimeCreated.String()
}
if r.TimeExpires != nil {
customerSecretKey["time_expires"] = r.TimeExpires.String()
}
resources = append(resources, customerSecretKey)
}
if f, fOk := s.D.GetOkExists("filter"); fOk {
resources = ApplyFilters(f.(*schema.Set), resources, IdentityCustomerSecretKeysDataSource().Schema["customer_secret_keys"].Elem.(*schema.Resource).Schema)
}
if err := s.D.Set("customer_secret_keys", resources); err != nil {
return err
}
return nil
}