-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19f23e3
commit f4d1dda
Showing
28 changed files
with
1,775 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
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_orn": { | ||
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") | ||
if !filtersOk { | ||
return fmt.Errorf("One of filters, only_linked, path_refix,... must be assigned") | ||
} | ||
filterReq := buildPoliciesFilters(filters.(*schema.Set)) | ||
req := oscgo.NewReadPoliciesRequest() | ||
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 | ||
} | ||
|
||
if _, ok := resp.GetPoliciesOk(); !ok { | ||
return fmt.Errorf("Unable to find Policies") | ||
} | ||
d.SetId(resource.UniqueId()) | ||
policyResp := resp.GetPolicies() | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
` |
Oops, something went wrong.