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

fix import on fortiadc_load_balance_pool_child_pool_member #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions fadc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 28 additions & 1 deletion fadc/resource_load_balance_pool_child_pool_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package fortiadc

import (
"context"
"fmt"
"log"
"strconv"
Expand All @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
```