-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_kms_kmip_adapter.go
117 lines (105 loc) · 3.41 KB
/
data_source_ibm_kms_kmip_adapter.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kms
import (
"context"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceIBMKMSKmipAdapterBaseSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"adapter_id": {
Type: schema.TypeString,
Computed: true,
Description: "The UUID of the KMIP adapter to be fetched",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the KMIP adapter to be fetched",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the KMIP adapter to be fetched",
},
"profile": {
Type: schema.TypeString,
Computed: true,
Description: "The profile of the KMIP adapter to be fetched",
},
"profile_data": {
Type: schema.TypeMap,
Computed: true,
Description: "The data specific to the KMIP Adapter profile",
},
"created_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier that is associated with the entity that created the adapter.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when a resource was created. The date format follows RFC 3339.",
},
"updated_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier that is associated with the entity that updated the adapter.",
},
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when a resource was updated. The date format follows RFC 3339.",
},
}
}
func DataSourceIBMKMSKmipAdapter() *schema.Resource {
baseMap := dataSourceIBMKMSKmipAdapterBaseSchema()
baseMap["endpoint_type"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "public or private",
}
baseMap["instance_id"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Key protect or hpcs instance GUID",
DiffSuppressFunc: suppressKMSInstanceIDDiff,
}
adapterIDSchema := baseMap["adapter_id"]
adapterIDSchema.Optional = true
adapterIDSchema.ExactlyOneOf = []string{"adapter_id", "name"}
baseMap["adapter_id"] = adapterIDSchema
adapterNameSchema := baseMap["name"]
adapterNameSchema.Optional = true
adapterNameSchema.ExactlyOneOf = []string{"adapter_id", "name"}
baseMap["name"] = adapterNameSchema
return &schema.Resource{
Read: dataSourceIBMKMSKmipAdapterRead,
Schema: baseMap,
}
}
func dataSourceIBMKMSKmipAdapterRead(d *schema.ResourceData, meta interface{}) error {
// initialize API
instanceID := getInstanceIDFromResourceData(d, "instance_id")
kpAPI, _, err := populateKPClient(d, meta, instanceID)
if err != nil {
return err
}
nameOrID, hasID := d.GetOk("adapter_id")
if !hasID {
nameOrID = d.Get("name")
}
adapterNameOrID := nameOrID.(string)
// call GetKMIPAdapter api
adapter, err := kpAPI.GetKMIPAdapter(context.Background(), adapterNameOrID)
if err != nil {
return err
}
// set computed values
return populateKMIPAdapterSchemaDataFromStruct(d, *adapter, instanceID)
}