-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_is_endpoint_gateway_targets.go
197 lines (187 loc) · 6.1 KB
/
data_source_ibm_is_endpoint_gateway_targets.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"net/url"
"strconv"
"strings"
"time"
"github.com/IBM-Cloud/bluemix-go/api/globalsearch/globalsearchv2"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM/platform-services-go-sdk/catalogmanagementv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isVPEResources = "resources"
isVPEResourceCRN = "crn"
isVPEResourceParent = "parent"
isVPEResourceName = "name"
isVPEResourceEndpointType = "endpoint_type"
isVPEResourceType = "resource_type"
isVPEResourceFullQualifiedDomainNames = "full_qualified_domain_names"
isVPEResourceServiceLocation = "location"
)
func DataSourceIBMISEndpointGatewayTargets() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMISEndpointGatewayTargetsRead,
Schema: map[string]*schema.Schema{
isVPEResources: {
Type: schema.TypeList,
Computed: true,
Description: "List of resources",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVPEResourceCRN: {
Type: schema.TypeString,
Computed: true,
Description: "CRN for this specific object",
},
isVPEResourceParent: {
Type: schema.TypeString,
Computed: true,
Description: "Parent for this specific object",
},
isVPEResourceName: {
Type: schema.TypeString,
Computed: true,
Description: "Display name in the requested language",
},
isVPEResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Resource type of this offering.",
},
isVPEResourceEndpointType: {
Type: schema.TypeString,
Computed: true,
Description: "Data endpoint type of this offering",
},
isVPEResourceFullQualifiedDomainNames: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Fully qualified domain names",
},
isVPEResourceServiceLocation: {
Type: schema.TypeString,
Computed: true,
Description: "Service location of this offering",
},
},
},
},
},
}
}
func dataSourceIBMISEndpointGatewayTargetsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
bmxSess, err := meta.(conns.ClientSession).BluemixSession()
if err != nil {
return diag.FromErr(err)
}
region := bmxSess.Config.Region
catalogManagementClient, err := meta.(conns.ClientSession).CatalogManagementV1()
if err != nil {
return diag.FromErr(err)
}
resourceInfo := make([]map[string]interface{}, 0)
getCatalogOptions := &catalogmanagementv1.SearchObjectsOptions{}
// query := "kind%3Avpe+AND+svc+AND+parent_id%3Aus-south"
query := fmt.Sprintf("kind:vpe AND svc AND parent_id:%s", region)
getCatalogOptions.Query = &query
digest := false
getCatalogOptions.Digest = &digest
start := int64(0)
catalog := []catalogmanagementv1.CatalogObject{}
for {
if start != int64(0) {
getCatalogOptions.Offset = &start
}
search, response, err := catalogManagementClient.SearchObjectsWithContext(context, getCatalogOptions)
if err != nil {
log.Printf("[DEBUG] GetCatalogWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
next := search.Next
if next == nil {
start = int64(0)
} else {
u, _ := url.Parse(fmt.Sprintf("%s", *next))
q := u.Query()
start, _ = strconv.ParseInt(q.Get("offset"), 10, 64)
}
catalog = append(catalog, search.Resources...)
if start == int64(0) {
break
}
}
if catalog != nil {
for _, res := range catalog {
l := map[string]interface{}{}
if res.ParentID != nil {
l[isVPEResourceParent] = *res.ParentID
}
l[isVPEResourceType] = "provider_cloud_service"
if res.Label != nil {
l[isVPEResourceName] = *res.Label
}
sl := ""
data := res.Data
if data != nil {
if serviceCrn, ok := data["service_crn"].(string); ok {
if serviceCrn != "" {
l[isVPEResourceCRN] = serviceCrn
crnFs := strings.Split(serviceCrn, ":")
if len(crnFs) > 5 {
sl = crnFs[5]
}
l[isVPEResourceServiceLocation] = sl
}
}
if data["endpoint_type"] != nil {
l[isVPEResourceEndpointType] = data["endpoint_type"]
}
if data["fully_qualified_domain_names"] != nil {
l[isVPEResourceFullQualifiedDomainNames] = data["fully_qualified_domain_names"]
}
}
resourceInfo = append(resourceInfo, l)
}
staticService := map[string]interface{}{}
staticService[isVPEResourceName] = "ibm-ntp-server"
staticService[isVPEResourceType] = "provider_infrastructure_service"
resourceInfo = append(resourceInfo, staticService)
}
queryString := "doc.extensions.virtual_private_endpoints.endpoints.ip_address:*"
fields := []string{"name", "region", "family", "type", "crn", "tags", "organization_guid", "doc.extensions", "doc.resource_group_id", "doc.space_guid", "resource_id"}
getSearchOptions := globalsearchv2.SearchBody{
Query: queryString,
Fields: fields,
}
globalSearchClient, err := meta.(conns.ClientSession).GlobalSearchAPI()
if err != nil {
return diag.FromErr(err)
}
searchResult, err := globalSearchClient.Searches().PostQuery(getSearchOptions)
if err != nil {
log.Printf("[DEBUG] PostQuery on globalSearchApi for query string %s failed %s", queryString, err)
return diag.FromErr(err)
}
searchItems := searchResult.Items
for _, item := range searchItems {
info := map[string]interface{}{}
info[isVPEResourceName] = item.Name
info[isVPEResourceType] = item.Type
info[isVPEResourceCRN] = item.CRN
resourceInfo = append(resourceInfo, info)
}
d.Set(isVPEResources, resourceInfo)
d.SetId(dataSourceIBMISEndpointGatewayTargetsId(d))
return nil
}
func dataSourceIBMISEndpointGatewayTargetsId(d *schema.ResourceData) string {
return time.Now().UTC().String()
}