@@ -15,6 +15,35 @@ import (
15
15
"github.com/overmindtech/cli/sources/shared"
16
16
)
17
17
18
+ var (
19
+ getDescription = func (sdpAssetType shared.ItemType , scope string , uniqueAttributeKeys []string ) string {
20
+ selector := "{name}"
21
+ if len (uniqueAttributeKeys ) > 1 {
22
+ // i.e.: {datasets|tables} for bigquery tables
23
+ selector = "{" + strings .Join (uniqueAttributeKeys , shared .QuerySeparator ) + "}"
24
+ }
25
+
26
+ return fmt .Sprintf ("Get a %s by its %s within its scope: %s" , sdpAssetType , selector , scope )
27
+ }
28
+
29
+ listDescription = func (sdpAssetType shared.ItemType , scope string ) string {
30
+ return fmt .Sprintf ("List all %s within its scope: %s" , sdpAssetType , scope )
31
+ }
32
+
33
+ searchDescription = func (sdpAssetType shared.ItemType , scope string , uniqueAttributeKeys []string ) string {
34
+ if len (uniqueAttributeKeys ) < 2 {
35
+ panic ("searchDescription requires at least two unique attribute keys" )
36
+ }
37
+ // For service directory endpoint adapter, the uniqueAttributeKeys is: []string{"locations", "namespaces", "services", "endpoints"}
38
+ // We want to create a selector like:
39
+ // {locations|namespaces|services}
40
+ // We remove the last key, because it defines the actual item selector
41
+ selector := "{" + strings .Join (uniqueAttributeKeys [:len (uniqueAttributeKeys )- 1 ], shared .QuerySeparator ) + "}"
42
+
43
+ return fmt .Sprintf ("Search for %s by its %s within its scope: %s" , sdpAssetType , selector , scope )
44
+ }
45
+ )
46
+
18
47
func linkItem (ctx context.Context , projectID string , sdpItem * sdp.Item , sdpAssetType shared.ItemType , linker * gcpshared.Linker , resp any , keys []string ) {
19
48
if value , ok := resp .(string ); ok {
20
49
linker .AutoLink (ctx , projectID , sdpItem , sdpAssetType , value , keys )
@@ -62,6 +91,7 @@ func externalToSDP(ctx context.Context, projectID string, scope string, uniqueAt
62
91
}
63
92
64
93
// We need to keep an eye on this.
94
+ // Name might not exist in the response for all APIs.
65
95
if name , ok := resp ["name" ].(string ); ok {
66
96
attrValues := gcpshared .ExtractPathParams (name , uniqueAttrKeys ... )
67
97
uniqueAttrValue := strings .Join (attrValues , shared .QuerySeparator )
@@ -70,7 +100,7 @@ func externalToSDP(ctx context.Context, projectID string, scope string, uniqueAt
70
100
return nil , err
71
101
}
72
102
} else {
73
- return nil , fmt .Errorf ("unable to determine self link " )
103
+ return nil , fmt .Errorf ("unable to determine the name " )
74
104
}
75
105
76
106
for k , v := range resp {
@@ -96,6 +126,15 @@ func externalCallSingle(ctx context.Context, httpCli *http.Client, httpHeaders h
96
126
defer resp .Body .Close ()
97
127
98
128
if resp .StatusCode != http .StatusOK {
129
+ body , err := io .ReadAll (resp .Body )
130
+ if err == nil {
131
+ return nil , fmt .Errorf ("failed to make a GET call: %s, HTTP Status: %s, HTTP Body: %s" , url , resp .Status , string (body ))
132
+ }
133
+
134
+ log .WithContext (ctx ).WithFields (log.Fields {
135
+ "ovm.gcp.dynamic.http.get.url" : url ,
136
+ "ovm.gcp.dynamic.http.get.responseStatus" : resp .Status ,
137
+ }).Warnf ("failed to read the response body: %v" , err )
99
138
return nil , fmt .Errorf ("failed to make call: %s" , resp .Status )
100
139
}
101
140
@@ -127,7 +166,17 @@ func externalCallMulti(ctx context.Context, itemsSelector string, httpCli *http.
127
166
defer resp .Body .Close ()
128
167
129
168
if resp .StatusCode != http .StatusOK {
130
- return nil , fmt .Errorf ("failed to make the GET call for the %s URL. HTTP Status: %s" , url , resp .Status )
169
+ // Read the body to provide more context in the error message
170
+ body , err := io .ReadAll (resp .Body )
171
+ if err == nil {
172
+ return nil , fmt .Errorf ("failed to make the GET call. HTTP Status: %s, HTTP Body: %s" , resp .Status , string (body ))
173
+ }
174
+
175
+ log .WithContext (ctx ).WithFields (log.Fields {
176
+ "ovm.gcp.dynamic.http.get.url" : url ,
177
+ "ovm.gcp.dynamic.http.get.responseStatus" : resp .Status ,
178
+ }).Warnf ("failed to read the response body: %v" , err )
179
+ return nil , fmt .Errorf ("failed to make the GET callL. HTTP Status: %s" , resp .Status )
131
180
}
132
181
133
182
data , err := io .ReadAll (resp .Body )
@@ -142,12 +191,13 @@ func externalCallMulti(ctx context.Context, itemsSelector string, httpCli *http.
142
191
143
192
items , ok := result [itemsSelector ].([]any )
144
193
if ! ok {
145
- // fallback to a generic "items" key if the itemsSelector is not found
146
- items , ok = result ["items" ].([]any )
194
+ itemsSelector = "items" // Fallback to a generic "items" key
195
+ items , ok = result [itemsSelector ].([]any )
147
196
if ! ok {
148
197
log .WithContext (ctx ).WithFields (log.Fields {
149
- "url" : url ,
150
- }).Warnf ("failed to cast resp as a list of items: %v" , result )
198
+ "ovm.gcp.dynamic.http.get.url" : url ,
199
+ "ovm.gcp.dynamic.http.get.itemsSelector" : itemsSelector ,
200
+ }).Warnf ("failed to cast resp as a list of %s: %v" , itemsSelector , result )
151
201
return nil , nil
152
202
}
153
203
}
0 commit comments