Skip to content

Commit

Permalink
CNAPP-12691: Added all_organizational_unit (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
YohaiCP authored Oct 8, 2024
1 parent 3d7984f commit 31c2bb3
Show file tree
Hide file tree
Showing 13 changed files with 402 additions and 93 deletions.
1 change: 1 addition & 0 deletions dome9/common/resourcetype/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
CloudAccountAWSSecurityGroupRule = "dome9_cloud_security_group_rule"
Role = "dome9_role"
OrganizationalUnit = "dome9_organizational_unit"
OrganizationalUnitAll = "dome9_all_organizational_units"
CloudAccountAzureSecurityGroup = "dome9_azure_security_group"
AttachIAMSafeToAwsCloudAccount = "dome9_attach_iam_safe"
User = "dome9_user"
Expand Down
192 changes: 122 additions & 70 deletions dome9/data_source_dome9_organizational_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,122 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

var OrganizationalUnitSchema = map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"parent_id": {
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Type: schema.TypeInt,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"updated": {
Type: schema.TypeString,
Computed: true,
},
"aws_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"azure_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"oci_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"google_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"k8s_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"shift_left_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"alibaba_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"container_registry_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"aws_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"azure_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"oci_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"google_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"k8s_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"shift_left_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"alibaba_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"container_registry_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"sub_organizational_units_count": {
Type: schema.TypeInt,
Computed: true,
},
"is_root": {
Type: schema.TypeBool,
Computed: true,
},
"is_parent_root": {
Type: schema.TypeBool,
Computed: true,
},
"path_str": {
Type: schema.TypeString,
Computed: true,
},
}

func dataSourceOrganizationalUnit() *schema.Resource {
return &schema.Resource{
Read: dataSourceOrganizationalUnitRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"parent_id": {
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"updated": {
Type: schema.TypeString,
Computed: true,
},
"aws_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"azure_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"google_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"aws_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"azure_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"google_aggregate_cloud_accounts_count": {
Type: schema.TypeInt,
Computed: true,
},
"sub_organizational_units_count": {
Type: schema.TypeInt,
Computed: true,
},
"is_root": {
Type: schema.TypeBool,
Computed: true,
},
"is_parent_root": {
Type: schema.TypeBool,
Computed: true,
},
"path_str": {
Type: schema.TypeString,
Computed: true,
},
},
Schema: OrganizationalUnitSchema,
}
}

Expand Down Expand Up @@ -107,6 +149,16 @@ func dataSourceOrganizationalUnitRead(d *schema.ResourceData, meta interface{})
_ = d.Set("azure_aggregate_cloud_accounts_count", resp.Item.AzureAggregateCloudAccountsCount)
_ = d.Set("google_cloud_accounts_count", resp.Item.GoogleCloudAccountsCount)
_ = d.Set("google_aggregate_cloud_accounts_count", resp.Item.GoogleAggregateCloudAccountsCount)
_ = d.Set("oci_cloud_accounts_count", resp.Item.OciCloudAccountsCount)
_ = d.Set("oci_aggregate_cloud_accounts_count", resp.Item.OciAggregateCloudAccountsCount)
_ = d.Set("k8s_cloud_accounts_count", resp.Item.K8sCloudAccountsCount)
_ = d.Set("k8s_aggregate_cloud_accounts_count", resp.Item.K8sAggregateCloudAccountsCount)
_ = d.Set("shift_left_cloud_accounts_count", resp.Item.ShiftLeftCloudAccountsCount)
_ = d.Set("shift_left_aggregate_cloud_accounts_count", resp.Item.ShiftLeftAggregateCloudAccountsCount)
_ = d.Set("alibaba_cloud_accounts_count", resp.Item.AlibabaCloudAccountsCount)
_ = d.Set("alibaba_aggregate_cloud_accounts_count", resp.Item.AlibabaAggregateCloudAccountsCount)
_ = d.Set("container_registry_cloud_accounts_count", resp.Item.ContainerRegistryAccountsCount)
_ = d.Set("container_registry_aggregate_cloud_accounts_count", resp.Item.ContainerRegistryAggregateCloudAccountsCount)
_ = d.Set("sub_organizational_units_count", resp.Item.SubOrganizationalUnitsCount)
_ = d.Set("is_root", resp.Item.IsRoot)
_ = d.Set("is_parent_root", resp.Item.IsParentRoot)
Expand Down
83 changes: 83 additions & 0 deletions dome9/data_source_dome9_organizational_unit_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dome9

import (
"github.com/dome9/dome9-sdk-go/services/organizationalunits"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)

