diff --git a/fadc/config.go b/fadc/config.go index 0a38a89..84f786c 100644 --- a/fadc/config.go +++ b/fadc/config.go @@ -368,3 +368,17 @@ func toCertFormat(v interface{}) interface{} { } return v } + +// parsePkeyMkeyImportID parses the import ID of a resource and returns the pkey and mkey and new state id +func parsePkeyMkeyImportID(id string) (string, string, string, error) { + idParts := strings.Split(id, ".") + if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + return "", "", "", fmt.Errorf("unexpected format of import ID (%q), expected pkey.mkey", id) + } + + pkey := idParts[0] + mkey := idParts[1] + newID := pkey + "_" + mkey + + return pkey, mkey, newID, nil +} diff --git a/fadc/resource_load_balance_pool_child_pool_member.go b/fadc/resource_load_balance_pool_child_pool_member.go index f4ef5d9..0fe9a3a 100644 --- a/fadc/resource_load_balance_pool_child_pool_member.go +++ b/fadc/resource_load_balance_pool_child_pool_member.go @@ -5,6 +5,7 @@ package fortiadc import ( + "context" "fmt" "log" "strconv" @@ -21,7 +22,33 @@ func resourceLoadBalancePoolChildPoolMember() *schema.Resource { Delete: resourceLoadBalancePoolChildPoolMemberDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + pkey, mkey, newID, err := parsePkeyMkeyImportID(d.Id()) + if err != nil { + return nil, err + } + + c := m.(*FortiClient).Client + c.Retries = 1 + + o, err := c.ReadLoadBalancePoolChildPoolMember(pkey, mkey, "") + if err != nil { + return nil, fmt.Errorf("error reading LoadBalancePoolChildPoolMember resource: %v", err) + } + + if o == nil { + return nil, fmt.Errorf("resource (%s) not found", d.Id()) + } + + err = d.Set("pkey", pkey) + if err != nil { + return nil, err + } + + d.SetId(newID) + + return []*schema.ResourceData{d}, nil + }, }, Schema: map[string]*schema.Schema{ diff --git a/website/docs/r/fortiadc_load_balance_pool_child_pool_member.html.markdown b/website/docs/r/fortiadc_load_balance_pool_child_pool_member.html.markdown index 9cc1e80..2491b58 100644 --- a/website/docs/r/fortiadc_load_balance_pool_child_pool_member.html.markdown +++ b/website/docs/r/fortiadc_load_balance_pool_child_pool_member.html.markdown @@ -66,5 +66,5 @@ In addition to all the above arguments, the following attributes are exported: ## Import Load Balance Pool Child Pool Member can be imported using any of these accepted formats: ``` -$ terraform import fortiadc_load_balance_pool_child_pool_member.labelname {{mkey}} +$ terraform import fortiadc_load_balance_pool_child_pool_member.labelname {{pkey}}.{{mkey}} ```