forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_obmcs_identity_user_group_memberships.go
108 lines (92 loc) · 2.52 KB
/
data_source_obmcs_identity_user_group_memberships.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
package main
import (
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/oracle/bmcs-go-sdk"
"github.com/oracle/terraform-provider-oci/crud"
"github.com/oracle/terraform-provider-oci/options"
)
func UserGroupMembershipDatasource() *schema.Resource {
return &schema.Resource{
Read: readUserGroupMemberships,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"compartment_id": {
Type: schema.TypeString,
Required: true,
},
"user_id": {
Type: schema.TypeString,
Optional: true,
},
"group_id": {
Type: schema.TypeString,
Optional: true,
},
"memberships": {
Type: schema.TypeList,
Computed: true,
Elem: UserGroupMembershipResource(),
},
},
}
}
func readUserGroupMemberships(d *schema.ResourceData, m interface{}) (e error) {
client := m.(*OracleClients)
sync := &UserGroupMembershipDatasourceCrud{}
sync.D = d
sync.Client = client.client
return crud.ReadResource(sync)
}
type UserGroupMembershipDatasourceCrud struct {
crud.BaseCrud
Res *baremetal.ListUserGroupMemberships
}
func (s *UserGroupMembershipDatasourceCrud) Get() (e error) {
opts := &baremetal.ListMembershipsOptions{}
if id, ok := s.D.GetOk("group_id"); ok {
opts.GroupID = id.(string)
}
if id, ok := s.D.GetOk("user_id"); ok {
opts.UserID = id.(string)
}
s.Res = &baremetal.ListUserGroupMemberships{Memberships: []baremetal.UserGroupMembership{}}
for {
var list *baremetal.ListUserGroupMemberships
if list, e = s.Client.ListUserGroupMemberships(opts); e != nil {
break
}
s.Res.Memberships = append(s.Res.Memberships, list.Memberships...)
if hasNexPage := options.SetNextPageOption(list.NextPage, &opts.PageListOptions); !hasNexPage {
break
}
}
return
}
func (s *UserGroupMembershipDatasourceCrud) SetData() {
if s.Res == nil {
return
}
s.D.SetId(time.Now().UTC().String())
resources := []map[string]interface{}{}
for _, v := range s.Res.Memberships {
res := map[string]interface{}{
"compartment_id": v.CompartmentID,
"id": v.ID,
"user_id": v.UserID,
"group_id": v.GroupID,
"inactive_state": v.InactiveStatus,
"state": v.State,
"time_created": v.TimeCreated.String(),
}
resources = append(resources, res)
}
if f, fOk := s.D.GetOk("filter"); fOk {
resources = ApplyFilters(f.(*schema.Set), resources)
}
if err := s.D.Set("memberships", resources); err != nil {
panic(err)
}
return
}