Skip to content

Commit 7b67554

Browse files
Fixed #630
1 parent d616ae9 commit 7b67554

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

dynatrace/api/v2/entity/service.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ func (me *dataSourceService) Get(ctx context.Context, id string, v *entity.Entit
7676
return me.client.Get(ctx, fmt.Sprintf(`/api/v2/entities/%s?from=%s&fields=tags`, url.PathEscape(id), url.QueryEscape("now-3y")), 200).Finish(v)
7777
}
7878

79-
result := getEntity(ctx, id, me.client, getEntitiesRecord(entityType))
79+
var result *entity.Entity
80+
81+
if os.Getenv("DYNATRACE_DISABLE_ENTITY_CACHE") == "true" {
82+
result = getEntityByID(ctx, me.client, id)
83+
} else {
84+
result = getEntity(ctx, id, me.client, getEntitiesRecord(entityType))
85+
}
8086
if result == nil {
8187
return rest.Error{Code: 404, Message: fmt.Sprintf("Unable to find entity with id %s", id)}
8288
}
@@ -166,6 +172,41 @@ func getEntity(ctx context.Context, id string, client rest.Client, record *Entit
166172
return entity
167173
}
168174

175+
func getEntityByID(ctx context.Context, client rest.Client, id string) *entity.Entity {
176+
entities := map[string]*entity.Entity{}
177+
nextPageKey := "-"
178+
for len(nextPageKey) > 0 {
179+
entitySelector := fmt.Sprintf("entityId(%s)", id)
180+
var u string
181+
if nextPageKey != "-" {
182+
u = fmt.Sprintf("/api/v2/entities?nextPageKey=%s", url.QueryEscape(nextPageKey))
183+
} else {
184+
u = fmt.Sprintf("/api/v2/entities?pageSize=4000&entitySelector=%s&from=%s&fields=tags", url.QueryEscape(entitySelector), url.QueryEscape("now-3y"))
185+
}
186+
var response EntitiesListResponse
187+
if err := client.Get(ctx, u, 200).Finish(&response); err != nil {
188+
return nil
189+
}
190+
for _, elem := range response.Entities {
191+
entity := &entity.Entity{
192+
EntityId: &elem.ID,
193+
DisplayName: &elem.DisplayName,
194+
Type: &elem.Type,
195+
}
196+
entities[elem.ID] = entity
197+
}
198+
nextPageKey = response.NextPageKey
199+
}
200+
if entities == nil {
201+
return nil
202+
}
203+
entity, found := entities[id]
204+
if !found {
205+
return nil
206+
}
207+
return entity
208+
}
209+
169210
func fetchEntities(ctx context.Context, client rest.Client, record *EntitiesRecord) map[string]*entity.Entity {
170211
results := map[string]*entity.Entity{}
171212
nextPageKey := "-"

0 commit comments

Comments
 (0)