-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_cf_service_key.go
57 lines (48 loc) · 1.51 KB
/
data_source_cf_service_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
package cloudfoundry
import (
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
)
func dataSourceServiceKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceServiceKeyRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"service_instance": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"credentials": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Sensitive: true,
},
},
}
}
func dataSourceServiceKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
session := meta.(*managers.Session)
serviceKeys, _, err := session.ClientV2.GetServiceKeys(
ccv2.FilterByName(d.Get("name").(string)),
ccv2.FilterEqual(constant.ServiceInstanceGUIDFilter, d.Get("service_instance").(string)),
)
if err != nil {
return diag.FromErr(err)
}
if len(serviceKeys) == 0 {
return diag.FromErr(NotFound)
}
serviceKey := serviceKeys[0]
d.SetId(serviceKey.GUID)
d.Set("name", serviceKey.Name)
d.Set("service_instance", serviceKey.ServiceInstanceGUID)
d.Set("credentials", normalizeMap(serviceKey.Credentials, make(map[string]interface{}), "", "_"))
return nil
}