Skip to content

Commit

Permalink
Add EIM policy/policy_version resource and datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Dec 11, 2024
1 parent b97fb0e commit 7387bfe
Show file tree
Hide file tree
Showing 7 changed files with 801 additions and 0 deletions.
142 changes: 142 additions & 0 deletions outscale/data_source_outscale_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package outscale

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/outscale/terraform-provider-outscale/utils"
"github.com/spf13/cast"
)

func DataSourcePolicies() *schema.Resource {
return &schema.Resource{
Read: DataSourcePoliciesRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"policy_name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"policy_id": {
Type: schema.TypeString,
Computed: true,
},
"resources_count": {
Type: schema.TypeInt,
Computed: true,
},
"policy_default_version_id": {
Type: schema.TypeString,
Computed: true,
},
"is_linkable": {
Type: schema.TypeBool,
Computed: true,
},
"orn": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func DataSourcePoliciesRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI

filters, filtersOk := d.GetOk("filter")
req := oscgo.NewReadPoliciesRequest()
if filtersOk {
filterReq := buildPoliciesFilters(filters.(*schema.Set))
req.SetFilters(*filterReq)
}
var resp oscgo.ReadPoliciesResponse
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.PolicyApi.ReadPolicies(context.Background()).ReadPoliciesRequest(*req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return err
}
policyResp := resp.GetPolicies()
if len(policyResp) == 0 {
return fmt.Errorf("Unable to find Policies with fileters: %v", filters.(*schema.Set))
}
d.SetId(resource.UniqueId())

policies := make([]map[string]interface{}, len(policyResp))

for i, v := range policyResp {
policy := make(map[string]interface{})
policy["policy_name"] = v.GetPolicyName()
policy["policy_id"] = v.GetPolicyId()
policy["path"] = v.GetPath()
policy["orn"] = v.GetOrn()
policy["resources_count"] = v.GetResourcesCount()
policy["is_linkable"] = v.GetIsLinkable()
policy["policy_default_version_id"] = v.GetPolicyDefaultVersionId()
policy["description"] = v.GetDescription()
policy["creation_date"] = v.GetCreationDate()
policy["last_modification_date"] = v.GetLastModificationDate()
policies[i] = policy
}
return d.Set("policies", policies)
}

func buildPoliciesFilters(set *schema.Set) *oscgo.ReadPoliciesFilters {
var filters oscgo.ReadPoliciesFilters
for _, v := range set.List() {
m := v.(map[string]interface{})
var filterValues []string
for _, e := range m["values"].([]interface{}) {
filterValues = append(filterValues, e.(string))
}

switch name := m["name"].(string); name {
case "only_linked":
filters.SetOnlyLinked(cast.ToBool(filterValues[0]))
case "path_prefix":
filters.SetPathPrefix(filterValues[0])
case "scope":
filters.SetScope(filterValues[0])
default:
log.Printf("[Debug] Unknown Filter Name: %s.", name)
}
}
return &filters
}
124 changes: 124 additions & 0 deletions outscale/data_source_outscale_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package outscale

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/outscale/terraform-provider-outscale/utils"
)

func DataSourcePolicy() *schema.Resource {
return &schema.Resource{
Read: DataSourcePolicyRead,
Schema: map[string]*schema.Schema{
"policy_orn": {
Type: schema.TypeString,
Required: true,
},
"policy_name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"document": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"policy_id": {
Type: schema.TypeString,
Computed: true,
},
"resources_count": {
Type: schema.TypeInt,
Computed: true,
},
"policy_default_version_id": {
Type: schema.TypeString,
Computed: true,
},
"is_linkable": {
Type: schema.TypeBool,
Computed: true,
},
"orn": {
Type: schema.TypeString,
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
},
"last_modification_date": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func DataSourcePolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*OutscaleClient).OSCAPI
req := oscgo.NewReadPolicyRequest(d.Get("policy_orn").(string))

var resp oscgo.ReadPolicyResponse
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
rp, httpResp, err := conn.PolicyApi.ReadPolicy(context.Background()).ReadPolicyRequest(*req).Execute()
if err != nil {
return utils.CheckThrottling(httpResp, err)
}
resp = rp
return nil
})

if err != nil {
return err
}

if _, ok := resp.GetPolicyOk(); !ok {
d.SetId("")
return nil
}
policy := resp.GetPolicy()
d.SetId(resource.UniqueId())
if err := d.Set("policy_name", policy.GetPolicyName()); err != nil {
return err
}
if err := d.Set("policy_id", policy.GetPolicyId()); err != nil {
return err
}
if err := d.Set("path", policy.GetPath()); err != nil {
return err
}
if err := d.Set("orn", policy.GetOrn()); err != nil {
return err
}
if err := d.Set("resources_count", policy.GetResourcesCount()); err != nil {
return err
}
if err := d.Set("is_linkable", policy.GetIsLinkable()); err != nil {
return err
}
if err := d.Set("policy_default_version_id", policy.GetPolicyDefaultVersionId()); err != nil {
return err
}
if err := d.Set("description", policy.GetDescription()); err != nil {
return err
}
if err := d.Set("creation_date", (policy.GetCreationDate())); err != nil {
return err
}
if err := d.Set("last_modification_date", (policy.GetLastModificationDate())); err != nil {
return err
}
return nil
}
37 changes: 37 additions & 0 deletions outscale/data_source_outscale_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package outscale

import (
"testing"

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

func TestAccOthers_data_policy_basic(t *testing.T) {
t.Parallel()
resourceName := "data.outscale_policy.data_test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccPolicyDataConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "policy_name"),
resource.TestCheckResourceAttr(resourceName, "path", "/"),
),
},
},
})
}

const testAccPolicyDataConfig = `
resource "outscale_policy" "data_policy" {
policy_name = "TestACC_resoucePolicy"
document = "{\"Statement\": [ {\"Effect\": \"Allow\", \"Action\": [\"*\"], \"Resource\": [\"*\"]} ]}"
path = "/"
}
data "outscale_policy" "data_test" {
policy_orn = outscale_policy.data_policy.orn
}
`
Loading

0 comments on commit 7387bfe

Please sign in to comment.