Skip to content

Commit

Permalink
fix: [CDS-71930]: fix infrastruture import to work at account and org…
Browse files Browse the repository at this point in the history
… level (#579)

* fix: [CDS-70013]: fix infrastruture import to work at infra and org level

* fix: [CDS-70013]: fix infrastruture import to work at infra and org level

* fix: [CDS-70013]: fix infrastruture import to work at infra and org level

* fix: [CDS-70013]: fix infrastruture import to work at account and org level

* fix: [CDS-70013]: fix infrastruture import to work at account and org level

* fix: [CDS-70013]: fix infrastruture import to work at account and org level

* fix: [CDS-70013]: fix infrastruture import to work at account and org level

* Update 577.txt
  • Loading branch information
vivekkarnatiharness committed Jun 14, 2023
1 parent 700d9f8 commit 20f68af
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changelog/577.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Fix for supporting import for account/org infrastructure.
```
60 changes: 49 additions & 11 deletions helpers/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,63 @@ var TriggerResourceImporter = &schema.ResourceImporter{
var EnvRelatedResourceImporter = &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
d.Set("org_id", parts[0])
d.Set("project_id", parts[1])
d.Set("env_id", parts[2])
d.Set("identifier", parts[3])
d.SetId(parts[3])
partCount := len(parts)
isAccountEntity := partCount == 2
isOrgEntity := partCount == 3
isProjectEntity := partCount == 4
if isAccountEntity {
d.Set("env_id", parts[0])
d.Set("identifier", parts[1])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
}
if isOrgEntity {
d.Set("org_id", parts[0])
d.Set("env_id", parts[1])
d.Set("identifier", parts[2])
d.SetId(parts[2])
return []*schema.ResourceData{d}, nil
}
if isProjectEntity {
d.Set("org_id", parts[0])
d.Set("project_id", parts[1])
d.Set("env_id", parts[2])
d.Set("identifier", parts[3])
d.SetId(parts[3])
return []*schema.ResourceData{d}, nil
}

return []*schema.ResourceData{d}, nil
return nil, fmt.Errorf("invalid identifier: %s", d.Id())
},
}

var ServiceOverrideResourceImporter = &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
d.Set("org_id", parts[0])
d.Set("project_id", parts[1])
d.Set("env_id", parts[2])
d.SetId(parts[3])
partCount := len(parts)
isAccountEntity := partCount == 2
isOrgEntity := partCount == 3
isProjectEntity := partCount == 4
if isAccountEntity {
d.Set("env_id", parts[0])
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
}
if isOrgEntity {
d.Set("org_id", parts[0])
d.Set("env_id", parts[1])
d.SetId(parts[2])
return []*schema.ResourceData{d}, nil
}
if isProjectEntity {
d.Set("org_id", parts[0])
d.Set("project_id", parts[1])
d.Set("env_id", parts[2])
d.SetId(parts[3])
return []*schema.ResourceData{d}, nil
}

return []*schema.ResourceData{d}, nil
return nil, fmt.Errorf("invalid identifier: %s", d.Id())
},
}

Expand Down
7 changes: 7 additions & 0 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func EnvRelatedResourceImportStateIdFunc(resourceName string) resource.ImportSta
if len(primary.Attributes["env_id"]) != 0 {
envId = primary.Attributes["env_id"]
}
if orgId == "" {
return fmt.Sprintf("%s/%s", envId, id), nil
}
if projId == "" {
return fmt.Sprintf("%s/%s/%s", orgId, envId, id), nil
}

return fmt.Sprintf("%s/%s/%s/%s", orgId, projId, envId, id), nil
}
}
Expand Down

0 comments on commit 20f68af

Please sign in to comment.