func dataSourceOrganizationalUnitAll() *schema.Resource {
return &schema.Resource{
Read: dataSourceOrganizationalUnitAllRead,

Schema: map[string]*schema.Schema{
"items": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: OrganizationalUnitSchema,
},
},
},
}
}

func dataSourceOrganizationalUnitAllRead(d *schema.ResourceData, meta interface{}) error {
d9Client := meta.(*Client)

log.Printf("[INFO] Getting all data for Organizational Units \n")

resp, _, err := d9Client.organizationalUnit.GetAll()
d.SetId("all_organizational_units")
if err != nil {
return err
}

if err := d.Set("items", flattenOrganizationalUnitItems(*resp)); err != nil {
return err
}
return nil
}

func flattenOrganizationalUnitItems(resp []organizationalunits.OUResponse) []interface{} {
var ouListItems []interface{}

for _, ou := range resp {
flatItem := map[string]interface{}{
"id": ou.Item.ID,
"name": ou.Item.Name,
"parent_id": ou.ParentID,
"account_id": ou.Item.AccountID,
"path": ou.Item.Path,
"created": ou.Item.Created.Format("2006-01-02 15:04:05"),
"updated": ou.Item.Updated.Format("2006-01-02 15:04:05"),
"aws_cloud_accounts_count": ou.Item.AwsCloudAcountsCount,
"aws_aggregate_cloud_accounts_count": ou.Item.AwsAggregatedCloudAcountsCount,
"azure_cloud_accounts_count": ou.Item.AzureCloudAccountsCount,
"azure_aggregate_cloud_accounts_count": ou.Item.AzureAggregateCloudAccountsCount,
"google_cloud_accounts_count": ou.Item.GoogleCloudAccountsCount,
"google_aggregate_cloud_accounts_count": ou.Item.GoogleAggregateCloudAccountsCount,
"oci_cloud_accounts_count": ou.Item.OciCloudAccountsCount,
"oci_aggregate_cloud_accounts_count": ou.Item.OciAggregateCloudAccountsCount,
"k8s_cloud_accounts_count": ou.Item.K8sCloudAccountsCount,
"k8s_aggregate_cloud_accounts_count": ou.Item.K8sAggregateCloudAccountsCount,
"shift_left_cloud_accounts_count": ou.Item.ShiftLeftCloudAccountsCount,
"shift_left_aggregate_cloud_accounts_count": ou.Item.ShiftLeftAggregateCloudAccountsCount,
"alibaba_cloud_accounts_count": ou.Item.AlibabaCloudAccountsCount,
"alibaba_aggregate_cloud_accounts_count": ou.Item.AlibabaAggregateCloudAccountsCount,
"container_registry_cloud_accounts_count": ou.Item.ContainerRegistryAccountsCount,
"container_registry_aggregate_cloud_accounts_count": ou.Item.ContainerRegistryAggregateCloudAccountsCount,
"sub_organizational_units_count": ou.Item.SubOrganizationalUnitsCount,
"is_root": ou.Item.IsRoot,
"is_parent_root": ou.Item.IsParentRoot,
"path_str": ou.Item.PathStr,
}
ouListItems = append(ouListItems, flatItem)

if len(ou.Children) > 0 {
childItems := flattenOrganizationalUnitItems(ou.Children)
ouListItems = append(ouListItems, childItems...)
}
}
return ouListItems
}
35 changes: 35 additions & 0 deletions dome9/data_source_dome9_organizational_unit_all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dome9

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceOrganizationalUnitAllBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckOrganizationalUnitDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckOrganizationalUnitAllBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.dome9_all_organizational_units.all_units", "items.#"),
),
},
},
})
}

func testAccCheckOrganizationalUnitAllBasic() string {
return `
data "dome9_all_organizational_units" "all_units" {}
output "all_organizational_units" {
value = data.dome9_all_organizational_units.all_units
}
`
}
1 change: 1 addition & 0 deletions dome9/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Provider() terraform.ResourceProvider {
resourcetype.CloudAccountAWSSecurityGroupRule: dataSourceCloudSecurityGroupAWSRule(),
resourcetype.Role: dataSourceRole(),
resourcetype.OrganizationalUnit: dataSourceOrganizationalUnit(),
resourcetype.OrganizationalUnitAll: dataSourceOrganizationalUnitAll(),
resourcetype.CloudAccountAzureSecurityGroup: dataSourceSecurityGroupAzure(),
resourcetype.User: dataSourceUser(),
resourcetype.ServiceAccount: dataSourceServiceAccount(),
Expand Down
Loading

0 comments on commit 31c2bb3

Please sign in to comment.