-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_code_engine_binding.go
142 lines (122 loc) · 4.13 KB
/
data_source_ibm_code_engine_binding.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package codeengine
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/code-engine-go-sdk/codeenginev2"
)
func DataSourceIbmCodeEngineBinding() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmCodeEngineBindingRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the project.",
},
"component": {
Type: schema.TypeList,
Computed: true,
Description: "A reference to another component.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the referenced component.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the referenced resource.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "When you provision a new binding, a URL is created identifying the location of the instance.",
},
"binding_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the binding.",
},
"prefix": {
Type: schema.TypeString,
Computed: true,
Description: "The value that is set as prefix in the component that is bound.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the binding.",
},
"secret_name": {
Type: schema.TypeString,
Computed: true,
Description: "The service access secret that is bound to a component.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The current status of the binding.",
},
},
}
}
func dataSourceIbmCodeEngineBindingRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
codeEngineClient, err := meta.(conns.ClientSession).CodeEngineV2()
if err != nil {
return diag.FromErr(err)
}
getBindingOptions := &codeenginev2.GetBindingOptions{}
getBindingOptions.SetProjectID(d.Get("project_id").(string))
getBindingOptions.SetID(d.Get("binding_id").(string))
binding, response, err := codeEngineClient.GetBindingWithContext(context, getBindingOptions)
if err != nil {
log.Printf("[DEBUG] GetBindingWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetBindingWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s/%s", *getBindingOptions.ProjectID, *getBindingOptions.ID))
component := []map[string]interface{}{}
if binding.Component != nil {
modelMap, err := dataSourceIbmCodeEngineBindingComponentRefToMap(binding.Component)
if err != nil {
return diag.FromErr(err)
}
component = append(component, modelMap)
}
errString := "Error setting %s %s"
if err = d.Set("component", component); err != nil {
return diag.FromErr(fmt.Errorf(errString, "component", err))
}
if err = d.Set("href", binding.Href); err != nil {
return diag.FromErr(fmt.Errorf(errString, "href", err))
}
if err = d.Set("prefix", binding.Prefix); err != nil {
return diag.FromErr(fmt.Errorf(errString, "prefix", err))
}
if err = d.Set("resource_type", binding.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf(errString, "resource_type", err))
}
if err = d.Set("secret_name", binding.SecretName); err != nil {
return diag.FromErr(fmt.Errorf(errString, "secret_name", err))
}
if err = d.Set("status", binding.Status); err != nil {
return diag.FromErr(fmt.Errorf(errString, "status", err))
}
return nil
}
func dataSourceIbmCodeEngineBindingComponentRefToMap(model *codeenginev2.ComponentRef) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
modelMap["resource_type"] = model.ResourceType
return modelMap, nil
}