Skip to content

Commit 7e9e153

Browse files
author
Pawel Peksa
committed
reviewfix2
1 parent 90fccab commit 7e9e153

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

castai/resource_pod_mutation.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"strings"
89
"time"
910

1011
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -457,13 +458,17 @@ func distributionGroupSchema() map[string]*schema.Schema {
457458
}
458459

459460
func podMutationStateImporter(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
460-
organizationID, id := parseImportID(d)
461-
if organizationID != "" {
462-
if err := d.Set(FieldPodMutationOrganizationID, organizationID); err != nil {
463-
return nil, err
464-
}
461+
parts := strings.Split(d.Id(), "/")
462+
if len(parts) != 3 {
463+
return nil, fmt.Errorf("invalid import ID %s, expected format: organization_id/cluster_id/mutation_id", d.Id())
464+
}
465+
if err := d.Set(FieldPodMutationOrganizationID, parts[0]); err != nil {
466+
return nil, err
467+
}
468+
if err := d.Set(FieldPodMutationClusterID, parts[1]); err != nil {
469+
return nil, err
465470
}
466-
d.SetId(id)
471+
d.SetId(parts[2])
467472
return []*schema.ResourceData{d}, nil
468473
}
469474

castai/resource_pod_mutation_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
)
2525

2626
const (
27-
testOrgID = "4e4cd9eb-82eb-407e-a926-e5fef81cab50"
28-
testClusterID = "b6bfc074-a267-400f-b8f1-db0850c369b1"
27+
testOrgID = "4e4cd9eb-82eb-407e-a926-e5fef81cab50"
28+
testClusterID = "b6bfc074-a267-400f-b8f1-db0850c369b1"
2929
testMutationID = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
3030
)
3131

@@ -301,6 +301,39 @@ func TestPodMutation_UpdateContext(t *testing.T) {
301301
})
302302
}
303303

304+
func TestPodMutation_StateImporter(t *testing.T) {
305+
t.Parallel()
306+
307+
t.Run("three part ID sets organization_id, cluster_id, and mutation_id", func(t *testing.T) {
308+
r := require.New(t)
309+
res := resourcePodMutation()
310+
d := res.Data(nil)
311+
d.SetId("org-123/cluster-456/mutation-789")
312+
313+
result, err := res.Importer.StateContext(t.Context(), d, nil)
314+
315+
r.NoError(err)
316+
r.Len(result, 1)
317+
r.Equal("mutation-789", d.Id())
318+
r.Equal("org-123", d.Get(FieldPodMutationOrganizationID))
319+
r.Equal("cluster-456", d.Get(FieldPodMutationClusterID))
320+
})
321+
322+
for _, id := range []string{"mutation-789", "org-123/mutation-789", "a/b/c/d"} {
323+
t.Run("invalid import ID "+id, func(t *testing.T) {
324+
r := require.New(t)
325+
res := resourcePodMutation()
326+
d := res.Data(nil)
327+
d.SetId(id)
328+
329+
_, err := res.Importer.StateContext(t.Context(), d, nil)
330+
331+
r.Error(err)
332+
r.Contains(err.Error(), "invalid import ID")
333+
})
334+
}
335+
}
336+
304337
func TestAccCloudAgnostic_ResourcePodMutation(t *testing.T) {
305338
rName := fmt.Sprintf("%v-pod-mutation-%v", ResourcePrefix, acctest.RandString(8))
306339
resourceName := "castai_pod_mutation.test"
@@ -343,6 +376,17 @@ func TestAccCloudAgnostic_ResourcePodMutation(t *testing.T) {
343376
resource.TestCheckResourceAttr(resourceName, "object_filter_v2.0.namespaces.0.value", "^prod-.*$"),
344377
),
345378
},
379+
{
380+
// Test import with organization_id/cluster_id/mutation_id
381+
ResourceName: resourceName,
382+
ImportStateIdFunc: func(s *tfterraform.State) (string, error) {
383+
rs := s.RootModule().Resources[resourceName]
384+
clusterID := rs.Primary.Attributes["cluster_id"]
385+
return fmt.Sprintf("%s/%s/%s", organizationID, clusterID, rs.Primary.ID), nil
386+
},
387+
ImportState: true,
388+
ImportStateVerify: true,
389+
},
346390
{
347391
// Test update to zero values - verifies no drift on next plan
348392
Config: testAccPodMutationConfigZeroValues(rName, clusterName, organizationID),

0 commit comments

Comments
 (0)