Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement create_import_id Terraform function to convert resources into import IDs #150

Open
Tracked by #148
kklimonda-cl opened this issue Aug 30, 2024 · 0 comments

Comments

@kklimonda-cl
Copy link
Contributor

kklimonda-cl commented Aug 30, 2024

Implementation

Basics

Function signature: provider::panos::create_import_id(string, object)

string -- this is resource name (e.g. panos_address_group)
object -- this is an instance of the resource that Import ID is being generated for

Example code

Basic implementation would look similar to the following:

var (
	_ function.Function = &ImportStateCreator{}
)

type ImportStateCreator struct{}

func (o *ImportStateCreator) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
	resp.Name = "generate_import_id"
}

func (o *ImportStateCreator) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
	resp.Definition = function.Definition{
		Summary:     "Generate Import ID",
		Description: "Generate Import ID for the given resource that can be used to import resources into the state.",

		Parameters: []function.Parameter{
			function.StringParameter{
				Name:        "resource_asn",
				Description: "Name of the resource",
			},
			function.DynamicParameter{
				Name:        "resource_data",
				Description: "Resource data",
			},
		},
		Return: function.StringReturn{},
	}
}

func (o *ImportStateCreator) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
	var resourceAsn string
	var dynamicResource types.Dynamic

	resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &resourceAsn, &dynamicResource))

	if resp.Error != nil {
		return
	}

	var resource types.Object
	switch value := dynamicResource.UnderlyingValue().(type) {
	case types.Object:
		resource = value
	default:
		return
	}

	var result string
	var err error
	switch resourceAsn {
	case "panos_address_group":
		result, err = AddressGroupImportStateCreator(ctx, resource)
		if err != nil {
			resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(err.Error()))
			return
		}
	}

	resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, result))
}

Instead of switching on resourceAsn value, it would be cleaner to generate a map of resource name -> creator function that can be then used like:

fn, ok := creatorFuncs[resourceAsn]
resource, err := fn(ctx, resource)
[...]

Usage Examples

data "panos_address_group" "test_group" {
  location = {
    shared               = true
    from_panorama_shared = false
  }

  name = "test-group"
}

output "test_group_import_id" {
  value = provider::panos::create_import_id("panos_address_group", data.panos_address_group.test_group)
}
@kklimonda-cl kklimonda-cl changed the title Implement create_import_id Terraform function that dispatches call to per-resource functions based on the resource name Implement create_import_id Terraform function to convert resources into import IDs Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant