|
| 1 | +package bcp_tf_exporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + dependentconsumers "github.com/mypurecloud/terraform-provider-genesyscloud/genesyscloud/dependent_consumers" |
| 7 | + "github.com/mypurecloud/terraform-provider-genesyscloud/genesyscloud/provider" |
| 8 | + resourceExporter "github.com/mypurecloud/terraform-provider-genesyscloud/genesyscloud/resource_exporter" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/mypurecloud/platform-client-sdk-go/v179/platformclientv2" |
| 12 | +) |
| 13 | + |
| 14 | +type BcpExporterProxy struct { |
| 15 | + ClientConfig *platformclientv2.Configuration |
| 16 | + GetFlowDependenciesAttr getFlowDependenciesFunc |
| 17 | + GetAllWithPooledClientAttr getPooledClientFunc |
| 18 | +} |
| 19 | + |
| 20 | +type getFlowDependenciesFunc func(ctx context.Context, p *BcpExporterProxy, resourceInfo resourceExporter.ResourceInfo) (resourceExporter.ResourceIDMetaMap, *resourceExporter.DependencyResource, error) |
| 21 | +type getPooledClientFunc func(ctx context.Context, method provider.GetCustomConfigFunc) (resourceExporter.ResourceIDMetaMap, *resourceExporter.DependencyResource, []string, diag.Diagnostics) |
| 22 | + |
| 23 | +var InternalProxy *BcpExporterProxy |
| 24 | + |
| 25 | +func GetBcpExporterProxy(ClientConfig *platformclientv2.Configuration) *BcpExporterProxy { |
| 26 | + return newBcpExporterProxy(ClientConfig) |
| 27 | +} |
| 28 | + |
| 29 | +func newBcpExporterProxy(ClientConfig *platformclientv2.Configuration) *BcpExporterProxy { |
| 30 | + if InternalProxy == nil { |
| 31 | + InternalProxy = &BcpExporterProxy{ |
| 32 | + GetAllWithPooledClientAttr: getPooledClientFn, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if ClientConfig != nil { |
| 37 | + InternalProxy.ClientConfig = ClientConfig |
| 38 | + InternalProxy.GetFlowDependenciesAttr = getFlowDependenciesFn |
| 39 | + } |
| 40 | + return InternalProxy |
| 41 | +} |
| 42 | + |
| 43 | +func (p *BcpExporterProxy) GetFlowDependencies(ctx context.Context, resourceInfo resourceExporter.ResourceInfo) (resourceExporter.ResourceIDMetaMap, *resourceExporter.DependencyResource, error) { |
| 44 | + return p.GetFlowDependenciesAttr(ctx, p, resourceInfo) |
| 45 | +} |
| 46 | + |
| 47 | +func (p *BcpExporterProxy) GetAllWithPooledClient(ctx context.Context, method provider.GetCustomConfigFunc) (resourceExporter.ResourceIDMetaMap, *resourceExporter.DependencyResource, []string, diag.Diagnostics) { |
| 48 | + return p.GetAllWithPooledClientAttr(ctx, method) |
| 49 | +} |
| 50 | + |
| 51 | +func getPooledClientFn(ctx context.Context, method provider.GetCustomConfigFunc) (resourceExporter.ResourceIDMetaMap, *resourceExporter.DependencyResource, []string, diag.Diagnostics) { |
| 52 | + resourceFunc := provider.GetAllWithPooledClientCustom(method) |
| 53 | + // Pass the context through - don't replace it |
| 54 | + resources, dependsMap, totalFlowResources, err := resourceFunc(ctx) |
| 55 | + if err != nil { |
| 56 | + return nil, nil, totalFlowResources, err |
| 57 | + } |
| 58 | + return resources, dependsMap, totalFlowResources, err |
| 59 | +} |
| 60 | + |
| 61 | +func getFlowDependenciesFn(ctx context.Context, p *BcpExporterProxy, resourceInfo resourceExporter.ResourceInfo) (resourceExporter.ResourceIDMetaMap, *resourceExporter.DependencyResource, error) { |
| 62 | + // Create fresh config to avoid pooled client's canceled context issue |
| 63 | + // Only copy essential auth settings for efficiency |
| 64 | + freshConfig := platformclientv2.NewConfiguration() |
| 65 | + freshConfig.BasePath = p.ClientConfig.BasePath |
| 66 | + freshConfig.AccessToken = p.ClientConfig.AccessToken |
| 67 | + freshConfig.DefaultHeader = p.ClientConfig.DefaultHeader // Direct assignment is safe for read-only use |
| 68 | + |
| 69 | + depConsumerProxy := dependentconsumers.GetDependentConsumerProxy(freshConfig) |
| 70 | + resources, dependsMap, _, err := depConsumerProxy.GetDependentConsumers(ctx, resourceInfo, []string{}) |
| 71 | + if err != nil { |
| 72 | + return nil, nil, err |
| 73 | + } |
| 74 | + return resources, dependsMap, nil |
| 75 | +} |
0 commit comments