diff --git a/outscale/data_source_outscale_entities_linked_to_policy.go b/outscale/data_source_outscale_entities_linked_to_policy.go new file mode 100644 index 000000000..02967182f --- /dev/null +++ b/outscale/data_source_outscale_entities_linked_to_policy.go @@ -0,0 +1,164 @@ +package outscale + +import ( + "context" + "fmt" + "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 DataSourceEntitiesLinkedToPolicy() *schema.Resource { + return &schema.Resource{ + Read: DataSourceEntitiesLinkedToPoliciesRead, + Schema: map[string]*schema.Schema{ + "policy_orn": { + Type: schema.TypeString, + Required: true, + }, + "entities_type": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "policy_entities": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "users": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "groups": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "accounts": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func DataSourceEntitiesLinkedToPoliciesRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*OutscaleClient).OSCAPI + orn := d.Get("policy_orn").(string) + req := oscgo.ReadEntitiesLinkedToPolicyRequest{PolicyOrn: &orn} + if entities := utils.SetToStringSlice(d.Get("entities_type").(*schema.Set)); len(entities) > 0 { + req.SetEntitiesType(entities) + } + + var resp oscgo.ReadEntitiesLinkedToPolicyResponse + err := resource.Retry(2*time.Minute, func() *resource.RetryError { + rp, httpResp, err := conn.PolicyApi.ReadEntitiesLinkedToPolicy(context.Background()).ReadEntitiesLinkedToPolicyRequest(req).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + + if err != nil { + return err + } + entities, ok := resp.GetPolicyEntitiesOk() + if !ok { + return fmt.Errorf("unable to find Entities linked to policy") + } + d.SetId(resource.UniqueId()) + + users := make([]map[string]interface{}, len(entities.GetUsers())) + groups := make([]map[string]interface{}, len(entities.GetGroups())) + accounts := make([]map[string]interface{}, len(entities.GetAccounts())) + if respUsers, ok := entities.GetUsersOk(); ok { + for i, v := range *respUsers { + user := make(map[string]interface{}) + user["id"] = v.GetId() + user["name"] = v.GetName() + user["orn"] = v.GetOrn() + users[i] = user + } + } + if respGroups, ok := entities.GetGroupsOk(); ok { + for i, v := range *respGroups { + group := make(map[string]interface{}) + group["name"] = v.GetName() + group["id"] = v.GetId() + group["orn"] = v.GetOrn() + groups[i] = group + } + } + if respAccounts, ok := entities.GetAccountsOk(); ok { + for i, v := range *respAccounts { + account := make(map[string]interface{}) + account["name"] = v.GetName() + account["id"] = v.GetId() + account["orn"] = v.GetOrn() + accounts[i] = account + } + } + + return d.Set("policy_entities", []map[string]interface{}{{ + "users": users, + "groups": groups, + "accounts": accounts, + }}) +} diff --git a/outscale/data_source_outscale_entities_linked_to_policy_test.go b/outscale/data_source_outscale_entities_linked_to_policy_test.go new file mode 100644 index 000000000..d71331a12 --- /dev/null +++ b/outscale/data_source_outscale_entities_linked_to_policy_test.go @@ -0,0 +1,64 @@ +package outscale + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccOthers_entities_linked_to_policy_basic(t *testing.T) { + t.Parallel() + resourceName := "data.outscale_entities_linked_to_policy.entitiesLinked" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataEntitiesLinkedConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "policy_entities.#"), + ), + }, + }, + }) +} + +const testAccDataEntitiesLinkedConfig = ` +resource "outscale_user" "user_01" { + user_name = "userLedGroup" + path = "/linkedUser/" + policy { + policy_orn = outscale_policy.policyEntities_01.orn + } +} +resource "outscale_user_group" "uGroupLinked" { + user_group_name = "GLinkedTestACC" + path = "/" + user { + user_name = outscale_user.user_01.user_name + path = "/linkedUser/" + } + policy { + policy_orn = outscale_policy.policyEntities_01.orn + } + depends_on = [outscale_user.user_01] +} +resource "outscale_user_group" "GroupLinkedPolicy" { + user_group_name = "GroupPolicyTestAcc" + path = "/TestPath/" + policy { + policy_orn = outscale_policy.policyEntities_01.orn + } +} +resource "outscale_policy" "policyEntities_01" { + description = "Example Entities Linked to policy" + document = "{\"Statement\": [ {\"Effect\": \"Allow\", \"Action\": [\"*\"], \"Resource\": [\"*\"]} ]}" + path = "/Okht_test/" + policy_name = "policyEntitiesLinked" +} + +data "outscale_entities_linked_to_policy" "entitiesLinked" { + policy_orn = outscale_policy.policyEntities_01.orn + depends_on = [outscale_user_group.uGroupLinked, outscale_user_group.GroupLinkedPolicy, outscale_user.user_01] +}` diff --git a/outscale/data_source_outscale_policies_linked_to_user.go b/outscale/data_source_outscale_policies_linked_to_user.go new file mode 100644 index 000000000..1159d5a56 --- /dev/null +++ b/outscale/data_source_outscale_policies_linked_to_user.go @@ -0,0 +1,90 @@ +package outscale + +import ( + "context" + "fmt" + "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 DataSourcePoliciesLinkedToUser() *schema.Resource { + return &schema.Resource{ + Read: DataSourcePoliciesLinkedToUserRead, + Schema: map[string]*schema.Schema{ + "user_name": { + Type: schema.TypeString, + Required: true, + }, + "policies": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + + "policy_name": { + Type: schema.TypeString, + Computed: true, + }, + "policy_id": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + "last_modification_date": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func DataSourcePoliciesLinkedToUserRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*OutscaleClient).OSCAPI + req := oscgo.NewReadLinkedPoliciesRequest(d.Get("user_name").(string)) + var resp oscgo.ReadLinkedPoliciesResponse + + err := resource.Retry(2*time.Minute, func() *resource.RetryError { + rp, httpResp, err := conn.PolicyApi.ReadLinkedPolicies(context.Background()).ReadLinkedPoliciesRequest(*req).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + + if err != nil { + return err + } + policiesList := resp.GetPolicies() + if len(policiesList) == 0 { + return fmt.Errorf("unable to find Policies linked to user: %v", d.Get("user_name").(string)) + } + d.SetId(resource.UniqueId()) + + policies := make([]map[string]interface{}, len(policiesList)) + + for i, v := range policiesList { + policy := make(map[string]interface{}) + policy["policy_name"] = v.GetPolicyName() + policy["policy_id"] = v.GetPolicyId() + policy["orn"] = v.GetOrn() + policy["creation_date"] = v.GetCreationDate() + policy["last_modification_date"] = v.GetLastModificationDate() + policies[i] = policy + } + return d.Set("policies", policies) +} diff --git a/outscale/data_source_outscale_policies_linked_to_user_group.go b/outscale/data_source_outscale_policies_linked_to_user_group.go new file mode 100644 index 000000000..f349cda76 --- /dev/null +++ b/outscale/data_source_outscale_policies_linked_to_user_group.go @@ -0,0 +1,93 @@ +package outscale + +import ( + "context" + "fmt" + "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 DataSourcePoliciesLinkedToUserGroup() *schema.Resource { + return &schema.Resource{ + Read: DataSourcePoliciesLinkedToUserGroupRead, + Schema: map[string]*schema.Schema{ + "filter": dataSourceFiltersSchema(), + "user_group_name": { + Type: schema.TypeString, + Required: true, + }, + "policies": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "policy_name": { + Type: schema.TypeString, + Computed: true, + }, + "policy_id": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + "last_modification_date": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func DataSourcePoliciesLinkedToUserGroupRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*OutscaleClient).OSCAPI + req := oscgo.ReadManagedPoliciesLinkedToUserGroupRequest{} + req.SetUserGroupName(d.Get("user_group_name").(string)) + + if filters, filtersOk := d.GetOk("filter"); filtersOk { + filterReq := buildUserGroupsFilters(filters.(*schema.Set)) + req.SetFilters(*filterReq) + } + + var resp oscgo.ReadManagedPoliciesLinkedToUserGroupResponse + err := resource.Retry(2*time.Minute, func() *resource.RetryError { + rp, httpResp, err := conn.PolicyApi.ReadManagedPoliciesLinkedToUserGroup(context.Background()).ReadManagedPoliciesLinkedToUserGroupRequest(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 linked to user group") + } + policiesResp := resp.GetPolicies() + d.SetId(resource.UniqueId()) + policies := make([]map[string]interface{}, len(policiesResp)) + for i, v := range policiesResp { + policy := make(map[string]interface{}) + policy["policy_name"] = v.GetPolicyName() + policy["policy_id"] = v.GetPolicyId() + policy["orn"] = v.GetOrn() + policy["creation_date"] = v.GetCreationDate() + policy["last_modification_date"] = v.GetLastModificationDate() + policies[i] = policy + } + return d.Set("policies", policies) +} diff --git a/outscale/data_source_outscale_policies_linked_to_user_group_test.go b/outscale/data_source_outscale_policies_linked_to_user_group_test.go new file mode 100644 index 000000000..fb7973dfd --- /dev/null +++ b/outscale/data_source_outscale_policies_linked_to_user_group_test.go @@ -0,0 +1,54 @@ +package outscale + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccOthers_policies_linked_to_user_group_basic(t *testing.T) { + t.Parallel() + resourceName := "data.outscale_policies_linked_to_user.userGroupPolicies01" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataPoliciesLinkedConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "policies.#"), + ), + }, + }, + }) +} + +const testAccDataPoliciesToGroupConfig = ` +resource "outscale_user_group" "userGroupPolicies01" { + user_group_name = "usergroup" + path = "/policiesGroup/" + policy { + policy_orn = outscale_policy.policyLinked_01.orn + } + policy { + policy_orn = outscale_policy.policyLinked_02.orn + } +} + +resource "outscale_policy" "groupLinked_01" { + description = "Example Linked to user_group" + document = "{\"Statement\": [ {\"Effect\": \"Allow\", \"Action\": [\"*\"], \"Resource\": [\"*\"]} ]}" + path = "/TestAllOK/" + policy_name = "policiesGroupLinked" +} +resource "outscale_policy" "groupLinked_02" { + description = "Example Linked policy to group" + document = "{\"Statement\": [ {\"Effect\": \"Deny\", \"Action\": [\"*\"], \"Resource\": [\"*\"]} ]}" + path = "/Okhtgroup/" + policy_name = "DenyPolicy" +} +data "outscale_policies_linked_to_user_group" "policiesLinkedToGroup" { + user_name = outscale_user.userPolicies01.user_name + depends_on = [outscale_user_group.userGroupPolicies01] +}` diff --git a/outscale/data_source_outscale_policies_linked_to_user_test.go b/outscale/data_source_outscale_policies_linked_to_user_test.go new file mode 100644 index 000000000..ddbf551c3 --- /dev/null +++ b/outscale/data_source_outscale_policies_linked_to_user_test.go @@ -0,0 +1,54 @@ +package outscale + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccOthers_policies_linked_to_user_basic(t *testing.T) { + t.Parallel() + resourceName := "data.outscale_policies_linked_to_user.policiesLinkedToUser" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataPoliciesLinkedConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "policies.#"), + ), + }, + }, + }) +} + +const testAccDataPoliciesLinkedConfig = ` +resource "outscale_user" "userPolicies01" { + user_name = "userLedGroup" + path = "/policiesUser/" + policy { + policy_orn = outscale_policy.policyLinked_01.orn + } + policy { + policy_orn = outscale_policy.policyLinked_02.orn + } +} + +resource "outscale_policy" "policyLinked_01" { + description = "Example Linked to user" + document = "{\"Statement\": [ {\"Effect\": \"Allow\", \"Action\": [\"*\"], \"Resource\": [\"*\"]} ]}" + path = "/Allow_est/" + policy_name = "policiesLinked" +} +resource "outscale_policy" "policyLinked_02" { + description = "Example Linked policy to user" + document = "{\"Statement\": [ {\"Effect\": \"Deny\", \"Action\": [\"*\"], \"Resource\": [\"*\"]} ]}" + path = "/OkhtTest/" + policy_name = "policyStopAll" +} +data "outscale_policies_linked_to_user" "policiesLinkedToUser" { + user_name = outscale_user.userPolicies01.user_name + depends_on = [outscale_user.userPolicies01] +}` diff --git a/outscale/data_source_outscale_user_groups_per_user.go b/outscale/data_source_outscale_user_groups_per_user.go new file mode 100644 index 000000000..7ef9ba067 --- /dev/null +++ b/outscale/data_source_outscale_user_groups_per_user.go @@ -0,0 +1,102 @@ +package outscale + +import ( + "context" + "fmt" + "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 DataSourceUserGroupsPerUser() *schema.Resource { + return &schema.Resource{ + Read: DataSourceUserGroupsPerUserRead, + Schema: map[string]*schema.Schema{ + "user_name": { + Type: schema.TypeString, + Required: true, + }, + "user_path": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "user_groups": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "user_group_name": { + Type: schema.TypeString, + Computed: true, + }, + "path": { + Type: schema.TypeString, + Computed: true, + }, + "user_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "orn": { + Type: schema.TypeString, + Computed: true, + }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, + "last_modification_date": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func DataSourceUserGroupsPerUserRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*OutscaleClient).OSCAPI + + req := oscgo.NewReadUserGroupsPerUserRequest(d.Get("user_name").(string)) + if userPath := d.Get("user_path").(string); userPath != "" { + req.SetUserPath(userPath) + } + var resp oscgo.ReadUserGroupsPerUserResponse + err := resource.Retry(2*time.Minute, func() *resource.RetryError { + rp, httpResp, err := conn.UserGroupApi.ReadUserGroupsPerUser(context.Background()).ReadUserGroupsPerUserRequest(*req).Execute() + if err != nil { + return utils.CheckThrottling(httpResp, err) + } + resp = rp + return nil + }) + + if err != nil { + return err + } + + if _, ok := resp.GetUserGroupsOk(); !ok { + return fmt.Errorf("unable to find user groups") + } + d.SetId(resource.UniqueId()) + userGps := resp.GetUserGroups() + userGroups := make([]map[string]interface{}, len(userGps)) + + for i, v := range userGps { + userGroup := make(map[string]interface{}) + userGroup["user_group_name"] = v.GetName() + userGroup["user_group_id"] = v.GetUserGroupId() + userGroup["path"] = v.GetPath() + userGroup["orn"] = v.GetOrn() + userGroup["creation_date"] = v.GetCreationDate() + userGroup["last_modification_date"] = v.GetLastModificationDate() + userGroups[i] = userGroup + } + return d.Set("user_groups", userGroups) +} diff --git a/outscale/data_source_outscale_user_groups_per_user_test.go b/outscale/data_source_outscale_user_groups_per_user_test.go new file mode 100644 index 000000000..67a3d1620 --- /dev/null +++ b/outscale/data_source_outscale_user_groups_per_user_test.go @@ -0,0 +1,51 @@ +package outscale + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccOthers_user_groups_per_user_basic(t *testing.T) { + t.Parallel() + resourceName := "data.outscale_user_groups_per_user.groupList" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataUserGroupsPerUserBasicConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceName, "user_groups.#"), + ), + }, + }, + }) +} + +const testAccDataUserGroupsPerUserBasicConfig = ` +resource "outscale_user" "userForGroup01" { + user_name = "user1_per_group" + path = "/groupsPerUser/" +} + +resource "outscale_user_group" "uGroupFORuser" { + user_group_name = "Group1TestACC" + path = "/" + user { + user_name = outscale_user.userForGroup01.user_name + path = "/groupsPerUser/" + } +} +resource "outscale_user_group" "uGroup2FORuser" { + user_group_name = "Group02TestACC" + path = "/TestPath/" + user { + user_name = outscale_user.userForGroup01.user_name + } +} +data "outscale_user_groups_per_user" "groupList" { + user_name = outscale_user.userForGroup01.user_name + depends_on =[outscale_user_group.uGroup2FORuser] +}`