-
Notifications
You must be signed in to change notification settings - Fork 650
/
data_source_ibm_code_engine_domain_mapping.go
219 lines (193 loc) · 7.43 KB
/
data_source_ibm_code_engine_domain_mapping.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// 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 DataSourceIbmCodeEngineDomainMapping() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmCodeEngineDomainMappingRead,
Schema: map[string]*schema.Schema{
"project_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The ID of the project.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of your domain mapping.",
},
"cname_target": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Exposes the value of the CNAME record that needs to be configured in the DNS settings of the domain, to route traffic properly to the target Code Engine region.",
},
"component": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "A reference to another component.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the referenced component.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of the referenced resource.",
},
},
},
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The timestamp when the resource was created.",
},
"domain_mapping_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The identifier of the resource.",
},
"entity_tag": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The version of the domain mapping instance, which is used to achieve optimistic locking.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "When you provision a new domain mapping, a URL is created identifying the location of the instance.",
},
"resource_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of the CE Resource.",
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The current status of the domain mapping.",
},
"status_details": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The detailed status of the domain mapping.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"reason": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Optional information to provide more context in case of a 'failed' or 'warning' status.",
},
},
},
},
"tls_secret": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the TLS secret that holds the certificate and private key of this domain mapping.",
},
"user_managed": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "Exposes whether the domain mapping is managed by the user or by Code Engine.",
},
"visibility": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Exposes whether the domain mapping is reachable through the public internet, or private IBM network, or only through other components within the same Code Engine project.",
},
},
}
}
func dataSourceIbmCodeEngineDomainMappingRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
codeEngineClient, err := meta.(conns.ClientSession).CodeEngineV2()
if err != nil {
return diag.FromErr(err)
}
getDomainMappingOptions := &codeenginev2.GetDomainMappingOptions{}
getDomainMappingOptions.SetProjectID(d.Get("project_id").(string))
getDomainMappingOptions.SetName(d.Get("name").(string))
domainMapping, response, err := codeEngineClient.GetDomainMappingWithContext(context, getDomainMappingOptions)
if err != nil {
log.Printf("[DEBUG] GetDomainMappingWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetDomainMappingWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s/%s", *getDomainMappingOptions.ProjectID, *getDomainMappingOptions.Name))
if err = d.Set("cname_target", domainMapping.CnameTarget); err != nil {
return diag.FromErr(fmt.Errorf("Error setting cname_target: %s", err))
}
component := []map[string]interface{}{}
if domainMapping.Component != nil {
modelMap, err := dataSourceIbmCodeEngineDomainMappingComponentRefToMap(domainMapping.Component)
if err != nil {
return diag.FromErr(err)
}
component = append(component, modelMap)
}
if err = d.Set("component", component); err != nil {
return diag.FromErr(fmt.Errorf("Error setting component %s", err))
}
if err = d.Set("created_at", domainMapping.CreatedAt); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
if err = d.Set("entity_tag", domainMapping.EntityTag); err != nil {
return diag.FromErr(fmt.Errorf("Error setting entity_tag: %s", err))
}
if err = d.Set("href", domainMapping.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("domain_mapping_id", domainMapping.ID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting domain_mapping_id: %s", err))
}
if err = d.Set("resource_type", domainMapping.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_type: %s", err))
}
if err = d.Set("status", domainMapping.Status); err != nil {
return diag.FromErr(fmt.Errorf("Error setting status: %s", err))
}
statusDetails := []map[string]interface{}{}
if domainMapping.StatusDetails != nil {
modelMap, err := dataSourceIbmCodeEngineDomainMappingDomainMappingStatusToMap(domainMapping.StatusDetails)
if err != nil {
return diag.FromErr(err)
}
statusDetails = append(statusDetails, modelMap)
}
if err = d.Set("status_details", statusDetails); err != nil {
return diag.FromErr(fmt.Errorf("Error setting status_details %s", err))
}
if err = d.Set("tls_secret", domainMapping.TlsSecret); err != nil {
return diag.FromErr(fmt.Errorf("Error setting tls_secret: %s", err))
}
if err = d.Set("user_managed", domainMapping.UserManaged); err != nil {
return diag.FromErr(fmt.Errorf("Error setting user_managed: %s", err))
}
if err = d.Set("visibility", domainMapping.Visibility); err != nil {
return diag.FromErr(fmt.Errorf("Error setting visibility: %s", err))
}
return nil
}
func dataSourceIbmCodeEngineDomainMappingComponentRefToMap(model *codeenginev2.ComponentRef) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
modelMap["resource_type"] = model.ResourceType
return modelMap, nil
}
func dataSourceIbmCodeEngineDomainMappingDomainMappingStatusToMap(model *codeenginev2.DomainMappingStatus) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Reason != nil {
modelMap["reason"] = model.Reason
}
return modelMap, nil
}