From f82d333b9a58a1564f1093fba32e45ed88b9a425 Mon Sep 17 00:00:00 2001 From: Mathieu Tortuyaux Date: Fri, 13 Nov 2020 14:46:14 +0100 Subject: [PATCH] azurerm: add cache for virtualMachines and app workflows --- azurerm/cache.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ azurerm/resources.go | 32 ++++++++++++-------- 2 files changed, 90 insertions(+), 12 deletions(-) diff --git a/azurerm/cache.go b/azurerm/cache.go index f496080cb4..346e04197d 100644 --- a/azurerm/cache.go +++ b/azurerm/cache.go @@ -43,3 +43,73 @@ func getVirtualNetworkNames(ctx context.Context, a *azurerm, rt string, filters return names, nil } + +func cacheVirtualMachines(ctx context.Context, a *azurerm, rt string, filters *filter.Filter) ([]provider.Resource, error) { + rs, err := a.cache.Get(rt) + if err != nil { + if errors.Cause(err) != errcode.ErrCacheKeyNotFound { + return nil, errors.WithStack(err) + } + + rs, err = virtualMachines(ctx, a, rt, filters) + if err != nil { + return nil, errors.Wrap(err, "unable to get virtual machines") + } + + err = a.cache.Set(rt, rs) + if err != nil { + return nil, err + } + } + + return rs, nil +} + +func getVirtualMachineNames(ctx context.Context, a *azurerm, rt string, filters *filter.Filter) ([]string, error) { + rs, err := cacheVirtualMachines(ctx, a, rt, filters) + if err != nil { + return nil, err + } + + names := make([]string, 0, len(rs)) + for _, i := range rs { + names = append(names, i.Data().Get("name").(string)) + } + + return names, nil +} + +func cacheWorkflows(ctx context.Context, a *azurerm, rt string, filters *filter.Filter) ([]provider.Resource, error) { + rs, err := a.cache.Get(rt) + if err != nil { + if errors.Cause(err) != errcode.ErrCacheKeyNotFound { + return nil, errors.WithStack(err) + } + + rs, err = logicAppWorkflows(ctx, a, rt, filters) + if err != nil { + return nil, errors.Wrap(err, "unable to get workflows") + } + + err = a.cache.Set(rt, rs) + if err != nil { + return nil, err + } + } + + return rs, nil +} + +func getWorkflowNames(ctx context.Context, a *azurerm, rt string, filters *filter.Filter) ([]string, error) { + rs, err := cacheWorkflows(ctx, a, rt, filters) + if err != nil { + return nil, err + } + + names := make([]string, 0, len(rs)) + for _, i := range rs { + names = append(names, i.Data().Get("name").(string)) + } + + return names, nil +} diff --git a/azurerm/resources.go b/azurerm/resources.go index 1972182f46..73d144a712 100644 --- a/azurerm/resources.go +++ b/azurerm/resources.go @@ -65,6 +65,9 @@ func virtualMachines(ctx context.Context, a *azurerm, resourceType string, filte resources := make([]provider.Resource, 0, len(virtualMachines)) for _, virtualMachine := range virtualMachines { r := provider.NewResource(*virtualMachine.ID, resourceType, a) + if err := r.Data().Set("name", *virtualMachine.Name); err != nil { + return nil, errors.Wrapf(err, "unable to set name data on the provider.Resource for the virtual machine '%s'", *virtualMachine.Name) + } resources = append(resources, r) } return resources, nil @@ -180,22 +183,27 @@ func logicAppWorkflows(ctx context.Context, a *azurerm, resourceType string, fil return nil, errors.Wrap(err, "unable to list logic app workflows from reader") } resources := make([]provider.Resource, 0, len(appWorkflows)) - for _, appWorklow := range appWorkflows { - r := provider.NewResource(*appWorklow.ID, resourceType, a) + for _, appWorkflow := range appWorkflows { + r := provider.NewResource(*appWorkflow.ID, resourceType, a) + // we set the name prior of reading it from the state + // as it is required to able to List resources depending on this one + if err := r.Data().Set("name", *appWorkflow.Name); err != nil { + return nil, errors.Wrapf(err, "unable to set name data on the provider.Resource for the app workflow '%s'", *appWorkflow.Name) + } resources = append(resources, r) } return resources, nil } func logicAppTriggerCustoms(ctx context.Context, a *azurerm, resourceType string, filters *filter.Filter) ([]provider.Resource, error) { - appWorkflows, err := a.azurer.ListWorkflows(ctx, nil, "") + appWorkflowNames, err := getWorkflowNames(ctx, a, resourceType, filters) if err != nil { return nil, errors.Wrap(err, "unable to list logic app workflows from reader") } resources := make([]provider.Resource, 0) - for _, appWorkflow := range appWorkflows { - triggers, err := a.azurer.ListWorkflowTriggers(ctx, *appWorkflow.Name, nil, "") + for _, appWorkflowName := range appWorkflowNames { + triggers, err := a.azurer.ListWorkflowTriggers(ctx, appWorkflowName, nil, "") if err != nil { return nil, errors.Wrap(err, "unable to list logic app trigger HTTP requests from reader") } @@ -208,20 +216,20 @@ func logicAppTriggerCustoms(ctx context.Context, a *azurerm, resourceType string } func logicAppActionCustoms(ctx context.Context, a *azurerm, resourceType string, filters *filter.Filter) ([]provider.Resource, error) { - appWorkflows, err := a.azurer.ListWorkflows(ctx, nil, "") + appWorkflowNames, err := getWorkflowNames(ctx, a, resourceType, filters) if err != nil { return nil, errors.Wrap(err, "unable to list logic app workflows from reader") } resources := make([]provider.Resource, 0) - for _, appWorkflow := range appWorkflows { - runs, err := a.azurer.ListWorkflowRuns(ctx, *appWorkflow.Name, nil, "") + for _, appWorkflowName := range appWorkflowNames { + runs, err := a.azurer.ListWorkflowRuns(ctx, appWorkflowName, nil, "") if err != nil { return nil, errors.Wrap(err, "unable to list workflow runs from reader") } for _, run := range runs { - actions, err := a.azurer.ListWorkflowRunActions(ctx, *appWorkflow.Name, *run.Name, nil, "") + actions, err := a.azurer.ListWorkflowRunActions(ctx, appWorkflowName, *run.Name, nil, "") if err != nil { return nil, errors.Wrap(err, "unable to list workflow run actions from reader") } @@ -235,14 +243,14 @@ func logicAppActionCustoms(ctx context.Context, a *azurerm, resourceType string, } func virtualMachineExtensions(ctx context.Context, a *azurerm, resourceType string, filters *filter.Filter) ([]provider.Resource, error) { - virtualMachines, err := a.azurer.ListVirtualMachines(ctx) + virtualMachineNames, err := getVirtualMachineNames(ctx, a, resourceType, filters) if err != nil { return nil, errors.Wrap(err, "unable to list virtual machines from reader") } resources := make([]provider.Resource, 0) - for _, virtualMachine := range virtualMachines { - extensions, err := a.azurer.ListVirtualMachineExtensions(ctx, *virtualMachine.Name, "") + for _, virtualMachineName := range virtualMachineNames { + extensions, err := a.azurer.ListVirtualMachineExtensions(ctx, virtualMachineName, "") if err != nil { return nil, errors.Wrap(err, "unable to list virtual machine extensions from reader") }