Skip to content

Commit 3bebccd

Browse files
authored
Add support to import ACL rules by ACL id (#224)
* Add support to import ACL rules by ACL id * remove id being set to aclid * remove id being set to aclid
1 parent 5105978 commit 3bebccd

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

cloudstack/resource_cloudstack_network_acl_rule.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func resourceCloudStackNetworkACLRule() *schema.Resource {
3838
Read: resourceCloudStackNetworkACLRuleRead,
3939
Update: resourceCloudStackNetworkACLRuleUpdate,
4040
Delete: resourceCloudStackNetworkACLRuleDelete,
41+
Importer: &schema.ResourceImporter{
42+
State: resourceCloudStackNetworkACLRuleImport,
43+
},
4144

4245
Schema: map[string]*schema.Schema{
4346
"acl_id": {
@@ -714,3 +717,36 @@ func retryableACLCreationFunc(
714717
return r, nil
715718
}
716719
}
720+
721+
func resourceCloudStackNetworkACLRuleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
722+
cs := meta.(*cloudstack.CloudStackClient)
723+
724+
aclID := d.Id()
725+
726+
log.Printf("[DEBUG] Attempting to import ACL list with ID: %s", aclID)
727+
if aclExists, err := checkACLListExists(cs, aclID); err != nil {
728+
return nil, fmt.Errorf("error checking ACL list existence: %v", err)
729+
} else if !aclExists {
730+
return nil, fmt.Errorf("ACL list with ID %s does not exist", aclID)
731+
}
732+
733+
log.Printf("[DEBUG] Found ACL list with ID: %s", aclID)
734+
d.Set("acl_id", aclID)
735+
736+
log.Printf("[DEBUG] Setting managed=true for ACL list import")
737+
d.Set("managed", true)
738+
739+
return []*schema.ResourceData{d}, nil
740+
}
741+
742+
func checkACLListExists(cs *cloudstack.CloudStackClient, aclID string) (bool, error) {
743+
log.Printf("[DEBUG] Checking if ACL list exists: %s", aclID)
744+
_, count, err := cs.NetworkACL.GetNetworkACLListByID(aclID)
745+
if err != nil {
746+
log.Printf("[DEBUG] Error getting ACL list by ID: %v", err)
747+
return false, err
748+
}
749+
750+
log.Printf("[DEBUG] ACL list check result: count=%d", count)
751+
return count > 0, nil
752+
}

0 commit comments

Comments
 (0)