Skip to content

Commit

Permalink
Add provider attribute to determine whether to export computed attrib…
Browse files Browse the repository at this point in the history
…utes or not
  • Loading branch information
bbbco committed Sep 27, 2024
1 parent 86257a1 commit 854d207
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 3 additions & 2 deletions docs/resources/tf_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
page_title: "genesyscloud_tf_export Resource - terraform-provider-genesyscloud"
subcategory: ""
description: |-
Genesys Cloud Resource to export Terraform config and (optionally) tfstate files to a local directory.
Genesys Cloud Resource to export Terraform config and (optionally) tfstate files to a local directory.
The config file is named 'genesyscloud.tf.json' or 'genesyscloud.tf', and the state file is named 'terraform.tfstate'.
---
# genesyscloud_tf_export (Resource)

Genesys Cloud Resource to export Terraform config and (optionally) tfstate files to a local directory.
Genesys Cloud Resource to export Terraform config and (optionally) tfstate files to a local directory.
The config file is named 'genesyscloud.tf.json' or 'genesyscloud.tf', and the state file is named 'terraform.tfstate'.

## API Usage
Expand Down Expand Up @@ -66,6 +66,7 @@ resource "genesyscloud_tf_export" "export" {
- `exclude_attributes` (List of String) Attributes to exclude from the config when exporting resources. Each value should be of the form {resource_name}.{attribute}, e.g. 'genesyscloud_user.skills'. Excluded attributes must be optional.
- `exclude_filter_resources` (List of String) Exclude resources that match either a resource type or a resource type::regular expression. See export guide for additional information
- `export_as_hcl` (Boolean) Export the config as HCL. Defaults to `false`.
- `export_computed` (Boolean) Export attributes that are marked as being Computed Defaults to `false`.
- `ignore_cyclic_deps` (Boolean) Ignore Cyclic Dependencies when building the flows and do not throw an error Defaults to `true`.
- `include_filter_resources` (List of String) Include only resources that match either a resource type or a resource type::regular expression. See export guide for additional information
- `include_state_file` (Boolean) Export a 'terraform.tfstate' file along with the config file. This can be used for orgs to begin managing existing resources with terraform. When `false`, GUID fields will be omitted from the config file unless a resource reference can be supplied. In this case, the resource type will need to be included in the `resource_types` array. Defaults to `false`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package integration_action

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"terraform-provider-genesyscloud/genesyscloud/provider"
resourceExporter "terraform-provider-genesyscloud/genesyscloud/resource_exporter"
registrar "terraform-provider-genesyscloud/genesyscloud/resource_register"
"terraform-provider-genesyscloud/genesyscloud/util"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
Expand Down
17 changes: 15 additions & 2 deletions genesyscloud/tfexporter/genesyscloud_resource_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type GenesysCloudResourceExporter struct {
cyclicDependsList []string
ignoreCyclicDeps bool
flowResourcesList []string
exportComputed bool
}

func configureExporterType(ctx context.Context, d *schema.ResourceData, gre *GenesysCloudResourceExporter, filterType ExporterFilterType) {
Expand Down Expand Up @@ -140,6 +141,7 @@ func NewGenesysCloudResourceExporter(ctx context.Context, d *schema.ResourceData
exportAsHCL: d.Get("export_as_hcl").(bool),
splitFilesByResource: d.Get("split_files_by_resource").(bool),
logPermissionErrors: d.Get("log_permission_errors").(bool),
exportComputed: d.Get("export_computed").(bool),
addDependsOn: computeDependsOn(d),
filterType: filterType,
includeStateFile: d.Get("include_state_file").(bool),
Expand Down Expand Up @@ -1006,6 +1008,8 @@ func (g *GenesysCloudResourceExporter) getResourcesForType(resType string, provi
return nil, diag.Errorf("Resource type %v not defined", resType)
}

exportComputed := g.exportComputed

ctyType := res.CoreConfigSchema().ImpliedType()
var wg sync.WaitGroup
wg.Add(lenResources)
Expand All @@ -1017,7 +1021,7 @@ func (g *GenesysCloudResourceExporter) getResourcesForType(resType string, provi
defer cancel()
// This calls into the resource's ReadContext method which
// will block until it can acquire a pooled client config object.
instanceState, err := getResourceState(ctx, res, id, resMeta, meta)
instanceState, err := getResourceState(ctx, res, id, resMeta, meta, exportComputed)

resourceType := ""
if g.isDataSource(resType, resMeta.Name) {
Expand Down Expand Up @@ -1108,7 +1112,7 @@ func (g *GenesysCloudResourceExporter) getResourcesForType(resType string, provi
}
}

func getResourceState(ctx context.Context, resource *schema.Resource, resID string, resMeta *resourceExporter.ResourceMeta, meta interface{}) (*terraform.InstanceState, diag.Diagnostics) {
func getResourceState(ctx context.Context, resource *schema.Resource, resID string, resMeta *resourceExporter.ResourceMeta, meta interface{}, exportComputed bool) (*terraform.InstanceState, diag.Diagnostics) {
// If defined, pass the full ID through the import method to generate a readable state
instanceState := &terraform.InstanceState{ID: resMeta.IdPrefix + resID}
if resource.Importer != nil && resource.Importer.StateContext != nil {
Expand All @@ -1133,6 +1137,15 @@ func getResourceState(ctx context.Context, resource *schema.Resource, resID stri
// Resource no longer exists
return nil, nil
}

if !exportComputed {
// Remove any computed attributes from being exported
for resType, resSchema := range resource.Schema {
if resSchema.Computed {
delete(state.Attributes, resType)
}
}
}
return state, nil
}

Expand Down
9 changes: 8 additions & 1 deletion genesyscloud/tfexporter/resource_genesyscloud_tf_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func SetRegistrar(l registrar.Registrar) {
func ResourceTfExport() *schema.Resource {
return &schema.Resource{
Description: fmt.Sprintf(`
Genesys Cloud Resource to export Terraform config and (optionally) tfstate files to a local directory.
Genesys Cloud Resource to export Terraform config and (optionally) tfstate files to a local directory.
The config file is named '%s' or '%s', and the state file is named '%s'.
`, defaultTfJSONFile, defaultTfHCLFile, defaultTfStateFile),

Expand Down Expand Up @@ -147,6 +147,13 @@ func ResourceTfExport() *schema.Resource {
Default: false,
ForceNew: true,
},
"export_computed": {
Description: "Export attributes that are marked as being Computed",
Default: false,
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
Expand Down

0 comments on commit 854d207

Please sign in to comment.