Skip to content

Commit

Permalink
Add tests for user and user_group
Browse files Browse the repository at this point in the history
  • Loading branch information
outscale-toa committed Oct 10, 2024
1 parent 19f23e3 commit f4d1dda
Show file tree
Hide file tree
Showing 28 changed files with 1,775 additions and 70 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/outscale/terraform-provider-outscale

go 1.22.0
go 1.23.0

require (
github.com/aws/aws-sdk-go v1.55.5
Expand All @@ -13,7 +13,7 @@ require (
github.com/hashicorp/terraform-plugin-mux v0.12.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/nav-inc/datetime v0.1.3
github.com/outscale/osc-sdk-go/v2 v2.23.0
github.com/outscale/osc-sdk-go/v2 v2.24.0
github.com/spf13/cast v1.6.0
)

Expand Down Expand Up @@ -58,7 +58,7 @@ require (
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ github.com/nav-inc/datetime v0.1.3 h1:PaybPUsScX+Cd3TEa1tYpfwU61deCEhMTlCO2hONm1
github.com/nav-inc/datetime v0.1.3/go.mod h1:gKGf5G+cW7qkTo5TC/sieNyz6lYdrA9cf1PNV+pXIOE=
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/outscale/osc-sdk-go/v2 v2.23.0 h1:Ib134+ThQHmMtURcMTD9eTiAKeF1gSufbH3RdYa9+bY=
github.com/outscale/osc-sdk-go/v2 v2.23.0/go.mod h1:kzhtUErCzKYl87bZ+kDMphDafmnwbsyFJY9iHF7NgNE=
github.com/outscale/osc-sdk-go/v2 v2.24.0 h1:4M0gJgYRKJQhIo5oZbiEbfuBv/Ls6iHaUyrEiBAbjYM=
github.com/outscale/osc-sdk-go/v2 v2.24.0/go.mod h1:kzhtUErCzKYl87bZ+kDMphDafmnwbsyFJY9iHF7NgNE=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -197,8 +197,8 @@ golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
143 changes: 143 additions & 0 deletions outscale/data_source_outscale_policies.go
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
}
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 f4d1dda

Please sign in to comment.