Categorygithub.com/zpp12354321/terraform_extension
modulepackage
0.0.0-20230316002705-61ec596bac29
Repository: https://github.com/zpp12354321/terraform_extension.git
Documentation: pkg.go.dev

# README

Introduction

Terraform plugin development framework adapter

Inspired by https://github.com/volcengine/terraform-provider-volcengine

Design

Adapter provides support for the interconversion between HCL and Unstructured Map

  • RequestAdapter
    • Transformer: trans HCL to Row Unstructured Map
    • Converter: filter and process the Unstructured Map, so that the final result completely matches the API request data
  • ResponseAdapter
    • Converter: filter and process the business output
    • Transformer: save the Converter Results to HCL

Example

Import

tfext "github.com/zpp12354321/terraform_extension"

Resource Interface

type Resource interface {
	// Adapt For Datasource
	Datasource(*schema.ResourceData) []Action

	// Adapt For Resource
	ReadResource(*schema.ResourceData) []Action
	CreateResource(*schema.ResourceData) []Action
	ModifyResource(*schema.ResourceData) []Action
	RemoveResource(*schema.ResourceData) []Action
}

Dispatcher

// resource
dispatcher := tfext.NewDispatcher(&VpcResource{}).WithResourceSchema(resourceSchema)
resource := &schema.Resource{
    Create: dispatcher.Create,
    Read:   dispatcher.Read,
    Update: dispatcher.Update,
    Delete: dispatcher.Delete,
    Importer: &schema.ResourceImporter{
        State: schema.ImportStatePassthrough,
    },
    Schema: resourceSchema,
}

// datasource
dispatcher := tfext.NewDispatcher(&VpcResource{}).WithDatasourceSchema(dataSchema)
datasource = &schema.Resource{
    Read:   dispatcher.Data,
    Schema: dataSchema,
}

Service

func (s *VpcResource) CreateResource(d *schema.ResourceData) []tfext.Action {
	res := []tfext.Action{
		{
			Name: "CreateVpc",
			RequestAdapter: &tfext.Adapter{
				Transformer: tfext.Transformer{StrCaseStyle: tfext.Camel},
			},
			Handlers: []tfext.HandlerFunc{
				func(info *tfext.Info) tfext.Then {
					params := info.GetRequestParams().(map[string]interface{})
					fmt.Println("get request params", params)
					// call create vpc api
					
					// mock result here
					resp := map[string]interface{}{
						"Result": map[string]interface{}{
							"VpcId": "1234567",
						},
					}
					info.SetResponseParams(resp)  // update result
					return tfext.ThenContinue
				},
				func(info *tfext.Info) tfext.Then {
					id, err := tfext.Select(".Result.VpcId", info.GetResponseParams())
					if err != nil {
						info.AppendError(err)
						return tfext.ThenHalt
					}
					d.SetId(id.(string))  // update id
					return tfext.ThenContinue
				},
				tfext.RefreshAction([]string{"Pending"}, []string{"Available"}, d.Timeout(schema.TimeoutCreate), 5*time.Second,
					func(info *tfext.Info) (result interface{}, state string, err error) {
						return s.Refresh(info.Meta, d.Id())
					}),
			},
		},
	}
	return res
}

# Functions

Flatten the map, it returns a map one level deep regardless of how nested the original map was.
GetSetDiff return add and remove elements github.com/volcengine/terraform-provider-volcengine/blob/master/common/common_volcengine_diff_collection.go.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewValueConverter using for update data select data through selectExpr, update using f func, and set data to targetExpr.
No description provided by the author
Select get element through jquery expression.
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Options the flatten options.
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author