Skip to content

Commit 7596b91

Browse files
authored
Merge pull request #3 from fnaoto/feature/add_deploygate_user
Add deploygate app collaborator user
2 parents 762d250 + 0005d22 commit 7596b91

12 files changed

+198
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
terraform-provider-deploygate
78

89
# Test binary, built with `go test -c`
910
*.test
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package deploygate
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
go_deploygate "github.com/recruit-mp/go-deploygate"
8+
)
9+
10+
func dataSourceAppCollaborator() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceAppCollaboratorRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"owner": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"platform": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"app_id": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
},
27+
},
28+
}
29+
}
30+
31+
func dataSourceAppCollaboratorRead(d *schema.ResourceData, meta interface{}) error {
32+
client := go_deploygate.DefaultClient()
33+
34+
owner := d.Get("owner").(string)
35+
platform := d.Get("platform").(string)
36+
appID := d.Get("app_id").(string)
37+
38+
g := &go_deploygate.GetAppCollaboratorInput{
39+
Owner: owner,
40+
Platform: platform,
41+
AppId: appID,
42+
}
43+
collaborator, _ := client.GetAppCollaborator(g)
44+
d.SetId(fmt.Sprintf("%s/%s/%s", owner, platform, appID))
45+
d.Set("users", collaborator.Results.Users)
46+
d.Set("teams", collaborator.Results.Teams)
47+
d.Set("usage", collaborator.Results.Usage)
48+
return nil
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 TestAppCollaborator_basic(t *testing.T) {
12+
resource.ParallelTest(t, resource.TestCase{
13+
PreCheck: func() { testDGPreCheck(t) },
14+
Providers: testDGProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAppCollaboratorConfig,
18+
Check: resource.ComposeTestCheckFunc(
19+
testAppCollaborator("data.deploygate_app_collaborator.current"),
20+
),
21+
},
22+
},
23+
})
24+
}
25+
26+
func testAppCollaborator(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 app users resource: %s", n)
31+
}
32+
33+
if rs.Primary.ID == "" {
34+
return fmt.Errorf("Account Id resource ID not set")
35+
}
36+
37+
if rs.Primary.Attributes["users"] == "" {
38+
return fmt.Errorf("Users expected to not be nil")
39+
}
40+
41+
if rs.Primary.Attributes["teams"] == "" {
42+
return fmt.Errorf("Teams expected to not be nil")
43+
}
44+
45+
if rs.Primary.Attributes["usage"] == "" {
46+
return fmt.Errorf("Usage expected to not be nil")
47+
}
48+
49+
return nil
50+
}
51+
}
52+
53+
const testAppCollaboratorConfig = `
54+
data "deploygate_app_collaborator" "current" {}
55+
`

deploygate/data_source_user.go

Whitespace-only changes.

deploygate/data_source_user_test.go

Whitespace-only changes.

deploygate/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hashicups
1+
package deploygate
22

33
import (
44
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -11,6 +11,7 @@ func Provider() *schema.Provider {
1111
"api_token": &schema.Schema{
1212
Type: schema.TypeString,
1313
Optional: true,
14+
Sensitive: true,
1415
DefaultFunc: schema.EnvDefaultFunc("DG_API_TOKEN", nil),
1516
},
1617
"user_name": &schema.Schema{

deploygate/provider_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package deploygate
2+
3+
import (
4+
"context"
5+
"os"
6+
"sync"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
)
12+
13+
const (
14+
ProviderNameDG = "deploygate"
15+
)
16+
17+
var testDGProvider *schema.Provider
18+
19+
var testDGProviderConfigure sync.Once
20+
21+
var testDGProviders map[string]*schema.Provider
22+
23+
func testDGPreCheck(t *testing.T) {
24+
testDGProviderConfigure.Do(func() {
25+
if os.Getenv("DG_API_TOKEN") == "" || os.Getenv("DG_USER_NAME") == "" {
26+
t.Fatal("DG_API_TOKEN and DG_USER_NAME must be set for acceptance tests")
27+
}
28+
29+
if os.Getenv("DG_API_TOKEN") == "" || os.Getenv("DG_ORGANIZATION_NAME") == "" {
30+
t.Fatal("DG_API_TOKEN and DG_ORGANIZATION_NAME must be set for acceptance tests")
31+
}
32+
33+
err := testDGProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
})
38+
}
39+
40+
func init() {
41+
testDGProvider = Provider()
42+
43+
testDGProviders = map[string]*schema.Provider{
44+
ProviderNameDG: testDGProvider,
45+
}
46+
}

deploygate/resource_user.go

Whitespace-only changes.

deploygate/resource_user_test.go

Whitespace-only changes.

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ module terraform-provider-deploygate
22

33
go 1.15
44

5-
require github.com/hashicorp/terraform-plugin-sdk/v2 v2.3.0
5+
require (
6+
github.com/ajg/form v1.5.1 // indirect
7+
github.com/aws/aws-sdk-go v1.25.3
8+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.3.0
9+
github.com/recruit-mp/go-deploygate v0.0.0-20161124091054-af415c893ce8
10+
)

0 commit comments

Comments
 (0)