Skip to content

Commit 4fc34be

Browse files
authored
Merge pull request #50 from jmpaloalto/importFeatureUpdate
Import feature update
2 parents 39d477e + 524ef58 commit 4fc34be

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

docs/resources/custom_rule.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
page_title: "prismacloudcompute_custom_rule Resource - terraform-provider-prismacloudcompute"
44
subcategory: ""
55
description: |-
6-
6+
77
---
88

99
# prismacloudcompute_custom_rule (Resource)
@@ -31,4 +31,8 @@ description: |-
3131
- **id** (String) ID of the custom rule.
3232
- **prisma_id** (Number) Prisma Cloud Compute ID of the custom rule.
3333

34+
## Import
3435

36+
```
37+
$ terraform import prismacloudcompute_custom_rule.example <rule_name>:<prisma_id>
38+
```

internal/provider/resource_custom_rule.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package provider
22

33
import (
44
"fmt"
5-
5+
"strings"
6+
"strconv"
67
"github.com/PaloAltoNetworks/terraform-provider-prismacloudcompute/internal/api"
78
"github.com/PaloAltoNetworks/terraform-provider-prismacloudcompute/internal/api/rule"
89
"github.com/PaloAltoNetworks/terraform-provider-prismacloudcompute/internal/convert"
@@ -17,7 +18,21 @@ func resourceCustomRule() *schema.Resource {
1718
Delete: deleteCustomRule,
1819

1920
Importer: &schema.ResourceImporter{
20-
StateContext: schema.ImportStatePassthroughContext,
21+
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
22+
23+
name, id, err := CustomRuleParseId(d.Id())
24+
25+
intVar, err := strconv.Atoi(id)
26+
27+
if err != nil {
28+
return []*schema.ResourceData{d}, nil
29+
}
30+
31+
var pid int = intVar
32+
d.Set("prisma_id", pid)
33+
d.SetId(name)
34+
return []*schema.ResourceData{d}, nil
35+
},
2136
},
2237

2338
Schema: map[string]*schema.Schema{
@@ -60,6 +75,17 @@ func resourceCustomRule() *schema.Resource {
6075
}
6176
}
6277

78+
79+
func CustomRuleParseId(id string) (string, string, error) {
80+
parts := strings.SplitN(id, ":", 2)
81+
82+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
83+
return "", "", fmt.Errorf("unexpected format of ID (%s), expected attribute1:attribute2", id)
84+
}
85+
86+
return parts[0], parts[1], nil
87+
}
88+
6389
func createCustomRule(d *schema.ResourceData, meta interface{}) error {
6490
client := meta.(*api.Client)
6591
parsedCustomRule := convert.SchemaToCustomRule(d)

0 commit comments

Comments
 (0)