Skip to content

Commit b8aa865

Browse files
authored
Merge pull request #5 from fnaoto/2020-12-28
Add data source for organization member.
2 parents 155d2f4 + 01742f2 commit b8aa865

15 files changed

+234
-129
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export DG_API_KEY=""
2-
export DG_USER_NAME=""
32
export TF_VAR_app_id=""
43
export TF_VAR_owner=""
54
export TF_VAR_platform=""
65
export TF_VAR_add_user_name=""
6+
export TF_VAR_organization=""

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ crash.log
2525
.terraform
2626
*.tfstate
2727
*.tfstate.*
28+
29+
# Go module
30+
vendor

deploygate/data_source_app_collaborator.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,6 @@ func dataSourceAppCollaborator() *schema.Resource {
4141
},
4242
},
4343
},
44-
"teams": {
45-
Type: schema.TypeSet,
46-
Computed: true,
47-
Elem: &schema.Resource{
48-
Schema: map[string]*schema.Schema{
49-
"name": {
50-
Type: schema.TypeString,
51-
Optional: true,
52-
},
53-
"role": {
54-
Type: schema.TypeInt,
55-
Optional: true,
56-
},
57-
},
58-
},
59-
},
60-
"usage": {
61-
Type: schema.TypeMap,
62-
Computed: true,
63-
Elem: schema.TypeInt,
64-
},
6544
},
6645
}
6746
}
@@ -91,11 +70,6 @@ func dataSourceAppCollaboratorRead(d *schema.ResourceData, meta interface{}) err
9170

9271
d.SetId(fmt.Sprintf("%s/%s/%s", owner, platform, appID))
9372
d.Set("users", rs.Users)
94-
d.Set("teams", rs.Teams)
95-
d.Set("usage", map[string]interface{}{
96-
"max": rs.Usage.Max,
97-
"used": rs.Usage.Used,
98-
})
9973

10074
return nil
10175
}

deploygate/data_source_app_collaborator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func testDataSourceAppCollaborator(n string) resource.TestCheckFunc {
2727
return func(s *terraform.State) error {
2828
rs, ok := s.RootModule().Resources[n]
2929
if !ok {
30-
return fmt.Errorf("Can't find app users resource: %s", n)
30+
return fmt.Errorf("Can't find resource: %s", n)
3131
}
3232

3333
if rs.Primary.ID == "" {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package deploygate
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
go_deploygate "github.com/recruit-mp/go-deploygate"
9+
)
10+
11+
func dataSourceOrganizationMember() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceOrganizationMemberRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"organization": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
"members": {
21+
Type: schema.TypeSet,
22+
Computed: true,
23+
Elem: &schema.Resource{
24+
Schema: map[string]*schema.Schema{
25+
"type": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
},
29+
"name": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
},
33+
"url": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
},
37+
"icon_url": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
},
41+
"inviting": {
42+
Type: schema.TypeBool,
43+
Optional: true,
44+
},
45+
},
46+
},
47+
},
48+
},
49+
}
50+
}
51+
52+
func dataSourceOrganizationMemberRead(d *schema.ResourceData, meta interface{}) error {
53+
client := meta.(*Client).client
54+
55+
organization := d.Get("organization").(string)
56+
57+
log.Printf("[DEBUG] dataSourceOrganizationMemberRead: %s", organization)
58+
59+
g := &go_deploygate.GetOrganizationMemberInput{
60+
OrganizationName: organization,
61+
}
62+
63+
om, err := client.GetOrganizationMember(g)
64+
65+
if err != nil {
66+
return err
67+
}
68+
69+
rs := om
70+
71+
d.SetId(fmt.Sprintf("%s", organization))
72+
d.Set("members", rs.Members)
73+
74+
return nil
75+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package deploygate
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func Test_DataSourceOrganizationMember_basic(t *testing.T) {
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { Test_DGPreCheck(t) },
14+
Providers: testDGProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testDataSourceOrganizationMemberConfig,
18+
Check: resource.ComposeTestCheckFunc(
19+
testDataSourceOrganizationMember("data.deploygate_organization_member.current"),
20+
),
21+
},
22+
},
23+
})
24+
}
25+
26+
func testDataSourceOrganizationMember(n string) resource.TestCheckFunc {
27+
return func(s *terraform.State) error {
28+
rs, ok := s.RootModule().Resources[n]
29+
if !ok {
30+
return fmt.Errorf("Can't find resource: %s", n)
31+
}
32+
33+
if rs.Primary.ID == "" {
34+
return fmt.Errorf("Resource ID not set")
35+
}
36+
37+
if rs.Primary.Attributes["organization"] == "" {
38+
return fmt.Errorf("organization expected to not be nil")
39+
}
40+
41+
return nil
42+
}
43+
}
44+
45+
const testDataSourceOrganizationMemberConfig = `
46+
data "deploygate_organization_member" "current" {
47+
organization = var.organization
48+
}
49+
50+
variable "organization" {
51+
type = string
52+
}
53+
`

deploygate/provider.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@ func Provider() *schema.Provider {
1515
Sensitive: true,
1616
DefaultFunc: schema.EnvDefaultFunc("DG_API_KEY", nil),
1717
},
18-
"user_name": {
19-
Type: schema.TypeString,
20-
Optional: true,
21-
DefaultFunc: schema.EnvDefaultFunc("DG_USER_NAME", nil),
22-
},
23-
"organization_name": {
24-
Type: schema.TypeString,
25-
Optional: true,
26-
Sensitive: true,
27-
DefaultFunc: schema.EnvDefaultFunc("DG_ORGANIZATION_NAME", nil),
28-
},
2918
},
3019
DataSourcesMap: map[string]*schema.Resource{
31-
"deploygate_app_collaborator": dataSourceAppCollaborator(),
20+
"deploygate_app_collaborator": dataSourceAppCollaborator(),
21+
"deploygate_organization_member": dataSourceOrganizationMember(),
3222
},
3323
ResourcesMap: map[string]*schema.Resource{
3424
"deploygate_app_collaborator": resourceAppCollaborator(),

deploygate/provider_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ func Test_DGPreCheck(t *testing.T) {
2626
t.Fatal("DG_API_KEY must be set for acceptance tests")
2727
}
2828

29-
if os.Getenv("DG_USER_NAME") == "" && os.Getenv("DG_ORGANIZATION_NAME") == "" {
30-
t.Fatal("DG_USER_NAME or DG_ORGANIZATION_NAME must be set for acceptance tests")
31-
}
32-
3329
err := testDGProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))
3430
if err != nil {
3531
t.Fatal(err)

0 commit comments

Comments
 (0)