-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement resource importing without Tfid (#157)
- Loading branch information
1 parent
3909962
commit 2f37d08
Showing
11 changed files
with
754 additions
and
439 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
assets/terraform/internal/provider/func_create_import_id.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var ( | ||
_ function.Function = &ImportStateCreator{} | ||
) | ||
|
||
type ImportStateCreator struct{} | ||
|
||
func NewCreateImportIdFunction() function.Function { | ||
return &ImportStateCreator{} | ||
} | ||
|
||
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: | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewArgumentFuncError(1, fmt.Sprintf("Wrong resource type: must be an object"))) | ||
return | ||
} | ||
|
||
var data []byte | ||
|
||
if resourceFuncs, found := resourceFuncMap[resourceAsn]; !found { | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewArgumentFuncError(0, fmt.Sprintf("Unsupported resource type: %s'", resourceAsn))) | ||
return | ||
} else { | ||
var err error | ||
data, err = resourceFuncs.CreateImportId(ctx, resource) | ||
if err != nil { | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(err.Error())) | ||
return | ||
} | ||
|
||
} | ||
|
||
result := base64.StdEncoding.EncodeToString(data) | ||
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, result)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.